xref: /freebsd/sys/dev/ath/if_ath.c (revision 216ca2346fff3a53770769146cf8a59a3f8c7b95)
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>
114b70f530bSAdrian Chadd #include <dev/ath/if_ath_btcoex.h>
1159af351f9SAdrian Chadd #include <dev/ath/if_ath_spectral.h>
116*216ca234SAdrian Chadd #include <dev/ath/if_ath_lna_div.h>
11748237774SAdrian Chadd #include <dev/ath/if_athdfs.h>
1185bc8125aSAdrian Chadd 
11986e07743SSam Leffler #ifdef ATH_TX99_DIAG
12086e07743SSam Leffler #include <dev/ath/ath_tx99/ath_tx99.h>
12186e07743SSam Leffler #endif
12286e07743SSam Leffler 
12389d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
124bdbb6e5bSAdrian Chadd #include <dev/ath/if_ath_alq.h>
125bdbb6e5bSAdrian Chadd #endif
126bdbb6e5bSAdrian Chadd 
127bdbb6e5bSAdrian Chadd /*
128bdbb6e5bSAdrian Chadd  * Only enable this if you're working on PS-POLL support.
129bdbb6e5bSAdrian Chadd  */
13022a3aee6SAdrian Chadd #define	ATH_SW_PSQ
131bdbb6e5bSAdrian Chadd 
132b032f27cSSam Leffler /*
133b032f27cSSam Leffler  * ATH_BCBUF determines the number of vap's that can transmit
134b032f27cSSam Leffler  * beacons and also (currently) the number of vap's that can
135b032f27cSSam Leffler  * have unique mac addresses/bssid.  When staggering beacons
136b032f27cSSam Leffler  * 4 is probably a good max as otherwise the beacons become
137b032f27cSSam Leffler  * very closely spaced and there is limited time for cab q traffic
138b032f27cSSam Leffler  * to go out.  You can burst beacons instead but that is not good
139b032f27cSSam Leffler  * for stations in power save and at some point you really want
140b032f27cSSam Leffler  * another radio (and channel).
141b032f27cSSam Leffler  *
142b032f27cSSam Leffler  * The limit on the number of mac addresses is tied to our use of
143b032f27cSSam Leffler  * the U/L bit and tracking addresses in a byte; it would be
144b032f27cSSam Leffler  * worthwhile to allow more for applications like proxy sta.
145b032f27cSSam Leffler  */
146b032f27cSSam Leffler CTASSERT(ATH_BCBUF <= 8);
147b032f27cSSam Leffler 
148b032f27cSSam Leffler static struct ieee80211vap *ath_vap_create(struct ieee80211com *,
149fcd9500fSBernhard Schmidt 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
150fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN],
151fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN]);
152b032f27cSSam Leffler static void	ath_vap_delete(struct ieee80211vap *);
1535591b213SSam Leffler static void	ath_init(void *);
154c42a7b7eSSam Leffler static void	ath_stop_locked(struct ifnet *);
1555591b213SSam Leffler static void	ath_stop(struct ifnet *);
156b032f27cSSam Leffler static int	ath_reset_vap(struct ieee80211vap *, u_long);
157cd7dffd0SAdrian Chadd static int	ath_transmit(struct ifnet *ifp, struct mbuf *m);
158cd7dffd0SAdrian Chadd static void	ath_qflush(struct ifnet *ifp);
1595591b213SSam Leffler static int	ath_media_change(struct ifnet *);
1602e986da5SSam Leffler static void	ath_watchdog(void *);
1615591b213SSam Leffler static int	ath_ioctl(struct ifnet *, u_long, caddr_t);
1625591b213SSam Leffler static void	ath_fatal_proc(void *, int);
163b032f27cSSam Leffler static void	ath_bmiss_vap(struct ieee80211vap *);
1645591b213SSam Leffler static void	ath_bmiss_proc(void *, int);
165b032f27cSSam Leffler static void	ath_key_update_begin(struct ieee80211vap *);
166b032f27cSSam Leffler static void	ath_key_update_end(struct ieee80211vap *);
167b032f27cSSam Leffler static void	ath_update_mcast(struct ifnet *);
168b032f27cSSam Leffler static void	ath_update_promisc(struct ifnet *);
169c42a7b7eSSam Leffler static void	ath_updateslot(struct ifnet *);
170c42a7b7eSSam Leffler static void	ath_bstuck_proc(void *, int);
171d52f7132SAdrian Chadd static void	ath_reset_proc(void *, int);
1725591b213SSam Leffler static int	ath_desc_alloc(struct ath_softc *);
1735591b213SSam Leffler static void	ath_desc_free(struct ath_softc *);
17438c208f8SSam Leffler static struct ieee80211_node *ath_node_alloc(struct ieee80211vap *,
17538c208f8SSam Leffler 			const uint8_t [IEEE80211_ADDR_LEN]);
1764afa805eSAdrian Chadd static void	ath_node_cleanup(struct ieee80211_node *);
177c42a7b7eSSam Leffler static void	ath_node_free(struct ieee80211_node *);
17868e8e04eSSam Leffler static void	ath_node_getsignal(const struct ieee80211_node *,
17968e8e04eSSam Leffler 			int8_t *, int8_t *);
180622b3fd2SSam Leffler static void	ath_txq_init(struct ath_softc *sc, struct ath_txq *, int);
181c42a7b7eSSam Leffler static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype);
182c42a7b7eSSam Leffler static int	ath_tx_setup(struct ath_softc *, int, int);
183c42a7b7eSSam Leffler static void	ath_tx_cleanupq(struct ath_softc *, struct ath_txq *);
184c42a7b7eSSam Leffler static void	ath_tx_cleanup(struct ath_softc *);
185788e6aa9SAdrian Chadd static int	ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq,
186788e6aa9SAdrian Chadd 		    int dosched);
187c42a7b7eSSam Leffler static void	ath_tx_proc_q0(void *, int);
188c42a7b7eSSam Leffler static void	ath_tx_proc_q0123(void *, int);
1895591b213SSam Leffler static void	ath_tx_proc(void *, int);
19003e9308fSAdrian Chadd static void	ath_txq_sched_tasklet(void *, int);
1915591b213SSam Leffler static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
192c42a7b7eSSam Leffler static void	ath_chan_change(struct ath_softc *, struct ieee80211_channel *);
19368e8e04eSSam Leffler static void	ath_scan_start(struct ieee80211com *);
19468e8e04eSSam Leffler static void	ath_scan_end(struct ieee80211com *);
19568e8e04eSSam Leffler static void	ath_set_channel(struct ieee80211com *);
196fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
197e7200579SAdrian Chadd static void	ath_update_chw(struct ieee80211com *);
198fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
1995591b213SSam Leffler static void	ath_calibrate(void *);
200b032f27cSSam Leffler static int	ath_newstate(struct ieee80211vap *, enum ieee80211_state, int);
201e8fd88a3SSam Leffler static void	ath_setup_stationkey(struct ieee80211_node *);
202e9962332SSam Leffler static void	ath_newassoc(struct ieee80211_node *, int);
203b032f27cSSam Leffler static int	ath_setregdomain(struct ieee80211com *,
204b032f27cSSam Leffler 		    struct ieee80211_regdomain *, int,
205b032f27cSSam Leffler 		    struct ieee80211_channel []);
2065fe9f044SSam Leffler static void	ath_getradiocaps(struct ieee80211com *, int, int *,
207b032f27cSSam Leffler 		    struct ieee80211_channel []);
208b032f27cSSam Leffler static int	ath_getchannels(struct ath_softc *);
2095591b213SSam Leffler 
210c42a7b7eSSam Leffler static int	ath_rate_setup(struct ath_softc *, u_int mode);
2115591b213SSam Leffler static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
212c42a7b7eSSam Leffler 
213c42a7b7eSSam Leffler static void	ath_announce(struct ath_softc *);
2145591b213SSam Leffler 
21548237774SAdrian Chadd static void	ath_dfs_tasklet(void *, int);
2160eb81626SAdrian Chadd static void	ath_node_powersave(struct ieee80211_node *, int);
217548a605dSAdrian Chadd static int	ath_node_set_tim(struct ieee80211_node *, int);
21822a3aee6SAdrian Chadd static void	ath_node_recv_pspoll(struct ieee80211_node *, struct mbuf *);
21948237774SAdrian Chadd 
220584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
221a35dae8dSAdrian Chadd #include <dev/ath/if_ath_tdma.h>
222a35dae8dSAdrian Chadd #endif
22310ad9a77SSam Leffler 
2245591b213SSam Leffler SYSCTL_DECL(_hw_ath);
2255591b213SSam Leffler 
2265591b213SSam Leffler /* XXX validate sysctl values */
2272dc7fcc4SSam Leffler static	int ath_longcalinterval = 30;		/* long cals every 30 secs */
2282dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, longcal, CTLFLAG_RW, &ath_longcalinterval,
2292dc7fcc4SSam Leffler 	    0, "long chip calibration interval (secs)");
2302dc7fcc4SSam Leffler static	int ath_shortcalinterval = 100;		/* short cals every 100 ms */
2312dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, shortcal, CTLFLAG_RW, &ath_shortcalinterval,
2322dc7fcc4SSam Leffler 	    0, "short chip calibration interval (msecs)");
2332dc7fcc4SSam Leffler static	int ath_resetcalinterval = 20*60;	/* reset cal state 20 mins */
2342dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, resetcal, CTLFLAG_RW, &ath_resetcalinterval,
2352dc7fcc4SSam Leffler 	    0, "reset chip calibration results (secs)");
236a108ab63SAdrian Chadd static	int ath_anicalinterval = 100;		/* ANI calibration - 100 msec */
237a108ab63SAdrian Chadd SYSCTL_INT(_hw_ath, OID_AUTO, anical, CTLFLAG_RW, &ath_anicalinterval,
238a108ab63SAdrian Chadd 	    0, "ANI calibration (msecs)");
2395591b213SSam Leffler 
2403d184db2SAdrian Chadd int ath_rxbuf = ATH_RXBUF;		/* # rx buffers to allocate */
241aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RW, &ath_rxbuf,
242e2d787faSSam Leffler 	    0, "rx buffers allocated");
243e2d787faSSam Leffler TUNABLE_INT("hw.ath.rxbuf", &ath_rxbuf);
2443d184db2SAdrian Chadd int ath_txbuf = ATH_TXBUF;		/* # tx buffers to allocate */
245aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RW, &ath_txbuf,
246e2d787faSSam Leffler 	    0, "tx buffers allocated");
247e2d787faSSam Leffler TUNABLE_INT("hw.ath.txbuf", &ath_txbuf);
2483d184db2SAdrian Chadd int ath_txbuf_mgmt = ATH_MGMT_TXBUF;	/* # mgmt tx buffers to allocate */
249af33d486SAdrian Chadd SYSCTL_INT(_hw_ath, OID_AUTO, txbuf_mgmt, CTLFLAG_RW, &ath_txbuf_mgmt,
250af33d486SAdrian Chadd 	    0, "tx (mgmt) buffers allocated");
251af33d486SAdrian Chadd TUNABLE_INT("hw.ath.txbuf_mgmt", &ath_txbuf_mgmt);
252e2d787faSSam Leffler 
253a35dae8dSAdrian Chadd int ath_bstuck_threshold = 4;		/* max missed beacons */
254a32ac9d3SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, bstuck, CTLFLAG_RW, &ath_bstuck_threshold,
255a32ac9d3SSam Leffler 	    0, "max missed beacon xmits before chip reset");
256a32ac9d3SSam Leffler 
2576b349e5aSAdrian Chadd MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
258c42a7b7eSSam Leffler 
259f8418db5SAdrian Chadd void
260f8418db5SAdrian Chadd ath_legacy_attach_comp_func(struct ath_softc *sc)
261f8418db5SAdrian Chadd {
262f8418db5SAdrian Chadd 
263f8418db5SAdrian Chadd 	/*
264f8418db5SAdrian Chadd 	 * Special case certain configurations.  Note the
265f8418db5SAdrian Chadd 	 * CAB queue is handled by these specially so don't
266f8418db5SAdrian Chadd 	 * include them when checking the txq setup mask.
267f8418db5SAdrian Chadd 	 */
268f8418db5SAdrian Chadd 	switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
269f8418db5SAdrian Chadd 	case 0x01:
270f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
271f8418db5SAdrian Chadd 		break;
272f8418db5SAdrian Chadd 	case 0x0f:
273f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
274f8418db5SAdrian Chadd 		break;
275f8418db5SAdrian Chadd 	default:
276f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
277f8418db5SAdrian Chadd 		break;
278f8418db5SAdrian Chadd 	}
279f8418db5SAdrian Chadd }
280f8418db5SAdrian Chadd 
28167397d39SAdrian Chadd #define	HAL_MODE_HT20 (HAL_MODE_11NG_HT20 | HAL_MODE_11NA_HT20)
28267397d39SAdrian Chadd #define	HAL_MODE_HT40 \
28367397d39SAdrian Chadd 	(HAL_MODE_11NG_HT40PLUS | HAL_MODE_11NG_HT40MINUS | \
28467397d39SAdrian Chadd 	HAL_MODE_11NA_HT40PLUS | HAL_MODE_11NA_HT40MINUS)
2855591b213SSam Leffler int
2865591b213SSam Leffler ath_attach(u_int16_t devid, struct ath_softc *sc)
2875591b213SSam Leffler {
288fc74a9f9SBrooks Davis 	struct ifnet *ifp;
289b032f27cSSam Leffler 	struct ieee80211com *ic;
290fc74a9f9SBrooks Davis 	struct ath_hal *ah = NULL;
2915591b213SSam Leffler 	HAL_STATUS status;
292c42a7b7eSSam Leffler 	int error = 0, i;
293411373ebSSam Leffler 	u_int wmodes;
29429aca940SSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
295a865860dSAdrian Chadd 	int rx_chainmask, tx_chainmask;
2965591b213SSam Leffler 
297c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
2985591b213SSam Leffler 
299a93c5097SAdrian Chadd 	CURVNET_SET(vnet0);
300b032f27cSSam Leffler 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
301fc74a9f9SBrooks Davis 	if (ifp == NULL) {
302fc74a9f9SBrooks Davis 		device_printf(sc->sc_dev, "can not if_alloc()\n");
303fc74a9f9SBrooks Davis 		error = ENOSPC;
304bb327d28SAdrian Chadd 		CURVNET_RESTORE();
305fc74a9f9SBrooks Davis 		goto bad;
306fc74a9f9SBrooks Davis 	}
307b032f27cSSam Leffler 	ic = ifp->if_l2com;
308fc74a9f9SBrooks Davis 
3095591b213SSam Leffler 	/* set these up early for if_printf use */
3109bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(sc->sc_dev),
3119bf40edeSBrooks Davis 		device_get_unit(sc->sc_dev));
312a93c5097SAdrian Chadd 	CURVNET_RESTORE();
3135591b213SSam Leffler 
3147e97436bSAdrian Chadd 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh,
3157e97436bSAdrian Chadd 	    sc->sc_eepromdata, &status);
3165591b213SSam Leffler 	if (ah == NULL) {
3175591b213SSam Leffler 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
3185591b213SSam Leffler 			status);
3195591b213SSam Leffler 		error = ENXIO;
3205591b213SSam Leffler 		goto bad;
3215591b213SSam Leffler 	}
3225591b213SSam Leffler 	sc->sc_ah = ah;
323b58b3803SSam Leffler 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
3243297be13SSam Leffler #ifdef	ATH_DEBUG
3253297be13SSam Leffler 	sc->sc_debug = ath_debug;
3263297be13SSam Leffler #endif
3275591b213SSam Leffler 
3285591b213SSam Leffler 	/*
329f8cc9b09SAdrian Chadd 	 * Setup the DMA/EDMA functions based on the current
330f8cc9b09SAdrian Chadd 	 * hardware support.
331f8cc9b09SAdrian Chadd 	 *
332f8cc9b09SAdrian Chadd 	 * This is required before the descriptors are allocated.
333f8cc9b09SAdrian Chadd 	 */
3343d184db2SAdrian Chadd 	if (ath_hal_hasedma(sc->sc_ah)) {
3353d184db2SAdrian Chadd 		sc->sc_isedma = 1;
336f8cc9b09SAdrian Chadd 		ath_recv_setup_edma(sc);
3373fdfc330SAdrian Chadd 		ath_xmit_setup_edma(sc);
3383fdfc330SAdrian Chadd 	} else {
339f8cc9b09SAdrian Chadd 		ath_recv_setup_legacy(sc);
3403fdfc330SAdrian Chadd 		ath_xmit_setup_legacy(sc);
3413fdfc330SAdrian Chadd 	}
342f8cc9b09SAdrian Chadd 
343f8cc9b09SAdrian Chadd 	/*
344c42a7b7eSSam Leffler 	 * Check if the MAC has multi-rate retry support.
345c42a7b7eSSam Leffler 	 * We do this by trying to setup a fake extended
346c42a7b7eSSam Leffler 	 * descriptor.  MAC's that don't have support will
347c42a7b7eSSam Leffler 	 * return false w/o doing anything.  MAC's that do
348c42a7b7eSSam Leffler 	 * support it will return true w/o doing anything.
349c42a7b7eSSam Leffler 	 */
350c42a7b7eSSam Leffler 	sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
351c42a7b7eSSam Leffler 
352c42a7b7eSSam Leffler 	/*
353c42a7b7eSSam Leffler 	 * Check if the device has hardware counters for PHY
354c42a7b7eSSam Leffler 	 * errors.  If so we need to enable the MIB interrupt
355c42a7b7eSSam Leffler 	 * so we can act on stat triggers.
356c42a7b7eSSam Leffler 	 */
357c42a7b7eSSam Leffler 	if (ath_hal_hwphycounters(ah))
358c42a7b7eSSam Leffler 		sc->sc_needmib = 1;
359c42a7b7eSSam Leffler 
360c42a7b7eSSam Leffler 	/*
361c42a7b7eSSam Leffler 	 * Get the hardware key cache size.
362c42a7b7eSSam Leffler 	 */
363c42a7b7eSSam Leffler 	sc->sc_keymax = ath_hal_keycachesize(ah);
364e8fd88a3SSam Leffler 	if (sc->sc_keymax > ATH_KEYMAX) {
365e8fd88a3SSam Leffler 		if_printf(ifp, "Warning, using only %u of %u key cache slots\n",
366e8fd88a3SSam Leffler 			ATH_KEYMAX, sc->sc_keymax);
367e8fd88a3SSam Leffler 		sc->sc_keymax = ATH_KEYMAX;
368c42a7b7eSSam Leffler 	}
369c42a7b7eSSam Leffler 	/*
370c42a7b7eSSam Leffler 	 * Reset the key cache since some parts do not
371c42a7b7eSSam Leffler 	 * reset the contents on initial power up.
372c42a7b7eSSam Leffler 	 */
373c42a7b7eSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
374c42a7b7eSSam Leffler 		ath_hal_keyreset(ah, i);
375c42a7b7eSSam Leffler 
376c42a7b7eSSam Leffler 	/*
377b032f27cSSam Leffler 	 * Collect the default channel list.
3785591b213SSam Leffler 	 */
379b032f27cSSam Leffler 	error = ath_getchannels(sc);
3805591b213SSam Leffler 	if (error != 0)
3815591b213SSam Leffler 		goto bad;
3825591b213SSam Leffler 
3835591b213SSam Leffler 	/*
3845591b213SSam Leffler 	 * Setup rate tables for all potential media types.
3855591b213SSam Leffler 	 */
3865591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11A);
3875591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11B);
3885591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11G);
389c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
390c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
39168e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_STURBO_A);
39268e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NA);
39368e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NG);
394724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_HALF);
395724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_QUARTER);
396aaa70f2fSSam Leffler 
397c42a7b7eSSam Leffler 	/* NB: setup here so ath_rate_update is happy */
398c42a7b7eSSam Leffler 	ath_setcurmode(sc, IEEE80211_MODE_11A);
3995591b213SSam Leffler 
400c42a7b7eSSam Leffler 	/*
4013fdfc330SAdrian Chadd 	 * Allocate TX descriptors and populate the lists.
402c42a7b7eSSam Leffler 	 */
4035591b213SSam Leffler 	error = ath_desc_alloc(sc);
4045591b213SSam Leffler 	if (error != 0) {
4053fdfc330SAdrian Chadd 		if_printf(ifp, "failed to allocate TX descriptors: %d\n",
4063fdfc330SAdrian Chadd 		    error);
4073fdfc330SAdrian Chadd 		goto bad;
4083fdfc330SAdrian Chadd 	}
4093fdfc330SAdrian Chadd 	error = ath_txdma_setup(sc);
4103fdfc330SAdrian Chadd 	if (error != 0) {
4113fdfc330SAdrian Chadd 		if_printf(ifp, "failed to allocate TX descriptors: %d\n",
4123fdfc330SAdrian Chadd 		    error);
4135591b213SSam Leffler 		goto bad;
4145591b213SSam Leffler 	}
4153d184db2SAdrian Chadd 
4163fdfc330SAdrian Chadd 	/*
4173fdfc330SAdrian Chadd 	 * Allocate RX descriptors and populate the lists.
4183fdfc330SAdrian Chadd 	 */
4193d184db2SAdrian Chadd 	error = ath_rxdma_setup(sc);
4203d184db2SAdrian Chadd 	if (error != 0) {
4213d184db2SAdrian Chadd 		if_printf(ifp, "failed to allocate RX descriptors: %d\n",
4223d184db2SAdrian Chadd 		    error);
4233d184db2SAdrian Chadd 		goto bad;
4243d184db2SAdrian Chadd 	}
4253d184db2SAdrian Chadd 
4262e986da5SSam Leffler 	callout_init_mtx(&sc->sc_cal_ch, &sc->sc_mtx, 0);
4272e986da5SSam Leffler 	callout_init_mtx(&sc->sc_wd_ch, &sc->sc_mtx, 0);
4285591b213SSam Leffler 
429f0b2a0beSSam Leffler 	ATH_TXBUF_LOCK_INIT(sc);
4305591b213SSam Leffler 
4310bbf5441SSam Leffler 	sc->sc_tq = taskqueue_create("ath_taskq", M_NOWAIT,
4320bbf5441SSam Leffler 		taskqueue_thread_enqueue, &sc->sc_tq);
4330bbf5441SSam Leffler 	taskqueue_start_threads(&sc->sc_tq, 1, PI_NET,
4340bbf5441SSam Leffler 		"%s taskq", ifp->if_xname);
4350bbf5441SSam Leffler 
436f8cc9b09SAdrian Chadd 	TASK_INIT(&sc->sc_rxtask, 0, sc->sc_rx.recv_tasklet, sc);
4375591b213SSam Leffler 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
438c42a7b7eSSam Leffler 	TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc);
439d52f7132SAdrian Chadd 	TASK_INIT(&sc->sc_resettask,0, ath_reset_proc, sc);
44003e9308fSAdrian Chadd 	TASK_INIT(&sc->sc_txqtask, 0, ath_txq_sched_tasklet, sc);
441f846cf42SAdrian Chadd 	TASK_INIT(&sc->sc_fataltask, 0, ath_fatal_proc, sc);
4425591b213SSam Leffler 
4435591b213SSam Leffler 	/*
444c42a7b7eSSam Leffler 	 * Allocate hardware transmit queues: one queue for
445c42a7b7eSSam Leffler 	 * beacon frames and one data queue for each QoS
4464fa8d4efSDaniel Eischen 	 * priority.  Note that the hal handles resetting
447c42a7b7eSSam Leffler 	 * these queues at the needed time.
448c42a7b7eSSam Leffler 	 *
449c42a7b7eSSam Leffler 	 * XXX PS-Poll
4505591b213SSam Leffler 	 */
451e1252ce1SAdrian Chadd 	sc->sc_bhalq = ath_beaconq_setup(sc);
4525591b213SSam Leffler 	if (sc->sc_bhalq == (u_int) -1) {
4535591b213SSam Leffler 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
454c42a7b7eSSam Leffler 		error = EIO;
455b28b4653SSam Leffler 		goto bad2;
4565591b213SSam Leffler 	}
457c42a7b7eSSam Leffler 	sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
458c42a7b7eSSam Leffler 	if (sc->sc_cabq == NULL) {
459c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup CAB xmit queue!\n");
460c42a7b7eSSam Leffler 		error = EIO;
461c42a7b7eSSam Leffler 		goto bad2;
462c42a7b7eSSam Leffler 	}
463c42a7b7eSSam Leffler 	/* NB: insure BK queue is the lowest priority h/w queue */
464c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
465c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup xmit queue for %s traffic!\n",
466c42a7b7eSSam Leffler 			ieee80211_wme_acnames[WME_AC_BK]);
467c42a7b7eSSam Leffler 		error = EIO;
468c42a7b7eSSam Leffler 		goto bad2;
469c42a7b7eSSam Leffler 	}
470c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
471c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
472c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
473c42a7b7eSSam Leffler 		/*
474c42a7b7eSSam Leffler 		 * Not enough hardware tx queues to properly do WME;
475c42a7b7eSSam Leffler 		 * just punt and assign them all to the same h/w queue.
476c42a7b7eSSam Leffler 		 * We could do a better job of this if, for example,
477c42a7b7eSSam Leffler 		 * we allocate queues when we switch from station to
478c42a7b7eSSam Leffler 		 * AP mode.
479c42a7b7eSSam Leffler 		 */
480c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_VI] != NULL)
481c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
482c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_BE] != NULL)
483c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
484c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
485c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
486c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
487c42a7b7eSSam Leffler 	}
488c42a7b7eSSam Leffler 
489c42a7b7eSSam Leffler 	/*
490f8418db5SAdrian Chadd 	 * Attach the TX completion function.
491f8418db5SAdrian Chadd 	 *
492f8418db5SAdrian Chadd 	 * The non-EDMA chips may have some special case optimisations;
493f8418db5SAdrian Chadd 	 * this method gives everyone a chance to attach cleanly.
494c42a7b7eSSam Leffler 	 */
495f8418db5SAdrian Chadd 	sc->sc_tx.xmit_attach_comp_func(sc);
496c42a7b7eSSam Leffler 
497c42a7b7eSSam Leffler 	/*
498c42a7b7eSSam Leffler 	 * Setup rate control.  Some rate control modules
499c42a7b7eSSam Leffler 	 * call back to change the anntena state so expose
500c42a7b7eSSam Leffler 	 * the necessary entry points.
501c42a7b7eSSam Leffler 	 * XXX maybe belongs in struct ath_ratectrl?
502c42a7b7eSSam Leffler 	 */
503c42a7b7eSSam Leffler 	sc->sc_setdefantenna = ath_setdefantenna;
504c42a7b7eSSam Leffler 	sc->sc_rc = ath_rate_attach(sc);
505c42a7b7eSSam Leffler 	if (sc->sc_rc == NULL) {
506c42a7b7eSSam Leffler 		error = EIO;
507c42a7b7eSSam Leffler 		goto bad2;
508c42a7b7eSSam Leffler 	}
509c42a7b7eSSam Leffler 
51048237774SAdrian Chadd 	/* Attach DFS module */
51148237774SAdrian Chadd 	if (! ath_dfs_attach(sc)) {
5127e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
5137e97436bSAdrian Chadd 		    "%s: unable to attach DFS\n", __func__);
51448237774SAdrian Chadd 		error = EIO;
51548237774SAdrian Chadd 		goto bad2;
51648237774SAdrian Chadd 	}
51748237774SAdrian Chadd 
5189af351f9SAdrian Chadd 	/* Attach spectral module */
5199af351f9SAdrian Chadd 	if (ath_spectral_attach(sc) < 0) {
5209af351f9SAdrian Chadd 		device_printf(sc->sc_dev,
5219af351f9SAdrian Chadd 		    "%s: unable to attach spectral\n", __func__);
5229af351f9SAdrian Chadd 		error = EIO;
5239af351f9SAdrian Chadd 		goto bad2;
5249af351f9SAdrian Chadd 	}
5259af351f9SAdrian Chadd 
526b70f530bSAdrian Chadd 	/* Attach bluetooth coexistence module */
527b70f530bSAdrian Chadd 	if (ath_btcoex_attach(sc) < 0) {
528b70f530bSAdrian Chadd 		device_printf(sc->sc_dev,
529b70f530bSAdrian Chadd 		    "%s: unable to attach bluetooth coexistence\n", __func__);
530b70f530bSAdrian Chadd 		error = EIO;
531b70f530bSAdrian Chadd 		goto bad2;
532b70f530bSAdrian Chadd 	}
533b70f530bSAdrian Chadd 
534*216ca234SAdrian Chadd 	/* Attach LNA diversity module */
535*216ca234SAdrian Chadd 	if (ath_lna_div_attach(sc) < 0) {
536*216ca234SAdrian Chadd 		device_printf(sc->sc_dev,
537*216ca234SAdrian Chadd 		    "%s: unable to attach LNA diversity\n", __func__);
538*216ca234SAdrian Chadd 		error = EIO;
539*216ca234SAdrian Chadd 		goto bad2;
540*216ca234SAdrian Chadd 	}
541*216ca234SAdrian Chadd 
54248237774SAdrian Chadd 	/* Start DFS processing tasklet */
54348237774SAdrian Chadd 	TASK_INIT(&sc->sc_dfstask, 0, ath_dfs_tasklet, sc);
54448237774SAdrian Chadd 
5453440495aSAdrian Chadd 	/* Configure LED state */
5463e50ec2cSSam Leffler 	sc->sc_blinking = 0;
547c42a7b7eSSam Leffler 	sc->sc_ledstate = 1;
5483e50ec2cSSam Leffler 	sc->sc_ledon = 0;			/* low true */
5493e50ec2cSSam Leffler 	sc->sc_ledidle = (2700*hz)/1000;	/* 2.7sec */
5503e50ec2cSSam Leffler 	callout_init(&sc->sc_ledtimer, CALLOUT_MPSAFE);
5513440495aSAdrian Chadd 
5523440495aSAdrian Chadd 	/*
5533440495aSAdrian Chadd 	 * Don't setup hardware-based blinking.
5543440495aSAdrian Chadd 	 *
5553440495aSAdrian Chadd 	 * Although some NICs may have this configured in the
5563440495aSAdrian Chadd 	 * default reset register values, the user may wish
5573440495aSAdrian Chadd 	 * to alter which pins have which function.
5583440495aSAdrian Chadd 	 *
5593440495aSAdrian Chadd 	 * The reference driver attaches the MAC network LED to GPIO1 and
5603440495aSAdrian Chadd 	 * the MAC power LED to GPIO2.  However, the DWA-552 cardbus
5613440495aSAdrian Chadd 	 * NIC has these reversed.
5623440495aSAdrian Chadd 	 */
5633440495aSAdrian Chadd 	sc->sc_hardled = (1 == 0);
5643440495aSAdrian Chadd 	sc->sc_led_net_pin = -1;
5653440495aSAdrian Chadd 	sc->sc_led_pwr_pin = -1;
566c42a7b7eSSam Leffler 	/*
567c42a7b7eSSam Leffler 	 * Auto-enable soft led processing for IBM cards and for
568c42a7b7eSSam Leffler 	 * 5211 minipci cards.  Users can also manually enable/disable
569c42a7b7eSSam Leffler 	 * support with a sysctl.
570c42a7b7eSSam Leffler 	 */
571c42a7b7eSSam Leffler 	sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
5726558ffd9SAdrian Chadd 	ath_led_config(sc);
573a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
5745591b213SSam Leffler 
5755591b213SSam Leffler 	ifp->if_softc = sc;
5765591b213SSam Leffler 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
577cd7dffd0SAdrian Chadd 	ifp->if_transmit = ath_transmit;
578cd7dffd0SAdrian Chadd 	ifp->if_qflush = ath_qflush;
5795591b213SSam Leffler 	ifp->if_ioctl = ath_ioctl;
5805591b213SSam Leffler 	ifp->if_init = ath_init;
581e50d35e6SMaxim Sobolev 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
582e50d35e6SMaxim Sobolev 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
583154b8df2SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
5845591b213SSam Leffler 
585c42a7b7eSSam Leffler 	ic->ic_ifp = ifp;
5865591b213SSam Leffler 	/* XXX not right but it's not used anywhere important */
5875591b213SSam Leffler 	ic->ic_phytype = IEEE80211_T_OFDM;
5885591b213SSam Leffler 	ic->ic_opmode = IEEE80211_M_STA;
589c42a7b7eSSam Leffler 	ic->ic_caps =
590c43feedeSSam Leffler 		  IEEE80211_C_STA		/* station mode */
591c43feedeSSam Leffler 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
592fe32c3efSSam Leffler 		| IEEE80211_C_HOSTAP		/* hostap mode */
593fe32c3efSSam Leffler 		| IEEE80211_C_MONITOR		/* monitor mode */
5947a04dc27SSam Leffler 		| IEEE80211_C_AHDEMO		/* adhoc demo mode */
595b032f27cSSam Leffler 		| IEEE80211_C_WDS		/* 4-address traffic works */
59659aa14a9SRui Paulo 		| IEEE80211_C_MBSS		/* mesh point link mode */
597fe32c3efSSam Leffler 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
598c42a7b7eSSam Leffler 		| IEEE80211_C_SHSLOT		/* short slot time supported */
599c42a7b7eSSam Leffler 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
6003b324f57SAdrian Chadd #ifndef	ATH_ENABLE_11N
60168e8e04eSSam Leffler 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
6023b324f57SAdrian Chadd #endif
60368e8e04eSSam Leffler 		| IEEE80211_C_TXFRAG		/* handle tx frags */
60410dc8de4SAdrian Chadd #ifdef	ATH_ENABLE_DFS
6057e97436bSAdrian Chadd 		| IEEE80211_C_DFS		/* Enable radar detection */
60610dc8de4SAdrian Chadd #endif
60701e7e035SSam Leffler 		;
608c42a7b7eSSam Leffler 	/*
609c42a7b7eSSam Leffler 	 * Query the hal to figure out h/w crypto support.
610c42a7b7eSSam Leffler 	 */
611c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
612b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
613c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
614b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_OCB;
615c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
616b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_CCM;
617c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
618b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_CKIP;
619c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
620b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIP;
621c42a7b7eSSam Leffler 		/*
622c42a7b7eSSam Leffler 		 * Check if h/w does the MIC and/or whether the
623c42a7b7eSSam Leffler 		 * separate key cache entries are required to
624c42a7b7eSSam Leffler 		 * handle both tx+rx MIC keys.
625c42a7b7eSSam Leffler 		 */
626c42a7b7eSSam Leffler 		if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
627b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
6285901d2d3SSam Leffler 		/*
6295901d2d3SSam Leffler 		 * If the h/w supports storing tx+rx MIC keys
6305901d2d3SSam Leffler 		 * in one cache slot automatically enable use.
6315901d2d3SSam Leffler 		 */
6325901d2d3SSam Leffler 		if (ath_hal_hastkipsplit(ah) ||
6335901d2d3SSam Leffler 		    !ath_hal_settkipsplit(ah, AH_FALSE))
634c42a7b7eSSam Leffler 			sc->sc_splitmic = 1;
635b032f27cSSam Leffler 		/*
636b032f27cSSam Leffler 		 * If the h/w can do TKIP MIC together with WME then
637b032f27cSSam Leffler 		 * we use it; otherwise we force the MIC to be done
638b032f27cSSam Leffler 		 * in software by the net80211 layer.
639b032f27cSSam Leffler 		 */
640b032f27cSSam Leffler 		if (ath_hal_haswmetkipmic(ah))
641b032f27cSSam Leffler 			sc->sc_wmetkipmic = 1;
642c42a7b7eSSam Leffler 	}
643e8fd88a3SSam Leffler 	sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR);
6449ac01d39SRui Paulo 	/*
6451ac5dac2SRui Paulo 	 * Check for multicast key search support.
6469ac01d39SRui Paulo 	 */
6479ac01d39SRui Paulo 	if (ath_hal_hasmcastkeysearch(sc->sc_ah) &&
6489ac01d39SRui Paulo 	    !ath_hal_getmcastkeysearch(sc->sc_ah)) {
6499ac01d39SRui Paulo 		ath_hal_setmcastkeysearch(sc->sc_ah, 1);
6509ac01d39SRui Paulo 	}
651e8fd88a3SSam Leffler 	sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah);
652c42a7b7eSSam Leffler 	/*
6535901d2d3SSam Leffler 	 * Mark key cache slots associated with global keys
6545901d2d3SSam Leffler 	 * as in use.  If we knew TKIP was not to be used we
6555901d2d3SSam Leffler 	 * could leave the +32, +64, and +32+64 slots free.
6565901d2d3SSam Leffler 	 */
6575901d2d3SSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
6585901d2d3SSam Leffler 		setbit(sc->sc_keymap, i);
6595901d2d3SSam Leffler 		setbit(sc->sc_keymap, i+64);
6605901d2d3SSam Leffler 		if (sc->sc_splitmic) {
6615901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32);
6625901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32+64);
6635901d2d3SSam Leffler 		}
6645901d2d3SSam Leffler 	}
6655901d2d3SSam Leffler 	/*
666c42a7b7eSSam Leffler 	 * TPC support can be done either with a global cap or
667c42a7b7eSSam Leffler 	 * per-packet support.  The latter is not available on
668c42a7b7eSSam Leffler 	 * all parts.  We're a bit pedantic here as all parts
669c42a7b7eSSam Leffler 	 * support a global cap.
670c42a7b7eSSam Leffler 	 */
671c59005e9SSam Leffler 	if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah))
672c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TXPMGT;
673c42a7b7eSSam Leffler 
674c42a7b7eSSam Leffler 	/*
675c42a7b7eSSam Leffler 	 * Mark WME capability only if we have sufficient
676c42a7b7eSSam Leffler 	 * hardware queues to do proper priority scheduling.
677c42a7b7eSSam Leffler 	 */
678c42a7b7eSSam Leffler 	if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
679c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_WME;
680c42a7b7eSSam Leffler 	/*
681e8fd88a3SSam Leffler 	 * Check for misc other capabilities.
682c42a7b7eSSam Leffler 	 */
683c42a7b7eSSam Leffler 	if (ath_hal_hasbursting(ah))
684c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_BURST;
685b032f27cSSam Leffler 	sc->sc_hasbmask = ath_hal_hasbssidmask(ah);
68659aa14a9SRui Paulo 	sc->sc_hasbmatch = ath_hal_hasbssidmatch(ah);
687b032f27cSSam Leffler 	sc->sc_hastsfadd = ath_hal_hastsfadjust(ah);
6888a2a6beeSAdrian Chadd 	sc->sc_rxslink = ath_hal_self_linked_final_rxdesc(ah);
689fc4de9b7SAdrian Chadd 	sc->sc_rxtsf32 = ath_hal_has_long_rxdesc_tsf(ah);
690dd6a574eSAdrian Chadd 	sc->sc_hasenforcetxop = ath_hal_hasenforcetxop(ah);
6913df7a8abSAdrian Chadd 	sc->sc_rx_lnamixer = ath_hal_hasrxlnamixer(ah);
692*216ca234SAdrian Chadd 	sc->sc_hasdivcomb = ath_hal_hasdivantcomb(ah);
693*216ca234SAdrian Chadd 
69468e8e04eSSam Leffler 	if (ath_hal_hasfastframes(ah))
69568e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_FF;
69659efa8b5SSam Leffler 	wmodes = ath_hal_getwirelessmodes(ah);
697411373ebSSam Leffler 	if (wmodes & (HAL_MODE_108G|HAL_MODE_TURBO))
69868e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TURBOP;
699584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
70010ad9a77SSam Leffler 	if (ath_hal_macversion(ah) > 0x78) {
70110ad9a77SSam Leffler 		ic->ic_caps |= IEEE80211_C_TDMA; /* capable of TDMA */
70210ad9a77SSam Leffler 		ic->ic_tdma_update = ath_tdma_update;
70310ad9a77SSam Leffler 	}
70410ad9a77SSam Leffler #endif
70567397d39SAdrian Chadd 
70667397d39SAdrian Chadd 	/*
7079c85ff91SAdrian Chadd 	 * TODO: enforce that at least this many frames are available
7089c85ff91SAdrian Chadd 	 * in the txbuf list before allowing data frames (raw or
7099c85ff91SAdrian Chadd 	 * otherwise) to be transmitted.
7109c85ff91SAdrian Chadd 	 */
7119c85ff91SAdrian Chadd 	sc->sc_txq_data_minfree = 10;
7129c85ff91SAdrian Chadd 	/*
7139c85ff91SAdrian Chadd 	 * Leave this as default to maintain legacy behaviour.
7149c85ff91SAdrian Chadd 	 * Shortening the cabq/mcastq may end up causing some
7159c85ff91SAdrian Chadd 	 * undesirable behaviour.
7169c85ff91SAdrian Chadd 	 */
7179c85ff91SAdrian Chadd 	sc->sc_txq_mcastq_maxdepth = ath_txbuf;
7189c85ff91SAdrian Chadd 
7197dcb2beaSAdrian Chadd 	/*
72022a3aee6SAdrian Chadd 	 * How deep can the node software TX queue get whilst it's asleep.
72122a3aee6SAdrian Chadd 	 */
72222a3aee6SAdrian Chadd 	sc->sc_txq_node_psq_maxdepth = 16;
72322a3aee6SAdrian Chadd 
72422a3aee6SAdrian Chadd 	/*
7257dcb2beaSAdrian Chadd 	 * Default the maximum queue depth for a given node
7267dcb2beaSAdrian Chadd 	 * to 1/4'th the TX buffers, or 64, whichever
7277dcb2beaSAdrian Chadd 	 * is larger.
7287dcb2beaSAdrian Chadd 	 */
7297dcb2beaSAdrian Chadd 	sc->sc_txq_node_maxdepth = MAX(64, ath_txbuf / 4);
7307dcb2beaSAdrian Chadd 
731b837332dSAdrian Chadd 	/* Enable CABQ by default */
732b837332dSAdrian Chadd 	sc->sc_cabq_enable = 1;
733b837332dSAdrian Chadd 
7349c85ff91SAdrian Chadd 	/*
735a865860dSAdrian Chadd 	 * Allow the TX and RX chainmasks to be overridden by
736a865860dSAdrian Chadd 	 * environment variables and/or device.hints.
737a865860dSAdrian Chadd 	 *
738a865860dSAdrian Chadd 	 * This must be done early - before the hardware is
739a865860dSAdrian Chadd 	 * calibrated or before the 802.11n stream calculation
740a865860dSAdrian Chadd 	 * is done.
741a865860dSAdrian Chadd 	 */
742a865860dSAdrian Chadd 	if (resource_int_value(device_get_name(sc->sc_dev),
743a865860dSAdrian Chadd 	    device_get_unit(sc->sc_dev), "rx_chainmask",
744a865860dSAdrian Chadd 	    &rx_chainmask) == 0) {
745a865860dSAdrian Chadd 		device_printf(sc->sc_dev, "Setting RX chainmask to 0x%x\n",
746a865860dSAdrian Chadd 		    rx_chainmask);
747a865860dSAdrian Chadd 		(void) ath_hal_setrxchainmask(sc->sc_ah, rx_chainmask);
748a865860dSAdrian Chadd 	}
749a865860dSAdrian Chadd 	if (resource_int_value(device_get_name(sc->sc_dev),
750a865860dSAdrian Chadd 	    device_get_unit(sc->sc_dev), "tx_chainmask",
751a865860dSAdrian Chadd 	    &tx_chainmask) == 0) {
752a865860dSAdrian Chadd 		device_printf(sc->sc_dev, "Setting TX chainmask to 0x%x\n",
753a865860dSAdrian Chadd 		    tx_chainmask);
754dc8552d5SAdrian Chadd 		(void) ath_hal_settxchainmask(sc->sc_ah, tx_chainmask);
755a865860dSAdrian Chadd 	}
756a865860dSAdrian Chadd 
757af017101SAdrian Chadd 	/*
758ff5b5634SAdrian Chadd 	 * Query the TX/RX chainmask configuration.
759ff5b5634SAdrian Chadd 	 *
760ff5b5634SAdrian Chadd 	 * This is only relevant for 11n devices.
761ff5b5634SAdrian Chadd 	 */
762ff5b5634SAdrian Chadd 	ath_hal_getrxchainmask(ah, &sc->sc_rxchainmask);
763ff5b5634SAdrian Chadd 	ath_hal_gettxchainmask(ah, &sc->sc_txchainmask);
764ff5b5634SAdrian Chadd 
765ff5b5634SAdrian Chadd 	/*
766af017101SAdrian Chadd 	 * Disable MRR with protected frames by default.
767af017101SAdrian Chadd 	 * Only 802.11n series NICs can handle this.
768af017101SAdrian Chadd 	 */
769af017101SAdrian Chadd 	sc->sc_mrrprot = 0;	/* XXX should be a capability */
770af017101SAdrian Chadd 
7715540369bSAdrian Chadd 	/*
7725540369bSAdrian Chadd 	 * Query the enterprise mode information the HAL.
7735540369bSAdrian Chadd 	 */
7745540369bSAdrian Chadd 	if (ath_hal_getcapability(ah, HAL_CAP_ENTERPRISE_MODE, 0,
7755540369bSAdrian Chadd 	    &sc->sc_ent_cfg) == HAL_OK)
7765540369bSAdrian Chadd 		sc->sc_use_ent = 1;
7775540369bSAdrian Chadd 
7788fd67f92SAdrian Chadd #ifdef	ATH_ENABLE_11N
77967397d39SAdrian Chadd 	/*
78067397d39SAdrian Chadd 	 * Query HT capabilities
78167397d39SAdrian Chadd 	 */
78267397d39SAdrian Chadd 	if (ath_hal_getcapability(ah, HAL_CAP_HT, 0, NULL) == HAL_OK &&
78367397d39SAdrian Chadd 	    (wmodes & (HAL_MODE_HT20 | HAL_MODE_HT40))) {
7846f4fb2d8SAdrian Chadd 		uint32_t rxs, txs;
78567397d39SAdrian Chadd 
78667397d39SAdrian Chadd 		device_printf(sc->sc_dev, "[HT] enabling HT modes\n");
787af017101SAdrian Chadd 
788af017101SAdrian Chadd 		sc->sc_mrrprot = 1;	/* XXX should be a capability */
789af017101SAdrian Chadd 
79067397d39SAdrian Chadd 		ic->ic_htcaps = IEEE80211_HTC_HT	/* HT operation */
79167397d39SAdrian Chadd 			    | IEEE80211_HTC_AMPDU	/* A-MPDU tx/rx */
79267397d39SAdrian Chadd 			    | IEEE80211_HTC_AMSDU	/* A-MSDU tx/rx */
7937e97436bSAdrian Chadd 			    | IEEE80211_HTCAP_MAXAMSDU_3839
7947e97436bSAdrian Chadd 			    				/* max A-MSDU length */
79567397d39SAdrian Chadd 			    | IEEE80211_HTCAP_SMPS_OFF;	/* SM power save off */
79667397d39SAdrian Chadd 			;
79767397d39SAdrian Chadd 
79876355edbSAdrian Chadd 		/*
79976355edbSAdrian Chadd 		 * Enable short-GI for HT20 only if the hardware
80076355edbSAdrian Chadd 		 * advertises support.
80176355edbSAdrian Chadd 		 * Notably, anything earlier than the AR9287 doesn't.
80276355edbSAdrian Chadd 		 */
80376355edbSAdrian Chadd 		if ((ath_hal_getcapability(ah,
80476355edbSAdrian Chadd 		    HAL_CAP_HT20_SGI, 0, NULL) == HAL_OK) &&
80576355edbSAdrian Chadd 		    (wmodes & HAL_MODE_HT20)) {
80676355edbSAdrian Chadd 			device_printf(sc->sc_dev,
80776355edbSAdrian Chadd 			    "[HT] enabling short-GI in 20MHz mode\n");
80876355edbSAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
80976355edbSAdrian Chadd 		}
81076355edbSAdrian Chadd 
81167397d39SAdrian Chadd 		if (wmodes & HAL_MODE_HT40)
81267397d39SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40
81367397d39SAdrian Chadd 			    |  IEEE80211_HTCAP_SHORTGI40;
81467397d39SAdrian Chadd 
81567397d39SAdrian Chadd 		/*
8167e97436bSAdrian Chadd 		 * TX/RX streams need to be taken into account when
8177e97436bSAdrian Chadd 		 * negotiating which MCS rates it'll receive and
81867397d39SAdrian Chadd 		 * what MCS rates are available for TX.
81967397d39SAdrian Chadd 		 */
82054517070SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 0, &txs);
82154517070SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 1, &rxs);
82267397d39SAdrian Chadd 		ic->ic_txstream = txs;
82367397d39SAdrian Chadd 		ic->ic_rxstream = rxs;
82467397d39SAdrian Chadd 
8256606ba81SAdrian Chadd 		/*
8266606ba81SAdrian Chadd 		 * Setup TX and RX STBC based on what the HAL allows and
8276606ba81SAdrian Chadd 		 * the currently configured chainmask set.
8286606ba81SAdrian Chadd 		 * Ie - don't enable STBC TX if only one chain is enabled.
8296606ba81SAdrian Chadd 		 * STBC RX is fine on a single RX chain; it just won't
8306606ba81SAdrian Chadd 		 * provide any real benefit.
8316606ba81SAdrian Chadd 		 */
8326606ba81SAdrian Chadd 		if (ath_hal_getcapability(ah, HAL_CAP_RX_STBC, 0,
8336606ba81SAdrian Chadd 		    NULL) == HAL_OK) {
8346606ba81SAdrian Chadd 			sc->sc_rx_stbc = 1;
8356606ba81SAdrian Chadd 			device_printf(sc->sc_dev,
8366606ba81SAdrian Chadd 			    "[HT] 1 stream STBC receive enabled\n");
8376606ba81SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_1STREAM;
8386606ba81SAdrian Chadd 		}
8396606ba81SAdrian Chadd 		if (txs > 1 && ath_hal_getcapability(ah, HAL_CAP_TX_STBC, 0,
8406606ba81SAdrian Chadd 		    NULL) == HAL_OK) {
8416606ba81SAdrian Chadd 			sc->sc_tx_stbc = 1;
8426606ba81SAdrian Chadd 			device_printf(sc->sc_dev,
8436606ba81SAdrian Chadd 			    "[HT] 1 stream STBC transmit enabled\n");
8446606ba81SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
8456606ba81SAdrian Chadd 		}
8466606ba81SAdrian Chadd 
847ce656facSAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_RTS_AGGR_LIMIT, 1,
848ce656facSAdrian Chadd 		    &sc->sc_rts_aggr_limit);
849ce656facSAdrian Chadd 		if (sc->sc_rts_aggr_limit != (64 * 1024))
850ce656facSAdrian Chadd 			device_printf(sc->sc_dev,
851ce656facSAdrian Chadd 			    "[HT] RTS aggregates limited to %d KiB\n",
852ce656facSAdrian Chadd 			    sc->sc_rts_aggr_limit / 1024);
853ce656facSAdrian Chadd 
8547e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
8557e97436bSAdrian Chadd 		    "[HT] %d RX streams; %d TX streams\n", rxs, txs);
85667397d39SAdrian Chadd 	}
85767397d39SAdrian Chadd #endif
85867397d39SAdrian Chadd 
859c42a7b7eSSam Leffler 	/*
860f8aa9fd5SAdrian Chadd 	 * Initial aggregation settings.
861f8aa9fd5SAdrian Chadd 	 */
86272910f03SAdrian Chadd 	sc->sc_hwq_limit_aggr = ATH_AGGR_MIN_QDEPTH;
86372910f03SAdrian Chadd 	sc->sc_hwq_limit_nonaggr = ATH_NONAGGR_MIN_QDEPTH;
864f8aa9fd5SAdrian Chadd 	sc->sc_tid_hwq_lo = ATH_AGGR_SCHED_LOW;
865f8aa9fd5SAdrian Chadd 	sc->sc_tid_hwq_hi = ATH_AGGR_SCHED_HIGH;
8664a502c33SAdrian Chadd 	sc->sc_aggr_limit = ATH_AGGR_MAXSIZE;
867a54ecf78SAdrian Chadd 	sc->sc_delim_min_pad = 0;
868f8aa9fd5SAdrian Chadd 
869f8aa9fd5SAdrian Chadd 	/*
870ddbe3036SAdrian Chadd 	 * Check if the hardware requires PCI register serialisation.
871ddbe3036SAdrian Chadd 	 * Some of the Owl based MACs require this.
872ddbe3036SAdrian Chadd 	 */
873ddbe3036SAdrian Chadd 	if (mp_ncpus > 1 &&
874ddbe3036SAdrian Chadd 	    ath_hal_getcapability(ah, HAL_CAP_SERIALISE_WAR,
875ddbe3036SAdrian Chadd 	     0, NULL) == HAL_OK) {
876ddbe3036SAdrian Chadd 		sc->sc_ah->ah_config.ah_serialise_reg_war = 1;
8777e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
8787e97436bSAdrian Chadd 		    "Enabling register serialisation\n");
879ddbe3036SAdrian Chadd 	}
880ddbe3036SAdrian Chadd 
881ddbe3036SAdrian Chadd 	/*
882f0db652cSAdrian Chadd 	 * Initialise the deferred completed RX buffer list.
883f0db652cSAdrian Chadd 	 */
8845d4dedadSAdrian Chadd 	TAILQ_INIT(&sc->sc_rx_rxlist[HAL_RX_QUEUE_HP]);
8855d4dedadSAdrian Chadd 	TAILQ_INIT(&sc->sc_rx_rxlist[HAL_RX_QUEUE_LP]);
886f0db652cSAdrian Chadd 
887f0db652cSAdrian Chadd 	/*
888c42a7b7eSSam Leffler 	 * Indicate we need the 802.11 header padded to a
889c42a7b7eSSam Leffler 	 * 32-bit boundary for 4-address and QoS frames.
890c42a7b7eSSam Leffler 	 */
891c42a7b7eSSam Leffler 	ic->ic_flags |= IEEE80211_F_DATAPAD;
892c42a7b7eSSam Leffler 
893c42a7b7eSSam Leffler 	/*
894c42a7b7eSSam Leffler 	 * Query the hal about antenna support.
895c42a7b7eSSam Leffler 	 */
896c42a7b7eSSam Leffler 	sc->sc_defant = ath_hal_getdefantenna(ah);
897c42a7b7eSSam Leffler 
898c42a7b7eSSam Leffler 	/*
899c42a7b7eSSam Leffler 	 * Not all chips have the VEOL support we want to
900c42a7b7eSSam Leffler 	 * use with IBSS beacons; check here for it.
901c42a7b7eSSam Leffler 	 */
902c42a7b7eSSam Leffler 	sc->sc_hasveol = ath_hal_hasveol(ah);
9035591b213SSam Leffler 
9045591b213SSam Leffler 	/* get mac address from hardware */
90529aca940SSam Leffler 	ath_hal_getmac(ah, macaddr);
906b032f27cSSam Leffler 	if (sc->sc_hasbmask)
907b032f27cSSam Leffler 		ath_hal_getbssidmask(ah, sc->sc_hwbssidmask);
9085591b213SSam Leffler 
909b032f27cSSam Leffler 	/* NB: used to size node table key mapping array */
910b032f27cSSam Leffler 	ic->ic_max_keyix = sc->sc_keymax;
9115591b213SSam Leffler 	/* call MI attach routine. */
91229aca940SSam Leffler 	ieee80211_ifattach(ic, macaddr);
913b032f27cSSam Leffler 	ic->ic_setregdomain = ath_setregdomain;
914b032f27cSSam Leffler 	ic->ic_getradiocaps = ath_getradiocaps;
915b032f27cSSam Leffler 	sc->sc_opmode = HAL_M_STA;
916b032f27cSSam Leffler 
9175591b213SSam Leffler 	/* override default methods */
918b032f27cSSam Leffler 	ic->ic_newassoc = ath_newassoc;
919b032f27cSSam Leffler 	ic->ic_updateslot = ath_updateslot;
920b032f27cSSam Leffler 	ic->ic_wme.wme_update = ath_wme_update;
921b032f27cSSam Leffler 	ic->ic_vap_create = ath_vap_create;
922b032f27cSSam Leffler 	ic->ic_vap_delete = ath_vap_delete;
923b032f27cSSam Leffler 	ic->ic_raw_xmit = ath_raw_xmit;
924b032f27cSSam Leffler 	ic->ic_update_mcast = ath_update_mcast;
925b032f27cSSam Leffler 	ic->ic_update_promisc = ath_update_promisc;
9265591b213SSam Leffler 	ic->ic_node_alloc = ath_node_alloc;
9271e774079SSam Leffler 	sc->sc_node_free = ic->ic_node_free;
9285591b213SSam Leffler 	ic->ic_node_free = ath_node_free;
9294afa805eSAdrian Chadd 	sc->sc_node_cleanup = ic->ic_node_cleanup;
9304afa805eSAdrian Chadd 	ic->ic_node_cleanup = ath_node_cleanup;
93168e8e04eSSam Leffler 	ic->ic_node_getsignal = ath_node_getsignal;
93268e8e04eSSam Leffler 	ic->ic_scan_start = ath_scan_start;
93368e8e04eSSam Leffler 	ic->ic_scan_end = ath_scan_end;
93468e8e04eSSam Leffler 	ic->ic_set_channel = ath_set_channel;
935fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
936eb6f0de0SAdrian Chadd 	/* 802.11n specific - but just override anyway */
937eb6f0de0SAdrian Chadd 	sc->sc_addba_request = ic->ic_addba_request;
938eb6f0de0SAdrian Chadd 	sc->sc_addba_response = ic->ic_addba_response;
939eb6f0de0SAdrian Chadd 	sc->sc_addba_stop = ic->ic_addba_stop;
940eb6f0de0SAdrian Chadd 	sc->sc_bar_response = ic->ic_bar_response;
941eb6f0de0SAdrian Chadd 	sc->sc_addba_response_timeout = ic->ic_addba_response_timeout;
942eb6f0de0SAdrian Chadd 
943eb6f0de0SAdrian Chadd 	ic->ic_addba_request = ath_addba_request;
944eb6f0de0SAdrian Chadd 	ic->ic_addba_response = ath_addba_response;
945eb6f0de0SAdrian Chadd 	ic->ic_addba_response_timeout = ath_addba_response_timeout;
946eb6f0de0SAdrian Chadd 	ic->ic_addba_stop = ath_addba_stop;
947eb6f0de0SAdrian Chadd 	ic->ic_bar_response = ath_bar_response;
948eb6f0de0SAdrian Chadd 
949fdd72b4aSAdrian Chadd 	ic->ic_update_chw = ath_update_chw;
950fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
951fdd72b4aSAdrian Chadd 
952e1b5ab97SAdrian Chadd #ifdef	ATH_ENABLE_RADIOTAP_VENDOR_EXT
953e1b5ab97SAdrian Chadd 	/*
954e1b5ab97SAdrian Chadd 	 * There's one vendor bitmap entry in the RX radiotap
955e1b5ab97SAdrian Chadd 	 * header; make sure that's taken into account.
956e1b5ab97SAdrian Chadd 	 */
957e1b5ab97SAdrian Chadd 	ieee80211_radiotap_attachv(ic,
958e1b5ab97SAdrian Chadd 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th), 0,
959e1b5ab97SAdrian Chadd 		ATH_TX_RADIOTAP_PRESENT,
960e1b5ab97SAdrian Chadd 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th), 1,
961e1b5ab97SAdrian Chadd 		ATH_RX_RADIOTAP_PRESENT);
962e1b5ab97SAdrian Chadd #else
963e1b5ab97SAdrian Chadd 	/*
964e1b5ab97SAdrian Chadd 	 * No vendor bitmap/extensions are present.
965e1b5ab97SAdrian Chadd 	 */
9665463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
9675463c4a4SSam Leffler 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
9685463c4a4SSam Leffler 		ATH_TX_RADIOTAP_PRESENT,
9695463c4a4SSam Leffler 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
9705463c4a4SSam Leffler 		ATH_RX_RADIOTAP_PRESENT);
971e1b5ab97SAdrian Chadd #endif	/* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
9725463c4a4SSam Leffler 
9734866e6c2SSam Leffler 	/*
974bdbb6e5bSAdrian Chadd 	 * Setup the ALQ logging if required
975bdbb6e5bSAdrian Chadd 	 */
97689d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
977bdbb6e5bSAdrian Chadd 	if_ath_alq_init(&sc->sc_alq, device_get_nameunit(sc->sc_dev));
978bb327d28SAdrian Chadd 	if_ath_alq_setcfg(&sc->sc_alq,
979bb327d28SAdrian Chadd 	    sc->sc_ah->ah_macVersion,
980bb327d28SAdrian Chadd 	    sc->sc_ah->ah_macRev,
981bb327d28SAdrian Chadd 	    sc->sc_ah->ah_phyRev,
982bb327d28SAdrian Chadd 	    sc->sc_ah->ah_magic);
983bdbb6e5bSAdrian Chadd #endif
984bdbb6e5bSAdrian Chadd 
985bdbb6e5bSAdrian Chadd 	/*
9864866e6c2SSam Leffler 	 * Setup dynamic sysctl's now that country code and
9874866e6c2SSam Leffler 	 * regdomain are available from the hal.
9884866e6c2SSam Leffler 	 */
9894866e6c2SSam Leffler 	ath_sysctlattach(sc);
990e8dabfbeSAdrian Chadd 	ath_sysctl_stats_attach(sc);
99137931a35SAdrian Chadd 	ath_sysctl_hal_attach(sc);
99273454c73SSam Leffler 
993c42a7b7eSSam Leffler 	if (bootverbose)
994c42a7b7eSSam Leffler 		ieee80211_announce(ic);
995c42a7b7eSSam Leffler 	ath_announce(sc);
9965591b213SSam Leffler 	return 0;
997b28b4653SSam Leffler bad2:
998c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
999b28b4653SSam Leffler 	ath_desc_free(sc);
10003fdfc330SAdrian Chadd 	ath_txdma_teardown(sc);
10013d184db2SAdrian Chadd 	ath_rxdma_teardown(sc);
10025591b213SSam Leffler bad:
10035591b213SSam Leffler 	if (ah)
10045591b213SSam Leffler 		ath_hal_detach(ah);
10058bf40208SAdrian Chadd 
10068bf40208SAdrian Chadd 	/*
10078bf40208SAdrian Chadd 	 * To work around scoping issues with CURVNET_SET/CURVNET_RESTORE..
10088bf40208SAdrian Chadd 	 */
10098bf40208SAdrian Chadd 	if (ifp != NULL && ifp->if_vnet) {
1010a93c5097SAdrian Chadd 		CURVNET_SET(ifp->if_vnet);
1011fc74a9f9SBrooks Davis 		if_free(ifp);
1012a93c5097SAdrian Chadd 		CURVNET_RESTORE();
10138bf40208SAdrian Chadd 	} else if (ifp != NULL)
10148bf40208SAdrian Chadd 		if_free(ifp);
10155591b213SSam Leffler 	sc->sc_invalid = 1;
10165591b213SSam Leffler 	return error;
10175591b213SSam Leffler }
10185591b213SSam Leffler 
10195591b213SSam Leffler int
10205591b213SSam Leffler ath_detach(struct ath_softc *sc)
10215591b213SSam Leffler {
1022fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
10235591b213SSam Leffler 
1024c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1025c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
10265591b213SSam Leffler 
1027c42a7b7eSSam Leffler 	/*
1028c42a7b7eSSam Leffler 	 * NB: the order of these is important:
102971b85077SSam Leffler 	 * o stop the chip so no more interrupts will fire
1030c42a7b7eSSam Leffler 	 * o call the 802.11 layer before detaching the hal to
1031c42a7b7eSSam Leffler 	 *   insure callbacks into the driver to delete global
1032c42a7b7eSSam Leffler 	 *   key cache entries can be handled
103371b85077SSam Leffler 	 * o free the taskqueue which drains any pending tasks
1034c42a7b7eSSam Leffler 	 * o reclaim the tx queue data structures after calling
1035c42a7b7eSSam Leffler 	 *   the 802.11 layer as we'll get called back to reclaim
1036c42a7b7eSSam Leffler 	 *   node state and potentially want to use them
1037c42a7b7eSSam Leffler 	 * o to cleanup the tx queues the hal is called, so detach
1038c42a7b7eSSam Leffler 	 *   it last
1039c42a7b7eSSam Leffler 	 * Other than that, it's straightforward...
1040c42a7b7eSSam Leffler 	 */
104171b85077SSam Leffler 	ath_stop(ifp);
1042b032f27cSSam Leffler 	ieee80211_ifdetach(ifp->if_l2com);
104371b85077SSam Leffler 	taskqueue_free(sc->sc_tq);
104486e07743SSam Leffler #ifdef ATH_TX99_DIAG
104586e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
104686e07743SSam Leffler 		sc->sc_tx99->detach(sc->sc_tx99);
104786e07743SSam Leffler #endif
1048c42a7b7eSSam Leffler 	ath_rate_detach(sc->sc_rc);
104989d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
1050bdbb6e5bSAdrian Chadd 	if_ath_alq_tidyup(&sc->sc_alq);
1051bdbb6e5bSAdrian Chadd #endif
1052*216ca234SAdrian Chadd 	ath_lna_div_detach(sc);
1053b70f530bSAdrian Chadd 	ath_btcoex_detach(sc);
10549af351f9SAdrian Chadd 	ath_spectral_detach(sc);
105548237774SAdrian Chadd 	ath_dfs_detach(sc);
10565591b213SSam Leffler 	ath_desc_free(sc);
10574bf404eaSAdrian Chadd 	ath_txdma_teardown(sc);
10583d184db2SAdrian Chadd 	ath_rxdma_teardown(sc);
1059c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
106071b85077SSam Leffler 	ath_hal_detach(sc->sc_ah);	/* NB: sets chip in full sleep */
1061a93c5097SAdrian Chadd 
1062a93c5097SAdrian Chadd 	CURVNET_SET(ifp->if_vnet);
1063c4c6f08fSRuslan Ermilov 	if_free(ifp);
1064a93c5097SAdrian Chadd 	CURVNET_RESTORE();
1065f0b2a0beSSam Leffler 
10665591b213SSam Leffler 	return 0;
10675591b213SSam Leffler }
10685591b213SSam Leffler 
1069b032f27cSSam Leffler /*
1070b032f27cSSam Leffler  * MAC address handling for multiple BSS on the same radio.
1071b032f27cSSam Leffler  * The first vap uses the MAC address from the EEPROM.  For
1072b032f27cSSam Leffler  * subsequent vap's we set the U/L bit (bit 1) in the MAC
1073b032f27cSSam Leffler  * address and use the next six bits as an index.
1074b032f27cSSam Leffler  */
1075b032f27cSSam Leffler static void
1076b032f27cSSam Leffler assign_address(struct ath_softc *sc, uint8_t mac[IEEE80211_ADDR_LEN], int clone)
1077b032f27cSSam Leffler {
1078b032f27cSSam Leffler 	int i;
1079b032f27cSSam Leffler 
1080b032f27cSSam Leffler 	if (clone && sc->sc_hasbmask) {
1081b032f27cSSam Leffler 		/* NB: we only do this if h/w supports multiple bssid */
1082b032f27cSSam Leffler 		for (i = 0; i < 8; i++)
1083b032f27cSSam Leffler 			if ((sc->sc_bssidmask & (1<<i)) == 0)
1084b032f27cSSam Leffler 				break;
1085b032f27cSSam Leffler 		if (i != 0)
1086b032f27cSSam Leffler 			mac[0] |= (i << 2)|0x2;
1087b032f27cSSam Leffler 	} else
1088b032f27cSSam Leffler 		i = 0;
1089b032f27cSSam Leffler 	sc->sc_bssidmask |= 1<<i;
1090b032f27cSSam Leffler 	sc->sc_hwbssidmask[0] &= ~mac[0];
1091b032f27cSSam Leffler 	if (i == 0)
1092b032f27cSSam Leffler 		sc->sc_nbssid0++;
1093b032f27cSSam Leffler }
1094b032f27cSSam Leffler 
1095b032f27cSSam Leffler static void
1096b032f27cSSam Leffler reclaim_address(struct ath_softc *sc, const uint8_t mac[IEEE80211_ADDR_LEN])
1097b032f27cSSam Leffler {
1098b032f27cSSam Leffler 	int i = mac[0] >> 2;
1099b032f27cSSam Leffler 	uint8_t mask;
1100b032f27cSSam Leffler 
1101b032f27cSSam Leffler 	if (i != 0 || --sc->sc_nbssid0 == 0) {
1102b032f27cSSam Leffler 		sc->sc_bssidmask &= ~(1<<i);
1103b032f27cSSam Leffler 		/* recalculate bssid mask from remaining addresses */
1104b032f27cSSam Leffler 		mask = 0xff;
1105b032f27cSSam Leffler 		for (i = 1; i < 8; i++)
1106b032f27cSSam Leffler 			if (sc->sc_bssidmask & (1<<i))
1107b032f27cSSam Leffler 				mask &= ~((i<<2)|0x2);
1108b032f27cSSam Leffler 		sc->sc_hwbssidmask[0] |= mask;
1109b032f27cSSam Leffler 	}
1110b032f27cSSam Leffler }
1111b032f27cSSam Leffler 
1112b032f27cSSam Leffler /*
1113b032f27cSSam Leffler  * Assign a beacon xmit slot.  We try to space out
1114b032f27cSSam Leffler  * assignments so when beacons are staggered the
1115b032f27cSSam Leffler  * traffic coming out of the cab q has maximal time
1116b032f27cSSam Leffler  * to go out before the next beacon is scheduled.
1117b032f27cSSam Leffler  */
1118b032f27cSSam Leffler static int
1119b032f27cSSam Leffler assign_bslot(struct ath_softc *sc)
1120b032f27cSSam Leffler {
1121b032f27cSSam Leffler 	u_int slot, free;
1122b032f27cSSam Leffler 
1123b032f27cSSam Leffler 	free = 0;
1124b032f27cSSam Leffler 	for (slot = 0; slot < ATH_BCBUF; slot++)
1125b032f27cSSam Leffler 		if (sc->sc_bslot[slot] == NULL) {
1126b032f27cSSam Leffler 			if (sc->sc_bslot[(slot+1)%ATH_BCBUF] == NULL &&
1127b032f27cSSam Leffler 			    sc->sc_bslot[(slot-1)%ATH_BCBUF] == NULL)
1128b032f27cSSam Leffler 				return slot;
1129b032f27cSSam Leffler 			free = slot;
1130b032f27cSSam Leffler 			/* NB: keep looking for a double slot */
1131b032f27cSSam Leffler 		}
1132b032f27cSSam Leffler 	return free;
1133b032f27cSSam Leffler }
1134b032f27cSSam Leffler 
1135b032f27cSSam Leffler static struct ieee80211vap *
1136fcd9500fSBernhard Schmidt ath_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
1137fcd9500fSBernhard Schmidt     enum ieee80211_opmode opmode, int flags,
1138b032f27cSSam Leffler     const uint8_t bssid[IEEE80211_ADDR_LEN],
1139b032f27cSSam Leffler     const uint8_t mac0[IEEE80211_ADDR_LEN])
1140b032f27cSSam Leffler {
1141b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
1142b032f27cSSam Leffler 	struct ath_vap *avp;
1143b032f27cSSam Leffler 	struct ieee80211vap *vap;
1144b032f27cSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN];
1145fcd9500fSBernhard Schmidt 	int needbeacon, error;
1146fcd9500fSBernhard Schmidt 	enum ieee80211_opmode ic_opmode;
1147b032f27cSSam Leffler 
1148b032f27cSSam Leffler 	avp = (struct ath_vap *) malloc(sizeof(struct ath_vap),
1149b032f27cSSam Leffler 	    M_80211_VAP, M_WAITOK | M_ZERO);
1150b032f27cSSam Leffler 	needbeacon = 0;
1151b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(mac, mac0);
1152b032f27cSSam Leffler 
1153b032f27cSSam Leffler 	ATH_LOCK(sc);
1154a8962181SSam Leffler 	ic_opmode = opmode;		/* default to opmode of new vap */
1155b032f27cSSam Leffler 	switch (opmode) {
1156b032f27cSSam Leffler 	case IEEE80211_M_STA:
1157a8962181SSam Leffler 		if (sc->sc_nstavaps != 0) {	/* XXX only 1 for now */
1158b032f27cSSam Leffler 			device_printf(sc->sc_dev, "only 1 sta vap supported\n");
1159b032f27cSSam Leffler 			goto bad;
1160b032f27cSSam Leffler 		}
1161b032f27cSSam Leffler 		if (sc->sc_nvaps) {
1162b032f27cSSam Leffler 			/*
1163a8962181SSam Leffler 			 * With multiple vaps we must fall back
1164a8962181SSam Leffler 			 * to s/w beacon miss handling.
1165b032f27cSSam Leffler 			 */
1166b032f27cSSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
1167b032f27cSSam Leffler 		}
1168a8962181SSam Leffler 		if (flags & IEEE80211_CLONE_NOBEACONS) {
1169a8962181SSam Leffler 			/*
1170a8962181SSam Leffler 			 * Station mode w/o beacons are implemented w/ AP mode.
1171a8962181SSam Leffler 			 */
1172b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
1173a8962181SSam Leffler 		}
1174b032f27cSSam Leffler 		break;
1175b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1176b032f27cSSam Leffler 		if (sc->sc_nvaps != 0) {	/* XXX only 1 for now */
1177b032f27cSSam Leffler 			device_printf(sc->sc_dev,
1178b032f27cSSam Leffler 			    "only 1 ibss vap supported\n");
1179b032f27cSSam Leffler 			goto bad;
1180b032f27cSSam Leffler 		}
1181b032f27cSSam Leffler 		needbeacon = 1;
1182b032f27cSSam Leffler 		break;
1183b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1184584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
118510ad9a77SSam Leffler 		if (flags & IEEE80211_CLONE_TDMA) {
1186a8962181SSam Leffler 			if (sc->sc_nvaps != 0) {
1187a8962181SSam Leffler 				device_printf(sc->sc_dev,
1188a8962181SSam Leffler 				    "only 1 tdma vap supported\n");
1189a8962181SSam Leffler 				goto bad;
1190a8962181SSam Leffler 			}
119110ad9a77SSam Leffler 			needbeacon = 1;
119210ad9a77SSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
119310ad9a77SSam Leffler 		}
1194b032f27cSSam Leffler 		/* fall thru... */
119510ad9a77SSam Leffler #endif
1196b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1197b032f27cSSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode != opmode) {
1198a8962181SSam Leffler 			/*
1199a8962181SSam Leffler 			 * Adopt existing mode.  Adding a monitor or ahdemo
1200a8962181SSam Leffler 			 * vap to an existing configuration is of dubious
1201a8962181SSam Leffler 			 * value but should be ok.
1202a8962181SSam Leffler 			 */
1203b032f27cSSam Leffler 			/* XXX not right for monitor mode */
1204b032f27cSSam Leffler 			ic_opmode = ic->ic_opmode;
1205a8962181SSam Leffler 		}
1206b032f27cSSam Leffler 		break;
1207b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
120859aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1209b032f27cSSam Leffler 		needbeacon = 1;
1210a8962181SSam Leffler 		break;
1211b032f27cSSam Leffler 	case IEEE80211_M_WDS:
1212a8962181SSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode == IEEE80211_M_STA) {
1213b032f27cSSam Leffler 			device_printf(sc->sc_dev,
1214b032f27cSSam Leffler 			    "wds not supported in sta mode\n");
1215b032f27cSSam Leffler 			goto bad;
1216b032f27cSSam Leffler 		}
1217b032f27cSSam Leffler 		/*
1218b032f27cSSam Leffler 		 * Silently remove any request for a unique
1219b032f27cSSam Leffler 		 * bssid; WDS vap's always share the local
1220b032f27cSSam Leffler 		 * mac address.
1221b032f27cSSam Leffler 		 */
1222b032f27cSSam Leffler 		flags &= ~IEEE80211_CLONE_BSSID;
1223a8962181SSam Leffler 		if (sc->sc_nvaps == 0)
1224b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
1225a8962181SSam Leffler 		else
1226a8962181SSam Leffler 			ic_opmode = ic->ic_opmode;
12277d261891SRui Paulo 		break;
1228b032f27cSSam Leffler 	default:
1229b032f27cSSam Leffler 		device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
1230b032f27cSSam Leffler 		goto bad;
1231b032f27cSSam Leffler 	}
1232b032f27cSSam Leffler 	/*
1233b032f27cSSam Leffler 	 * Check that a beacon buffer is available; the code below assumes it.
1234b032f27cSSam Leffler 	 */
12356b349e5aSAdrian Chadd 	if (needbeacon & TAILQ_EMPTY(&sc->sc_bbuf)) {
1236b032f27cSSam Leffler 		device_printf(sc->sc_dev, "no beacon buffer available\n");
1237b032f27cSSam Leffler 		goto bad;
1238b032f27cSSam Leffler 	}
1239b032f27cSSam Leffler 
1240b032f27cSSam Leffler 	/* STA, AHDEMO? */
124159aa14a9SRui Paulo 	if (opmode == IEEE80211_M_HOSTAP || opmode == IEEE80211_M_MBSS) {
1242b032f27cSSam Leffler 		assign_address(sc, mac, flags & IEEE80211_CLONE_BSSID);
1243b032f27cSSam Leffler 		ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1244b032f27cSSam Leffler 	}
1245b032f27cSSam Leffler 
1246b032f27cSSam Leffler 	vap = &avp->av_vap;
1247b032f27cSSam Leffler 	/* XXX can't hold mutex across if_alloc */
1248b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1249b032f27cSSam Leffler 	error = ieee80211_vap_setup(ic, vap, name, unit, opmode, flags,
1250b032f27cSSam Leffler 	    bssid, mac);
1251b032f27cSSam Leffler 	ATH_LOCK(sc);
1252b032f27cSSam Leffler 	if (error != 0) {
1253b032f27cSSam Leffler 		device_printf(sc->sc_dev, "%s: error %d creating vap\n",
1254b032f27cSSam Leffler 		    __func__, error);
1255b032f27cSSam Leffler 		goto bad2;
1256b032f27cSSam Leffler 	}
1257b032f27cSSam Leffler 
1258b032f27cSSam Leffler 	/* h/w crypto support */
1259b032f27cSSam Leffler 	vap->iv_key_alloc = ath_key_alloc;
1260b032f27cSSam Leffler 	vap->iv_key_delete = ath_key_delete;
1261b032f27cSSam Leffler 	vap->iv_key_set = ath_key_set;
1262b032f27cSSam Leffler 	vap->iv_key_update_begin = ath_key_update_begin;
1263b032f27cSSam Leffler 	vap->iv_key_update_end = ath_key_update_end;
1264b032f27cSSam Leffler 
1265b032f27cSSam Leffler 	/* override various methods */
1266b032f27cSSam Leffler 	avp->av_recv_mgmt = vap->iv_recv_mgmt;
1267b032f27cSSam Leffler 	vap->iv_recv_mgmt = ath_recv_mgmt;
1268b032f27cSSam Leffler 	vap->iv_reset = ath_reset_vap;
1269b032f27cSSam Leffler 	vap->iv_update_beacon = ath_beacon_update;
1270b032f27cSSam Leffler 	avp->av_newstate = vap->iv_newstate;
1271b032f27cSSam Leffler 	vap->iv_newstate = ath_newstate;
1272b032f27cSSam Leffler 	avp->av_bmiss = vap->iv_bmiss;
1273b032f27cSSam Leffler 	vap->iv_bmiss = ath_bmiss_vap;
1274b032f27cSSam Leffler 
12750eb81626SAdrian Chadd 	avp->av_node_ps = vap->iv_node_ps;
12760eb81626SAdrian Chadd 	vap->iv_node_ps = ath_node_powersave;
12770eb81626SAdrian Chadd 
1278548a605dSAdrian Chadd 	avp->av_set_tim = vap->iv_set_tim;
1279548a605dSAdrian Chadd 	vap->iv_set_tim = ath_node_set_tim;
1280548a605dSAdrian Chadd 
128122a3aee6SAdrian Chadd 	avp->av_recv_pspoll = vap->iv_recv_pspoll;
128222a3aee6SAdrian Chadd 	vap->iv_recv_pspoll = ath_node_recv_pspoll;
128322a3aee6SAdrian Chadd 
12849be25f4aSAdrian Chadd 	/* Set default parameters */
12859be25f4aSAdrian Chadd 
12869be25f4aSAdrian Chadd 	/*
12879be25f4aSAdrian Chadd 	 * Anything earlier than some AR9300 series MACs don't
12889be25f4aSAdrian Chadd 	 * support a smaller MPDU density.
12899be25f4aSAdrian Chadd 	 */
12909be25f4aSAdrian Chadd 	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_8;
12919be25f4aSAdrian Chadd 	/*
12929be25f4aSAdrian Chadd 	 * All NICs can handle the maximum size, however
12939be25f4aSAdrian Chadd 	 * AR5416 based MACs can only TX aggregates w/ RTS
12949be25f4aSAdrian Chadd 	 * protection when the total aggregate size is <= 8k.
12959be25f4aSAdrian Chadd 	 * However, for now that's enforced by the TX path.
12969be25f4aSAdrian Chadd 	 */
12979be25f4aSAdrian Chadd 	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K;
12989be25f4aSAdrian Chadd 
1299b032f27cSSam Leffler 	avp->av_bslot = -1;
1300b032f27cSSam Leffler 	if (needbeacon) {
1301b032f27cSSam Leffler 		/*
1302b032f27cSSam Leffler 		 * Allocate beacon state and setup the q for buffered
1303b032f27cSSam Leffler 		 * multicast frames.  We know a beacon buffer is
1304b032f27cSSam Leffler 		 * available because we checked above.
1305b032f27cSSam Leffler 		 */
13066b349e5aSAdrian Chadd 		avp->av_bcbuf = TAILQ_FIRST(&sc->sc_bbuf);
13076b349e5aSAdrian Chadd 		TAILQ_REMOVE(&sc->sc_bbuf, avp->av_bcbuf, bf_list);
1308b032f27cSSam Leffler 		if (opmode != IEEE80211_M_IBSS || !sc->sc_hasveol) {
1309b032f27cSSam Leffler 			/*
1310b032f27cSSam Leffler 			 * Assign the vap to a beacon xmit slot.  As above
1311b032f27cSSam Leffler 			 * this cannot fail to find a free one.
1312b032f27cSSam Leffler 			 */
1313b032f27cSSam Leffler 			avp->av_bslot = assign_bslot(sc);
1314b032f27cSSam Leffler 			KASSERT(sc->sc_bslot[avp->av_bslot] == NULL,
1315b032f27cSSam Leffler 			    ("beacon slot %u not empty", avp->av_bslot));
1316b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = vap;
1317b032f27cSSam Leffler 			sc->sc_nbcnvaps++;
1318b032f27cSSam Leffler 		}
1319b032f27cSSam Leffler 		if (sc->sc_hastsfadd && sc->sc_nbcnvaps > 0) {
1320b032f27cSSam Leffler 			/*
1321b032f27cSSam Leffler 			 * Multple vaps are to transmit beacons and we
1322b032f27cSSam Leffler 			 * have h/w support for TSF adjusting; enable
1323b032f27cSSam Leffler 			 * use of staggered beacons.
1324b032f27cSSam Leffler 			 */
1325b032f27cSSam Leffler 			sc->sc_stagbeacons = 1;
1326b032f27cSSam Leffler 		}
1327b032f27cSSam Leffler 		ath_txq_init(sc, &avp->av_mcastq, ATH_TXQ_SWQ);
1328b032f27cSSam Leffler 	}
1329b032f27cSSam Leffler 
1330b032f27cSSam Leffler 	ic->ic_opmode = ic_opmode;
1331b032f27cSSam Leffler 	if (opmode != IEEE80211_M_WDS) {
1332b032f27cSSam Leffler 		sc->sc_nvaps++;
1333b032f27cSSam Leffler 		if (opmode == IEEE80211_M_STA)
1334b032f27cSSam Leffler 			sc->sc_nstavaps++;
1335fe0dd789SSam Leffler 		if (opmode == IEEE80211_M_MBSS)
1336fe0dd789SSam Leffler 			sc->sc_nmeshvaps++;
1337b032f27cSSam Leffler 	}
1338b032f27cSSam Leffler 	switch (ic_opmode) {
1339b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1340b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_IBSS;
1341b032f27cSSam Leffler 		break;
1342b032f27cSSam Leffler 	case IEEE80211_M_STA:
1343b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_STA;
1344b032f27cSSam Leffler 		break;
1345b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1346584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
134710ad9a77SSam Leffler 		if (vap->iv_caps & IEEE80211_C_TDMA) {
134810ad9a77SSam Leffler 			sc->sc_tdma = 1;
134910ad9a77SSam Leffler 			/* NB: disable tsf adjust */
135010ad9a77SSam Leffler 			sc->sc_stagbeacons = 0;
135110ad9a77SSam Leffler 		}
135210ad9a77SSam Leffler 		/*
135310ad9a77SSam Leffler 		 * NB: adhoc demo mode is a pseudo mode; to the hal it's
135410ad9a77SSam Leffler 		 * just ap mode.
135510ad9a77SSam Leffler 		 */
135610ad9a77SSam Leffler 		/* fall thru... */
135710ad9a77SSam Leffler #endif
1358b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
135959aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1360b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_HOSTAP;
1361b032f27cSSam Leffler 		break;
1362b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1363b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_MONITOR;
1364b032f27cSSam Leffler 		break;
1365b032f27cSSam Leffler 	default:
1366b032f27cSSam Leffler 		/* XXX should not happen */
1367b032f27cSSam Leffler 		break;
1368b032f27cSSam Leffler 	}
1369b032f27cSSam Leffler 	if (sc->sc_hastsfadd) {
1370b032f27cSSam Leffler 		/*
1371b032f27cSSam Leffler 		 * Configure whether or not TSF adjust should be done.
1372b032f27cSSam Leffler 		 */
1373b032f27cSSam Leffler 		ath_hal_settsfadjust(sc->sc_ah, sc->sc_stagbeacons);
1374b032f27cSSam Leffler 	}
137510ad9a77SSam Leffler 	if (flags & IEEE80211_CLONE_NOBEACONS) {
137610ad9a77SSam Leffler 		/*
137710ad9a77SSam Leffler 		 * Enable s/w beacon miss handling.
137810ad9a77SSam Leffler 		 */
137910ad9a77SSam Leffler 		sc->sc_swbmiss = 1;
138010ad9a77SSam Leffler 	}
1381b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1382b032f27cSSam Leffler 
1383b032f27cSSam Leffler 	/* complete setup */
1384b032f27cSSam Leffler 	ieee80211_vap_attach(vap, ath_media_change, ieee80211_media_status);
1385b032f27cSSam Leffler 	return vap;
1386b032f27cSSam Leffler bad2:
1387b032f27cSSam Leffler 	reclaim_address(sc, mac);
1388b032f27cSSam Leffler 	ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1389b032f27cSSam Leffler bad:
1390b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1391b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1392b032f27cSSam Leffler 	return NULL;
1393b032f27cSSam Leffler }
1394b032f27cSSam Leffler 
1395b032f27cSSam Leffler static void
1396b032f27cSSam Leffler ath_vap_delete(struct ieee80211vap *vap)
1397b032f27cSSam Leffler {
1398b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1399b032f27cSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1400b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1401b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1402b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
1403b032f27cSSam Leffler 
1404f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
1405b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1406b032f27cSSam Leffler 		/*
1407b032f27cSSam Leffler 		 * Quiesce the hardware while we remove the vap.  In
1408b032f27cSSam Leffler 		 * particular we need to reclaim all references to
1409b032f27cSSam Leffler 		 * the vap state by any frames pending on the tx queues.
1410b032f27cSSam Leffler 		 */
1411b032f27cSSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
1412517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);		/* stop hw xmit side */
1413517526efSAdrian Chadd 		/* XXX Do all frames from all vaps/nodes need draining here? */
14149a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* stop recv side */
1415b032f27cSSam Leffler 	}
1416b032f27cSSam Leffler 
1417b032f27cSSam Leffler 	ieee80211_vap_detach(vap);
141816d4de92SAdrian Chadd 
141916d4de92SAdrian Chadd 	/*
142016d4de92SAdrian Chadd 	 * XXX Danger Will Robinson! Danger!
142116d4de92SAdrian Chadd 	 *
142216d4de92SAdrian Chadd 	 * Because ieee80211_vap_detach() can queue a frame (the station
142316d4de92SAdrian Chadd 	 * diassociate message?) after we've drained the TXQ and
142416d4de92SAdrian Chadd 	 * flushed the software TXQ, we will end up with a frame queued
142516d4de92SAdrian Chadd 	 * to a node whose vap is about to be freed.
142616d4de92SAdrian Chadd 	 *
142716d4de92SAdrian Chadd 	 * To work around this, flush the hardware/software again.
142816d4de92SAdrian Chadd 	 * This may be racy - the ath task may be running and the packet
142916d4de92SAdrian Chadd 	 * may be being scheduled between sw->hw txq. Tsk.
143016d4de92SAdrian Chadd 	 *
143116d4de92SAdrian Chadd 	 * TODO: figure out why a new node gets allocated somewhere around
143216d4de92SAdrian Chadd 	 * here (after the ath_tx_swq() call; and after an ath_stop_locked()
143316d4de92SAdrian Chadd 	 * call!)
143416d4de92SAdrian Chadd 	 */
143516d4de92SAdrian Chadd 
143616d4de92SAdrian Chadd 	ath_draintxq(sc, ATH_RESET_DEFAULT);
143716d4de92SAdrian Chadd 
1438b032f27cSSam Leffler 	ATH_LOCK(sc);
1439b032f27cSSam Leffler 	/*
1440b032f27cSSam Leffler 	 * Reclaim beacon state.  Note this must be done before
1441b032f27cSSam Leffler 	 * the vap instance is reclaimed as we may have a reference
1442b032f27cSSam Leffler 	 * to it in the buffer for the beacon frame.
1443b032f27cSSam Leffler 	 */
1444b032f27cSSam Leffler 	if (avp->av_bcbuf != NULL) {
1445b032f27cSSam Leffler 		if (avp->av_bslot != -1) {
1446b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = NULL;
1447b032f27cSSam Leffler 			sc->sc_nbcnvaps--;
1448b032f27cSSam Leffler 		}
1449b032f27cSSam Leffler 		ath_beacon_return(sc, avp->av_bcbuf);
1450b032f27cSSam Leffler 		avp->av_bcbuf = NULL;
1451b032f27cSSam Leffler 		if (sc->sc_nbcnvaps == 0) {
1452b032f27cSSam Leffler 			sc->sc_stagbeacons = 0;
1453b032f27cSSam Leffler 			if (sc->sc_hastsfadd)
1454b032f27cSSam Leffler 				ath_hal_settsfadjust(sc->sc_ah, 0);
1455b032f27cSSam Leffler 		}
1456b032f27cSSam Leffler 		/*
1457b032f27cSSam Leffler 		 * Reclaim any pending mcast frames for the vap.
1458b032f27cSSam Leffler 		 */
1459b032f27cSSam Leffler 		ath_tx_draintxq(sc, &avp->av_mcastq);
1460b032f27cSSam Leffler 	}
1461b032f27cSSam Leffler 	/*
1462b032f27cSSam Leffler 	 * Update bookkeeping.
1463b032f27cSSam Leffler 	 */
1464b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
1465b032f27cSSam Leffler 		sc->sc_nstavaps--;
1466b032f27cSSam Leffler 		if (sc->sc_nstavaps == 0 && sc->sc_swbmiss)
1467b032f27cSSam Leffler 			sc->sc_swbmiss = 0;
146859aa14a9SRui Paulo 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
146959aa14a9SRui Paulo 	    vap->iv_opmode == IEEE80211_M_MBSS) {
1470b032f27cSSam Leffler 		reclaim_address(sc, vap->iv_myaddr);
1471b032f27cSSam Leffler 		ath_hal_setbssidmask(ah, sc->sc_hwbssidmask);
1472fe0dd789SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_MBSS)
1473fe0dd789SSam Leffler 			sc->sc_nmeshvaps--;
1474b032f27cSSam Leffler 	}
1475b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_WDS)
1476b032f27cSSam Leffler 		sc->sc_nvaps--;
1477584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
147810ad9a77SSam Leffler 	/* TDMA operation ceases when the last vap is destroyed */
147910ad9a77SSam Leffler 	if (sc->sc_tdma && sc->sc_nvaps == 0) {
148010ad9a77SSam Leffler 		sc->sc_tdma = 0;
148110ad9a77SSam Leffler 		sc->sc_swbmiss = 0;
148210ad9a77SSam Leffler 	}
148310ad9a77SSam Leffler #endif
1484b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1485b032f27cSSam Leffler 
1486b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1487b032f27cSSam Leffler 		/*
1488b032f27cSSam Leffler 		 * Restart rx+tx machines if still running (RUNNING will
1489b032f27cSSam Leffler 		 * be reset if we just destroyed the last vap).
1490b032f27cSSam Leffler 		 */
1491b032f27cSSam Leffler 		if (ath_startrecv(sc) != 0)
1492b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to restart recv logic\n",
1493b032f27cSSam Leffler 			    __func__);
1494c89b957aSSam Leffler 		if (sc->sc_beacons) {		/* restart beacons */
1495c89b957aSSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
1496c89b957aSSam Leffler 			if (sc->sc_tdma)
1497c89b957aSSam Leffler 				ath_tdma_config(sc, NULL);
1498c89b957aSSam Leffler 			else
1499c89b957aSSam Leffler #endif
1500b032f27cSSam Leffler 				ath_beacon_config(sc, NULL);
1501c89b957aSSam Leffler 		}
1502b032f27cSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
1503b032f27cSSam Leffler 	}
150416d4de92SAdrian Chadd 	ATH_UNLOCK(sc);
1505b032f27cSSam Leffler }
1506b032f27cSSam Leffler 
15075591b213SSam Leffler void
15085591b213SSam Leffler ath_suspend(struct ath_softc *sc)
15095591b213SSam Leffler {
1510fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1511d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
15125591b213SSam Leffler 
1513c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1514c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
15155591b213SSam Leffler 
1516d3ac945bSSam Leffler 	sc->sc_resume_up = (ifp->if_flags & IFF_UP) != 0;
1517d1328898SAdrian Chadd 
1518d3ac945bSSam Leffler 	ieee80211_suspend_all(ic);
1519d3ac945bSSam Leffler 	/*
1520d3ac945bSSam Leffler 	 * NB: don't worry about putting the chip in low power
1521d3ac945bSSam Leffler 	 * mode; pci will power off our socket on suspend and
1522f29b8b7fSWarner Losh 	 * CardBus detaches the device.
1523d3ac945bSSam Leffler 	 */
1524d73df6d5SAdrian Chadd 
1525ae2a0aa4SAdrian Chadd 	/*
1526ae2a0aa4SAdrian Chadd 	 * XXX ensure none of the taskqueues are running
1527ae2a0aa4SAdrian Chadd 	 * XXX ensure sc_invalid is 1
1528ae2a0aa4SAdrian Chadd 	 * XXX ensure the calibration callout is disabled
1529ae2a0aa4SAdrian Chadd 	 */
1530ae2a0aa4SAdrian Chadd 
1531ae2a0aa4SAdrian Chadd 	/* Disable the PCIe PHY, complete with workarounds */
1532ae2a0aa4SAdrian Chadd 	ath_hal_enablepcie(sc->sc_ah, 1, 1);
1533d3ac945bSSam Leffler }
1534d3ac945bSSam Leffler 
1535d3ac945bSSam Leffler /*
1536d3ac945bSSam Leffler  * Reset the key cache since some parts do not reset the
1537d3ac945bSSam Leffler  * contents on resume.  First we clear all entries, then
1538d3ac945bSSam Leffler  * re-load keys that the 802.11 layer assumes are setup
1539d3ac945bSSam Leffler  * in h/w.
1540d3ac945bSSam Leffler  */
1541d3ac945bSSam Leffler static void
1542d3ac945bSSam Leffler ath_reset_keycache(struct ath_softc *sc)
1543d3ac945bSSam Leffler {
1544d3ac945bSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1545d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1546d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1547d3ac945bSSam Leffler 	int i;
1548d3ac945bSSam Leffler 
1549d3ac945bSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
1550d3ac945bSSam Leffler 		ath_hal_keyreset(ah, i);
1551d3ac945bSSam Leffler 	ieee80211_crypto_reload_keys(ic);
15525591b213SSam Leffler }
15535591b213SSam Leffler 
15546322256bSAdrian Chadd /*
15556322256bSAdrian Chadd  * Fetch the current chainmask configuration based on the current
15566322256bSAdrian Chadd  * operating channel and options.
15576322256bSAdrian Chadd  */
15586322256bSAdrian Chadd static void
15596322256bSAdrian Chadd ath_update_chainmasks(struct ath_softc *sc, struct ieee80211_channel *chan)
15606322256bSAdrian Chadd {
15616322256bSAdrian Chadd 
15626322256bSAdrian Chadd 	/*
15636322256bSAdrian Chadd 	 * Set TX chainmask to the currently configured chainmask;
15646322256bSAdrian Chadd 	 * the TX chainmask depends upon the current operating mode.
15656322256bSAdrian Chadd 	 */
15666322256bSAdrian Chadd 	sc->sc_cur_rxchainmask = sc->sc_rxchainmask;
15676322256bSAdrian Chadd 	if (IEEE80211_IS_CHAN_HT(chan)) {
15686322256bSAdrian Chadd 		sc->sc_cur_txchainmask = sc->sc_txchainmask;
15696322256bSAdrian Chadd 	} else {
15706322256bSAdrian Chadd 		sc->sc_cur_txchainmask = 1;
15716322256bSAdrian Chadd 	}
15727904f516SAdrian Chadd 
15737904f516SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
15747904f516SAdrian Chadd 	    "%s: TX chainmask is now 0x%x, RX is now 0x%x\n",
15757904f516SAdrian Chadd 	    __func__,
15767904f516SAdrian Chadd 	    sc->sc_cur_txchainmask,
15777904f516SAdrian Chadd 	    sc->sc_cur_rxchainmask);
15786322256bSAdrian Chadd }
15796322256bSAdrian Chadd 
15805591b213SSam Leffler void
15815591b213SSam Leffler ath_resume(struct ath_softc *sc)
15825591b213SSam Leffler {
1583fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1584d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1585d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1586d3ac945bSSam Leffler 	HAL_STATUS status;
15875591b213SSam Leffler 
1588c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1589c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
15905591b213SSam Leffler 
1591d73df6d5SAdrian Chadd 	/* Re-enable PCIe, re-enable the PCIe bus */
1592ae2a0aa4SAdrian Chadd 	ath_hal_enablepcie(ah, 0, 0);
1593d73df6d5SAdrian Chadd 
1594d3ac945bSSam Leffler 	/*
1595d3ac945bSSam Leffler 	 * Must reset the chip before we reload the
1596d3ac945bSSam Leffler 	 * keycache as we were powered down on suspend.
1597d3ac945bSSam Leffler 	 */
15986322256bSAdrian Chadd 	ath_update_chainmasks(sc,
15996322256bSAdrian Chadd 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan);
16006322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
16016322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
1602054d7b69SSam Leffler 	ath_hal_reset(ah, sc->sc_opmode,
1603054d7b69SSam Leffler 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan,
1604054d7b69SSam Leffler 	    AH_FALSE, &status);
1605d3ac945bSSam Leffler 	ath_reset_keycache(sc);
16067e5eb44dSAdrian Chadd 
16077e5eb44dSAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
16087e5eb44dSAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
16097e5eb44dSAdrian Chadd 
16109af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
16119af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
16129af351f9SAdrian Chadd 
1613dd6a574eSAdrian Chadd 	/*
1614b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
1615b70f530bSAdrian Chadd 	 */
1616b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
1617b70f530bSAdrian Chadd 
1618b70f530bSAdrian Chadd 	/*
1619dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
1620dd6a574eSAdrian Chadd 	 * support it.
1621dd6a574eSAdrian Chadd 	 */
1622dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
1623dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
1624dd6a574eSAdrian Chadd 	else
1625dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
1626dd6a574eSAdrian Chadd 
1627a497cd88SAdrian Chadd 	/* Restore the LED configuration */
1628a497cd88SAdrian Chadd 	ath_led_config(sc);
1629a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
1630a497cd88SAdrian Chadd 
1631d1328898SAdrian Chadd 	if (sc->sc_resume_up)
1632021a0db5SAdrian Chadd 		ieee80211_resume_all(ic);
16332fd9aabbSAdrian Chadd 
16342fd9aabbSAdrian Chadd 	/* XXX beacons ? */
16356b59f5e3SSam Leffler }
16365591b213SSam Leffler 
16375591b213SSam Leffler void
16385591b213SSam Leffler ath_shutdown(struct ath_softc *sc)
16395591b213SSam Leffler {
1640fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
16415591b213SSam Leffler 
1642c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1643c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
16445591b213SSam Leffler 
16455591b213SSam Leffler 	ath_stop(ifp);
1646d3ac945bSSam Leffler 	/* NB: no point powering down chip as we're about to reboot */
16475591b213SSam Leffler }
16485591b213SSam Leffler 
1649c42a7b7eSSam Leffler /*
1650c42a7b7eSSam Leffler  * Interrupt handler.  Most of the actual processing is deferred.
1651c42a7b7eSSam Leffler  */
16525591b213SSam Leffler void
16535591b213SSam Leffler ath_intr(void *arg)
16545591b213SSam Leffler {
16555591b213SSam Leffler 	struct ath_softc *sc = arg;
1656fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
16575591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
16586f5fe81eSAdrian Chadd 	HAL_INT status = 0;
16598f939e79SAdrian Chadd 	uint32_t txqs;
16605591b213SSam Leffler 
1661ef27340cSAdrian Chadd 	/*
1662ef27340cSAdrian Chadd 	 * If we're inside a reset path, just print a warning and
1663ef27340cSAdrian Chadd 	 * clear the ISR. The reset routine will finish it for us.
1664ef27340cSAdrian Chadd 	 */
1665ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
1666ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt) {
1667ef27340cSAdrian Chadd 		HAL_INT status;
1668ef27340cSAdrian Chadd 		ath_hal_getisr(ah, &status);	/* clear ISR */
1669ef27340cSAdrian Chadd 		ath_hal_intrset(ah, 0);		/* disable further intr's */
1670ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_ANY,
1671ef27340cSAdrian Chadd 		    "%s: in reset, ignoring: status=0x%x\n",
1672ef27340cSAdrian Chadd 		    __func__, status);
1673ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
1674ef27340cSAdrian Chadd 		return;
1675ef27340cSAdrian Chadd 	}
1676ef27340cSAdrian Chadd 
16775591b213SSam Leffler 	if (sc->sc_invalid) {
16785591b213SSam Leffler 		/*
1679b58b3803SSam Leffler 		 * The hardware is not ready/present, don't touch anything.
1680b58b3803SSam Leffler 		 * Note this can happen early on if the IRQ is shared.
16815591b213SSam Leffler 		 */
1682c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
1683ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
16845591b213SSam Leffler 		return;
16855591b213SSam Leffler 	}
1686ef27340cSAdrian Chadd 	if (!ath_hal_intrpend(ah)) {		/* shared irq, not for us */
1687ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
1688fdd758d4SSam Leffler 		return;
1689ef27340cSAdrian Chadd 	}
1690ef27340cSAdrian Chadd 
169168e8e04eSSam Leffler 	if ((ifp->if_flags & IFF_UP) == 0 ||
169268e8e04eSSam Leffler 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
169368e8e04eSSam Leffler 		HAL_INT status;
169468e8e04eSSam Leffler 
1695c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1696c42a7b7eSSam Leffler 			__func__, ifp->if_flags);
16975591b213SSam Leffler 		ath_hal_getisr(ah, &status);	/* clear ISR */
16985591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable further intr's */
1699ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
17005591b213SSam Leffler 		return;
17015591b213SSam Leffler 	}
1702ef27340cSAdrian Chadd 
1703c42a7b7eSSam Leffler 	/*
1704c42a7b7eSSam Leffler 	 * Figure out the reason(s) for the interrupt.  Note
1705c42a7b7eSSam Leffler 	 * that the hal returns a pseudo-ISR that may include
1706c42a7b7eSSam Leffler 	 * bits we haven't explicitly enabled so we mask the
1707c42a7b7eSSam Leffler 	 * value to insure we only process bits we requested.
1708c42a7b7eSSam Leffler 	 */
17095591b213SSam Leffler 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
1710c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
171103682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 1, "ath_intr: mask=0x%.8x", status);
1712a26f3327SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
1713a26f3327SAdrian Chadd 	if_ath_alq_post_intr(&sc->sc_alq, status, ah->ah_intrstate,
1714a26f3327SAdrian Chadd 	    ah->ah_syncstate);
1715a26f3327SAdrian Chadd #endif	/* ATH_DEBUG_ALQ */
171631fdf3d6SAdrian Chadd #ifdef	ATH_KTR_INTR_DEBUG
171703682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 5,
1718f52d3452SAdrian Chadd 	    "ath_intr: ISR=0x%.8x, ISR_S0=0x%.8x, ISR_S1=0x%.8x, ISR_S2=0x%.8x, ISR_S5=0x%.8x",
1719f52d3452SAdrian Chadd 	    ah->ah_intrstate[0],
1720f52d3452SAdrian Chadd 	    ah->ah_intrstate[1],
1721f52d3452SAdrian Chadd 	    ah->ah_intrstate[2],
1722f52d3452SAdrian Chadd 	    ah->ah_intrstate[3],
1723f52d3452SAdrian Chadd 	    ah->ah_intrstate[6]);
172431fdf3d6SAdrian Chadd #endif
17259467e3f3SAdrian Chadd 
17269467e3f3SAdrian Chadd 	/* Squirrel away SYNC interrupt debugging */
17279467e3f3SAdrian Chadd 	if (ah->ah_syncstate != 0) {
17289467e3f3SAdrian Chadd 		int i;
17299467e3f3SAdrian Chadd 		for (i = 0; i < 32; i++)
17309467e3f3SAdrian Chadd 			if (ah->ah_syncstate & (i << i))
17319467e3f3SAdrian Chadd 				sc->sc_intr_stats.sync_intr[i]++;
17329467e3f3SAdrian Chadd 	}
17339467e3f3SAdrian Chadd 
1734ecddff40SSam Leffler 	status &= sc->sc_imask;			/* discard unasked for bits */
17356f5fe81eSAdrian Chadd 
17366f5fe81eSAdrian Chadd 	/* Short-circuit un-handled interrupts */
1737ef27340cSAdrian Chadd 	if (status == 0x0) {
1738ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
17396f5fe81eSAdrian Chadd 		return;
1740ef27340cSAdrian Chadd 	}
17416f5fe81eSAdrian Chadd 
1742ef27340cSAdrian Chadd 	/*
1743ef27340cSAdrian Chadd 	 * Take a note that we're inside the interrupt handler, so
1744ef27340cSAdrian Chadd 	 * the reset routines know to wait.
1745ef27340cSAdrian Chadd 	 */
1746ef27340cSAdrian Chadd 	sc->sc_intr_cnt++;
1747ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
1748ef27340cSAdrian Chadd 
1749ef27340cSAdrian Chadd 	/*
1750ef27340cSAdrian Chadd 	 * Handle the interrupt. We won't run concurrent with the reset
1751ef27340cSAdrian Chadd 	 * or channel change routines as they'll wait for sc_intr_cnt
1752ef27340cSAdrian Chadd 	 * to be 0 before continuing.
1753ef27340cSAdrian Chadd 	 */
17545591b213SSam Leffler 	if (status & HAL_INT_FATAL) {
17555591b213SSam Leffler 		sc->sc_stats.ast_hardware++;
17565591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
1757f846cf42SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_fataltask);
17585591b213SSam Leffler 	} else {
1759c42a7b7eSSam Leffler 		if (status & HAL_INT_SWBA) {
1760c42a7b7eSSam Leffler 			/*
1761c42a7b7eSSam Leffler 			 * Software beacon alert--time to send a beacon.
1762c42a7b7eSSam Leffler 			 * Handle beacon transmission directly; deferring
1763c42a7b7eSSam Leffler 			 * this is too slow to meet timing constraints
1764c42a7b7eSSam Leffler 			 * under load.
1765c42a7b7eSSam Leffler 			 */
1766584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
176710ad9a77SSam Leffler 			if (sc->sc_tdma) {
176810ad9a77SSam Leffler 				if (sc->sc_tdmaswba == 0) {
176910ad9a77SSam Leffler 					struct ieee80211com *ic = ifp->if_l2com;
177010ad9a77SSam Leffler 					struct ieee80211vap *vap =
177110ad9a77SSam Leffler 					    TAILQ_FIRST(&ic->ic_vaps);
177210ad9a77SSam Leffler 					ath_tdma_beacon_send(sc, vap);
177310ad9a77SSam Leffler 					sc->sc_tdmaswba =
177410ad9a77SSam Leffler 					    vap->iv_tdma->tdma_bintval;
177510ad9a77SSam Leffler 				} else
177610ad9a77SSam Leffler 					sc->sc_tdmaswba--;
177710ad9a77SSam Leffler 			} else
177810ad9a77SSam Leffler #endif
1779339ccfb3SSam Leffler 			{
1780c42a7b7eSSam Leffler 				ath_beacon_proc(sc, 0);
1781339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1782339ccfb3SSam Leffler 				/*
1783339ccfb3SSam Leffler 				 * Schedule the rx taskq in case there's no
1784339ccfb3SSam Leffler 				 * traffic so any frames held on the staging
1785339ccfb3SSam Leffler 				 * queue are aged and potentially flushed.
1786339ccfb3SSam Leffler 				 */
1787f0db652cSAdrian Chadd 				sc->sc_rx.recv_sched(sc, 1);
1788339ccfb3SSam Leffler #endif
1789339ccfb3SSam Leffler 			}
1790c42a7b7eSSam Leffler 		}
17915591b213SSam Leffler 		if (status & HAL_INT_RXEOL) {
17928f939e79SAdrian Chadd 			int imask;
179303682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXEOL");
1794ef27340cSAdrian Chadd 			ATH_PCU_LOCK(sc);
17955591b213SSam Leffler 			/*
17965591b213SSam Leffler 			 * NB: the hardware should re-read the link when
17975591b213SSam Leffler 			 *     RXE bit is written, but it doesn't work at
17985591b213SSam Leffler 			 *     least on older hardware revs.
17995591b213SSam Leffler 			 */
18005591b213SSam Leffler 			sc->sc_stats.ast_rxeol++;
180173f895fcSAdrian Chadd 			/*
180273f895fcSAdrian Chadd 			 * Disable RXEOL/RXORN - prevent an interrupt
180373f895fcSAdrian Chadd 			 * storm until the PCU logic can be reset.
18041fdadc0fSAdrian Chadd 			 * In case the interface is reset some other
18051fdadc0fSAdrian Chadd 			 * way before "sc_kickpcu" is called, don't
18061fdadc0fSAdrian Chadd 			 * modify sc_imask - that way if it is reset
18071fdadc0fSAdrian Chadd 			 * by a call to ath_reset() somehow, the
18081fdadc0fSAdrian Chadd 			 * interrupt mask will be correctly reprogrammed.
180973f895fcSAdrian Chadd 			 */
18108f939e79SAdrian Chadd 			imask = sc->sc_imask;
18111fdadc0fSAdrian Chadd 			imask &= ~(HAL_INT_RXEOL | HAL_INT_RXORN);
18121fdadc0fSAdrian Chadd 			ath_hal_intrset(ah, imask);
18131fdadc0fSAdrian Chadd 			/*
18148f939e79SAdrian Chadd 			 * Only blank sc_rxlink if we've not yet kicked
18158f939e79SAdrian Chadd 			 * the PCU.
18168f939e79SAdrian Chadd 			 *
18178f939e79SAdrian Chadd 			 * This isn't entirely correct - the correct solution
18188f939e79SAdrian Chadd 			 * would be to have a PCU lock and engage that for
18198f939e79SAdrian Chadd 			 * the duration of the PCU fiddling; which would include
18208f939e79SAdrian Chadd 			 * running the RX process. Otherwise we could end up
18218f939e79SAdrian Chadd 			 * messing up the RX descriptor chain and making the
18228f939e79SAdrian Chadd 			 * RX desc list much shorter.
18238f939e79SAdrian Chadd 			 */
18248f939e79SAdrian Chadd 			if (! sc->sc_kickpcu)
18258f939e79SAdrian Chadd 				sc->sc_rxlink = NULL;
18268f939e79SAdrian Chadd 			sc->sc_kickpcu = 1;
1827f0db652cSAdrian Chadd 			ATH_PCU_UNLOCK(sc);
18288f939e79SAdrian Chadd 			/*
18291fdadc0fSAdrian Chadd 			 * Enqueue an RX proc, to handled whatever
18301fdadc0fSAdrian Chadd 			 * is in the RX queue.
18311fdadc0fSAdrian Chadd 			 * This will then kick the PCU.
18321fdadc0fSAdrian Chadd 			 */
1833f0db652cSAdrian Chadd 			sc->sc_rx.recv_sched(sc, 1);
18345591b213SSam Leffler 		}
18355591b213SSam Leffler 		if (status & HAL_INT_TXURN) {
18365591b213SSam Leffler 			sc->sc_stats.ast_txurn++;
18375591b213SSam Leffler 			/* bump tx trigger level */
18385591b213SSam Leffler 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
18395591b213SSam Leffler 		}
1840bcbb08ceSAdrian Chadd 		/*
1841bcbb08ceSAdrian Chadd 		 * Handle both the legacy and RX EDMA interrupt bits.
1842bcbb08ceSAdrian Chadd 		 * Note that HAL_INT_RXLP is also HAL_INT_RXDESC.
1843bcbb08ceSAdrian Chadd 		 */
1844bcbb08ceSAdrian Chadd 		if (status & (HAL_INT_RX | HAL_INT_RXHP | HAL_INT_RXLP)) {
18458f939e79SAdrian Chadd 			sc->sc_stats.ast_rx_intr++;
1846f0db652cSAdrian Chadd 			sc->sc_rx.recv_sched(sc, 1);
18478f939e79SAdrian Chadd 		}
18488f939e79SAdrian Chadd 		if (status & HAL_INT_TX) {
18498f939e79SAdrian Chadd 			sc->sc_stats.ast_tx_intr++;
18508f939e79SAdrian Chadd 			/*
18518f939e79SAdrian Chadd 			 * Grab all the currently set bits in the HAL txq bitmap
18528f939e79SAdrian Chadd 			 * and blank them. This is the only place we should be
18538f939e79SAdrian Chadd 			 * doing this.
18548f939e79SAdrian Chadd 			 */
1855bad98824SAdrian Chadd 			if (! sc->sc_isedma) {
1856ef27340cSAdrian Chadd 				ATH_PCU_LOCK(sc);
18578f939e79SAdrian Chadd 				txqs = 0xffffffff;
18588f939e79SAdrian Chadd 				ath_hal_gettxintrtxqs(sc->sc_ah, &txqs);
185903682514SAdrian Chadd 				ATH_KTR(sc, ATH_KTR_INTERRUPTS, 3,
186003682514SAdrian Chadd 				    "ath_intr: TX; txqs=0x%08x, txq_active was 0x%08x, now 0x%08x",
186103682514SAdrian Chadd 				    txqs,
186203682514SAdrian Chadd 				    sc->sc_txq_active,
186303682514SAdrian Chadd 				    sc->sc_txq_active | txqs);
18648f939e79SAdrian Chadd 				sc->sc_txq_active |= txqs;
1865ef27340cSAdrian Chadd 				ATH_PCU_UNLOCK(sc);
18668f939e79SAdrian Chadd 			}
1867bad98824SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask);
1868bad98824SAdrian Chadd 		}
18695591b213SSam Leffler 		if (status & HAL_INT_BMISS) {
18705591b213SSam Leffler 			sc->sc_stats.ast_bmiss++;
18710bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask);
18725591b213SSam Leffler 		}
18736ad02dbaSAdrian Chadd 		if (status & HAL_INT_GTT)
18746ad02dbaSAdrian Chadd 			sc->sc_stats.ast_tx_timeout++;
18755594f5c0SAdrian Chadd 		if (status & HAL_INT_CST)
18765594f5c0SAdrian Chadd 			sc->sc_stats.ast_tx_cst++;
1877c42a7b7eSSam Leffler 		if (status & HAL_INT_MIB) {
1878c42a7b7eSSam Leffler 			sc->sc_stats.ast_mib++;
1879ef27340cSAdrian Chadd 			ATH_PCU_LOCK(sc);
1880c42a7b7eSSam Leffler 			/*
1881c42a7b7eSSam Leffler 			 * Disable interrupts until we service the MIB
1882c42a7b7eSSam Leffler 			 * interrupt; otherwise it will continue to fire.
1883c42a7b7eSSam Leffler 			 */
1884c42a7b7eSSam Leffler 			ath_hal_intrset(ah, 0);
1885c42a7b7eSSam Leffler 			/*
1886c42a7b7eSSam Leffler 			 * Let the hal handle the event.  We assume it will
1887c42a7b7eSSam Leffler 			 * clear whatever condition caused the interrupt.
1888c42a7b7eSSam Leffler 			 */
1889ffa2cab6SSam Leffler 			ath_hal_mibevent(ah, &sc->sc_halstats);
18908f939e79SAdrian Chadd 			/*
18918f939e79SAdrian Chadd 			 * Don't reset the interrupt if we've just
18928f939e79SAdrian Chadd 			 * kicked the PCU, or we may get a nested
18938f939e79SAdrian Chadd 			 * RXEOL before the rxproc has had a chance
18948f939e79SAdrian Chadd 			 * to run.
18958f939e79SAdrian Chadd 			 */
18968f939e79SAdrian Chadd 			if (sc->sc_kickpcu == 0)
1897c42a7b7eSSam Leffler 				ath_hal_intrset(ah, sc->sc_imask);
1898ef27340cSAdrian Chadd 			ATH_PCU_UNLOCK(sc);
1899c42a7b7eSSam Leffler 		}
19009c4fc1e8SSam Leffler 		if (status & HAL_INT_RXORN) {
19019c4fc1e8SSam Leffler 			/* NB: hal marks HAL_INT_FATAL when RXORN is fatal */
190203682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXORN");
19039c4fc1e8SSam Leffler 			sc->sc_stats.ast_rxorn++;
19049c4fc1e8SSam Leffler 		}
19055591b213SSam Leffler 	}
1906ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
1907ef27340cSAdrian Chadd 	sc->sc_intr_cnt--;
1908ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
19095591b213SSam Leffler }
19105591b213SSam Leffler 
19115591b213SSam Leffler static void
19125591b213SSam Leffler ath_fatal_proc(void *arg, int pending)
19135591b213SSam Leffler {
19145591b213SSam Leffler 	struct ath_softc *sc = arg;
1915fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
191616c8acaaSSam Leffler 	u_int32_t *state;
191716c8acaaSSam Leffler 	u_int32_t len;
191868e8e04eSSam Leffler 	void *sp;
19195591b213SSam Leffler 
1920c42a7b7eSSam Leffler 	if_printf(ifp, "hardware error; resetting\n");
192116c8acaaSSam Leffler 	/*
192216c8acaaSSam Leffler 	 * Fatal errors are unrecoverable.  Typically these
192316c8acaaSSam Leffler 	 * are caused by DMA errors.  Collect h/w state from
192416c8acaaSSam Leffler 	 * the hal so we can diagnose what's going on.
192516c8acaaSSam Leffler 	 */
192668e8e04eSSam Leffler 	if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) {
192716c8acaaSSam Leffler 		KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len));
192868e8e04eSSam Leffler 		state = sp;
192916c8acaaSSam Leffler 		if_printf(ifp, "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n",
193016c8acaaSSam Leffler 		    state[0], state[1] , state[2], state[3],
193116c8acaaSSam Leffler 		    state[4], state[5]);
193216c8acaaSSam Leffler 	}
1933517526efSAdrian Chadd 	ath_reset(ifp, ATH_RESET_NOLOSS);
19345591b213SSam Leffler }
19355591b213SSam Leffler 
19365591b213SSam Leffler static void
1937b032f27cSSam Leffler ath_bmiss_vap(struct ieee80211vap *vap)
19385591b213SSam Leffler {
193959fbb257SSam Leffler 	/*
194059fbb257SSam Leffler 	 * Workaround phantom bmiss interrupts by sanity-checking
194159fbb257SSam Leffler 	 * the time of our last rx'd frame.  If it is within the
194259fbb257SSam Leffler 	 * beacon miss interval then ignore the interrupt.  If it's
194359fbb257SSam Leffler 	 * truly a bmiss we'll get another interrupt soon and that'll
194459fbb257SSam Leffler 	 * be dispatched up for processing.  Note this applies only
194559fbb257SSam Leffler 	 * for h/w beacon miss events.
194659fbb257SSam Leffler 	 */
194759fbb257SSam Leffler 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
1948a7ace843SSam Leffler 		struct ifnet *ifp = vap->iv_ic->ic_ifp;
1949a7ace843SSam Leffler 		struct ath_softc *sc = ifp->if_softc;
1950d7736e13SSam Leffler 		u_int64_t lastrx = sc->sc_lastrx;
1951d7736e13SSam Leffler 		u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah);
195280767531SAdrian Chadd 		/* XXX should take a locked ref to iv_bss */
1953d7736e13SSam Leffler 		u_int bmisstimeout =
1954b032f27cSSam Leffler 			vap->iv_bmissthreshold * vap->iv_bss->ni_intval * 1024;
1955d7736e13SSam Leffler 
1956d7736e13SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
1957d7736e13SSam Leffler 		    "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n",
1958d7736e13SSam Leffler 		    __func__, (unsigned long long) tsf,
1959d7736e13SSam Leffler 		    (unsigned long long)(tsf - lastrx),
1960d7736e13SSam Leffler 		    (unsigned long long) lastrx, bmisstimeout);
196159fbb257SSam Leffler 
196259fbb257SSam Leffler 		if (tsf - lastrx <= bmisstimeout) {
1963d7736e13SSam Leffler 			sc->sc_stats.ast_bmiss_phantom++;
196459fbb257SSam Leffler 			return;
196559fbb257SSam Leffler 		}
196659fbb257SSam Leffler 	}
196759fbb257SSam Leffler 	ATH_VAP(vap)->av_bmiss(vap);
1968e585d188SSam Leffler }
1969b032f27cSSam Leffler 
1970b837332dSAdrian Chadd int
1971459bc4f0SSam Leffler ath_hal_gethangstate(struct ath_hal *ah, uint32_t mask, uint32_t *hangs)
1972459bc4f0SSam Leffler {
1973459bc4f0SSam Leffler 	uint32_t rsize;
1974459bc4f0SSam Leffler 	void *sp;
1975459bc4f0SSam Leffler 
197625c96056SAdrian Chadd 	if (!ath_hal_getdiagstate(ah, HAL_DIAG_CHECK_HANGS, &mask, sizeof(mask), &sp, &rsize))
1977459bc4f0SSam Leffler 		return 0;
1978459bc4f0SSam Leffler 	KASSERT(rsize == sizeof(uint32_t), ("resultsize %u", rsize));
1979459bc4f0SSam Leffler 	*hangs = *(uint32_t *)sp;
1980459bc4f0SSam Leffler 	return 1;
1981459bc4f0SSam Leffler }
1982459bc4f0SSam Leffler 
1983b032f27cSSam Leffler static void
1984b032f27cSSam Leffler ath_bmiss_proc(void *arg, int pending)
1985b032f27cSSam Leffler {
1986b032f27cSSam Leffler 	struct ath_softc *sc = arg;
1987b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1988459bc4f0SSam Leffler 	uint32_t hangs;
1989b032f27cSSam Leffler 
1990b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
1991459bc4f0SSam Leffler 
1992a74ebfe5SAdrian Chadd 	/*
1993a74ebfe5SAdrian Chadd 	 * Do a reset upon any becaon miss event.
1994a74ebfe5SAdrian Chadd 	 *
1995a74ebfe5SAdrian Chadd 	 * It may be a non-recognised RX clear hang which needs a reset
1996a74ebfe5SAdrian Chadd 	 * to clear.
1997a74ebfe5SAdrian Chadd 	 */
1998459bc4f0SSam Leffler 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) {
1999517526efSAdrian Chadd 		ath_reset(ifp, ATH_RESET_NOLOSS);
2000a74ebfe5SAdrian Chadd 		if_printf(ifp, "bb hang detected (0x%x), resetting\n", hangs);
2001a74ebfe5SAdrian Chadd 	} else {
2002a74ebfe5SAdrian Chadd 		ath_reset(ifp, ATH_RESET_NOLOSS);
2003b032f27cSSam Leffler 		ieee80211_beacon_miss(ifp->if_l2com);
20045591b213SSam Leffler 	}
2005a74ebfe5SAdrian Chadd }
20065591b213SSam Leffler 
2007724c193aSSam Leffler /*
2008b032f27cSSam Leffler  * Handle TKIP MIC setup to deal hardware that doesn't do MIC
2009b032f27cSSam Leffler  * calcs together with WME.  If necessary disable the crypto
2010b032f27cSSam Leffler  * hardware and mark the 802.11 state so keys will be setup
2011b032f27cSSam Leffler  * with the MIC work done in software.
2012b032f27cSSam Leffler  */
2013b032f27cSSam Leffler static void
2014b032f27cSSam Leffler ath_settkipmic(struct ath_softc *sc)
2015b032f27cSSam Leffler {
2016b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
2017b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
2018b032f27cSSam Leffler 
2019b032f27cSSam Leffler 	if ((ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIP) && !sc->sc_wmetkipmic) {
2020b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WME) {
2021b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_FALSE);
2022b032f27cSSam Leffler 			ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC;
2023b032f27cSSam Leffler 		} else {
2024b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_TRUE);
2025b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
2026b032f27cSSam Leffler 		}
2027b032f27cSSam Leffler 	}
2028b032f27cSSam Leffler }
2029b032f27cSSam Leffler 
20305591b213SSam Leffler static void
20315591b213SSam Leffler ath_init(void *arg)
20325591b213SSam Leffler {
20335591b213SSam Leffler 	struct ath_softc *sc = (struct ath_softc *) arg;
2034fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
2035b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
20365591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
20375591b213SSam Leffler 	HAL_STATUS status;
20385591b213SSam Leffler 
2039c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
2040c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
20415591b213SSam Leffler 
2042f0b2a0beSSam Leffler 	ATH_LOCK(sc);
20435591b213SSam Leffler 	/*
20445591b213SSam Leffler 	 * Stop anything previously setup.  This is safe
20455591b213SSam Leffler 	 * whether this is the first time through or not.
20465591b213SSam Leffler 	 */
2047c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
20485591b213SSam Leffler 
20495591b213SSam Leffler 	/*
20505591b213SSam Leffler 	 * The basic interface to setting the hardware in a good
20515591b213SSam Leffler 	 * state is ``reset''.  On return the hardware is known to
20525591b213SSam Leffler 	 * be powered up and with interrupts disabled.  This must
20535591b213SSam Leffler 	 * be followed by initialization of the appropriate bits
20545591b213SSam Leffler 	 * and then setup of the interrupt mask.
20555591b213SSam Leffler 	 */
2056b032f27cSSam Leffler 	ath_settkipmic(sc);
20576322256bSAdrian Chadd 	ath_update_chainmasks(sc, ic->ic_curchan);
20586322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
20596322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
206059efa8b5SSam Leffler 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_FALSE, &status)) {
20615591b213SSam Leffler 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
20625591b213SSam Leffler 			status);
2063b032f27cSSam Leffler 		ATH_UNLOCK(sc);
2064b032f27cSSam Leffler 		return;
20655591b213SSam Leffler 	}
2066b032f27cSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
20675591b213SSam Leffler 
206848237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
206948237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
207048237774SAdrian Chadd 
20719af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
20729af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
20739af351f9SAdrian Chadd 
20745591b213SSam Leffler 	/*
2075b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
2076b70f530bSAdrian Chadd 	 */
2077b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
2078b70f530bSAdrian Chadd 
2079b70f530bSAdrian Chadd 	/*
2080dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
2081dd6a574eSAdrian Chadd 	 * support it.
2082dd6a574eSAdrian Chadd 	 */
2083dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
2084dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
2085dd6a574eSAdrian Chadd 	else
2086dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
2087dd6a574eSAdrian Chadd 
2088dd6a574eSAdrian Chadd 	/*
2089c59005e9SSam Leffler 	 * Likewise this is set during reset so update
2090c59005e9SSam Leffler 	 * state cached in the driver.
2091c59005e9SSam Leffler 	 */
2092c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
20932dc7fcc4SSam Leffler 	sc->sc_lastlongcal = 0;
20942dc7fcc4SSam Leffler 	sc->sc_resetcal = 1;
20952dc7fcc4SSam Leffler 	sc->sc_lastcalreset = 0;
2096a108ab63SAdrian Chadd 	sc->sc_lastani = 0;
2097a108ab63SAdrian Chadd 	sc->sc_lastshortcal = 0;
2098a108ab63SAdrian Chadd 	sc->sc_doresetcal = AH_FALSE;
20992fd9aabbSAdrian Chadd 	/*
21002fd9aabbSAdrian Chadd 	 * Beacon timers were cleared here; give ath_newstate()
21012fd9aabbSAdrian Chadd 	 * a hint that the beacon timers should be poked when
21022fd9aabbSAdrian Chadd 	 * things transition to the RUN state.
21032fd9aabbSAdrian Chadd 	 */
21042fd9aabbSAdrian Chadd 	sc->sc_beacons = 0;
2105c42a7b7eSSam Leffler 
2106c42a7b7eSSam Leffler 	/*
21075591b213SSam Leffler 	 * Setup the hardware after reset: the key cache
21085591b213SSam Leffler 	 * is filled as needed and the receive engine is
21095591b213SSam Leffler 	 * set going.  Frame transmit is handled entirely
21105591b213SSam Leffler 	 * in the frame output path; there's nothing to do
21115591b213SSam Leffler 	 * here except setup the interrupt mask.
21125591b213SSam Leffler 	 */
21135591b213SSam Leffler 	if (ath_startrecv(sc) != 0) {
21145591b213SSam Leffler 		if_printf(ifp, "unable to start recv logic\n");
2115b032f27cSSam Leffler 		ATH_UNLOCK(sc);
2116b032f27cSSam Leffler 		return;
21175591b213SSam Leffler 	}
21185591b213SSam Leffler 
21195591b213SSam Leffler 	/*
21205591b213SSam Leffler 	 * Enable interrupts.
21215591b213SSam Leffler 	 */
21225591b213SSam Leffler 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
21235591b213SSam Leffler 		  | HAL_INT_RXEOL | HAL_INT_RXORN
212469930f87SAdrian Chadd 		  | HAL_INT_TXURN
21255591b213SSam Leffler 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
2126bcbb08ceSAdrian Chadd 
2127bcbb08ceSAdrian Chadd 	/*
2128bcbb08ceSAdrian Chadd 	 * Enable RX EDMA bits.  Note these overlap with
2129bcbb08ceSAdrian Chadd 	 * HAL_INT_RX and HAL_INT_RXDESC respectively.
2130bcbb08ceSAdrian Chadd 	 */
2131bcbb08ceSAdrian Chadd 	if (sc->sc_isedma)
2132bcbb08ceSAdrian Chadd 		sc->sc_imask |= (HAL_INT_RXHP | HAL_INT_RXLP);
2133bcbb08ceSAdrian Chadd 
2134c42a7b7eSSam Leffler 	/*
2135c42a7b7eSSam Leffler 	 * Enable MIB interrupts when there are hardware phy counters.
2136c42a7b7eSSam Leffler 	 * Note we only do this (at the moment) for station mode.
2137c42a7b7eSSam Leffler 	 */
2138c42a7b7eSSam Leffler 	if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
2139c42a7b7eSSam Leffler 		sc->sc_imask |= HAL_INT_MIB;
21405591b213SSam Leffler 
21415594f5c0SAdrian Chadd 	/* Enable global TX timeout and carrier sense timeout if available */
21426ad02dbaSAdrian Chadd 	if (ath_hal_gtxto_supported(ah))
21433788ebedSAdrian Chadd 		sc->sc_imask |= HAL_INT_GTT;
2144d0a0ebc6SAdrian Chadd 
2145d0a0ebc6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: imask=0x%x\n",
2146d0a0ebc6SAdrian Chadd 		__func__, sc->sc_imask);
21476ad02dbaSAdrian Chadd 
214813f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
21492e986da5SSam Leffler 	callout_reset(&sc->sc_wd_ch, hz, ath_watchdog, sc);
2150b032f27cSSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
21515591b213SSam Leffler 
2152b032f27cSSam Leffler 	ATH_UNLOCK(sc);
2153b032f27cSSam Leffler 
215486e07743SSam Leffler #ifdef ATH_TX99_DIAG
215586e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
215686e07743SSam Leffler 		sc->sc_tx99->start(sc->sc_tx99);
215786e07743SSam Leffler 	else
215886e07743SSam Leffler #endif
2159b032f27cSSam Leffler 	ieee80211_start_all(ic);		/* start all vap's */
21605591b213SSam Leffler }
21615591b213SSam Leffler 
21625591b213SSam Leffler static void
2163c42a7b7eSSam Leffler ath_stop_locked(struct ifnet *ifp)
21645591b213SSam Leffler {
21655591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
21665591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
21675591b213SSam Leffler 
2168c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
2169c42a7b7eSSam Leffler 		__func__, sc->sc_invalid, ifp->if_flags);
21705591b213SSam Leffler 
2171c42a7b7eSSam Leffler 	ATH_LOCK_ASSERT(sc);
217213f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
21735591b213SSam Leffler 		/*
21745591b213SSam Leffler 		 * Shutdown the hardware and driver:
2175c42a7b7eSSam Leffler 		 *    reset 802.11 state machine
21765591b213SSam Leffler 		 *    turn off timers
2177c42a7b7eSSam Leffler 		 *    disable interrupts
2178c42a7b7eSSam Leffler 		 *    turn off the radio
21795591b213SSam Leffler 		 *    clear transmit machinery
21805591b213SSam Leffler 		 *    clear receive machinery
21815591b213SSam Leffler 		 *    drain and release tx queues
21825591b213SSam Leffler 		 *    reclaim beacon resources
21835591b213SSam Leffler 		 *    power down hardware
21845591b213SSam Leffler 		 *
21855591b213SSam Leffler 		 * Note that some of this work is not possible if the
21865591b213SSam Leffler 		 * hardware is gone (invalid).
21875591b213SSam Leffler 		 */
218886e07743SSam Leffler #ifdef ATH_TX99_DIAG
218986e07743SSam Leffler 		if (sc->sc_tx99 != NULL)
219086e07743SSam Leffler 			sc->sc_tx99->stop(sc->sc_tx99);
219186e07743SSam Leffler #endif
21922e986da5SSam Leffler 		callout_stop(&sc->sc_wd_ch);
21932e986da5SSam Leffler 		sc->sc_wd_timer = 0;
219413f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2195c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
21963e50ec2cSSam Leffler 			if (sc->sc_softled) {
21973e50ec2cSSam Leffler 				callout_stop(&sc->sc_ledtimer);
21983e50ec2cSSam Leffler 				ath_hal_gpioset(ah, sc->sc_ledpin,
21993e50ec2cSSam Leffler 					!sc->sc_ledon);
22003e50ec2cSSam Leffler 				sc->sc_blinking = 0;
22013e50ec2cSSam Leffler 			}
22025591b213SSam Leffler 			ath_hal_intrset(ah, 0);
2203c42a7b7eSSam Leffler 		}
2204517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);
2205c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
22069a842e8bSAdrian Chadd 			ath_stoprecv(sc, 1);
2207c42a7b7eSSam Leffler 			ath_hal_phydisable(ah);
2208c42a7b7eSSam Leffler 		} else
22095591b213SSam Leffler 			sc->sc_rxlink = NULL;
2210b032f27cSSam Leffler 		ath_beacon_free(sc);	/* XXX not needed */
2211c42a7b7eSSam Leffler 	}
2212c42a7b7eSSam Leffler }
2213c42a7b7eSSam Leffler 
2214ef27340cSAdrian Chadd #define	MAX_TXRX_ITERATIONS	1000
2215ef27340cSAdrian Chadd static void
221621008bf1SAdrian Chadd ath_txrx_stop_locked(struct ath_softc *sc)
2217ef27340cSAdrian Chadd {
2218ef27340cSAdrian Chadd 	int i = MAX_TXRX_ITERATIONS;
2219ef27340cSAdrian Chadd 
2220ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
222121008bf1SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
222221008bf1SAdrian Chadd 
2223ef27340cSAdrian Chadd 	/*
2224ef27340cSAdrian Chadd 	 * Sleep until all the pending operations have completed.
2225ef27340cSAdrian Chadd 	 *
2226ef27340cSAdrian Chadd 	 * The caller must ensure that reset has been incremented
2227ef27340cSAdrian Chadd 	 * or the pending operations may continue being queued.
2228ef27340cSAdrian Chadd 	 */
2229ef27340cSAdrian Chadd 	while (sc->sc_rxproc_cnt || sc->sc_txproc_cnt ||
2230ef27340cSAdrian Chadd 	    sc->sc_txstart_cnt || sc->sc_intr_cnt) {
2231ef27340cSAdrian Chadd 		if (i <= 0)
2232ef27340cSAdrian Chadd 			break;
2233a2d8240dSAdrian Chadd 		msleep(sc, &sc->sc_pcu_mtx, 0, "ath_txrx_stop", 1);
2234ef27340cSAdrian Chadd 		i--;
2235ef27340cSAdrian Chadd 	}
2236ef27340cSAdrian Chadd 
2237ef27340cSAdrian Chadd 	if (i <= 0)
2238ef27340cSAdrian Chadd 		device_printf(sc->sc_dev,
2239ef27340cSAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2240ef27340cSAdrian Chadd 		    __func__, MAX_TXRX_ITERATIONS);
2241ef27340cSAdrian Chadd }
2242ef27340cSAdrian Chadd #undef	MAX_TXRX_ITERATIONS
2243ef27340cSAdrian Chadd 
2244e78719adSAdrian Chadd #if 0
2245ef27340cSAdrian Chadd static void
224621008bf1SAdrian Chadd ath_txrx_stop(struct ath_softc *sc)
224721008bf1SAdrian Chadd {
224821008bf1SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
224921008bf1SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
225021008bf1SAdrian Chadd 
225121008bf1SAdrian Chadd 	ATH_PCU_LOCK(sc);
225221008bf1SAdrian Chadd 	ath_txrx_stop_locked(sc);
225321008bf1SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
225421008bf1SAdrian Chadd }
2255e78719adSAdrian Chadd #endif
225621008bf1SAdrian Chadd 
225721008bf1SAdrian Chadd static void
2258ef27340cSAdrian Chadd ath_txrx_start(struct ath_softc *sc)
2259ef27340cSAdrian Chadd {
2260ef27340cSAdrian Chadd 
2261ef27340cSAdrian Chadd 	taskqueue_unblock(sc->sc_tq);
2262ef27340cSAdrian Chadd }
2263ef27340cSAdrian Chadd 
2264ee321975SAdrian Chadd /*
2265ee321975SAdrian Chadd  * Grab the reset lock, and wait around until noone else
2266ee321975SAdrian Chadd  * is trying to do anything with it.
2267ee321975SAdrian Chadd  *
2268ee321975SAdrian Chadd  * This is totally horrible but we can't hold this lock for
2269ee321975SAdrian Chadd  * long enough to do TX/RX or we end up with net80211/ip stack
2270ee321975SAdrian Chadd  * LORs and eventual deadlock.
2271ee321975SAdrian Chadd  *
2272ee321975SAdrian Chadd  * "dowait" signals whether to spin, waiting for the reset
2273ee321975SAdrian Chadd  * lock count to reach 0. This should (for now) only be used
2274ee321975SAdrian Chadd  * during the reset path, as the rest of the code may not
2275ee321975SAdrian Chadd  * be locking-reentrant enough to behave correctly.
2276ee321975SAdrian Chadd  *
2277ee321975SAdrian Chadd  * Another, cleaner way should be found to serialise all of
2278ee321975SAdrian Chadd  * these operations.
2279ee321975SAdrian Chadd  */
2280ee321975SAdrian Chadd #define	MAX_RESET_ITERATIONS	10
2281ee321975SAdrian Chadd static int
2282ee321975SAdrian Chadd ath_reset_grablock(struct ath_softc *sc, int dowait)
2283ee321975SAdrian Chadd {
2284ee321975SAdrian Chadd 	int w = 0;
2285ee321975SAdrian Chadd 	int i = MAX_RESET_ITERATIONS;
2286ee321975SAdrian Chadd 
2287ee321975SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
2288ee321975SAdrian Chadd 	do {
2289ee321975SAdrian Chadd 		if (sc->sc_inreset_cnt == 0) {
2290ee321975SAdrian Chadd 			w = 1;
2291ee321975SAdrian Chadd 			break;
2292ee321975SAdrian Chadd 		}
2293ee321975SAdrian Chadd 		if (dowait == 0) {
2294ee321975SAdrian Chadd 			w = 0;
2295ee321975SAdrian Chadd 			break;
2296ee321975SAdrian Chadd 		}
2297ee321975SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2298ee321975SAdrian Chadd 		pause("ath_reset_grablock", 1);
2299ee321975SAdrian Chadd 		i--;
2300ee321975SAdrian Chadd 		ATH_PCU_LOCK(sc);
2301ee321975SAdrian Chadd 	} while (i > 0);
2302ee321975SAdrian Chadd 
2303ee321975SAdrian Chadd 	/*
2304ee321975SAdrian Chadd 	 * We always increment the refcounter, regardless
2305ee321975SAdrian Chadd 	 * of whether we succeeded to get it in an exclusive
2306ee321975SAdrian Chadd 	 * way.
2307ee321975SAdrian Chadd 	 */
2308ee321975SAdrian Chadd 	sc->sc_inreset_cnt++;
2309ee321975SAdrian Chadd 
2310ee321975SAdrian Chadd 	if (i <= 0)
2311ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2312ee321975SAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2313ee321975SAdrian Chadd 		    __func__, MAX_RESET_ITERATIONS);
2314ee321975SAdrian Chadd 
2315ee321975SAdrian Chadd 	if (w == 0)
2316ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2317ee321975SAdrian Chadd 		    "%s: warning, recursive reset path!\n",
2318ee321975SAdrian Chadd 		    __func__);
2319ee321975SAdrian Chadd 
2320ee321975SAdrian Chadd 	return w;
2321ee321975SAdrian Chadd }
2322ee321975SAdrian Chadd #undef MAX_RESET_ITERATIONS
2323ee321975SAdrian Chadd 
2324ee321975SAdrian Chadd /*
2325ee321975SAdrian Chadd  * XXX TODO: write ath_reset_releaselock
2326ee321975SAdrian Chadd  */
2327ee321975SAdrian Chadd 
2328c42a7b7eSSam Leffler static void
2329c42a7b7eSSam Leffler ath_stop(struct ifnet *ifp)
2330c42a7b7eSSam Leffler {
2331c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2332c42a7b7eSSam Leffler 
2333c42a7b7eSSam Leffler 	ATH_LOCK(sc);
2334c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
2335f0b2a0beSSam Leffler 	ATH_UNLOCK(sc);
23365591b213SSam Leffler }
23375591b213SSam Leffler 
23385591b213SSam Leffler /*
23395591b213SSam Leffler  * Reset the hardware w/o losing operational state.  This is
23405591b213SSam Leffler  * basically a more efficient way of doing ath_stop, ath_init,
23415591b213SSam Leffler  * followed by state transitions to the current 802.11
2342c42a7b7eSSam Leffler  * operational state.  Used to recover from various errors and
2343c42a7b7eSSam Leffler  * to reset or reload hardware state.
23445591b213SSam Leffler  */
23456079fdbeSAdrian Chadd int
2346517526efSAdrian Chadd ath_reset(struct ifnet *ifp, ATH_RESET_TYPE reset_type)
23475591b213SSam Leffler {
2348c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2349b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
23505591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
23515591b213SSam Leffler 	HAL_STATUS status;
2352ef27340cSAdrian Chadd 	int i;
23535591b213SSam Leffler 
2354f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
235516d4de92SAdrian Chadd 
2356ee321975SAdrian Chadd 	/* Ensure ATH_LOCK isn't held; ath_rx_proc can't be locked */
2357ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
2358ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
2359ef27340cSAdrian Chadd 
2360d52f7132SAdrian Chadd 	/* Try to (stop any further TX/RX from occuring */
2361d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
2362d52f7132SAdrian Chadd 
2363ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2364904e385eSAdrian Chadd 
2365904e385eSAdrian Chadd 	/*
2366904e385eSAdrian Chadd 	 * Grab the reset lock before TX/RX is stopped.
2367904e385eSAdrian Chadd 	 *
2368904e385eSAdrian Chadd 	 * This is needed to ensure that when the TX/RX actually does finish,
2369904e385eSAdrian Chadd 	 * no further TX/RX/reset runs in parallel with this.
2370904e385eSAdrian Chadd 	 */
2371ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
2372ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
2373ef27340cSAdrian Chadd 		    __func__);
2374ef27340cSAdrian Chadd 	}
2375904e385eSAdrian Chadd 
2376904e385eSAdrian Chadd 	/* disable interrupts */
2377904e385eSAdrian Chadd 	ath_hal_intrset(ah, 0);
2378904e385eSAdrian Chadd 
2379904e385eSAdrian Chadd 	/*
2380904e385eSAdrian Chadd 	 * Now, ensure that any in progress TX/RX completes before we
2381904e385eSAdrian Chadd 	 * continue.
2382904e385eSAdrian Chadd 	 */
2383904e385eSAdrian Chadd 	ath_txrx_stop_locked(sc);
2384904e385eSAdrian Chadd 
2385ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2386ef27340cSAdrian Chadd 
2387f52d3452SAdrian Chadd 	/*
23889a842e8bSAdrian Chadd 	 * Should now wait for pending TX/RX to complete
23899a842e8bSAdrian Chadd 	 * and block future ones from occuring. This needs to be
23909a842e8bSAdrian Chadd 	 * done before the TX queue is drained.
2391f52d3452SAdrian Chadd 	 */
2392ef27340cSAdrian Chadd 	ath_draintxq(sc, reset_type);	/* stop xmit side */
2393ef27340cSAdrian Chadd 
2394ef27340cSAdrian Chadd 	/*
2395ef27340cSAdrian Chadd 	 * Regardless of whether we're doing a no-loss flush or
2396ef27340cSAdrian Chadd 	 * not, stop the PCU and handle what's in the RX queue.
2397ef27340cSAdrian Chadd 	 * That way frames aren't dropped which shouldn't be.
2398ef27340cSAdrian Chadd 	 */
23999a842e8bSAdrian Chadd 	ath_stoprecv(sc, (reset_type != ATH_RESET_NOLOSS));
2400f8cc9b09SAdrian Chadd 	ath_rx_flush(sc);
2401ef27340cSAdrian Chadd 
2402b032f27cSSam Leffler 	ath_settkipmic(sc);		/* configure TKIP MIC handling */
24035591b213SSam Leffler 	/* NB: indicate channel change so we do a full reset */
24046322256bSAdrian Chadd 	ath_update_chainmasks(sc, ic->ic_curchan);
24056322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
24066322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
240759efa8b5SSam Leffler 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_TRUE, &status))
24085591b213SSam Leffler 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
24095591b213SSam Leffler 			__func__, status);
2410c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
241148237774SAdrian Chadd 
241248237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
241348237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
241448237774SAdrian Chadd 
24159af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
24169af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
24179af351f9SAdrian Chadd 
2418dd6a574eSAdrian Chadd 	/*
2419b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
2420b70f530bSAdrian Chadd 	 */
2421b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
2422b70f530bSAdrian Chadd 
2423b70f530bSAdrian Chadd 	/*
2424dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
2425dd6a574eSAdrian Chadd 	 * support it.
2426dd6a574eSAdrian Chadd 	 */
2427dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
2428dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
2429dd6a574eSAdrian Chadd 	else
2430dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
2431dd6a574eSAdrian Chadd 
243268e8e04eSSam Leffler 	if (ath_startrecv(sc) != 0)	/* restart recv */
243368e8e04eSSam Leffler 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
2434c42a7b7eSSam Leffler 	/*
2435c42a7b7eSSam Leffler 	 * We may be doing a reset in response to an ioctl
2436c42a7b7eSSam Leffler 	 * that changes the channel so update any state that
2437c42a7b7eSSam Leffler 	 * might change as a result.
2438c42a7b7eSSam Leffler 	 */
2439724c193aSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
2440c89b957aSSam Leffler 	if (sc->sc_beacons) {		/* restart beacons */
2441584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
244210ad9a77SSam Leffler 		if (sc->sc_tdma)
244310ad9a77SSam Leffler 			ath_tdma_config(sc, NULL);
244410ad9a77SSam Leffler 		else
244510ad9a77SSam Leffler #endif
2446c89b957aSSam Leffler 			ath_beacon_config(sc, NULL);
244710ad9a77SSam Leffler 	}
2448c42a7b7eSSam Leffler 
2449ef27340cSAdrian Chadd 	/*
2450ef27340cSAdrian Chadd 	 * Release the reset lock and re-enable interrupts here.
2451ef27340cSAdrian Chadd 	 * If an interrupt was being processed in ath_intr(),
2452ef27340cSAdrian Chadd 	 * it would disable interrupts at this point. So we have
2453ef27340cSAdrian Chadd 	 * to atomically enable interrupts and decrement the
2454ef27340cSAdrian Chadd 	 * reset counter - this way ath_intr() doesn't end up
2455ef27340cSAdrian Chadd 	 * disabling interrupts without a corresponding enable
2456ef27340cSAdrian Chadd 	 * in the rest or channel change path.
2457ef27340cSAdrian Chadd 	 */
2458ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2459ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
2460ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
2461ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
2462ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2463ef27340cSAdrian Chadd 
2464ef27340cSAdrian Chadd 	/*
2465ef27340cSAdrian Chadd 	 * TX and RX can be started here. If it were started with
2466ef27340cSAdrian Chadd 	 * sc_inreset_cnt > 0, the TX and RX path would abort.
2467ef27340cSAdrian Chadd 	 * Thus if this is a nested call through the reset or
2468ef27340cSAdrian Chadd 	 * channel change code, TX completion will occur but
2469ef27340cSAdrian Chadd 	 * RX completion and ath_start / ath_tx_start will not
2470ef27340cSAdrian Chadd 	 * run.
2471ef27340cSAdrian Chadd 	 */
2472ef27340cSAdrian Chadd 
2473ef27340cSAdrian Chadd 	/* Restart TX/RX as needed */
2474ef27340cSAdrian Chadd 	ath_txrx_start(sc);
2475ef27340cSAdrian Chadd 
2476375307d4SAdrian Chadd 	/* Restart TX completion and pending TX */
2477ef27340cSAdrian Chadd 	if (reset_type == ATH_RESET_NOLOSS) {
2478ef27340cSAdrian Chadd 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
2479ef27340cSAdrian Chadd 			if (ATH_TXQ_SETUP(sc, i)) {
2480b837332dSAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
2481ef27340cSAdrian Chadd 				ath_txq_restart_dma(sc, &sc->sc_txq[i]);
2482b837332dSAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
2483b837332dSAdrian Chadd 
2484b837332dSAdrian Chadd 				ATH_TX_LOCK(sc);
2485ef27340cSAdrian Chadd 				ath_txq_sched(sc, &sc->sc_txq[i]);
2486375307d4SAdrian Chadd 				ATH_TX_UNLOCK(sc);
2487ef27340cSAdrian Chadd 			}
2488b837332dSAdrian Chadd 		}
2489b837332dSAdrian Chadd 	}
2490ef27340cSAdrian Chadd 
2491ef27340cSAdrian Chadd 	/*
2492ef27340cSAdrian Chadd 	 * This may have been set during an ath_start() call which
2493ef27340cSAdrian Chadd 	 * set this once it detected a concurrent TX was going on.
2494ef27340cSAdrian Chadd 	 * So, clear it.
2495ef27340cSAdrian Chadd 	 */
2496e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
2497ef27340cSAdrian Chadd 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2498e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
2499ef27340cSAdrian Chadd 
2500ef27340cSAdrian Chadd 	/* Handle any frames in the TX queue */
2501ef27340cSAdrian Chadd 	/*
2502ef27340cSAdrian Chadd 	 * XXX should this be done by the caller, rather than
2503ef27340cSAdrian Chadd 	 * ath_reset() ?
2504ef27340cSAdrian Chadd 	 */
25058e739394SAdrian Chadd 	ath_tx_kick(sc);		/* restart xmit */
2506c42a7b7eSSam Leffler 	return 0;
25075591b213SSam Leffler }
25085591b213SSam Leffler 
250968e8e04eSSam Leffler static int
2510b032f27cSSam Leffler ath_reset_vap(struct ieee80211vap *vap, u_long cmd)
2511b032f27cSSam Leffler {
25124b54a231SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
25134b54a231SSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
25144b54a231SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
25154b54a231SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
25164b54a231SSam Leffler 
25174b54a231SSam Leffler 	switch (cmd) {
25184b54a231SSam Leffler 	case IEEE80211_IOC_TXPOWER:
25194b54a231SSam Leffler 		/*
25204b54a231SSam Leffler 		 * If per-packet TPC is enabled, then we have nothing
25214b54a231SSam Leffler 		 * to do; otherwise we need to force the global limit.
25224b54a231SSam Leffler 		 * All this can happen directly; no need to reset.
25234b54a231SSam Leffler 		 */
25244b54a231SSam Leffler 		if (!ath_hal_gettpc(ah))
25254b54a231SSam Leffler 			ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
25264b54a231SSam Leffler 		return 0;
25274b54a231SSam Leffler 	}
2528517526efSAdrian Chadd 	/* XXX? Full or NOLOSS? */
2529517526efSAdrian Chadd 	return ath_reset(ifp, ATH_RESET_FULL);
2530b032f27cSSam Leffler }
2531b032f27cSSam Leffler 
2532b8e788a5SAdrian Chadd struct ath_buf *
2533af33d486SAdrian Chadd _ath_getbuf_locked(struct ath_softc *sc, ath_buf_type_t btype)
253410ad9a77SSam Leffler {
253510ad9a77SSam Leffler 	struct ath_buf *bf;
253610ad9a77SSam Leffler 
253710ad9a77SSam Leffler 	ATH_TXBUF_LOCK_ASSERT(sc);
253810ad9a77SSam Leffler 
2539af33d486SAdrian Chadd 	if (btype == ATH_BUFTYPE_MGMT)
2540af33d486SAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_txbuf_mgmt);
2541af33d486SAdrian Chadd 	else
25426b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_txbuf);
2543af33d486SAdrian Chadd 
2544e346b073SAdrian Chadd 	if (bf == NULL) {
2545e346b073SAdrian Chadd 		sc->sc_stats.ast_tx_getnobuf++;
2546e346b073SAdrian Chadd 	} else {
2547e346b073SAdrian Chadd 		if (bf->bf_flags & ATH_BUF_BUSY) {
2548e346b073SAdrian Chadd 			sc->sc_stats.ast_tx_getbusybuf++;
2549e346b073SAdrian Chadd 			bf = NULL;
2550e346b073SAdrian Chadd 		}
2551e346b073SAdrian Chadd 	}
2552e346b073SAdrian Chadd 
2553af33d486SAdrian Chadd 	if (bf != NULL && (bf->bf_flags & ATH_BUF_BUSY) == 0) {
2554af33d486SAdrian Chadd 		if (btype == ATH_BUFTYPE_MGMT)
2555af33d486SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_txbuf_mgmt, bf, bf_list);
255623ced6c1SAdrian Chadd 		else {
2557af33d486SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
255823ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt--;
255923ced6c1SAdrian Chadd 
256023ced6c1SAdrian Chadd 			/*
256123ced6c1SAdrian Chadd 			 * This shuldn't happen; however just to be
256223ced6c1SAdrian Chadd 			 * safe print a warning and fudge the txbuf
256323ced6c1SAdrian Chadd 			 * count.
256423ced6c1SAdrian Chadd 			 */
256523ced6c1SAdrian Chadd 			if (sc->sc_txbuf_cnt < 0) {
256623ced6c1SAdrian Chadd 				device_printf(sc->sc_dev,
256723ced6c1SAdrian Chadd 				    "%s: sc_txbuf_cnt < 0?\n",
256823ced6c1SAdrian Chadd 				    __func__);
256923ced6c1SAdrian Chadd 				sc->sc_txbuf_cnt = 0;
257023ced6c1SAdrian Chadd 			}
257123ced6c1SAdrian Chadd 		}
2572af33d486SAdrian Chadd 	} else
257310ad9a77SSam Leffler 		bf = NULL;
2574e346b073SAdrian Chadd 
257510ad9a77SSam Leffler 	if (bf == NULL) {
2576af33d486SAdrian Chadd 		/* XXX should check which list, mgmt or otherwise */
257710ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: %s\n", __func__,
25786b349e5aSAdrian Chadd 		    TAILQ_FIRST(&sc->sc_txbuf) == NULL ?
257910ad9a77SSam Leffler 			"out of xmit buffers" : "xmit buffer busy");
2580e346b073SAdrian Chadd 		return NULL;
258110ad9a77SSam Leffler 	}
2582e346b073SAdrian Chadd 
2583af33d486SAdrian Chadd 	/* XXX TODO: should do this at buffer list initialisation */
2584af33d486SAdrian Chadd 	/* XXX (then, ensure the buffer has the right flag set) */
25853feffbd7SAdrian Chadd 	bf->bf_flags = 0;
2586af33d486SAdrian Chadd 	if (btype == ATH_BUFTYPE_MGMT)
2587af33d486SAdrian Chadd 		bf->bf_flags |= ATH_BUF_MGMT;
2588af33d486SAdrian Chadd 	else
2589af33d486SAdrian Chadd 		bf->bf_flags &= (~ATH_BUF_MGMT);
2590af33d486SAdrian Chadd 
2591e346b073SAdrian Chadd 	/* Valid bf here; clear some basic fields */
2592e346b073SAdrian Chadd 	bf->bf_next = NULL;	/* XXX just to be sure */
2593e346b073SAdrian Chadd 	bf->bf_last = NULL;	/* XXX again, just to be sure */
2594e346b073SAdrian Chadd 	bf->bf_comp = NULL;	/* XXX again, just to be sure */
2595e346b073SAdrian Chadd 	bzero(&bf->bf_state, sizeof(bf->bf_state));
2596e346b073SAdrian Chadd 
259785bf9bc3SAdrian Chadd 	/*
259885bf9bc3SAdrian Chadd 	 * Track the descriptor ID only if doing EDMA
259985bf9bc3SAdrian Chadd 	 */
260085bf9bc3SAdrian Chadd 	if (sc->sc_isedma) {
260185bf9bc3SAdrian Chadd 		bf->bf_descid = sc->sc_txbuf_descid;
260285bf9bc3SAdrian Chadd 		sc->sc_txbuf_descid++;
260385bf9bc3SAdrian Chadd 	}
260485bf9bc3SAdrian Chadd 
260510ad9a77SSam Leffler 	return bf;
260610ad9a77SSam Leffler }
260710ad9a77SSam Leffler 
2608e346b073SAdrian Chadd /*
2609e346b073SAdrian Chadd  * When retrying a software frame, buffers marked ATH_BUF_BUSY
2610e346b073SAdrian Chadd  * can't be thrown back on the queue as they could still be
2611e346b073SAdrian Chadd  * in use by the hardware.
2612e346b073SAdrian Chadd  *
2613e346b073SAdrian Chadd  * This duplicates the buffer, or returns NULL.
2614e346b073SAdrian Chadd  *
2615e346b073SAdrian Chadd  * The descriptor is also copied but the link pointers and
2616e346b073SAdrian Chadd  * the DMA segments aren't copied; this frame should thus
2617e346b073SAdrian Chadd  * be again passed through the descriptor setup/chain routines
2618e346b073SAdrian Chadd  * so the link is correct.
2619e346b073SAdrian Chadd  *
2620e346b073SAdrian Chadd  * The caller must free the buffer using ath_freebuf().
2621e346b073SAdrian Chadd  */
2622e346b073SAdrian Chadd struct ath_buf *
26233f3a5dbdSAdrian Chadd ath_buf_clone(struct ath_softc *sc, struct ath_buf *bf)
2624e346b073SAdrian Chadd {
2625e346b073SAdrian Chadd 	struct ath_buf *tbf;
2626e346b073SAdrian Chadd 
2627af33d486SAdrian Chadd 	tbf = ath_getbuf(sc,
2628af33d486SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_MGMT) ?
2629af33d486SAdrian Chadd 	     ATH_BUFTYPE_MGMT : ATH_BUFTYPE_NORMAL);
2630e346b073SAdrian Chadd 	if (tbf == NULL)
2631e346b073SAdrian Chadd 		return NULL;	/* XXX failure? Why? */
2632e346b073SAdrian Chadd 
2633e346b073SAdrian Chadd 	/* Copy basics */
2634e346b073SAdrian Chadd 	tbf->bf_next = NULL;
2635e346b073SAdrian Chadd 	tbf->bf_nseg = bf->bf_nseg;
26363feffbd7SAdrian Chadd 	tbf->bf_flags = bf->bf_flags & ATH_BUF_FLAGS_CLONE;
2637e346b073SAdrian Chadd 	tbf->bf_status = bf->bf_status;
2638e346b073SAdrian Chadd 	tbf->bf_m = bf->bf_m;
2639e346b073SAdrian Chadd 	tbf->bf_node = bf->bf_node;
2640e346b073SAdrian Chadd 	/* will be setup by the chain/setup function */
2641e346b073SAdrian Chadd 	tbf->bf_lastds = NULL;
2642e346b073SAdrian Chadd 	/* for now, last == self */
2643e346b073SAdrian Chadd 	tbf->bf_last = tbf;
2644e346b073SAdrian Chadd 	tbf->bf_comp = bf->bf_comp;
2645e346b073SAdrian Chadd 
2646e346b073SAdrian Chadd 	/* NOTE: DMA segments will be setup by the setup/chain functions */
2647e346b073SAdrian Chadd 
2648e346b073SAdrian Chadd 	/* The caller has to re-init the descriptor + links */
2649e346b073SAdrian Chadd 
26503f3a5dbdSAdrian Chadd 	/*
26513f3a5dbdSAdrian Chadd 	 * Free the DMA mapping here, before we NULL the mbuf.
26523f3a5dbdSAdrian Chadd 	 * We must only call bus_dmamap_unload() once per mbuf chain
26533f3a5dbdSAdrian Chadd 	 * or behaviour is undefined.
26543f3a5dbdSAdrian Chadd 	 */
26553f3a5dbdSAdrian Chadd 	if (bf->bf_m != NULL) {
26563f3a5dbdSAdrian Chadd 		/*
26573f3a5dbdSAdrian Chadd 		 * XXX is this POSTWRITE call required?
26583f3a5dbdSAdrian Chadd 		 */
26593f3a5dbdSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
26603f3a5dbdSAdrian Chadd 		    BUS_DMASYNC_POSTWRITE);
26613f3a5dbdSAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
26623f3a5dbdSAdrian Chadd 	}
26633f3a5dbdSAdrian Chadd 
26643f3a5dbdSAdrian Chadd 	bf->bf_m = NULL;
26653f3a5dbdSAdrian Chadd 	bf->bf_node = NULL;
26663f3a5dbdSAdrian Chadd 
2667e346b073SAdrian Chadd 	/* Copy state */
2668e346b073SAdrian Chadd 	memcpy(&tbf->bf_state, &bf->bf_state, sizeof(bf->bf_state));
2669e346b073SAdrian Chadd 
2670e346b073SAdrian Chadd 	return tbf;
2671e346b073SAdrian Chadd }
2672e346b073SAdrian Chadd 
2673b8e788a5SAdrian Chadd struct ath_buf *
2674af33d486SAdrian Chadd ath_getbuf(struct ath_softc *sc, ath_buf_type_t btype)
267510ad9a77SSam Leffler {
267610ad9a77SSam Leffler 	struct ath_buf *bf;
267710ad9a77SSam Leffler 
267810ad9a77SSam Leffler 	ATH_TXBUF_LOCK(sc);
2679af33d486SAdrian Chadd 	bf = _ath_getbuf_locked(sc, btype);
2680af33d486SAdrian Chadd 	/*
2681af33d486SAdrian Chadd 	 * If a mgmt buffer was requested but we're out of those,
2682af33d486SAdrian Chadd 	 * try requesting a normal one.
2683af33d486SAdrian Chadd 	 */
2684af33d486SAdrian Chadd 	if (bf == NULL && btype == ATH_BUFTYPE_MGMT)
2685af33d486SAdrian Chadd 		bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL);
2686e4e7938aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
268710ad9a77SSam Leffler 	if (bf == NULL) {
268810ad9a77SSam Leffler 		struct ifnet *ifp = sc->sc_ifp;
268910ad9a77SSam Leffler 
269010ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: stop queue\n", __func__);
269110ad9a77SSam Leffler 		sc->sc_stats.ast_tx_qstop++;
2692e4e7938aSAdrian Chadd 		IF_LOCK(&ifp->if_snd);
269310ad9a77SSam Leffler 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2694e4e7938aSAdrian Chadd 		IF_UNLOCK(&ifp->if_snd);
269510ad9a77SSam Leffler 	}
269610ad9a77SSam Leffler 	return bf;
269710ad9a77SSam Leffler }
269810ad9a77SSam Leffler 
26998e739394SAdrian Chadd static void
2700cd7dffd0SAdrian Chadd ath_qflush(struct ifnet *ifp)
27015591b213SSam Leffler {
27025591b213SSam Leffler 
2703cd7dffd0SAdrian Chadd 	/* XXX TODO */
27048e739394SAdrian Chadd }
27058e739394SAdrian Chadd 
27067dcb2beaSAdrian Chadd /*
2707cd7dffd0SAdrian Chadd  * Transmit a single frame.
2708cd7dffd0SAdrian Chadd  *
2709cd7dffd0SAdrian Chadd  * net80211 will free the node reference if the transmit
2710cd7dffd0SAdrian Chadd  * fails, so don't free the node reference here.
27117dcb2beaSAdrian Chadd  */
2712cd7dffd0SAdrian Chadd static int
2713cd7dffd0SAdrian Chadd ath_transmit(struct ifnet *ifp, struct mbuf *m)
2714cd7dffd0SAdrian Chadd {
2715cd7dffd0SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
2716cd7dffd0SAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
2717cd7dffd0SAdrian Chadd 	struct ieee80211_node *ni;
2718cd7dffd0SAdrian Chadd 	struct mbuf *next;
2719cd7dffd0SAdrian Chadd 	struct ath_buf *bf;
2720cd7dffd0SAdrian Chadd 	ath_bufhead frags;
2721cd7dffd0SAdrian Chadd 	int retval = 0;
2722cd7dffd0SAdrian Chadd 
2723cd7dffd0SAdrian Chadd 	/*
2724cd7dffd0SAdrian Chadd 	 * Tell the reset path that we're currently transmitting.
2725cd7dffd0SAdrian Chadd 	 */
2726cd7dffd0SAdrian Chadd 	ATH_PCU_LOCK(sc);
2727cd7dffd0SAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
2728cd7dffd0SAdrian Chadd 		device_printf(sc->sc_dev,
2729cd7dffd0SAdrian Chadd 		    "%s: sc_inreset_cnt > 0; bailing\n", __func__);
2730cd7dffd0SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2731cd7dffd0SAdrian Chadd 		IF_LOCK(&ifp->if_snd);
2732cd7dffd0SAdrian Chadd 		sc->sc_stats.ast_tx_qstop++;
2733cd7dffd0SAdrian Chadd 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2734cd7dffd0SAdrian Chadd 		IF_UNLOCK(&ifp->if_snd);
2735cd7dffd0SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TX, 0, "ath_start_task: OACTIVE, finish");
2736cd7dffd0SAdrian Chadd 		return (ENOBUFS);	/* XXX should be EINVAL or? */
2737cd7dffd0SAdrian Chadd 	}
2738cd7dffd0SAdrian Chadd 	sc->sc_txstart_cnt++;
2739cd7dffd0SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2740cd7dffd0SAdrian Chadd 
2741cd7dffd0SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_transmit: start");
2742cd7dffd0SAdrian Chadd 	/*
2743cd7dffd0SAdrian Chadd 	 * Grab the TX lock - it's ok to do this here; we haven't
2744cd7dffd0SAdrian Chadd 	 * yet started transmitting.
2745cd7dffd0SAdrian Chadd 	 */
2746cd7dffd0SAdrian Chadd 	ATH_TX_LOCK(sc);
2747cd7dffd0SAdrian Chadd 
2748cd7dffd0SAdrian Chadd 	/*
2749cd7dffd0SAdrian Chadd 	 * Node reference, if there's one.
2750cd7dffd0SAdrian Chadd 	 */
27517dcb2beaSAdrian Chadd 	ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
27527dcb2beaSAdrian Chadd 
27537dcb2beaSAdrian Chadd 	/*
27547dcb2beaSAdrian Chadd 	 * Enforce how deep a node queue can get.
27557dcb2beaSAdrian Chadd 	 *
27567dcb2beaSAdrian Chadd 	 * XXX it would be nicer if we kept an mbuf queue per
27577dcb2beaSAdrian Chadd 	 * node and only whacked them into ath_bufs when we
27587dcb2beaSAdrian Chadd 	 * are ready to schedule some traffic from them.
27597dcb2beaSAdrian Chadd 	 * .. that may come later.
27607dcb2beaSAdrian Chadd 	 *
27617dcb2beaSAdrian Chadd 	 * XXX we should also track the per-node hardware queue
27627dcb2beaSAdrian Chadd 	 * depth so it is easy to limit the _SUM_ of the swq and
27637dcb2beaSAdrian Chadd 	 * hwq frames.  Since we only schedule two HWQ frames
27647dcb2beaSAdrian Chadd 	 * at a time, this should be OK for now.
27657dcb2beaSAdrian Chadd 	 */
27667dcb2beaSAdrian Chadd 	if ((!(m->m_flags & M_EAPOL)) &&
27677dcb2beaSAdrian Chadd 	    (ATH_NODE(ni)->an_swq_depth > sc->sc_txq_node_maxdepth)) {
27687dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nodeq_overflow++;
27697dcb2beaSAdrian Chadd 		m_freem(m);
27707dcb2beaSAdrian Chadd 		m = NULL;
2771cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
2772cd7dffd0SAdrian Chadd 		goto finish;
27737dcb2beaSAdrian Chadd 	}
27747dcb2beaSAdrian Chadd 
27757dcb2beaSAdrian Chadd 	/*
27767dcb2beaSAdrian Chadd 	 * Check how many TX buffers are available.
27777dcb2beaSAdrian Chadd 	 *
27787dcb2beaSAdrian Chadd 	 * If this is for non-EAPOL traffic, just leave some
27797dcb2beaSAdrian Chadd 	 * space free in order for buffer cloning and raw
27807dcb2beaSAdrian Chadd 	 * frame transmission to occur.
27817dcb2beaSAdrian Chadd 	 *
27827dcb2beaSAdrian Chadd 	 * If it's for EAPOL traffic, ignore this for now.
27837dcb2beaSAdrian Chadd 	 * Management traffic will be sent via the raw transmit
27847dcb2beaSAdrian Chadd 	 * method which bypasses this check.
27857dcb2beaSAdrian Chadd 	 *
27867dcb2beaSAdrian Chadd 	 * This is needed to ensure that EAPOL frames during
27877dcb2beaSAdrian Chadd 	 * (re) keying have a chance to go out.
27887dcb2beaSAdrian Chadd 	 *
27897dcb2beaSAdrian Chadd 	 * See kern/138379 for more information.
27907dcb2beaSAdrian Chadd 	 */
27917dcb2beaSAdrian Chadd 	if ((!(m->m_flags & M_EAPOL)) &&
27927dcb2beaSAdrian Chadd 	    (sc->sc_txbuf_cnt <= sc->sc_txq_data_minfree)) {
27937dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
2794cd7dffd0SAdrian Chadd 		m_freem(m);
27957dcb2beaSAdrian Chadd 		m = NULL;
2796cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
2797cd7dffd0SAdrian Chadd 		goto finish;
279823ced6c1SAdrian Chadd 	}
279923ced6c1SAdrian Chadd 
28005591b213SSam Leffler 	/*
28015591b213SSam Leffler 	 * Grab a TX buffer and associated resources.
28027dcb2beaSAdrian Chadd 	 *
28037dcb2beaSAdrian Chadd 	 * If it's an EAPOL frame, allocate a MGMT ath_buf.
28047dcb2beaSAdrian Chadd 	 * That way even with temporary buffer exhaustion due to
28057dcb2beaSAdrian Chadd 	 * the data path doesn't leave us without the ability
28067dcb2beaSAdrian Chadd 	 * to transmit management frames.
28077dcb2beaSAdrian Chadd 	 *
28087dcb2beaSAdrian Chadd 	 * Otherwise allocate a normal buffer.
28095591b213SSam Leffler 	 */
28107dcb2beaSAdrian Chadd 	if (m->m_flags & M_EAPOL)
28117dcb2beaSAdrian Chadd 		bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT);
28127dcb2beaSAdrian Chadd 	else
2813af33d486SAdrian Chadd 		bf = ath_getbuf(sc, ATH_BUFTYPE_NORMAL);
28141a85141aSAdrian Chadd 
28157dcb2beaSAdrian Chadd 	if (bf == NULL) {
28167dcb2beaSAdrian Chadd 		/*
2817cd7dffd0SAdrian Chadd 		 * If we failed to allocate a buffer, fail.
28187dcb2beaSAdrian Chadd 		 *
28197dcb2beaSAdrian Chadd 		 * We shouldn't fail normally, due to the check
28207dcb2beaSAdrian Chadd 		 * above.
28217dcb2beaSAdrian Chadd 		 */
28227dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
28237dcb2beaSAdrian Chadd 		IF_LOCK(&ifp->if_snd);
28247dcb2beaSAdrian Chadd 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
28257dcb2beaSAdrian Chadd 		IF_UNLOCK(&ifp->if_snd);
2826cd7dffd0SAdrian Chadd 		m_freem(m);
28277dcb2beaSAdrian Chadd 		m = NULL;
2828cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
2829cd7dffd0SAdrian Chadd 		goto finish;
2830b032f27cSSam Leffler 	}
28317dcb2beaSAdrian Chadd 
2832cd7dffd0SAdrian Chadd 	/*
2833cd7dffd0SAdrian Chadd 	 * At this point we have a buffer; so we need to free it
2834cd7dffd0SAdrian Chadd 	 * if we hit any error conditions.
2835cd7dffd0SAdrian Chadd 	 */
28367dcb2beaSAdrian Chadd 
283768e8e04eSSam Leffler 	/*
283868e8e04eSSam Leffler 	 * Check for fragmentation.  If this frame
283968e8e04eSSam Leffler 	 * has been broken up verify we have enough
284068e8e04eSSam Leffler 	 * buffers to send all the fragments so all
284168e8e04eSSam Leffler 	 * go out or none...
284268e8e04eSSam Leffler 	 */
28436b349e5aSAdrian Chadd 	TAILQ_INIT(&frags);
28441a85141aSAdrian Chadd 	if ((m->m_flags & M_FRAG) &&
28451a85141aSAdrian Chadd 	    !ath_txfrag_setup(sc, &frags, m, ni)) {
284668e8e04eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT,
284768e8e04eSSam Leffler 		    "%s: out of txfrag buffers\n", __func__);
284836c6be9aSSam Leffler 		sc->sc_stats.ast_tx_nofrag++;
28499cb93076SSam Leffler 		ifp->if_oerrors++;
28501a85141aSAdrian Chadd 		ath_freetx(m);
285168e8e04eSSam Leffler 		goto bad;
285268e8e04eSSam Leffler 	}
2853cd7dffd0SAdrian Chadd 
2854cd7dffd0SAdrian Chadd 	/*
2855cd7dffd0SAdrian Chadd 	 * At this point if we have any TX fragments, then we will
2856cd7dffd0SAdrian Chadd 	 * have bumped the node reference once for each of those.
2857cd7dffd0SAdrian Chadd 	 */
2858cd7dffd0SAdrian Chadd 
2859cd7dffd0SAdrian Chadd 	/*
2860cd7dffd0SAdrian Chadd 	 * XXX Is there anything actually _enforcing_ that the
2861cd7dffd0SAdrian Chadd 	 * fragments are being transmitted in one hit, rather than
2862cd7dffd0SAdrian Chadd 	 * being interleaved with other transmissions on that
2863cd7dffd0SAdrian Chadd 	 * hardware queue?
2864cd7dffd0SAdrian Chadd 	 *
2865cd7dffd0SAdrian Chadd 	 * The ATH TX output lock is the only thing serialising this
2866cd7dffd0SAdrian Chadd 	 * right now.
2867cd7dffd0SAdrian Chadd 	 */
2868cd7dffd0SAdrian Chadd 
2869cd7dffd0SAdrian Chadd 	/*
2870cd7dffd0SAdrian Chadd 	 * Calculate the "next fragment" length field in ath_buf
2871cd7dffd0SAdrian Chadd 	 * in order to let the transmit path know enough about
2872cd7dffd0SAdrian Chadd 	 * what to next write to the hardware.
2873cd7dffd0SAdrian Chadd 	 */
2874cd7dffd0SAdrian Chadd 	if (m->m_flags & M_FRAG) {
2875cd7dffd0SAdrian Chadd 		struct ath_buf *fbf = bf;
2876cd7dffd0SAdrian Chadd 		struct ath_buf *n_fbf = NULL;
2877cd7dffd0SAdrian Chadd 		struct mbuf *fm = m->m_nextpkt;
2878cd7dffd0SAdrian Chadd 
2879cd7dffd0SAdrian Chadd 		/*
2880cd7dffd0SAdrian Chadd 		 * We need to walk the list of fragments and set
2881cd7dffd0SAdrian Chadd 		 * the next size to the following buffer.
2882cd7dffd0SAdrian Chadd 		 * However, the first buffer isn't in the frag
2883cd7dffd0SAdrian Chadd 		 * list, so we have to do some gymnastics here.
2884cd7dffd0SAdrian Chadd 		 */
2885cd7dffd0SAdrian Chadd 		TAILQ_FOREACH(n_fbf, &frags, bf_list) {
2886cd7dffd0SAdrian Chadd 			fbf->bf_nextfraglen = fm->m_pkthdr.len;
2887cd7dffd0SAdrian Chadd 			fbf = n_fbf;
2888cd7dffd0SAdrian Chadd 			fm = fm->m_nextpkt;
2889cd7dffd0SAdrian Chadd 		}
2890cd7dffd0SAdrian Chadd 	}
2891cd7dffd0SAdrian Chadd 
2892cd7dffd0SAdrian Chadd 	/*
2893cd7dffd0SAdrian Chadd 	 * Bump the ifp output counter.
2894cd7dffd0SAdrian Chadd 	 *
2895cd7dffd0SAdrian Chadd 	 * XXX should use atomics?
2896cd7dffd0SAdrian Chadd 	 */
28971a85141aSAdrian Chadd 	ifp->if_opackets++;
28981a85141aSAdrian Chadd nextfrag:
289968e8e04eSSam Leffler 	/*
29001a85141aSAdrian Chadd 	 * Pass the frame to the h/w for transmission.
29011a85141aSAdrian Chadd 	 * Fragmented frames have each frag chained together
29021a85141aSAdrian Chadd 	 * with m_nextpkt.  We know there are sufficient ath_buf's
29031a85141aSAdrian Chadd 	 * to send all the frags because of work done by
29041a85141aSAdrian Chadd 	 * ath_txfrag_setup.  We leave m_nextpkt set while
29051a85141aSAdrian Chadd 	 * calling ath_tx_start so it can use it to extend the
29061a85141aSAdrian Chadd 	 * the tx duration to cover the subsequent frag and
29071a85141aSAdrian Chadd 	 * so it can reclaim all the mbufs in case of an error;
29081a85141aSAdrian Chadd 	 * ath_tx_start clears m_nextpkt once it commits to
29091a85141aSAdrian Chadd 	 * handing the frame to the hardware.
2910cd7dffd0SAdrian Chadd 	 *
2911cd7dffd0SAdrian Chadd 	 * Note: if this fails, then the mbufs are freed but
2912cd7dffd0SAdrian Chadd 	 * not the node reference.
291368e8e04eSSam Leffler 	 */
29141a85141aSAdrian Chadd 	next = m->m_nextpkt;
29151a85141aSAdrian Chadd 	if (ath_tx_start(sc, ni, bf, m)) {
29165591b213SSam Leffler bad:
29171a85141aSAdrian Chadd 		ifp->if_oerrors++;
29181a85141aSAdrian Chadd reclaim:
291968e8e04eSSam Leffler 		bf->bf_m = NULL;
292068e8e04eSSam Leffler 		bf->bf_node = NULL;
2921c42a7b7eSSam Leffler 		ATH_TXBUF_LOCK(sc);
2922e1a50456SAdrian Chadd 		ath_returnbuf_head(sc, bf);
2923cd7dffd0SAdrian Chadd 		/*
2924cd7dffd0SAdrian Chadd 		 * Free the rest of the node references and
2925cd7dffd0SAdrian Chadd 		 * buffers for the fragment list.
2926cd7dffd0SAdrian Chadd 		 */
292768e8e04eSSam Leffler 		ath_txfrag_cleanup(sc, &frags, ni);
2928c42a7b7eSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
2929cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
2930cd7dffd0SAdrian Chadd 		goto finish;
29311a85141aSAdrian Chadd 	}
29321a85141aSAdrian Chadd 
2933548a605dSAdrian Chadd 	/*
2934548a605dSAdrian Chadd 	 * Check here if the node is in power save state.
2935548a605dSAdrian Chadd 	 */
2936548a605dSAdrian Chadd 	ath_tx_update_tim(sc, ni, 1);
2937548a605dSAdrian Chadd 
29381a85141aSAdrian Chadd 	if (next != NULL) {
293968e8e04eSSam Leffler 		/*
29401a85141aSAdrian Chadd 		 * Beware of state changing between frags.
29411a85141aSAdrian Chadd 		 * XXX check sta power-save state?
294268e8e04eSSam Leffler 		 */
29431a85141aSAdrian Chadd 		if (ni->ni_vap->iv_state != IEEE80211_S_RUN) {
2944c5239edbSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
29451a85141aSAdrian Chadd 			    "%s: flush fragmented packet, state %s\n",
29461a85141aSAdrian Chadd 			    __func__,
29471a85141aSAdrian Chadd 			    ieee80211_state_name[ni->ni_vap->iv_state]);
2948a91ab3c0SAdrian Chadd 			/* XXX dmamap */
29491a85141aSAdrian Chadd 			ath_freetx(next);
29501a85141aSAdrian Chadd 			goto reclaim;
2951c5239edbSAdrian Chadd 		}
29521a85141aSAdrian Chadd 		m = next;
29531a85141aSAdrian Chadd 		bf = TAILQ_FIRST(&frags);
29541a85141aSAdrian Chadd 		KASSERT(bf != NULL, ("no buf for txfrag"));
29551a85141aSAdrian Chadd 		TAILQ_REMOVE(&frags, bf, bf_list);
29561a85141aSAdrian Chadd 		goto nextfrag;
2957c5239edbSAdrian Chadd 	}
2958c5239edbSAdrian Chadd 
2959cd7dffd0SAdrian Chadd 	/*
2960cd7dffd0SAdrian Chadd 	 * Bump watchdog timer.
2961cd7dffd0SAdrian Chadd 	 */
29621a85141aSAdrian Chadd 	sc->sc_wd_timer = 5;
2963cd7dffd0SAdrian Chadd 
2964cd7dffd0SAdrian Chadd finish:
2965cd7dffd0SAdrian Chadd 	ATH_TX_UNLOCK(sc);
2966cd7dffd0SAdrian Chadd 
2967cd7dffd0SAdrian Chadd 	/*
2968cd7dffd0SAdrian Chadd 	 * Finished transmitting!
2969cd7dffd0SAdrian Chadd 	 */
2970cd7dffd0SAdrian Chadd 	ATH_PCU_LOCK(sc);
2971cd7dffd0SAdrian Chadd 	sc->sc_txstart_cnt--;
2972cd7dffd0SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2973cd7dffd0SAdrian Chadd 
2974cd7dffd0SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_transmit: finished");
2975cd7dffd0SAdrian Chadd 
2976cd7dffd0SAdrian Chadd 	return (retval);
29775591b213SSam Leffler }
2978cd7dffd0SAdrian Chadd 
29795591b213SSam Leffler static int
29805591b213SSam Leffler ath_media_change(struct ifnet *ifp)
29815591b213SSam Leffler {
2982b032f27cSSam Leffler 	int error = ieee80211_media_change(ifp);
2983b032f27cSSam Leffler 	/* NB: only the fixed rate can change and that doesn't need a reset */
2984b032f27cSSam Leffler 	return (error == ENETRESET ? 0 : error);
29855591b213SSam Leffler }
29865591b213SSam Leffler 
2987c42a7b7eSSam Leffler /*
2988c42a7b7eSSam Leffler  * Block/unblock tx+rx processing while a key change is done.
2989c42a7b7eSSam Leffler  * We assume the caller serializes key management operations
2990c42a7b7eSSam Leffler  * so we only need to worry about synchronization with other
2991c42a7b7eSSam Leffler  * uses that originate in the driver.
2992c42a7b7eSSam Leffler  */
2993c42a7b7eSSam Leffler static void
2994b032f27cSSam Leffler ath_key_update_begin(struct ieee80211vap *vap)
2995c42a7b7eSSam Leffler {
2996b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2997c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2998c42a7b7eSSam Leffler 
2999c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
3000b032f27cSSam Leffler 	taskqueue_block(sc->sc_tq);
3001c42a7b7eSSam Leffler 	IF_LOCK(&ifp->if_snd);		/* NB: doesn't block mgmt frames */
3002c42a7b7eSSam Leffler }
3003c42a7b7eSSam Leffler 
3004c42a7b7eSSam Leffler static void
3005b032f27cSSam Leffler ath_key_update_end(struct ieee80211vap *vap)
3006c42a7b7eSSam Leffler {
3007b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
3008c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
3009c42a7b7eSSam Leffler 
3010c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
3011c42a7b7eSSam Leffler 	IF_UNLOCK(&ifp->if_snd);
3012b032f27cSSam Leffler 	taskqueue_unblock(sc->sc_tq);
3013c42a7b7eSSam Leffler }
30145591b213SSam Leffler 
3015b032f27cSSam Leffler static void
3016b032f27cSSam Leffler ath_update_promisc(struct ifnet *ifp)
3017b032f27cSSam Leffler {
3018b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
3019b032f27cSSam Leffler 	u_int32_t rfilt;
3020b032f27cSSam Leffler 
3021b032f27cSSam Leffler 	/* configure rx filter */
3022b032f27cSSam Leffler 	rfilt = ath_calcrxfilter(sc);
3023b032f27cSSam Leffler 	ath_hal_setrxfilter(sc->sc_ah, rfilt);
3024b032f27cSSam Leffler 
3025b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x\n", __func__, rfilt);
3026b032f27cSSam Leffler }
3027b032f27cSSam Leffler 
3028b032f27cSSam Leffler static void
3029b032f27cSSam Leffler ath_update_mcast(struct ifnet *ifp)
3030b032f27cSSam Leffler {
3031b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
3032b032f27cSSam Leffler 	u_int32_t mfilt[2];
3033b032f27cSSam Leffler 
3034b032f27cSSam Leffler 	/* calculate and install multicast filter */
3035b032f27cSSam Leffler 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
3036b032f27cSSam Leffler 		struct ifmultiaddr *ifma;
3037b032f27cSSam Leffler 		/*
3038b032f27cSSam Leffler 		 * Merge multicast addresses to form the hardware filter.
3039b032f27cSSam Leffler 		 */
3040b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = 0;
3041eb956cd0SRobert Watson 		if_maddr_rlock(ifp);	/* XXX need some fiddling to remove? */
3042b032f27cSSam Leffler 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3043b032f27cSSam Leffler 			caddr_t dl;
3044b032f27cSSam Leffler 			u_int32_t val;
3045b032f27cSSam Leffler 			u_int8_t pos;
3046b032f27cSSam Leffler 
3047b032f27cSSam Leffler 			/* calculate XOR of eight 6bit values */
3048b032f27cSSam Leffler 			dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
3049b032f27cSSam Leffler 			val = LE_READ_4(dl + 0);
3050b032f27cSSam Leffler 			pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
3051b032f27cSSam Leffler 			val = LE_READ_4(dl + 3);
3052b032f27cSSam Leffler 			pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
3053b032f27cSSam Leffler 			pos &= 0x3f;
3054b032f27cSSam Leffler 			mfilt[pos / 32] |= (1 << (pos % 32));
3055b032f27cSSam Leffler 		}
3056eb956cd0SRobert Watson 		if_maddr_runlock(ifp);
3057b032f27cSSam Leffler 	} else
3058b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = ~0;
3059b032f27cSSam Leffler 	ath_hal_setmcastfilter(sc->sc_ah, mfilt[0], mfilt[1]);
3060b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: MC filter %08x:%08x\n",
3061b032f27cSSam Leffler 		__func__, mfilt[0], mfilt[1]);
30624bc0e754SSam Leffler }
30634bc0e754SSam Leffler 
3064e60c4fc2SAdrian Chadd void
30655591b213SSam Leffler ath_mode_init(struct ath_softc *sc)
30665591b213SSam Leffler {
3067fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
3068b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3069b032f27cSSam Leffler 	u_int32_t rfilt;
30705591b213SSam Leffler 
30714bc0e754SSam Leffler 	/* configure rx filter */
307268e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
30734bc0e754SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
30744bc0e754SSam Leffler 
30755591b213SSam Leffler 	/* configure operational mode */
3076c42a7b7eSSam Leffler 	ath_hal_setopmode(ah);
3077c42a7b7eSSam Leffler 
30783d184db2SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_STATE | ATH_DEBUG_MODE,
30793d184db2SAdrian Chadd 	    "%s: ah=%p, ifp=%p, if_addr=%p\n",
30803d184db2SAdrian Chadd 	    __func__,
30813d184db2SAdrian Chadd 	    ah,
30823d184db2SAdrian Chadd 	    ifp,
30833d184db2SAdrian Chadd 	    (ifp == NULL) ? NULL : ifp->if_addr);
30843d184db2SAdrian Chadd 
308529aca940SSam Leffler 	/* handle any link-level address change */
308629aca940SSam Leffler 	ath_hal_setmac(ah, IF_LLADDR(ifp));
30875591b213SSam Leffler 
30885591b213SSam Leffler 	/* calculate and install multicast filter */
3089b032f27cSSam Leffler 	ath_update_mcast(ifp);
30905591b213SSam Leffler }
30915591b213SSam Leffler 
3092c42a7b7eSSam Leffler /*
3093c42a7b7eSSam Leffler  * Set the slot time based on the current setting.
3094c42a7b7eSSam Leffler  */
3095ba5c15d9SAdrian Chadd void
3096c42a7b7eSSam Leffler ath_setslottime(struct ath_softc *sc)
3097c42a7b7eSSam Leffler {
3098b032f27cSSam Leffler 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3099c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3100aaa70f2fSSam Leffler 	u_int usec;
3101c42a7b7eSSam Leffler 
3102aaa70f2fSSam Leffler 	if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan))
3103aaa70f2fSSam Leffler 		usec = 13;
3104aaa70f2fSSam Leffler 	else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan))
3105aaa70f2fSSam Leffler 		usec = 21;
3106724c193aSSam Leffler 	else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) {
3107724c193aSSam Leffler 		/* honor short/long slot time only in 11g */
3108724c193aSSam Leffler 		/* XXX shouldn't honor on pure g or turbo g channel */
3109724c193aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
3110aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_9;
3111aaa70f2fSSam Leffler 		else
3112aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_20;
3113724c193aSSam Leffler 	} else
3114724c193aSSam Leffler 		usec = HAL_SLOT_TIME_9;
3115aaa70f2fSSam Leffler 
3116aaa70f2fSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
3117aaa70f2fSSam Leffler 	    "%s: chan %u MHz flags 0x%x %s slot, %u usec\n",
3118aaa70f2fSSam Leffler 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
3119aaa70f2fSSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec);
3120aaa70f2fSSam Leffler 
3121aaa70f2fSSam Leffler 	ath_hal_setslottime(ah, usec);
3122c42a7b7eSSam Leffler 	sc->sc_updateslot = OK;
3123c42a7b7eSSam Leffler }
3124c42a7b7eSSam Leffler 
3125c42a7b7eSSam Leffler /*
3126c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update the
3127c42a7b7eSSam Leffler  * slot time based on the current setting.
3128c42a7b7eSSam Leffler  */
3129c42a7b7eSSam Leffler static void
3130c42a7b7eSSam Leffler ath_updateslot(struct ifnet *ifp)
3131c42a7b7eSSam Leffler {
3132c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
3133b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
3134c42a7b7eSSam Leffler 
3135c42a7b7eSSam Leffler 	/*
3136c42a7b7eSSam Leffler 	 * When not coordinating the BSS, change the hardware
3137c42a7b7eSSam Leffler 	 * immediately.  For other operation we defer the change
3138c42a7b7eSSam Leffler 	 * until beacon updates have propagated to the stations.
3139c42a7b7eSSam Leffler 	 */
314059aa14a9SRui Paulo 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
314159aa14a9SRui Paulo 	    ic->ic_opmode == IEEE80211_M_MBSS)
3142c42a7b7eSSam Leffler 		sc->sc_updateslot = UPDATE;
3143c42a7b7eSSam Leffler 	else
3144c42a7b7eSSam Leffler 		ath_setslottime(sc);
3145c42a7b7eSSam Leffler }
3146c42a7b7eSSam Leffler 
3147c42a7b7eSSam Leffler /*
3148622b3fd2SSam Leffler  * Append the contents of src to dst; both queues
3149622b3fd2SSam Leffler  * are assumed to be locked.
3150622b3fd2SSam Leffler  */
3151ba5c15d9SAdrian Chadd void
3152622b3fd2SSam Leffler ath_txqmove(struct ath_txq *dst, struct ath_txq *src)
3153622b3fd2SSam Leffler {
3154e86fd7a7SAdrian Chadd 
3155b837332dSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(src);
3156b837332dSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(dst);
3157b837332dSAdrian Chadd 
31586b349e5aSAdrian Chadd 	TAILQ_CONCAT(&dst->axq_q, &src->axq_q, bf_list);
3159622b3fd2SSam Leffler 	dst->axq_link = src->axq_link;
3160622b3fd2SSam Leffler 	src->axq_link = NULL;
3161622b3fd2SSam Leffler 	dst->axq_depth += src->axq_depth;
31626edf1dc7SAdrian Chadd 	dst->axq_aggr_depth += src->axq_aggr_depth;
3163622b3fd2SSam Leffler 	src->axq_depth = 0;
31646edf1dc7SAdrian Chadd 	src->axq_aggr_depth = 0;
3165622b3fd2SSam Leffler }
3166622b3fd2SSam Leffler 
3167622b3fd2SSam Leffler /*
3168d52f7132SAdrian Chadd  * Reset the hardware, with no loss.
3169d52f7132SAdrian Chadd  *
3170d52f7132SAdrian Chadd  * This can't be used for a general case reset.
3171d52f7132SAdrian Chadd  */
3172d52f7132SAdrian Chadd static void
3173d52f7132SAdrian Chadd ath_reset_proc(void *arg, int pending)
3174d52f7132SAdrian Chadd {
3175d52f7132SAdrian Chadd 	struct ath_softc *sc = arg;
3176d52f7132SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
3177d52f7132SAdrian Chadd 
3178d52f7132SAdrian Chadd #if 0
3179d52f7132SAdrian Chadd 	if_printf(ifp, "%s: resetting\n", __func__);
3180d52f7132SAdrian Chadd #endif
3181d52f7132SAdrian Chadd 	ath_reset(ifp, ATH_RESET_NOLOSS);
3182d52f7132SAdrian Chadd }
3183d52f7132SAdrian Chadd 
3184d52f7132SAdrian Chadd /*
3185c42a7b7eSSam Leffler  * Reset the hardware after detecting beacons have stopped.
3186c42a7b7eSSam Leffler  */
3187c42a7b7eSSam Leffler static void
3188c42a7b7eSSam Leffler ath_bstuck_proc(void *arg, int pending)
3189c42a7b7eSSam Leffler {
3190c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
3191fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
319216d4de92SAdrian Chadd 	uint32_t hangs = 0;
319316d4de92SAdrian Chadd 
319416d4de92SAdrian Chadd 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0)
319516d4de92SAdrian Chadd 		if_printf(ifp, "bb hang detected (0x%x)\n", hangs);
3196c42a7b7eSSam Leffler 
3197370f81faSAdrian Chadd #ifdef	ATH_DEBUG_ALQ
3198370f81faSAdrian Chadd 	if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_STUCK_BEACON))
3199370f81faSAdrian Chadd 		if_ath_alq_post(&sc->sc_alq, ATH_ALQ_STUCK_BEACON, 0, NULL);
3200370f81faSAdrian Chadd #endif
3201370f81faSAdrian Chadd 
3202c42a7b7eSSam Leffler 	if_printf(ifp, "stuck beacon; resetting (bmiss count %u)\n",
3203c42a7b7eSSam Leffler 		sc->sc_bmisscount);
3204c2e34459SSam Leffler 	sc->sc_stats.ast_bstuck++;
320516d4de92SAdrian Chadd 	/*
320616d4de92SAdrian Chadd 	 * This assumes that there's no simultaneous channel mode change
320716d4de92SAdrian Chadd 	 * occuring.
320816d4de92SAdrian Chadd 	 */
3209517526efSAdrian Chadd 	ath_reset(ifp, ATH_RESET_NOLOSS);
3210c42a7b7eSSam Leffler }
3211c42a7b7eSSam Leffler 
32125591b213SSam Leffler static void
32135591b213SSam Leffler ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
32145591b213SSam Leffler {
32155591b213SSam Leffler 	bus_addr_t *paddr = (bus_addr_t*) arg;
3216d77367bfSSam Leffler 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
32175591b213SSam Leffler 	*paddr = segs->ds_addr;
32185591b213SSam Leffler }
32195591b213SSam Leffler 
3220c9f78537SAdrian Chadd /*
3221c9f78537SAdrian Chadd  * Allocate the descriptors and appropriate DMA tag/setup.
3222c9f78537SAdrian Chadd  *
3223c9f78537SAdrian Chadd  * For some situations (eg EDMA TX completion), there isn't a requirement
3224c9f78537SAdrian Chadd  * for the ath_buf entries to be allocated.
3225c9f78537SAdrian Chadd  */
32263d184db2SAdrian Chadd int
3227c9f78537SAdrian Chadd ath_descdma_alloc_desc(struct ath_softc *sc,
3228c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head,
3229b39722d6SAdrian Chadd 	const char *name, int ds_size, int ndesc)
3230c42a7b7eSSam Leffler {
3231c42a7b7eSSam Leffler #define	DS2PHYS(_dd, _ds) \
3232c42a7b7eSSam Leffler 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
323345abcd6cSAdrian Chadd #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
323445abcd6cSAdrian Chadd 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
3235fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
3236c9f78537SAdrian Chadd 	int error;
323745abcd6cSAdrian Chadd 
32381006fc0cSAdrian Chadd 	dd->dd_descsize = ds_size;
3239c42a7b7eSSam Leffler 
32403d9b1596SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
3241b39722d6SAdrian Chadd 	    "%s: %s DMA: %u desc, %d bytes per descriptor\n",
3242b39722d6SAdrian Chadd 	    __func__, name, ndesc, dd->dd_descsize);
3243c42a7b7eSSam Leffler 
3244c42a7b7eSSam Leffler 	dd->dd_name = name;
3245b39722d6SAdrian Chadd 	dd->dd_desc_len = dd->dd_descsize * ndesc;
324645abcd6cSAdrian Chadd 
324745abcd6cSAdrian Chadd 	/*
324845abcd6cSAdrian Chadd 	 * Merlin work-around:
324945abcd6cSAdrian Chadd 	 * Descriptors that cross the 4KB boundary can't be used.
325045abcd6cSAdrian Chadd 	 * Assume one skipped descriptor per 4KB page.
325145abcd6cSAdrian Chadd 	 */
325245abcd6cSAdrian Chadd 	if (! ath_hal_split4ktrans(sc->sc_ah)) {
3253b39722d6SAdrian Chadd 		int numpages = dd->dd_desc_len / 4096;
3254b39722d6SAdrian Chadd 		dd->dd_desc_len += ds_size * numpages;
325545abcd6cSAdrian Chadd 	}
3256c42a7b7eSSam Leffler 
3257c42a7b7eSSam Leffler 	/*
3258c42a7b7eSSam Leffler 	 * Setup DMA descriptor area.
3259a91ab3c0SAdrian Chadd 	 *
3260a91ab3c0SAdrian Chadd 	 * BUS_DMA_ALLOCNOW is not used; we never use bounce
3261a91ab3c0SAdrian Chadd 	 * buffers for the descriptors themselves.
3262c42a7b7eSSam Leffler 	 */
3263c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev),	/* parent */
3264c42a7b7eSSam Leffler 		       PAGE_SIZE, 0,		/* alignment, bounds */
3265c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
3266c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR,	/* highaddr */
3267c42a7b7eSSam Leffler 		       NULL, NULL,		/* filter, filterarg */
3268c42a7b7eSSam Leffler 		       dd->dd_desc_len,		/* maxsize */
3269c42a7b7eSSam Leffler 		       1,			/* nsegments */
32706ccb8ea7SSam Leffler 		       dd->dd_desc_len,		/* maxsegsize */
3271a91ab3c0SAdrian Chadd 		       0,			/* flags */
3272c42a7b7eSSam Leffler 		       NULL,			/* lockfunc */
3273c42a7b7eSSam Leffler 		       NULL,			/* lockarg */
3274c42a7b7eSSam Leffler 		       &dd->dd_dmat);
3275c42a7b7eSSam Leffler 	if (error != 0) {
3276c42a7b7eSSam Leffler 		if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name);
3277c42a7b7eSSam Leffler 		return error;
3278c42a7b7eSSam Leffler 	}
3279c42a7b7eSSam Leffler 
3280c42a7b7eSSam Leffler 	/* allocate descriptors */
3281c42a7b7eSSam Leffler 	error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
32820553a01fSSam Leffler 				 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
32830553a01fSSam Leffler 				 &dd->dd_dmamap);
3284c42a7b7eSSam Leffler 	if (error != 0) {
3285c42a7b7eSSam Leffler 		if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
3286b39722d6SAdrian Chadd 			"error %u\n", ndesc, dd->dd_name, error);
3287c42a7b7eSSam Leffler 		goto fail1;
3288c42a7b7eSSam Leffler 	}
3289c42a7b7eSSam Leffler 
3290c42a7b7eSSam Leffler 	error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
3291c42a7b7eSSam Leffler 				dd->dd_desc, dd->dd_desc_len,
3292c42a7b7eSSam Leffler 				ath_load_cb, &dd->dd_desc_paddr,
3293c42a7b7eSSam Leffler 				BUS_DMA_NOWAIT);
3294c42a7b7eSSam Leffler 	if (error != 0) {
3295c42a7b7eSSam Leffler 		if_printf(ifp, "unable to map %s descriptors, error %u\n",
3296c42a7b7eSSam Leffler 			dd->dd_name, error);
3297c42a7b7eSSam Leffler 		goto fail2;
3298c42a7b7eSSam Leffler 	}
3299c42a7b7eSSam Leffler 
3300c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
3301c9f78537SAdrian Chadd 	    __func__, dd->dd_name, (uint8_t *) dd->dd_desc,
3302c9f78537SAdrian Chadd 	    (u_long) dd->dd_desc_len, (caddr_t) dd->dd_desc_paddr,
3303c9f78537SAdrian Chadd 	    /*XXX*/ (u_long) dd->dd_desc_len);
3304c9f78537SAdrian Chadd 
3305c9f78537SAdrian Chadd 	return (0);
3306c9f78537SAdrian Chadd 
3307c9f78537SAdrian Chadd fail2:
3308c9f78537SAdrian Chadd 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3309c9f78537SAdrian Chadd fail1:
3310c9f78537SAdrian Chadd 	bus_dma_tag_destroy(dd->dd_dmat);
3311c9f78537SAdrian Chadd 	memset(dd, 0, sizeof(*dd));
3312c9f78537SAdrian Chadd 	return error;
3313c9f78537SAdrian Chadd #undef DS2PHYS
3314c9f78537SAdrian Chadd #undef ATH_DESC_4KB_BOUND_CHECK
3315c9f78537SAdrian Chadd }
3316c9f78537SAdrian Chadd 
3317c9f78537SAdrian Chadd int
3318c9f78537SAdrian Chadd ath_descdma_setup(struct ath_softc *sc,
3319c9f78537SAdrian Chadd 	struct ath_descdma *dd, ath_bufhead *head,
3320c9f78537SAdrian Chadd 	const char *name, int ds_size, int nbuf, int ndesc)
3321c9f78537SAdrian Chadd {
3322c9f78537SAdrian Chadd #define	DS2PHYS(_dd, _ds) \
3323c9f78537SAdrian Chadd 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
3324c9f78537SAdrian Chadd #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
3325c9f78537SAdrian Chadd 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
3326c9f78537SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
3327c9f78537SAdrian Chadd 	uint8_t *ds;
3328c9f78537SAdrian Chadd 	struct ath_buf *bf;
3329c9f78537SAdrian Chadd 	int i, bsize, error;
3330c9f78537SAdrian Chadd 
3331c9f78537SAdrian Chadd 	/* Allocate descriptors */
3332c9f78537SAdrian Chadd 	error = ath_descdma_alloc_desc(sc, dd, head, name, ds_size,
3333b39722d6SAdrian Chadd 	    nbuf * ndesc);
3334c9f78537SAdrian Chadd 
3335c9f78537SAdrian Chadd 	/* Assume any errors during allocation were dealt with */
3336c9f78537SAdrian Chadd 	if (error != 0) {
3337c9f78537SAdrian Chadd 		return (error);
3338c9f78537SAdrian Chadd 	}
3339c9f78537SAdrian Chadd 
3340c9f78537SAdrian Chadd 	ds = (uint8_t *) dd->dd_desc;
3341c42a7b7eSSam Leffler 
3342ebecf802SSam Leffler 	/* allocate rx buffers */
3343c42a7b7eSSam Leffler 	bsize = sizeof(struct ath_buf) * nbuf;
3344c42a7b7eSSam Leffler 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
3345c42a7b7eSSam Leffler 	if (bf == NULL) {
3346c42a7b7eSSam Leffler 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
3347c42a7b7eSSam Leffler 			dd->dd_name, bsize);
3348c42a7b7eSSam Leffler 		goto fail3;
3349c42a7b7eSSam Leffler 	}
3350c42a7b7eSSam Leffler 	dd->dd_bufptr = bf;
3351c42a7b7eSSam Leffler 
33526b349e5aSAdrian Chadd 	TAILQ_INIT(head);
33533d9b1596SAdrian Chadd 	for (i = 0; i < nbuf; i++, bf++, ds += (ndesc * dd->dd_descsize)) {
335445abcd6cSAdrian Chadd 		bf->bf_desc = (struct ath_desc *) ds;
3355c42a7b7eSSam Leffler 		bf->bf_daddr = DS2PHYS(dd, ds);
335645abcd6cSAdrian Chadd 		if (! ath_hal_split4ktrans(sc->sc_ah)) {
335745abcd6cSAdrian Chadd 			/*
335845abcd6cSAdrian Chadd 			 * Merlin WAR: Skip descriptor addresses which
335945abcd6cSAdrian Chadd 			 * cause 4KB boundary crossing along any point
336045abcd6cSAdrian Chadd 			 * in the descriptor.
336145abcd6cSAdrian Chadd 			 */
336245abcd6cSAdrian Chadd 			 if (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr,
33637ef7f613SAdrian Chadd 			     dd->dd_descsize)) {
336445abcd6cSAdrian Chadd 				/* Start at the next page */
336545abcd6cSAdrian Chadd 				ds += 0x1000 - (bf->bf_daddr & 0xFFF);
336645abcd6cSAdrian Chadd 				bf->bf_desc = (struct ath_desc *) ds;
336745abcd6cSAdrian Chadd 				bf->bf_daddr = DS2PHYS(dd, ds);
336845abcd6cSAdrian Chadd 			}
336945abcd6cSAdrian Chadd 		}
3370c42a7b7eSSam Leffler 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
3371c42a7b7eSSam Leffler 				&bf->bf_dmamap);
3372c42a7b7eSSam Leffler 		if (error != 0) {
3373c42a7b7eSSam Leffler 			if_printf(ifp, "unable to create dmamap for %s "
3374c42a7b7eSSam Leffler 				"buffer %u, error %u\n", dd->dd_name, i, error);
3375c42a7b7eSSam Leffler 			ath_descdma_cleanup(sc, dd, head);
3376c42a7b7eSSam Leffler 			return error;
3377c42a7b7eSSam Leffler 		}
33786edf1dc7SAdrian Chadd 		bf->bf_lastds = bf->bf_desc;	/* Just an initial value */
33796b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(head, bf, bf_list);
3380c42a7b7eSSam Leffler 	}
33817ef7f613SAdrian Chadd 
33827ef7f613SAdrian Chadd 	/*
33837ef7f613SAdrian Chadd 	 * XXX TODO: ensure that ds doesn't overflow the descriptor
33847ef7f613SAdrian Chadd 	 * allocation otherwise weird stuff will occur and crash your
33857ef7f613SAdrian Chadd 	 * machine.
33867ef7f613SAdrian Chadd 	 */
3387c42a7b7eSSam Leffler 	return 0;
3388c9f78537SAdrian Chadd 	/* XXX this should likely just call ath_descdma_cleanup() */
3389c42a7b7eSSam Leffler fail3:
3390c42a7b7eSSam Leffler 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3391c42a7b7eSSam Leffler 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3392c42a7b7eSSam Leffler 	bus_dma_tag_destroy(dd->dd_dmat);
3393c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3394c42a7b7eSSam Leffler 	return error;
3395c42a7b7eSSam Leffler #undef DS2PHYS
339645abcd6cSAdrian Chadd #undef ATH_DESC_4KB_BOUND_CHECK
3397c42a7b7eSSam Leffler }
3398c42a7b7eSSam Leffler 
339939abbd9bSAdrian Chadd /*
340039abbd9bSAdrian Chadd  * Allocate ath_buf entries but no descriptor contents.
340139abbd9bSAdrian Chadd  *
340239abbd9bSAdrian Chadd  * This is for RX EDMA where the descriptors are the header part of
340339abbd9bSAdrian Chadd  * the RX buffer.
340439abbd9bSAdrian Chadd  */
340539abbd9bSAdrian Chadd int
340639abbd9bSAdrian Chadd ath_descdma_setup_rx_edma(struct ath_softc *sc,
340739abbd9bSAdrian Chadd 	struct ath_descdma *dd, ath_bufhead *head,
340839abbd9bSAdrian Chadd 	const char *name, int nbuf, int rx_status_len)
340939abbd9bSAdrian Chadd {
341039abbd9bSAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
341139abbd9bSAdrian Chadd 	struct ath_buf *bf;
341239abbd9bSAdrian Chadd 	int i, bsize, error;
341339abbd9bSAdrian Chadd 
341439abbd9bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers\n",
341539abbd9bSAdrian Chadd 	    __func__, name, nbuf);
341639abbd9bSAdrian Chadd 
341739abbd9bSAdrian Chadd 	dd->dd_name = name;
341839abbd9bSAdrian Chadd 	/*
341939abbd9bSAdrian Chadd 	 * This is (mostly) purely for show.  We're not allocating any actual
342039abbd9bSAdrian Chadd 	 * descriptors here as EDMA RX has the descriptor be part
342139abbd9bSAdrian Chadd 	 * of the RX buffer.
342239abbd9bSAdrian Chadd 	 *
342339abbd9bSAdrian Chadd 	 * However, dd_desc_len is used by ath_descdma_free() to determine
342439abbd9bSAdrian Chadd 	 * whether we have already freed this DMA mapping.
342539abbd9bSAdrian Chadd 	 */
34263d9b1596SAdrian Chadd 	dd->dd_desc_len = rx_status_len * nbuf;
34273d9b1596SAdrian Chadd 	dd->dd_descsize = rx_status_len;
342839abbd9bSAdrian Chadd 
342939abbd9bSAdrian Chadd 	/* allocate rx buffers */
343039abbd9bSAdrian Chadd 	bsize = sizeof(struct ath_buf) * nbuf;
343139abbd9bSAdrian Chadd 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
343239abbd9bSAdrian Chadd 	if (bf == NULL) {
343339abbd9bSAdrian Chadd 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
343439abbd9bSAdrian Chadd 			dd->dd_name, bsize);
3435b5b60f35SAdrian Chadd 		error = ENOMEM;
343639abbd9bSAdrian Chadd 		goto fail3;
343739abbd9bSAdrian Chadd 	}
343839abbd9bSAdrian Chadd 	dd->dd_bufptr = bf;
343939abbd9bSAdrian Chadd 
344039abbd9bSAdrian Chadd 	TAILQ_INIT(head);
344139abbd9bSAdrian Chadd 	for (i = 0; i < nbuf; i++, bf++) {
344239abbd9bSAdrian Chadd 		bf->bf_desc = NULL;
344339abbd9bSAdrian Chadd 		bf->bf_daddr = 0;
344439abbd9bSAdrian Chadd 		bf->bf_lastds = NULL;	/* Just an initial value */
344539abbd9bSAdrian Chadd 
344639abbd9bSAdrian Chadd 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
344739abbd9bSAdrian Chadd 				&bf->bf_dmamap);
344839abbd9bSAdrian Chadd 		if (error != 0) {
344939abbd9bSAdrian Chadd 			if_printf(ifp, "unable to create dmamap for %s "
345039abbd9bSAdrian Chadd 				"buffer %u, error %u\n", dd->dd_name, i, error);
345139abbd9bSAdrian Chadd 			ath_descdma_cleanup(sc, dd, head);
345239abbd9bSAdrian Chadd 			return error;
345339abbd9bSAdrian Chadd 		}
345439abbd9bSAdrian Chadd 		TAILQ_INSERT_TAIL(head, bf, bf_list);
345539abbd9bSAdrian Chadd 	}
345639abbd9bSAdrian Chadd 	return 0;
345739abbd9bSAdrian Chadd fail3:
345839abbd9bSAdrian Chadd 	memset(dd, 0, sizeof(*dd));
345939abbd9bSAdrian Chadd 	return error;
346039abbd9bSAdrian Chadd }
346139abbd9bSAdrian Chadd 
34623d184db2SAdrian Chadd void
3463c42a7b7eSSam Leffler ath_descdma_cleanup(struct ath_softc *sc,
3464c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head)
3465c42a7b7eSSam Leffler {
3466c42a7b7eSSam Leffler 	struct ath_buf *bf;
3467c42a7b7eSSam Leffler 	struct ieee80211_node *ni;
3468a91ab3c0SAdrian Chadd 	int do_warning = 0;
3469c42a7b7eSSam Leffler 
34708d467c41SAdrian Chadd 	if (dd->dd_dmamap != 0) {
3471c42a7b7eSSam Leffler 		bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3472c42a7b7eSSam Leffler 		bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3473c42a7b7eSSam Leffler 		bus_dma_tag_destroy(dd->dd_dmat);
34748d467c41SAdrian Chadd 	}
3475c42a7b7eSSam Leffler 
34769ed9f02bSAdrian Chadd 	if (head != NULL) {
34776b349e5aSAdrian Chadd 		TAILQ_FOREACH(bf, head, bf_list) {
3478c42a7b7eSSam Leffler 			if (bf->bf_m) {
3479a91ab3c0SAdrian Chadd 				/*
3480a91ab3c0SAdrian Chadd 				 * XXX warn if there's buffers here.
3481a91ab3c0SAdrian Chadd 				 * XXX it should have been freed by the
3482a91ab3c0SAdrian Chadd 				 * owner!
3483a91ab3c0SAdrian Chadd 				 */
3484a91ab3c0SAdrian Chadd 
3485a91ab3c0SAdrian Chadd 				if (do_warning == 0) {
3486a91ab3c0SAdrian Chadd 					do_warning = 1;
3487a91ab3c0SAdrian Chadd 					device_printf(sc->sc_dev,
3488a91ab3c0SAdrian Chadd 					    "%s: %s: mbuf should've been"
3489a91ab3c0SAdrian Chadd 					    " unmapped/freed!\n",
3490a91ab3c0SAdrian Chadd 					    __func__,
3491a91ab3c0SAdrian Chadd 					    dd->dd_name);
3492a91ab3c0SAdrian Chadd 				}
3493a91ab3c0SAdrian Chadd 				bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
3494a91ab3c0SAdrian Chadd 				    BUS_DMASYNC_POSTREAD);
3495a91ab3c0SAdrian Chadd 				bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3496c42a7b7eSSam Leffler 				m_freem(bf->bf_m);
3497c42a7b7eSSam Leffler 				bf->bf_m = NULL;
3498c42a7b7eSSam Leffler 			}
3499c42a7b7eSSam Leffler 			if (bf->bf_dmamap != NULL) {
3500c42a7b7eSSam Leffler 				bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
3501c42a7b7eSSam Leffler 				bf->bf_dmamap = NULL;
3502c42a7b7eSSam Leffler 			}
3503c42a7b7eSSam Leffler 			ni = bf->bf_node;
3504c42a7b7eSSam Leffler 			bf->bf_node = NULL;
3505c42a7b7eSSam Leffler 			if (ni != NULL) {
3506c42a7b7eSSam Leffler 				/*
3507c42a7b7eSSam Leffler 				 * Reclaim node reference.
3508c42a7b7eSSam Leffler 				 */
3509c42a7b7eSSam Leffler 				ieee80211_free_node(ni);
3510c42a7b7eSSam Leffler 			}
3511c42a7b7eSSam Leffler 		}
35129ed9f02bSAdrian Chadd 	}
3513c42a7b7eSSam Leffler 
35149ed9f02bSAdrian Chadd 	if (head != NULL)
35156b349e5aSAdrian Chadd 		TAILQ_INIT(head);
35169ed9f02bSAdrian Chadd 
35179ed9f02bSAdrian Chadd 	if (dd->dd_bufptr != NULL)
3518c42a7b7eSSam Leffler 		free(dd->dd_bufptr, M_ATHDEV);
3519c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3520c42a7b7eSSam Leffler }
3521c42a7b7eSSam Leffler 
3522c42a7b7eSSam Leffler static int
35235591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc)
35245591b213SSam Leffler {
3525c42a7b7eSSam Leffler 	int error;
35265591b213SSam Leffler 
3527c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
352809067b6eSAdrian Chadd 		    "tx", sc->sc_tx_desclen, ath_txbuf, ATH_MAX_SCATTER);
3529c42a7b7eSSam Leffler 	if (error != 0) {
35305591b213SSam Leffler 		return error;
3531c42a7b7eSSam Leffler 	}
353223ced6c1SAdrian Chadd 	sc->sc_txbuf_cnt = ath_txbuf;
3533c42a7b7eSSam Leffler 
3534af33d486SAdrian Chadd 	error = ath_descdma_setup(sc, &sc->sc_txdma_mgmt, &sc->sc_txbuf_mgmt,
35351006fc0cSAdrian Chadd 		    "tx_mgmt", sc->sc_tx_desclen, ath_txbuf_mgmt,
35361006fc0cSAdrian Chadd 		    ATH_TXDESC);
3537af33d486SAdrian Chadd 	if (error != 0) {
3538af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3539af33d486SAdrian Chadd 		return error;
3540af33d486SAdrian Chadd 	}
3541af33d486SAdrian Chadd 
3542af33d486SAdrian Chadd 	/*
3543af33d486SAdrian Chadd 	 * XXX mark txbuf_mgmt frames with ATH_BUF_MGMT, so the
3544af33d486SAdrian Chadd 	 * flag doesn't have to be set in ath_getbuf_locked().
3545af33d486SAdrian Chadd 	 */
3546af33d486SAdrian Chadd 
3547c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
35481006fc0cSAdrian Chadd 			"beacon", sc->sc_tx_desclen, ATH_BCBUF, 1);
3549c42a7b7eSSam Leffler 	if (error != 0) {
3550af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3551af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
3552af33d486SAdrian Chadd 		    &sc->sc_txbuf_mgmt);
3553c42a7b7eSSam Leffler 		return error;
3554c42a7b7eSSam Leffler 	}
35555591b213SSam Leffler 	return 0;
35565591b213SSam Leffler }
35575591b213SSam Leffler 
35585591b213SSam Leffler static void
35595591b213SSam Leffler ath_desc_free(struct ath_softc *sc)
35605591b213SSam Leffler {
35615591b213SSam Leffler 
3562c42a7b7eSSam Leffler 	if (sc->sc_bdma.dd_desc_len != 0)
3563c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
3564c42a7b7eSSam Leffler 	if (sc->sc_txdma.dd_desc_len != 0)
3565c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3566af33d486SAdrian Chadd 	if (sc->sc_txdma_mgmt.dd_desc_len != 0)
3567af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
3568af33d486SAdrian Chadd 		    &sc->sc_txbuf_mgmt);
35695591b213SSam Leffler }
35705591b213SSam Leffler 
35715591b213SSam Leffler static struct ieee80211_node *
357238c208f8SSam Leffler ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
35735591b213SSam Leffler {
357438c208f8SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3575c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3576c42a7b7eSSam Leffler 	const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
3577c42a7b7eSSam Leffler 	struct ath_node *an;
3578c42a7b7eSSam Leffler 
3579c42a7b7eSSam Leffler 	an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
3580c42a7b7eSSam Leffler 	if (an == NULL) {
3581c42a7b7eSSam Leffler 		/* XXX stat+msg */
3582de5af704SSam Leffler 		return NULL;
35835591b213SSam Leffler 	}
3584c42a7b7eSSam Leffler 	ath_rate_node_init(sc, an);
35855591b213SSam Leffler 
35863dd85b26SAdrian Chadd 	/* Setup the mutex - there's no associd yet so set the name to NULL */
35873dd85b26SAdrian Chadd 	snprintf(an->an_name, sizeof(an->an_name), "%s: node %p",
35883dd85b26SAdrian Chadd 	    device_get_nameunit(sc->sc_dev), an);
35893dd85b26SAdrian Chadd 	mtx_init(&an->an_mtx, an->an_name, NULL, MTX_DEF);
35903dd85b26SAdrian Chadd 
3591eb6f0de0SAdrian Chadd 	/* XXX setup ath_tid */
3592eb6f0de0SAdrian Chadd 	ath_tx_tid_init(sc, an);
3593eb6f0de0SAdrian Chadd 
35949b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__, mac, ":", an);
3595c42a7b7eSSam Leffler 	return &an->an_node;
3596c42a7b7eSSam Leffler }
3597c42a7b7eSSam Leffler 
35985591b213SSam Leffler static void
35994afa805eSAdrian Chadd ath_node_cleanup(struct ieee80211_node *ni)
36004afa805eSAdrian Chadd {
36014afa805eSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
36024afa805eSAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
36034afa805eSAdrian Chadd 
36049b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__,
36059b48fb4bSAdrian Chadd 	    ni->ni_macaddr, ":", ATH_NODE(ni));
36069b48fb4bSAdrian Chadd 
36074afa805eSAdrian Chadd 	/* Cleanup ath_tid, free unused bufs, unlink bufs in TXQ */
3608eb6f0de0SAdrian Chadd 	ath_tx_node_flush(sc, ATH_NODE(ni));
36094afa805eSAdrian Chadd 	ath_rate_node_cleanup(sc, ATH_NODE(ni));
36104afa805eSAdrian Chadd 	sc->sc_node_cleanup(ni);
36114afa805eSAdrian Chadd }
36124afa805eSAdrian Chadd 
36134afa805eSAdrian Chadd static void
3614c42a7b7eSSam Leffler ath_node_free(struct ieee80211_node *ni)
36155591b213SSam Leffler {
3616c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
3617c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
36181e774079SSam Leffler 
36199b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__,
36209b48fb4bSAdrian Chadd 	    ni->ni_macaddr, ":", ATH_NODE(ni));
36213dd85b26SAdrian Chadd 	mtx_destroy(&ATH_NODE(ni)->an_mtx);
3622c42a7b7eSSam Leffler 	sc->sc_node_free(ni);
36235591b213SSam Leffler }
36245591b213SSam Leffler 
362568e8e04eSSam Leffler static void
362668e8e04eSSam Leffler ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
362768e8e04eSSam Leffler {
362868e8e04eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
362968e8e04eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
363068e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
363168e8e04eSSam Leffler 
3632b032f27cSSam Leffler 	*rssi = ic->ic_node_getrssi(ni);
363359efa8b5SSam Leffler 	if (ni->ni_chan != IEEE80211_CHAN_ANYC)
363459efa8b5SSam Leffler 		*noise = ath_hal_getchannoise(ah, ni->ni_chan);
363559efa8b5SSam Leffler 	else
363668e8e04eSSam Leffler 		*noise = -95;		/* nominally correct */
363768e8e04eSSam Leffler }
363868e8e04eSSam Leffler 
3639c42a7b7eSSam Leffler /*
3640c42a7b7eSSam Leffler  * Set the default antenna.
3641c42a7b7eSSam Leffler  */
3642e60c4fc2SAdrian Chadd void
3643c42a7b7eSSam Leffler ath_setdefantenna(struct ath_softc *sc, u_int antenna)
3644c42a7b7eSSam Leffler {
3645c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3646c42a7b7eSSam Leffler 
3647c42a7b7eSSam Leffler 	/* XXX block beacon interrupts */
3648c42a7b7eSSam Leffler 	ath_hal_setdefantenna(ah, antenna);
3649c42a7b7eSSam Leffler 	if (sc->sc_defant != antenna)
3650c42a7b7eSSam Leffler 		sc->sc_stats.ast_ant_defswitch++;
3651c42a7b7eSSam Leffler 	sc->sc_defant = antenna;
3652c42a7b7eSSam Leffler 	sc->sc_rxotherant = 0;
3653c42a7b7eSSam Leffler }
3654c42a7b7eSSam Leffler 
36555463c4a4SSam Leffler static void
3656622b3fd2SSam Leffler ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum)
3657622b3fd2SSam Leffler {
3658622b3fd2SSam Leffler 	txq->axq_qnum = qnum;
3659339ccfb3SSam Leffler 	txq->axq_ac = 0;
3660622b3fd2SSam Leffler 	txq->axq_depth = 0;
366116d4de92SAdrian Chadd 	txq->axq_aggr_depth = 0;
3662622b3fd2SSam Leffler 	txq->axq_intrcnt = 0;
3663622b3fd2SSam Leffler 	txq->axq_link = NULL;
36646b349e5aSAdrian Chadd 	txq->axq_softc = sc;
36656b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_q);
36666b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_tidq);
36673feffbd7SAdrian Chadd 	TAILQ_INIT(&txq->fifo.axq_q);
3668b837332dSAdrian Chadd 	ATH_TXQ_LOCK_INIT(sc, txq);
3669622b3fd2SSam Leffler }
3670622b3fd2SSam Leffler 
36715591b213SSam Leffler /*
3672c42a7b7eSSam Leffler  * Setup a h/w transmit queue.
36735591b213SSam Leffler  */
3674c42a7b7eSSam Leffler static struct ath_txq *
3675c42a7b7eSSam Leffler ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
3676c42a7b7eSSam Leffler {
3677c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
3678c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3679c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
3680c42a7b7eSSam Leffler 	int qnum;
3681c42a7b7eSSam Leffler 
3682c42a7b7eSSam Leffler 	memset(&qi, 0, sizeof(qi));
3683c42a7b7eSSam Leffler 	qi.tqi_subtype = subtype;
3684c42a7b7eSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
3685c42a7b7eSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
3686c42a7b7eSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
3687c42a7b7eSSam Leffler 	/*
3688c42a7b7eSSam Leffler 	 * Enable interrupts only for EOL and DESC conditions.
3689c42a7b7eSSam Leffler 	 * We mark tx descriptors to receive a DESC interrupt
3690c42a7b7eSSam Leffler 	 * when a tx queue gets deep; otherwise waiting for the
3691c42a7b7eSSam Leffler 	 * EOL to reap descriptors.  Note that this is done to
3692c42a7b7eSSam Leffler 	 * reduce interrupt load and this only defers reaping
3693c42a7b7eSSam Leffler 	 * descriptors, never transmitting frames.  Aside from
3694c42a7b7eSSam Leffler 	 * reducing interrupts this also permits more concurrency.
3695c42a7b7eSSam Leffler 	 * The only potential downside is if the tx queue backs
3696c42a7b7eSSam Leffler 	 * up in which case the top half of the kernel may backup
3697c42a7b7eSSam Leffler 	 * due to a lack of tx descriptors.
3698c42a7b7eSSam Leffler 	 */
36996961e9edSAdrian Chadd 	if (sc->sc_isedma)
37006961e9edSAdrian Chadd 		qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE |
37016961e9edSAdrian Chadd 		    HAL_TXQ_TXOKINT_ENABLE;
37026961e9edSAdrian Chadd 	else
37036961e9edSAdrian Chadd 		qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE |
37046961e9edSAdrian Chadd 		    HAL_TXQ_TXDESCINT_ENABLE;
37056961e9edSAdrian Chadd 
3706c42a7b7eSSam Leffler 	qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
3707c42a7b7eSSam Leffler 	if (qnum == -1) {
3708c42a7b7eSSam Leffler 		/*
3709c42a7b7eSSam Leffler 		 * NB: don't print a message, this happens
3710a614e076SSam Leffler 		 * normally on parts with too few tx queues
3711c42a7b7eSSam Leffler 		 */
3712c42a7b7eSSam Leffler 		return NULL;
3713c42a7b7eSSam Leffler 	}
3714c42a7b7eSSam Leffler 	if (qnum >= N(sc->sc_txq)) {
37156891c875SPeter Wemm 		device_printf(sc->sc_dev,
37166891c875SPeter Wemm 			"hal qnum %u out of range, max %zu!\n",
3717c42a7b7eSSam Leffler 			qnum, N(sc->sc_txq));
3718c42a7b7eSSam Leffler 		ath_hal_releasetxqueue(ah, qnum);
3719c42a7b7eSSam Leffler 		return NULL;
3720c42a7b7eSSam Leffler 	}
3721c42a7b7eSSam Leffler 	if (!ATH_TXQ_SETUP(sc, qnum)) {
3722622b3fd2SSam Leffler 		ath_txq_init(sc, &sc->sc_txq[qnum], qnum);
3723c42a7b7eSSam Leffler 		sc->sc_txqsetup |= 1<<qnum;
3724c42a7b7eSSam Leffler 	}
3725c42a7b7eSSam Leffler 	return &sc->sc_txq[qnum];
3726c42a7b7eSSam Leffler #undef N
3727c42a7b7eSSam Leffler }
3728c42a7b7eSSam Leffler 
3729c42a7b7eSSam Leffler /*
3730c42a7b7eSSam Leffler  * Setup a hardware data transmit queue for the specified
3731c42a7b7eSSam Leffler  * access control.  The hal may not support all requested
3732c42a7b7eSSam Leffler  * queues in which case it will return a reference to a
3733c42a7b7eSSam Leffler  * previously setup queue.  We record the mapping from ac's
3734c42a7b7eSSam Leffler  * to h/w queues for use by ath_tx_start and also track
3735c42a7b7eSSam Leffler  * the set of h/w queues being used to optimize work in the
3736c42a7b7eSSam Leffler  * transmit interrupt handler and related routines.
3737c42a7b7eSSam Leffler  */
3738c42a7b7eSSam Leffler static int
3739c42a7b7eSSam Leffler ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
3740c42a7b7eSSam Leffler {
3741c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
3742c42a7b7eSSam Leffler 	struct ath_txq *txq;
3743c42a7b7eSSam Leffler 
3744c42a7b7eSSam Leffler 	if (ac >= N(sc->sc_ac2q)) {
37456891c875SPeter Wemm 		device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
3746c42a7b7eSSam Leffler 			ac, N(sc->sc_ac2q));
3747c42a7b7eSSam Leffler 		return 0;
3748c42a7b7eSSam Leffler 	}
3749c42a7b7eSSam Leffler 	txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
3750c42a7b7eSSam Leffler 	if (txq != NULL) {
3751339ccfb3SSam Leffler 		txq->axq_ac = ac;
3752c42a7b7eSSam Leffler 		sc->sc_ac2q[ac] = txq;
3753c42a7b7eSSam Leffler 		return 1;
3754c42a7b7eSSam Leffler 	} else
3755c42a7b7eSSam Leffler 		return 0;
3756c42a7b7eSSam Leffler #undef N
3757c42a7b7eSSam Leffler }
3758c42a7b7eSSam Leffler 
3759c42a7b7eSSam Leffler /*
3760c42a7b7eSSam Leffler  * Update WME parameters for a transmit queue.
3761c42a7b7eSSam Leffler  */
3762c42a7b7eSSam Leffler static int
3763c42a7b7eSSam Leffler ath_txq_update(struct ath_softc *sc, int ac)
3764c42a7b7eSSam Leffler {
3765c42a7b7eSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<v)-1)
3766c42a7b7eSSam Leffler #define	ATH_TXOP_TO_US(v)		(v<<5)
3767b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
3768b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
3769c42a7b7eSSam Leffler 	struct ath_txq *txq = sc->sc_ac2q[ac];
3770c42a7b7eSSam Leffler 	struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
3771c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3772c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
3773c42a7b7eSSam Leffler 
3774c42a7b7eSSam Leffler 	ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
3775584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
377610ad9a77SSam Leffler 	if (sc->sc_tdma) {
377710ad9a77SSam Leffler 		/*
377810ad9a77SSam Leffler 		 * AIFS is zero so there's no pre-transmit wait.  The
377910ad9a77SSam Leffler 		 * burst time defines the slot duration and is configured
378009be6601SSam Leffler 		 * through net80211.  The QCU is setup to not do post-xmit
378110ad9a77SSam Leffler 		 * back off, lockout all lower-priority QCU's, and fire
378210ad9a77SSam Leffler 		 * off the DMA beacon alert timer which is setup based
378310ad9a77SSam Leffler 		 * on the slot configuration.
378410ad9a77SSam Leffler 		 */
378510ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
378610ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
378710ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
378810ad9a77SSam Leffler 			      | HAL_TXQ_TXEOLINT_ENABLE
378910ad9a77SSam Leffler 			      | HAL_TXQ_DBA_GATED
379010ad9a77SSam Leffler 			      | HAL_TXQ_BACKOFF_DISABLE
379110ad9a77SSam Leffler 			      | HAL_TXQ_ARB_LOCKOUT_GLOBAL
379210ad9a77SSam Leffler 			      ;
379310ad9a77SSam Leffler 		qi.tqi_aifs = 0;
379410ad9a77SSam Leffler 		/* XXX +dbaprep? */
379510ad9a77SSam Leffler 		qi.tqi_readyTime = sc->sc_tdmaslotlen;
379610ad9a77SSam Leffler 		qi.tqi_burstTime = qi.tqi_readyTime;
379710ad9a77SSam Leffler 	} else {
379810ad9a77SSam Leffler #endif
379916d4de92SAdrian Chadd 		/*
380016d4de92SAdrian Chadd 		 * XXX shouldn't this just use the default flags
380116d4de92SAdrian Chadd 		 * used in the previous queue setup?
380216d4de92SAdrian Chadd 		 */
380310ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
380410ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
380510ad9a77SSam Leffler 			      | HAL_TXQ_TXDESCINT_ENABLE
380610ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
38071f25c0f7SAdrian Chadd 			      | HAL_TXQ_TXEOLINT_ENABLE
380810ad9a77SSam Leffler 			      ;
3809c42a7b7eSSam Leffler 		qi.tqi_aifs = wmep->wmep_aifsn;
3810c42a7b7eSSam Leffler 		qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
3811c42a7b7eSSam Leffler 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
381210ad9a77SSam Leffler 		qi.tqi_readyTime = 0;
3813c42a7b7eSSam Leffler 		qi.tqi_burstTime = ATH_TXOP_TO_US(wmep->wmep_txopLimit);
3814584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
381510ad9a77SSam Leffler 	}
381610ad9a77SSam Leffler #endif
381710ad9a77SSam Leffler 
381810ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
381910ad9a77SSam Leffler 	    "%s: Q%u qflags 0x%x aifs %u cwmin %u cwmax %u burstTime %u\n",
382010ad9a77SSam Leffler 	    __func__, txq->axq_qnum, qi.tqi_qflags,
382110ad9a77SSam Leffler 	    qi.tqi_aifs, qi.tqi_cwmin, qi.tqi_cwmax, qi.tqi_burstTime);
3822c42a7b7eSSam Leffler 
3823c42a7b7eSSam Leffler 	if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
3824b032f27cSSam Leffler 		if_printf(ifp, "unable to update hardware queue "
3825c42a7b7eSSam Leffler 			"parameters for %s traffic!\n",
3826c42a7b7eSSam Leffler 			ieee80211_wme_acnames[ac]);
3827c42a7b7eSSam Leffler 		return 0;
3828c42a7b7eSSam Leffler 	} else {
3829c42a7b7eSSam Leffler 		ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
3830c42a7b7eSSam Leffler 		return 1;
3831c42a7b7eSSam Leffler 	}
3832c42a7b7eSSam Leffler #undef ATH_TXOP_TO_US
3833c42a7b7eSSam Leffler #undef ATH_EXPONENT_TO_VALUE
3834c42a7b7eSSam Leffler }
3835c42a7b7eSSam Leffler 
3836c42a7b7eSSam Leffler /*
3837c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update WME parameters.
3838c42a7b7eSSam Leffler  */
3839a35dae8dSAdrian Chadd int
3840c42a7b7eSSam Leffler ath_wme_update(struct ieee80211com *ic)
3841c42a7b7eSSam Leffler {
3842c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3843c42a7b7eSSam Leffler 
3844c42a7b7eSSam Leffler 	return !ath_txq_update(sc, WME_AC_BE) ||
3845c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_BK) ||
3846c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VI) ||
3847c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
3848c42a7b7eSSam Leffler }
3849c42a7b7eSSam Leffler 
3850c42a7b7eSSam Leffler /*
3851c42a7b7eSSam Leffler  * Reclaim resources for a setup queue.
3852c42a7b7eSSam Leffler  */
3853c42a7b7eSSam Leffler static void
3854c42a7b7eSSam Leffler ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
3855c42a7b7eSSam Leffler {
3856c42a7b7eSSam Leffler 
3857c42a7b7eSSam Leffler 	ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
3858c42a7b7eSSam Leffler 	sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
3859b837332dSAdrian Chadd 	ATH_TXQ_LOCK_DESTROY(txq);
3860c42a7b7eSSam Leffler }
3861c42a7b7eSSam Leffler 
3862c42a7b7eSSam Leffler /*
3863c42a7b7eSSam Leffler  * Reclaim all tx queue resources.
3864c42a7b7eSSam Leffler  */
3865c42a7b7eSSam Leffler static void
3866c42a7b7eSSam Leffler ath_tx_cleanup(struct ath_softc *sc)
3867c42a7b7eSSam Leffler {
3868c42a7b7eSSam Leffler 	int i;
3869c42a7b7eSSam Leffler 
3870c42a7b7eSSam Leffler 	ATH_TXBUF_LOCK_DESTROY(sc);
3871c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
3872c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
3873c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, &sc->sc_txq[i]);
3874c42a7b7eSSam Leffler }
38755591b213SSam Leffler 
387699d258fdSSam Leffler /*
3877ab06fdf2SSam Leffler  * Return h/w rate index for an IEEE rate (w/o basic rate bit)
3878ab06fdf2SSam Leffler  * using the current rates in sc_rixmap.
38798b5341deSSam Leffler  */
3880b8e788a5SAdrian Chadd int
3881ab06fdf2SSam Leffler ath_tx_findrix(const struct ath_softc *sc, uint8_t rate)
38828b5341deSSam Leffler {
3883ab06fdf2SSam Leffler 	int rix = sc->sc_rixmap[rate];
3884ab06fdf2SSam Leffler 	/* NB: return lowest rix for invalid rate */
3885ab06fdf2SSam Leffler 	return (rix == 0xff ? 0 : rix);
38868b5341deSSam Leffler }
38878b5341deSSam Leffler 
38889352fb7aSAdrian Chadd static void
38899352fb7aSAdrian Chadd ath_tx_update_stats(struct ath_softc *sc, struct ath_tx_status *ts,
38909352fb7aSAdrian Chadd     struct ath_buf *bf)
38919352fb7aSAdrian Chadd {
38929352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
38939352fb7aSAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
38949352fb7aSAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
38959352fb7aSAdrian Chadd 	int sr, lr, pri;
38969352fb7aSAdrian Chadd 
38979352fb7aSAdrian Chadd 	if (ts->ts_status == 0) {
38989352fb7aSAdrian Chadd 		u_int8_t txant = ts->ts_antenna;
38999352fb7aSAdrian Chadd 		sc->sc_stats.ast_ant_tx[txant]++;
39009352fb7aSAdrian Chadd 		sc->sc_ant_tx[txant]++;
39019352fb7aSAdrian Chadd 		if (ts->ts_finaltsi != 0)
39029352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_altrate++;
39039352fb7aSAdrian Chadd 		pri = M_WME_GETAC(bf->bf_m);
39049352fb7aSAdrian Chadd 		if (pri >= WME_AC_VO)
39059352fb7aSAdrian Chadd 			ic->ic_wme.wme_hipri_traffic++;
3906875a9451SAdrian Chadd 		if ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)
39079352fb7aSAdrian Chadd 			ni->ni_inact = ni->ni_inact_reload;
39089352fb7aSAdrian Chadd 	} else {
39099352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XRETRY)
39109352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xretries++;
39119352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FIFO)
39129352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_fifoerr++;
39139352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FILT)
39149352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_filtered++;
39159352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XTXOP)
39169352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xtxop++;
39179352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_TIMER_EXPIRED)
39189352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_timerexpired++;
39199352fb7aSAdrian Chadd 
39209352fb7aSAdrian Chadd 		if (bf->bf_m->m_flags & M_FF)
39219352fb7aSAdrian Chadd 			sc->sc_stats.ast_ff_txerr++;
39229352fb7aSAdrian Chadd 	}
39239352fb7aSAdrian Chadd 	/* XXX when is this valid? */
3924158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DESC_CFG_ERR)
39259352fb7aSAdrian Chadd 		sc->sc_stats.ast_tx_desccfgerr++;
3926158cb431SAdrian Chadd 	/*
3927158cb431SAdrian Chadd 	 * This can be valid for successful frame transmission!
3928158cb431SAdrian Chadd 	 * If there's a TX FIFO underrun during aggregate transmission,
3929158cb431SAdrian Chadd 	 * the MAC will pad the rest of the aggregate with delimiters.
3930158cb431SAdrian Chadd 	 * If a BA is returned, the frame is marked as "OK" and it's up
3931158cb431SAdrian Chadd 	 * to the TX completion code to notice which frames weren't
3932158cb431SAdrian Chadd 	 * successfully transmitted.
3933158cb431SAdrian Chadd 	 */
3934158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DATA_UNDERRUN)
3935158cb431SAdrian Chadd 		sc->sc_stats.ast_tx_data_underrun++;
3936158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DELIM_UNDERRUN)
3937158cb431SAdrian Chadd 		sc->sc_stats.ast_tx_delim_underrun++;
39389352fb7aSAdrian Chadd 
39399352fb7aSAdrian Chadd 	sr = ts->ts_shortretry;
39409352fb7aSAdrian Chadd 	lr = ts->ts_longretry;
39419352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_shortretry += sr;
39429352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_longretry += lr;
39439352fb7aSAdrian Chadd 
39449352fb7aSAdrian Chadd }
39459352fb7aSAdrian Chadd 
39469352fb7aSAdrian Chadd /*
39479352fb7aSAdrian Chadd  * The default completion. If fail is 1, this means
39489352fb7aSAdrian Chadd  * "please don't retry the frame, and just return -1 status
39499352fb7aSAdrian Chadd  * to the net80211 stack.
39509352fb7aSAdrian Chadd  */
39519352fb7aSAdrian Chadd void
39529352fb7aSAdrian Chadd ath_tx_default_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
39539352fb7aSAdrian Chadd {
39549352fb7aSAdrian Chadd 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
39559352fb7aSAdrian Chadd 	int st;
39569352fb7aSAdrian Chadd 
39579352fb7aSAdrian Chadd 	if (fail == 1)
39589352fb7aSAdrian Chadd 		st = -1;
39599352fb7aSAdrian Chadd 	else
3960875a9451SAdrian Chadd 		st = ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) ?
39619352fb7aSAdrian Chadd 		    ts->ts_status : HAL_TXERR_XRETRY;
39629352fb7aSAdrian Chadd 
3963ce597531SAdrian Chadd #if 0
39649352fb7aSAdrian Chadd 	if (bf->bf_state.bfs_dobaw)
39659352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
3966a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: dobaw should've been cleared!\n",
3967a66d5089SAdrian Chadd 		    __func__,
3968a66d5089SAdrian Chadd 		    bf,
3969a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
3970ce597531SAdrian Chadd #endif
39719352fb7aSAdrian Chadd 	if (bf->bf_next != NULL)
39729352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
3973a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: bf_next not NULL!\n",
3974a66d5089SAdrian Chadd 		    __func__,
3975a66d5089SAdrian Chadd 		    bf,
3976a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
39779352fb7aSAdrian Chadd 
39789352fb7aSAdrian Chadd 	/*
3979548a605dSAdrian Chadd 	 * Check if the node software queue is empty; if so
3980548a605dSAdrian Chadd 	 * then clear the TIM.
3981548a605dSAdrian Chadd 	 *
3982548a605dSAdrian Chadd 	 * This needs to be done before the buffer is freed as
3983548a605dSAdrian Chadd 	 * otherwise the node reference will have been released
3984548a605dSAdrian Chadd 	 * and the node may not actually exist any longer.
3985548a605dSAdrian Chadd 	 *
3986548a605dSAdrian Chadd 	 * XXX I don't like this belonging here, but it's cleaner
3987548a605dSAdrian Chadd 	 * to do it here right now then all the other places
3988548a605dSAdrian Chadd 	 * where ath_tx_default_comp() is called.
3989548a605dSAdrian Chadd 	 *
3990548a605dSAdrian Chadd 	 * XXX TODO: during drain, ensure that the callback is
3991548a605dSAdrian Chadd 	 * being called so we get a chance to update the TIM.
3992548a605dSAdrian Chadd 	 */
39934bed2b67SAdrian Chadd 	if (bf->bf_node) {
39944bed2b67SAdrian Chadd 		ATH_TX_LOCK(sc);
3995548a605dSAdrian Chadd 		ath_tx_update_tim(sc, bf->bf_node, 0);
39964bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
39974bed2b67SAdrian Chadd 	}
3998548a605dSAdrian Chadd 
3999548a605dSAdrian Chadd 	/*
40009352fb7aSAdrian Chadd 	 * Do any tx complete callback.  Note this must
40019352fb7aSAdrian Chadd 	 * be done before releasing the node reference.
40029352fb7aSAdrian Chadd 	 * This will free the mbuf, release the net80211
40039352fb7aSAdrian Chadd 	 * node and recycle the ath_buf.
40049352fb7aSAdrian Chadd 	 */
40059352fb7aSAdrian Chadd 	ath_tx_freebuf(sc, bf, st);
40069352fb7aSAdrian Chadd }
40079352fb7aSAdrian Chadd 
40089352fb7aSAdrian Chadd /*
4009eb6f0de0SAdrian Chadd  * Update rate control with the given completion status.
4010eb6f0de0SAdrian Chadd  */
4011eb6f0de0SAdrian Chadd void
4012eb6f0de0SAdrian Chadd ath_tx_update_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
4013eb6f0de0SAdrian Chadd     struct ath_rc_series *rc, struct ath_tx_status *ts, int frmlen,
4014eb6f0de0SAdrian Chadd     int nframes, int nbad)
4015eb6f0de0SAdrian Chadd {
4016eb6f0de0SAdrian Chadd 	struct ath_node *an;
4017eb6f0de0SAdrian Chadd 
4018eb6f0de0SAdrian Chadd 	/* Only for unicast frames */
4019eb6f0de0SAdrian Chadd 	if (ni == NULL)
4020eb6f0de0SAdrian Chadd 		return;
4021eb6f0de0SAdrian Chadd 
4022eb6f0de0SAdrian Chadd 	an = ATH_NODE(ni);
4023548a605dSAdrian Chadd 	ATH_NODE_UNLOCK_ASSERT(an);
4024eb6f0de0SAdrian Chadd 
4025eb6f0de0SAdrian Chadd 	if ((ts->ts_status & HAL_TXERR_FILT) == 0) {
4026eb6f0de0SAdrian Chadd 		ATH_NODE_LOCK(an);
4027eb6f0de0SAdrian Chadd 		ath_rate_tx_complete(sc, an, rc, ts, frmlen, nframes, nbad);
4028eb6f0de0SAdrian Chadd 		ATH_NODE_UNLOCK(an);
4029eb6f0de0SAdrian Chadd 	}
4030eb6f0de0SAdrian Chadd }
4031eb6f0de0SAdrian Chadd 
4032eb6f0de0SAdrian Chadd /*
4033bad98824SAdrian Chadd  * Process the completion of the given buffer.
4034bad98824SAdrian Chadd  *
4035bad98824SAdrian Chadd  * This calls the rate control update and then the buffer completion.
4036bad98824SAdrian Chadd  * This will either free the buffer or requeue it.  In any case, the
4037bad98824SAdrian Chadd  * bf pointer should be treated as invalid after this function is called.
4038bad98824SAdrian Chadd  */
4039bad98824SAdrian Chadd void
4040bad98824SAdrian Chadd ath_tx_process_buf_completion(struct ath_softc *sc, struct ath_txq *txq,
4041bad98824SAdrian Chadd     struct ath_tx_status *ts, struct ath_buf *bf)
4042bad98824SAdrian Chadd {
4043bad98824SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
4044bad98824SAdrian Chadd 	struct ath_node *an = NULL;
4045bad98824SAdrian Chadd 
4046375307d4SAdrian Chadd 	ATH_TX_UNLOCK_ASSERT(sc);
40475e018508SAdrian Chadd 	ATH_TXQ_UNLOCK_ASSERT(txq);
4048bad98824SAdrian Chadd 
4049bad98824SAdrian Chadd 	/* If unicast frame, update general statistics */
4050bad98824SAdrian Chadd 	if (ni != NULL) {
4051bad98824SAdrian Chadd 		an = ATH_NODE(ni);
4052bad98824SAdrian Chadd 		/* update statistics */
4053bad98824SAdrian Chadd 		ath_tx_update_stats(sc, ts, bf);
4054bad98824SAdrian Chadd 	}
4055bad98824SAdrian Chadd 
4056bad98824SAdrian Chadd 	/*
4057bad98824SAdrian Chadd 	 * Call the completion handler.
4058bad98824SAdrian Chadd 	 * The completion handler is responsible for
4059bad98824SAdrian Chadd 	 * calling the rate control code.
4060bad98824SAdrian Chadd 	 *
4061bad98824SAdrian Chadd 	 * Frames with no completion handler get the
4062bad98824SAdrian Chadd 	 * rate control code called here.
4063bad98824SAdrian Chadd 	 */
4064bad98824SAdrian Chadd 	if (bf->bf_comp == NULL) {
4065bad98824SAdrian Chadd 		if ((ts->ts_status & HAL_TXERR_FILT) == 0 &&
4066bad98824SAdrian Chadd 		    (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) {
4067bad98824SAdrian Chadd 			/*
4068bad98824SAdrian Chadd 			 * XXX assume this isn't an aggregate
4069bad98824SAdrian Chadd 			 * frame.
4070bad98824SAdrian Chadd 			 */
4071bad98824SAdrian Chadd 			ath_tx_update_ratectrl(sc, ni,
4072bad98824SAdrian Chadd 			     bf->bf_state.bfs_rc, ts,
4073bad98824SAdrian Chadd 			    bf->bf_state.bfs_pktlen, 1,
4074bad98824SAdrian Chadd 			    (ts->ts_status == 0 ? 0 : 1));
4075bad98824SAdrian Chadd 		}
4076bad98824SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4077bad98824SAdrian Chadd 	} else
4078bad98824SAdrian Chadd 		bf->bf_comp(sc, bf, 0);
4079bad98824SAdrian Chadd }
4080bad98824SAdrian Chadd 
4081bad98824SAdrian Chadd 
4082bad98824SAdrian Chadd 
4083bad98824SAdrian Chadd /*
4084c42a7b7eSSam Leffler  * Process completed xmit descriptors from the specified queue.
4085eb6f0de0SAdrian Chadd  * Kick the packet scheduler if needed. This can occur from this
4086eb6f0de0SAdrian Chadd  * particular task.
4087c42a7b7eSSam Leffler  */
4088788e6aa9SAdrian Chadd static int
4089788e6aa9SAdrian Chadd ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq, int dosched)
40905591b213SSam Leffler {
40915591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
40929352fb7aSAdrian Chadd 	struct ath_buf *bf;
40936edf1dc7SAdrian Chadd 	struct ath_desc *ds;
409465f9edeeSSam Leffler 	struct ath_tx_status *ts;
40955591b213SSam Leffler 	struct ieee80211_node *ni;
409653e98d5aSAdrian Chadd #ifdef	IEEE80211_SUPPORT_SUPERG
409743faa6b2SAdrian Chadd 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
409853e98d5aSAdrian Chadd #endif	/* IEEE80211_SUPPORT_SUPERG */
40999352fb7aSAdrian Chadd 	int nacked;
41005591b213SSam Leffler 	HAL_STATUS status;
41015591b213SSam Leffler 
4102c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
4103c42a7b7eSSam Leffler 		__func__, txq->axq_qnum,
4104c42a7b7eSSam Leffler 		(caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
4105c42a7b7eSSam Leffler 		txq->axq_link);
410603682514SAdrian Chadd 
410703682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 4,
410803682514SAdrian Chadd 	    "ath_tx_processq: txq=%u head %p link %p depth %p",
410903682514SAdrian Chadd 	    txq->axq_qnum,
411003682514SAdrian Chadd 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
411103682514SAdrian Chadd 	    txq->axq_link,
411203682514SAdrian Chadd 	    txq->axq_depth);
411303682514SAdrian Chadd 
4114d7736e13SSam Leffler 	nacked = 0;
41155591b213SSam Leffler 	for (;;) {
4116b837332dSAdrian Chadd 		ATH_TXQ_LOCK(txq);
4117c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;	/* reset periodic desc intr count */
41186b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&txq->axq_q);
41195591b213SSam Leffler 		if (bf == NULL) {
4120b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
41215591b213SSam Leffler 			break;
41225591b213SSam Leffler 		}
41236edf1dc7SAdrian Chadd 		ds = bf->bf_lastds;	/* XXX must be setup correctly! */
412465f9edeeSSam Leffler 		ts = &bf->bf_status.ds_txstat;
412503682514SAdrian Chadd 
412665f9edeeSSam Leffler 		status = ath_hal_txprocdesc(ah, ds, ts);
4127a585a9a1SSam Leffler #ifdef ATH_DEBUG
4128c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
41296902009eSSam Leffler 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
41306902009eSSam Leffler 			    status == HAL_OK);
413103682514SAdrian Chadd 		else if ((sc->sc_debug & ATH_DEBUG_RESET) && (dosched == 0))
4132d6b20023SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
4133d6b20023SAdrian Chadd 			    status == HAL_OK);
41345591b213SSam Leffler #endif
4135bb327d28SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
4136bb327d28SAdrian Chadd 		if (if_ath_alq_checkdebug(&sc->sc_alq,
4137bb327d28SAdrian Chadd 		    ATH_ALQ_EDMA_TXSTATUS)) {
4138bb327d28SAdrian Chadd 			if_ath_alq_post(&sc->sc_alq, ATH_ALQ_EDMA_TXSTATUS,
4139bb327d28SAdrian Chadd 			sc->sc_tx_statuslen,
4140bb327d28SAdrian Chadd 			(char *) ds);
4141bb327d28SAdrian Chadd 		}
4142bb327d28SAdrian Chadd #endif
414303682514SAdrian Chadd 
41445591b213SSam Leffler 		if (status == HAL_EINPROGRESS) {
414503682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_TXCOMP, 3,
414603682514SAdrian Chadd 			    "ath_tx_processq: txq=%u, bf=%p ds=%p, HAL_EINPROGRESS",
414703682514SAdrian Chadd 			    txq->axq_qnum, bf, ds);
4148b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
41495591b213SSam Leffler 			break;
41505591b213SSam Leffler 		}
41516b349e5aSAdrian Chadd 		ATH_TXQ_REMOVE(txq, bf, bf_list);
41525e018508SAdrian Chadd 
41535e018508SAdrian Chadd 		/*
41545e018508SAdrian Chadd 		 * Sanity check.
41555e018508SAdrian Chadd 		 */
41565e018508SAdrian Chadd 		if (txq->axq_qnum != bf->bf_state.bfs_tx_queue) {
41575e018508SAdrian Chadd 			device_printf(sc->sc_dev,
41585e018508SAdrian Chadd 			    "%s: TXQ=%d: bf=%p, bfs_tx_queue=%d\n",
41595e018508SAdrian Chadd 			    __func__,
41605e018508SAdrian Chadd 			    txq->axq_qnum,
41615e018508SAdrian Chadd 			    bf,
41625e018508SAdrian Chadd 			    bf->bf_state.bfs_tx_queue);
41635e018508SAdrian Chadd 		}
41645e018508SAdrian Chadd 		if (txq->axq_qnum != bf->bf_last->bf_state.bfs_tx_queue) {
41655e018508SAdrian Chadd 			device_printf(sc->sc_dev,
41665e018508SAdrian Chadd 			    "%s: TXQ=%d: bf_last=%p, bfs_tx_queue=%d\n",
41675e018508SAdrian Chadd 			    __func__,
41685e018508SAdrian Chadd 			    txq->axq_qnum,
41695e018508SAdrian Chadd 			    bf->bf_last,
41705e018508SAdrian Chadd 			    bf->bf_last->bf_state.bfs_tx_queue);
41715e018508SAdrian Chadd 		}
41725e018508SAdrian Chadd 
41735e018508SAdrian Chadd #if 0
4174d3731e4bSAdrian Chadd 		if (txq->axq_depth > 0) {
417510ad9a77SSam Leffler 			/*
4176d3731e4bSAdrian Chadd 			 * More frames follow.  Mark the buffer busy
4177d3731e4bSAdrian Chadd 			 * so it's not re-used while the hardware may
4178d3731e4bSAdrian Chadd 			 * still re-read the link field in the descriptor.
41796edf1dc7SAdrian Chadd 			 *
4180d3731e4bSAdrian Chadd 			 * Use the last buffer in an aggregate as that
4181d3731e4bSAdrian Chadd 			 * is where the hardware may be - intermediate
4182d3731e4bSAdrian Chadd 			 * descriptors won't be "busy".
418310ad9a77SSam Leffler 			 */
41846edf1dc7SAdrian Chadd 			bf->bf_last->bf_flags |= ATH_BUF_BUSY;
4185d3731e4bSAdrian Chadd 		} else
4186d3731e4bSAdrian Chadd 			txq->axq_link = NULL;
41875e018508SAdrian Chadd #else
41885e018508SAdrian Chadd 		bf->bf_last->bf_flags |= ATH_BUF_BUSY;
41895e018508SAdrian Chadd #endif
41906edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
41916edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
41925591b213SSam Leffler 
41935591b213SSam Leffler 		ni = bf->bf_node;
419403682514SAdrian Chadd 
419503682514SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TXCOMP, 5,
419603682514SAdrian Chadd 		    "ath_tx_processq: txq=%u, bf=%p, ds=%p, ni=%p, ts_status=0x%08x",
419703682514SAdrian Chadd 		    txq->axq_qnum, bf, ds, ni, ts->ts_status);
4198c42a7b7eSSam Leffler 		/*
41999352fb7aSAdrian Chadd 		 * If unicast frame was ack'd update RSSI,
420084784be1SSam Leffler 		 * including the last rx time used to
420184784be1SSam Leffler 		 * workaround phantom bmiss interrupts.
4202d7736e13SSam Leffler 		 */
42039352fb7aSAdrian Chadd 		if (ni != NULL && ts->ts_status == 0 &&
4204875a9451SAdrian Chadd 		    ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)) {
4205d7736e13SSam Leffler 			nacked++;
420684784be1SSam Leffler 			sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
420784784be1SSam Leffler 			ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi,
420884784be1SSam Leffler 				ts->ts_rssi);
420984784be1SSam Leffler 		}
4210b837332dSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
42119352fb7aSAdrian Chadd 
4212bad98824SAdrian Chadd 		/*
4213bad98824SAdrian Chadd 		 * Update statistics and call completion
4214bad98824SAdrian Chadd 		 */
4215bad98824SAdrian Chadd 		ath_tx_process_buf_completion(sc, txq, ts, bf);
4216548a605dSAdrian Chadd 
4217548a605dSAdrian Chadd 		/* XXX at this point, bf and ni may be totally invalid */
42185591b213SSam Leffler 	}
4219339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
422068e8e04eSSam Leffler 	/*
422168e8e04eSSam Leffler 	 * Flush fast-frame staging queue when traffic slows.
422268e8e04eSSam Leffler 	 */
422368e8e04eSSam Leffler 	if (txq->axq_depth <= 1)
422404f19fd6SSam Leffler 		ieee80211_ff_flush(ic, txq->axq_ac);
4225339ccfb3SSam Leffler #endif
4226eb6f0de0SAdrian Chadd 
422721bca442SAdrian Chadd 	/* Kick the software TXQ scheduler */
4228eb6f0de0SAdrian Chadd 	if (dosched) {
4229a40880adSAdrian Chadd 		ATH_TX_LOCK(sc);
4230a40880adSAdrian Chadd 		ath_txq_sched(sc, txq);
4231a40880adSAdrian Chadd 		ATH_TX_UNLOCK(sc);
4232eb6f0de0SAdrian Chadd 	}
4233eb6f0de0SAdrian Chadd 
423403682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
423503682514SAdrian Chadd 	    "ath_tx_processq: txq=%u: done",
423603682514SAdrian Chadd 	    txq->axq_qnum);
423703682514SAdrian Chadd 
4238d7736e13SSam Leffler 	return nacked;
4239d7736e13SSam Leffler }
4240d7736e13SSam Leffler 
42418f939e79SAdrian Chadd #define	TXQACTIVE(t, q)		( (t) & (1 << (q)))
4242c42a7b7eSSam Leffler 
4243c42a7b7eSSam Leffler /*
4244c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4245c42a7b7eSSam Leffler  * for a single hardware transmit queue (e.g. 5210 and 5211).
4246c42a7b7eSSam Leffler  */
4247c42a7b7eSSam Leffler static void
4248c42a7b7eSSam Leffler ath_tx_proc_q0(void *arg, int npending)
4249c42a7b7eSSam Leffler {
4250c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4251fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
42528f939e79SAdrian Chadd 	uint32_t txqs;
4253c42a7b7eSSam Leffler 
4254ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4255ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
42568f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
42578f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4258ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
42598f939e79SAdrian Chadd 
426003682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
426103682514SAdrian Chadd 	    "ath_tx_proc_q0: txqs=0x%08x", txqs);
426203682514SAdrian Chadd 
426396ff485dSAdrian Chadd 	if (TXQACTIVE(txqs, 0) && ath_tx_processq(sc, &sc->sc_txq[0], 1))
42648f939e79SAdrian Chadd 		/* XXX why is lastrx updated in tx code? */
4265d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
42668f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
426796ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
4268e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
426913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4270e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
42712e986da5SSam Leffler 	sc->sc_wd_timer = 0;
42725591b213SSam Leffler 
42733e50ec2cSSam Leffler 	if (sc->sc_softled)
427446d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
42753e50ec2cSSam Leffler 
4276ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4277ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4278ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
42791a85141aSAdrian Chadd 
42801a85141aSAdrian Chadd 	ath_tx_kick(sc);
42815591b213SSam Leffler }
42825591b213SSam Leffler 
42835591b213SSam Leffler /*
4284c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4285c42a7b7eSSam Leffler  * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
42865591b213SSam Leffler  */
42875591b213SSam Leffler static void
4288c42a7b7eSSam Leffler ath_tx_proc_q0123(void *arg, int npending)
4289c42a7b7eSSam Leffler {
4290c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4291fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4292d7736e13SSam Leffler 	int nacked;
42938f939e79SAdrian Chadd 	uint32_t txqs;
42948f939e79SAdrian Chadd 
4295ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4296ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
42978f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
42988f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4299ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4300c42a7b7eSSam Leffler 
430103682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
430203682514SAdrian Chadd 	    "ath_tx_proc_q0123: txqs=0x%08x", txqs);
430303682514SAdrian Chadd 
4304c42a7b7eSSam Leffler 	/*
4305c42a7b7eSSam Leffler 	 * Process each active queue.
4306c42a7b7eSSam Leffler 	 */
4307d7736e13SSam Leffler 	nacked = 0;
43088f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 0))
430996ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[0], 1);
43108f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 1))
431196ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[1], 1);
43128f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 2))
431396ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[2], 1);
43148f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 3))
431596ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[3], 1);
43168f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
431796ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
4318d7736e13SSam Leffler 	if (nacked)
4319d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4320c42a7b7eSSam Leffler 
4321e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
432213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4323e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
43242e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4325c42a7b7eSSam Leffler 
43263e50ec2cSSam Leffler 	if (sc->sc_softled)
432746d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
43283e50ec2cSSam Leffler 
4329ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4330ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4331ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
43321a85141aSAdrian Chadd 
43331a85141aSAdrian Chadd 	ath_tx_kick(sc);
4334c42a7b7eSSam Leffler }
4335c42a7b7eSSam Leffler 
4336c42a7b7eSSam Leffler /*
4337c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt.
4338c42a7b7eSSam Leffler  */
4339c42a7b7eSSam Leffler static void
4340c42a7b7eSSam Leffler ath_tx_proc(void *arg, int npending)
4341c42a7b7eSSam Leffler {
4342c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4343fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4344d7736e13SSam Leffler 	int i, nacked;
43458f939e79SAdrian Chadd 	uint32_t txqs;
43468f939e79SAdrian Chadd 
4347ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4348ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
43498f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
43508f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4351ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4352c42a7b7eSSam Leffler 
435303682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1, "ath_tx_proc: txqs=0x%08x", txqs);
435403682514SAdrian Chadd 
4355c42a7b7eSSam Leffler 	/*
4356c42a7b7eSSam Leffler 	 * Process each active queue.
4357c42a7b7eSSam Leffler 	 */
4358d7736e13SSam Leffler 	nacked = 0;
4359c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
43608f939e79SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i) && TXQACTIVE(txqs, i))
436196ff485dSAdrian Chadd 			nacked += ath_tx_processq(sc, &sc->sc_txq[i], 1);
4362d7736e13SSam Leffler 	if (nacked)
4363d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4364c42a7b7eSSam Leffler 
4365ef27340cSAdrian Chadd 	/* XXX check this inside of IF_LOCK? */
4366e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
436713f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4368e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
43692e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4370c42a7b7eSSam Leffler 
43713e50ec2cSSam Leffler 	if (sc->sc_softled)
437246d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
43733e50ec2cSSam Leffler 
4374ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4375ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4376ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
43771a85141aSAdrian Chadd 
43781a85141aSAdrian Chadd 	ath_tx_kick(sc);
4379c42a7b7eSSam Leffler }
438016d4de92SAdrian Chadd #undef	TXQACTIVE
4381c42a7b7eSSam Leffler 
43829352fb7aSAdrian Chadd /*
438303e9308fSAdrian Chadd  * Deferred processing of TXQ rescheduling.
438403e9308fSAdrian Chadd  */
438503e9308fSAdrian Chadd static void
438603e9308fSAdrian Chadd ath_txq_sched_tasklet(void *arg, int npending)
438703e9308fSAdrian Chadd {
438803e9308fSAdrian Chadd 	struct ath_softc *sc = arg;
438903e9308fSAdrian Chadd 	int i;
439003e9308fSAdrian Chadd 
439103e9308fSAdrian Chadd 	/* XXX is skipping ok? */
439203e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
439303e9308fSAdrian Chadd #if 0
439403e9308fSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
439503e9308fSAdrian Chadd 		device_printf(sc->sc_dev,
439603e9308fSAdrian Chadd 		    "%s: sc_inreset_cnt > 0; skipping\n", __func__);
439703e9308fSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
439803e9308fSAdrian Chadd 		return;
439903e9308fSAdrian Chadd 	}
440003e9308fSAdrian Chadd #endif
440103e9308fSAdrian Chadd 	sc->sc_txproc_cnt++;
440203e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
440303e9308fSAdrian Chadd 
4404375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
440503e9308fSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
4406b5a9dfd5SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
440703e9308fSAdrian Chadd 			ath_txq_sched(sc, &sc->sc_txq[i]);
4408b5a9dfd5SAdrian Chadd 		}
440903e9308fSAdrian Chadd 	}
4410375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
441103e9308fSAdrian Chadd 
441203e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
441303e9308fSAdrian Chadd 	sc->sc_txproc_cnt--;
441403e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
441503e9308fSAdrian Chadd }
441603e9308fSAdrian Chadd 
4417e1a50456SAdrian Chadd void
4418e1a50456SAdrian Chadd ath_returnbuf_tail(struct ath_softc *sc, struct ath_buf *bf)
4419e1a50456SAdrian Chadd {
4420e1a50456SAdrian Chadd 
4421e1a50456SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
4422e1a50456SAdrian Chadd 
4423af33d486SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_MGMT)
4424af33d486SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf_mgmt, bf, bf_list);
442523ced6c1SAdrian Chadd 	else {
4426e1a50456SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
442723ced6c1SAdrian Chadd 		sc->sc_txbuf_cnt++;
442823ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt > ath_txbuf) {
442923ced6c1SAdrian Chadd 			device_printf(sc->sc_dev,
443023ced6c1SAdrian Chadd 			    "%s: sc_txbuf_cnt > %d?\n",
443123ced6c1SAdrian Chadd 			    __func__,
443223ced6c1SAdrian Chadd 			    ath_txbuf);
443323ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt = ath_txbuf;
443423ced6c1SAdrian Chadd 		}
443523ced6c1SAdrian Chadd 	}
4436e1a50456SAdrian Chadd }
4437e1a50456SAdrian Chadd 
4438e1a50456SAdrian Chadd void
4439e1a50456SAdrian Chadd ath_returnbuf_head(struct ath_softc *sc, struct ath_buf *bf)
4440e1a50456SAdrian Chadd {
4441e1a50456SAdrian Chadd 
4442e1a50456SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
4443e1a50456SAdrian Chadd 
4444af33d486SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_MGMT)
4445af33d486SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf_mgmt, bf, bf_list);
444623ced6c1SAdrian Chadd 	else {
4447e1a50456SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
444823ced6c1SAdrian Chadd 		sc->sc_txbuf_cnt++;
444923ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt > ATH_TXBUF) {
445023ced6c1SAdrian Chadd 			device_printf(sc->sc_dev,
445123ced6c1SAdrian Chadd 			    "%s: sc_txbuf_cnt > %d?\n",
445223ced6c1SAdrian Chadd 			    __func__,
445323ced6c1SAdrian Chadd 			    ATH_TXBUF);
445423ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt = ATH_TXBUF;
445523ced6c1SAdrian Chadd 		}
445623ced6c1SAdrian Chadd 	}
4457e1a50456SAdrian Chadd }
4458e1a50456SAdrian Chadd 
445903e9308fSAdrian Chadd /*
4460629ce218SAdrian Chadd  * Free the holding buffer if it exists
4461629ce218SAdrian Chadd  */
44623feffbd7SAdrian Chadd void
4463629ce218SAdrian Chadd ath_txq_freeholdingbuf(struct ath_softc *sc, struct ath_txq *txq)
4464629ce218SAdrian Chadd {
44655e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK_ASSERT(sc);
44665e018508SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
4467629ce218SAdrian Chadd 
4468629ce218SAdrian Chadd 	if (txq->axq_holdingbf == NULL)
4469629ce218SAdrian Chadd 		return;
4470629ce218SAdrian Chadd 
4471629ce218SAdrian Chadd 	txq->axq_holdingbf->bf_flags &= ~ATH_BUF_BUSY;
44725e018508SAdrian Chadd 
44735e018508SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
4474629ce218SAdrian Chadd 	ath_returnbuf_tail(sc, txq->axq_holdingbf);
44755e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
44765e018508SAdrian Chadd 
4477629ce218SAdrian Chadd 	txq->axq_holdingbf = NULL;
4478629ce218SAdrian Chadd }
4479629ce218SAdrian Chadd 
4480629ce218SAdrian Chadd /*
4481629ce218SAdrian Chadd  * Add this buffer to the holding queue, freeing the previous
4482629ce218SAdrian Chadd  * one if it exists.
4483629ce218SAdrian Chadd  */
4484629ce218SAdrian Chadd static void
4485629ce218SAdrian Chadd ath_txq_addholdingbuf(struct ath_softc *sc, struct ath_buf *bf)
4486629ce218SAdrian Chadd {
4487629ce218SAdrian Chadd 	struct ath_txq *txq;
4488629ce218SAdrian Chadd 
44895e018508SAdrian Chadd 	txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue];
44905e018508SAdrian Chadd 
44915e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK_ASSERT(sc);
44925e018508SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
44935f2f0e61SAdrian Chadd 
4494629ce218SAdrian Chadd 	/* XXX assert ATH_BUF_BUSY is set */
4495629ce218SAdrian Chadd 
4496629ce218SAdrian Chadd 	/* XXX assert the tx queue is under the max number */
4497629ce218SAdrian Chadd 	if (bf->bf_state.bfs_tx_queue > HAL_NUM_TX_QUEUES) {
4498629ce218SAdrian Chadd 		device_printf(sc->sc_dev, "%s: bf=%p: invalid tx queue (%d)\n",
4499629ce218SAdrian Chadd 		    __func__,
4500629ce218SAdrian Chadd 		    bf,
4501629ce218SAdrian Chadd 		    bf->bf_state.bfs_tx_queue);
4502629ce218SAdrian Chadd 		bf->bf_flags &= ~ATH_BUF_BUSY;
4503629ce218SAdrian Chadd 		ath_returnbuf_tail(sc, bf);
4504629ce218SAdrian Chadd 		return;
4505629ce218SAdrian Chadd 	}
4506629ce218SAdrian Chadd 	ath_txq_freeholdingbuf(sc, txq);
4507629ce218SAdrian Chadd 	txq->axq_holdingbf = bf;
4508629ce218SAdrian Chadd }
4509629ce218SAdrian Chadd 
4510629ce218SAdrian Chadd /*
45119352fb7aSAdrian Chadd  * Return a buffer to the pool and update the 'busy' flag on the
45129352fb7aSAdrian Chadd  * previous 'tail' entry.
45139352fb7aSAdrian Chadd  *
45149352fb7aSAdrian Chadd  * This _must_ only be called when the buffer is involved in a completed
45159352fb7aSAdrian Chadd  * TX. The logic is that if it was part of an active TX, the previous
45169352fb7aSAdrian Chadd  * buffer on the list is now not involved in a halted TX DMA queue, waiting
45179352fb7aSAdrian Chadd  * for restart (eg for TDMA.)
45189352fb7aSAdrian Chadd  *
45199352fb7aSAdrian Chadd  * The caller must free the mbuf and recycle the node reference.
45205e018508SAdrian Chadd  *
45215e018508SAdrian Chadd  * XXX This method of handling busy / holding buffers is insanely stupid.
45225e018508SAdrian Chadd  * It requires bf_state.bfs_tx_queue to be correctly assigned.  It would
45235e018508SAdrian Chadd  * be much nicer if buffers in the processq() methods would instead be
45245e018508SAdrian Chadd  * always completed there (pushed onto a txq or ath_bufhead) so we knew
45255e018508SAdrian Chadd  * exactly what hardware queue they came from in the first place.
45269352fb7aSAdrian Chadd  */
45279352fb7aSAdrian Chadd void
45289352fb7aSAdrian Chadd ath_freebuf(struct ath_softc *sc, struct ath_buf *bf)
45299352fb7aSAdrian Chadd {
45305e018508SAdrian Chadd 	struct ath_txq *txq;
45315e018508SAdrian Chadd 
45325e018508SAdrian Chadd 	txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue];
45335e018508SAdrian Chadd 
45349352fb7aSAdrian Chadd 	KASSERT((bf->bf_node == NULL), ("%s: bf->bf_node != NULL\n", __func__));
45359352fb7aSAdrian Chadd 	KASSERT((bf->bf_m == NULL), ("%s: bf->bf_m != NULL\n", __func__));
45369352fb7aSAdrian Chadd 
4537629ce218SAdrian Chadd 	/*
45385e018508SAdrian Chadd 	 * If this buffer is busy, push it onto the holding queue.
4539629ce218SAdrian Chadd 	 */
4540629ce218SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_BUSY) {
45415e018508SAdrian Chadd 		ATH_TXQ_LOCK(txq);
4542629ce218SAdrian Chadd 		ath_txq_addholdingbuf(sc, bf);
45435e018508SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
4544629ce218SAdrian Chadd 		return;
4545629ce218SAdrian Chadd 	}
4546629ce218SAdrian Chadd 
4547629ce218SAdrian Chadd 	/*
4548629ce218SAdrian Chadd 	 * Not a busy buffer, so free normally
4549629ce218SAdrian Chadd 	 */
45509352fb7aSAdrian Chadd 	ATH_TXBUF_LOCK(sc);
4551e1a50456SAdrian Chadd 	ath_returnbuf_tail(sc, bf);
45529352fb7aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
45539352fb7aSAdrian Chadd }
45549352fb7aSAdrian Chadd 
45559352fb7aSAdrian Chadd /*
45569352fb7aSAdrian Chadd  * This is currently used by ath_tx_draintxq() and
45579352fb7aSAdrian Chadd  * ath_tx_tid_free_pkts().
45589352fb7aSAdrian Chadd  *
45599352fb7aSAdrian Chadd  * It recycles a single ath_buf.
45609352fb7aSAdrian Chadd  */
45619352fb7aSAdrian Chadd void
45629352fb7aSAdrian Chadd ath_tx_freebuf(struct ath_softc *sc, struct ath_buf *bf, int status)
45639352fb7aSAdrian Chadd {
45649352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
45659352fb7aSAdrian Chadd 	struct mbuf *m0 = bf->bf_m;
45669352fb7aSAdrian Chadd 
45673f3a5dbdSAdrian Chadd 	/*
45683f3a5dbdSAdrian Chadd 	 * Make sure that we only sync/unload if there's an mbuf.
45693f3a5dbdSAdrian Chadd 	 * If not (eg we cloned a buffer), the unload will have already
45703f3a5dbdSAdrian Chadd 	 * occured.
45713f3a5dbdSAdrian Chadd 	 */
45723f3a5dbdSAdrian Chadd 	if (bf->bf_m != NULL) {
45733f3a5dbdSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
45743f3a5dbdSAdrian Chadd 		    BUS_DMASYNC_POSTWRITE);
45753f3a5dbdSAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
45763f3a5dbdSAdrian Chadd 	}
45773f3a5dbdSAdrian Chadd 
45789352fb7aSAdrian Chadd 	bf->bf_node = NULL;
45799352fb7aSAdrian Chadd 	bf->bf_m = NULL;
45809352fb7aSAdrian Chadd 
45819352fb7aSAdrian Chadd 	/* Free the buffer, it's not needed any longer */
45829352fb7aSAdrian Chadd 	ath_freebuf(sc, bf);
45839352fb7aSAdrian Chadd 
45849352fb7aSAdrian Chadd 	if (ni != NULL) {
45859352fb7aSAdrian Chadd 		/*
45869352fb7aSAdrian Chadd 		 * Do any callback and reclaim the node reference.
45879352fb7aSAdrian Chadd 		 */
45889352fb7aSAdrian Chadd 		if (m0->m_flags & M_TXCB)
45899352fb7aSAdrian Chadd 			ieee80211_process_callback(ni, m0, status);
45909352fb7aSAdrian Chadd 		ieee80211_free_node(ni);
45919352fb7aSAdrian Chadd 	}
45929352fb7aSAdrian Chadd 
45933f3a5dbdSAdrian Chadd 	/* Finally, we don't need this mbuf any longer */
45943f3a5dbdSAdrian Chadd 	m_freem(m0);
45959352fb7aSAdrian Chadd }
45969352fb7aSAdrian Chadd 
45973feffbd7SAdrian Chadd static struct ath_buf *
45983feffbd7SAdrian Chadd ath_tx_draintxq_get_one(struct ath_softc *sc, struct ath_txq *txq)
45993feffbd7SAdrian Chadd {
46003feffbd7SAdrian Chadd 	struct ath_buf *bf;
46013feffbd7SAdrian Chadd 
46023feffbd7SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
46033feffbd7SAdrian Chadd 
46043feffbd7SAdrian Chadd 	/*
46053feffbd7SAdrian Chadd 	 * Drain the FIFO queue first, then if it's
46063feffbd7SAdrian Chadd 	 * empty, move to the normal frame queue.
46073feffbd7SAdrian Chadd 	 */
46083feffbd7SAdrian Chadd 	bf = TAILQ_FIRST(&txq->fifo.axq_q);
46093feffbd7SAdrian Chadd 	if (bf != NULL) {
46103feffbd7SAdrian Chadd 		/*
46113feffbd7SAdrian Chadd 		 * Is it the last buffer in this set?
46123feffbd7SAdrian Chadd 		 * Decrement the FIFO counter.
46133feffbd7SAdrian Chadd 		 */
46143feffbd7SAdrian Chadd 		if (bf->bf_flags & ATH_BUF_FIFOEND) {
46153feffbd7SAdrian Chadd 			if (txq->axq_fifo_depth == 0) {
46163feffbd7SAdrian Chadd 				device_printf(sc->sc_dev,
46173feffbd7SAdrian Chadd 				    "%s: Q%d: fifo_depth=0, fifo.axq_depth=%d?\n",
46183feffbd7SAdrian Chadd 				    __func__,
46193feffbd7SAdrian Chadd 				    txq->axq_qnum,
46203feffbd7SAdrian Chadd 				    txq->fifo.axq_depth);
46213feffbd7SAdrian Chadd 			} else
46223feffbd7SAdrian Chadd 				txq->axq_fifo_depth--;
46233feffbd7SAdrian Chadd 		}
46243feffbd7SAdrian Chadd 		ATH_TXQ_REMOVE(&txq->fifo, bf, bf_list);
46253feffbd7SAdrian Chadd 		return (bf);
46263feffbd7SAdrian Chadd 	}
46273feffbd7SAdrian Chadd 
46283feffbd7SAdrian Chadd 	/*
46293feffbd7SAdrian Chadd 	 * Debugging!
46303feffbd7SAdrian Chadd 	 */
46313feffbd7SAdrian Chadd 	if (txq->axq_fifo_depth != 0 || txq->fifo.axq_depth != 0) {
46323feffbd7SAdrian Chadd 		device_printf(sc->sc_dev,
46333feffbd7SAdrian Chadd 		    "%s: Q%d: fifo_depth=%d, fifo.axq_depth=%d\n",
46343feffbd7SAdrian Chadd 		    __func__,
46353feffbd7SAdrian Chadd 		    txq->axq_qnum,
46363feffbd7SAdrian Chadd 		    txq->axq_fifo_depth,
46373feffbd7SAdrian Chadd 		    txq->fifo.axq_depth);
46383feffbd7SAdrian Chadd 	}
46393feffbd7SAdrian Chadd 
46403feffbd7SAdrian Chadd 	/*
46413feffbd7SAdrian Chadd 	 * Now drain the pending queue.
46423feffbd7SAdrian Chadd 	 */
46433feffbd7SAdrian Chadd 	bf = TAILQ_FIRST(&txq->axq_q);
46443feffbd7SAdrian Chadd 	if (bf == NULL) {
46453feffbd7SAdrian Chadd 		txq->axq_link = NULL;
46463feffbd7SAdrian Chadd 		return (NULL);
46473feffbd7SAdrian Chadd 	}
46483feffbd7SAdrian Chadd 	ATH_TXQ_REMOVE(txq, bf, bf_list);
46493feffbd7SAdrian Chadd 	return (bf);
46503feffbd7SAdrian Chadd }
46513feffbd7SAdrian Chadd 
46529352fb7aSAdrian Chadd void
46531762ec94SAdrian Chadd ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
46545591b213SSam Leffler {
4655a585a9a1SSam Leffler #ifdef ATH_DEBUG
46565591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4657d2f6ed15SSam Leffler #endif
46585591b213SSam Leffler 	struct ath_buf *bf;
46597a4c5ed9SSam Leffler 	u_int ix;
46605591b213SSam Leffler 
4661c42a7b7eSSam Leffler 	/*
4662c42a7b7eSSam Leffler 	 * NB: this assumes output has been stopped and
46635d61b5e8SSam Leffler 	 *     we do not need to block ath_tx_proc
4664c42a7b7eSSam Leffler 	 */
46657a4c5ed9SSam Leffler 	for (ix = 0;; ix++) {
4666b837332dSAdrian Chadd 		ATH_TXQ_LOCK(txq);
46673feffbd7SAdrian Chadd 		bf = ath_tx_draintxq_get_one(sc, txq);
46685591b213SSam Leffler 		if (bf == NULL) {
4669b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
46705591b213SSam Leffler 			break;
46715591b213SSam Leffler 		}
46726edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
46736edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
4674a585a9a1SSam Leffler #ifdef ATH_DEBUG
46754a3ac3fcSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RESET) {
4676b032f27cSSam Leffler 			struct ieee80211com *ic = sc->sc_ifp->if_l2com;
46771762ec94SAdrian Chadd 			int status = 0;
4678b032f27cSSam Leffler 
46791762ec94SAdrian Chadd 			/*
46801762ec94SAdrian Chadd 			 * EDMA operation has a TX completion FIFO
46811762ec94SAdrian Chadd 			 * separate from the TX descriptor, so this
46821762ec94SAdrian Chadd 			 * method of checking the "completion" status
46831762ec94SAdrian Chadd 			 * is wrong.
46841762ec94SAdrian Chadd 			 */
46851762ec94SAdrian Chadd 			if (! sc->sc_isedma) {
46861762ec94SAdrian Chadd 				status = (ath_hal_txprocdesc(ah,
46871762ec94SAdrian Chadd 				    bf->bf_lastds,
468865f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
46891762ec94SAdrian Chadd 			}
46901762ec94SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, ix, status);
4691e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ic, mtod(bf->bf_m, const uint8_t *),
46924a3ac3fcSSam Leffler 			    bf->bf_m->m_len, 0, -1);
46934a3ac3fcSSam Leffler 		}
4694a585a9a1SSam Leffler #endif /* ATH_DEBUG */
469523428eafSSam Leffler 		/*
46969352fb7aSAdrian Chadd 		 * Since we're now doing magic in the completion
46979352fb7aSAdrian Chadd 		 * functions, we -must- call it for aggregation
46989352fb7aSAdrian Chadd 		 * destinations or BAW tracking will get upset.
469923428eafSSam Leffler 		 */
47009352fb7aSAdrian Chadd 		/*
47019352fb7aSAdrian Chadd 		 * Clear ATH_BUF_BUSY; the completion handler
47029352fb7aSAdrian Chadd 		 * will free the buffer.
47039352fb7aSAdrian Chadd 		 */
4704b837332dSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
470510ad9a77SSam Leffler 		bf->bf_flags &= ~ATH_BUF_BUSY;
47069352fb7aSAdrian Chadd 		if (bf->bf_comp)
47079352fb7aSAdrian Chadd 			bf->bf_comp(sc, bf, 1);
47089352fb7aSAdrian Chadd 		else
47099352fb7aSAdrian Chadd 			ath_tx_default_comp(sc, bf, 1);
47105591b213SSam Leffler 	}
47119352fb7aSAdrian Chadd 
4712eb6f0de0SAdrian Chadd 	/*
4713629ce218SAdrian Chadd 	 * Free the holding buffer if it exists
4714629ce218SAdrian Chadd 	 */
47155e018508SAdrian Chadd 	ATH_TXQ_LOCK(txq);
4716629ce218SAdrian Chadd 	ath_txq_freeholdingbuf(sc, txq);
47175e018508SAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
4718629ce218SAdrian Chadd 
4719629ce218SAdrian Chadd 	/*
4720eb6f0de0SAdrian Chadd 	 * Drain software queued frames which are on
4721eb6f0de0SAdrian Chadd 	 * active TIDs.
4722eb6f0de0SAdrian Chadd 	 */
4723eb6f0de0SAdrian Chadd 	ath_tx_txq_drain(sc, txq);
4724c42a7b7eSSam Leffler }
4725c42a7b7eSSam Leffler 
4726c42a7b7eSSam Leffler static void
4727c42a7b7eSSam Leffler ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
4728c42a7b7eSSam Leffler {
4729c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4730c42a7b7eSSam Leffler 
47319be82a42SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
47329be82a42SAdrian Chadd 
47339d2a962bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
4734dfaf8de9SAdrian Chadd 	    "%s: tx queue [%u] %p, active=%d, hwpending=%d, flags 0x%08x, "
4735dfaf8de9SAdrian Chadd 	    "link %p, holdingbf=%p\n",
47369d2a962bSAdrian Chadd 	    __func__,
47379d2a962bSAdrian Chadd 	    txq->axq_qnum,
47386891c875SPeter Wemm 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
47398d060542SAdrian Chadd 	    (int) (!! ath_hal_txqenabled(ah, txq->axq_qnum)),
47408d060542SAdrian Chadd 	    (int) ath_hal_numtxpending(ah, txq->axq_qnum),
47419d2a962bSAdrian Chadd 	    txq->axq_flags,
4742dfaf8de9SAdrian Chadd 	    txq->axq_link,
4743dfaf8de9SAdrian Chadd 	    txq->axq_holdingbf);
4744dfaf8de9SAdrian Chadd 
47454a3ac3fcSSam Leffler 	(void) ath_hal_stoptxdma(ah, txq->axq_qnum);
47469be82a42SAdrian Chadd 	/* We've stopped TX DMA, so mark this as stopped. */
47479be82a42SAdrian Chadd 	txq->axq_flags &= ~ATH_TXQ_PUTRUNNING;
4748dfaf8de9SAdrian Chadd 
4749dfaf8de9SAdrian Chadd #ifdef	ATH_DEBUG
4750dfaf8de9SAdrian Chadd 	if ((sc->sc_debug & ATH_DEBUG_RESET)
4751dfaf8de9SAdrian Chadd 	    && (txq->axq_holdingbf != NULL)) {
4752dfaf8de9SAdrian Chadd 		ath_printtxbuf(sc, txq->axq_holdingbf, txq->axq_qnum, 0, 0);
4753dfaf8de9SAdrian Chadd 	}
4754dfaf8de9SAdrian Chadd #endif
4755c42a7b7eSSam Leffler }
4756c42a7b7eSSam Leffler 
4757bad98824SAdrian Chadd int
47582d433424SAdrian Chadd ath_stoptxdma(struct ath_softc *sc)
4759c42a7b7eSSam Leffler {
4760c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4761c42a7b7eSSam Leffler 	int i;
4762c42a7b7eSSam Leffler 
4763c42a7b7eSSam Leffler 	/* XXX return value */
47642d433424SAdrian Chadd 	if (sc->sc_invalid)
47652d433424SAdrian Chadd 		return 0;
47662d433424SAdrian Chadd 
4767c42a7b7eSSam Leffler 	if (!sc->sc_invalid) {
4768c42a7b7eSSam Leffler 		/* don't touch the hardware if marked invalid */
47694a3ac3fcSSam Leffler 		DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
47704a3ac3fcSSam Leffler 		    __func__, sc->sc_bhalq,
47714a3ac3fcSSam Leffler 		    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq),
47724a3ac3fcSSam Leffler 		    NULL);
47739be82a42SAdrian Chadd 
47749be82a42SAdrian Chadd 		/* stop the beacon queue */
4775c42a7b7eSSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
47769be82a42SAdrian Chadd 
47779be82a42SAdrian Chadd 		/* Stop the data queues */
47789be82a42SAdrian Chadd 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
47799be82a42SAdrian Chadd 			if (ATH_TXQ_SETUP(sc, i)) {
47809be82a42SAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
4781c42a7b7eSSam Leffler 				ath_tx_stopdma(sc, &sc->sc_txq[i]);
47829be82a42SAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
47839be82a42SAdrian Chadd 			}
47849be82a42SAdrian Chadd 		}
4785c42a7b7eSSam Leffler 	}
47862d433424SAdrian Chadd 
47872d433424SAdrian Chadd 	return 1;
47882d433424SAdrian Chadd }
47892d433424SAdrian Chadd 
479007187d11SAdrian Chadd #ifdef	ATH_DEBUG
47919be82a42SAdrian Chadd void
4792ed261a61SAdrian Chadd ath_tx_dump(struct ath_softc *sc, struct ath_txq *txq)
4793ed261a61SAdrian Chadd {
4794ed261a61SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
4795ed261a61SAdrian Chadd 	struct ath_buf *bf;
4796ed261a61SAdrian Chadd 	int i = 0;
4797ed261a61SAdrian Chadd 
4798ed261a61SAdrian Chadd 	if (! (sc->sc_debug & ATH_DEBUG_RESET))
4799ed261a61SAdrian Chadd 		return;
4800ed261a61SAdrian Chadd 
4801ed261a61SAdrian Chadd 	device_printf(sc->sc_dev, "%s: Q%d: begin\n",
4802ed261a61SAdrian Chadd 	    __func__, txq->axq_qnum);
4803ed261a61SAdrian Chadd 	TAILQ_FOREACH(bf, &txq->axq_q, bf_list) {
4804ed261a61SAdrian Chadd 		ath_printtxbuf(sc, bf, txq->axq_qnum, i,
4805ed261a61SAdrian Chadd 			ath_hal_txprocdesc(ah, bf->bf_lastds,
4806ed261a61SAdrian Chadd 			    &bf->bf_status.ds_txstat) == HAL_OK);
4807ed261a61SAdrian Chadd 		i++;
4808ed261a61SAdrian Chadd 	}
4809ed261a61SAdrian Chadd 	device_printf(sc->sc_dev, "%s: Q%d: end\n",
4810ed261a61SAdrian Chadd 	    __func__, txq->axq_qnum);
4811ed261a61SAdrian Chadd }
481207187d11SAdrian Chadd #endif /* ATH_DEBUG */
4813ed261a61SAdrian Chadd 
48142d433424SAdrian Chadd /*
48152d433424SAdrian Chadd  * Drain the transmit queues and reclaim resources.
48162d433424SAdrian Chadd  */
4817788e6aa9SAdrian Chadd void
4818788e6aa9SAdrian Chadd ath_legacy_tx_drain(struct ath_softc *sc, ATH_RESET_TYPE reset_type)
48192d433424SAdrian Chadd {
48202d433424SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
48212d433424SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
48222d433424SAdrian Chadd 	int i;
48239be82a42SAdrian Chadd 	struct ath_buf *bf_last;
48242d433424SAdrian Chadd 
48252d433424SAdrian Chadd 	(void) ath_stoptxdma(sc);
48262d433424SAdrian Chadd 
4827ed261a61SAdrian Chadd 	/*
4828ed261a61SAdrian Chadd 	 * Dump the queue contents
4829ed261a61SAdrian Chadd 	 */
4830ef27340cSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
4831ef27340cSAdrian Chadd 		/*
4832ef27340cSAdrian Chadd 		 * XXX TODO: should we just handle the completed TX frames
4833ef27340cSAdrian Chadd 		 * here, whether or not the reset is a full one or not?
4834ef27340cSAdrian Chadd 		 */
4835ef27340cSAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
483607187d11SAdrian Chadd #ifdef	ATH_DEBUG
4837ed261a61SAdrian Chadd 			if (sc->sc_debug & ATH_DEBUG_RESET)
4838ed261a61SAdrian Chadd 				ath_tx_dump(sc, &sc->sc_txq[i]);
483907187d11SAdrian Chadd #endif	/* ATH_DEBUG */
48408328d6e4SAdrian Chadd 			if (reset_type == ATH_RESET_NOLOSS) {
4841ef27340cSAdrian Chadd 				ath_tx_processq(sc, &sc->sc_txq[i], 0);
48428328d6e4SAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
48438328d6e4SAdrian Chadd 				/*
48448328d6e4SAdrian Chadd 				 * Free the holding buffer; DMA is now
48458328d6e4SAdrian Chadd 				 * stopped.
48468328d6e4SAdrian Chadd 				 */
48478328d6e4SAdrian Chadd 				ath_txq_freeholdingbuf(sc, &sc->sc_txq[i]);
48488328d6e4SAdrian Chadd 				/*
48499be82a42SAdrian Chadd 				 * Setup the link pointer to be the
48509be82a42SAdrian Chadd 				 * _last_ buffer/descriptor in the list.
48519be82a42SAdrian Chadd 				 * If there's nothing in the list, set it
48529be82a42SAdrian Chadd 				 * to NULL.
48538328d6e4SAdrian Chadd 				 */
48549be82a42SAdrian Chadd 				bf_last = ATH_TXQ_LAST(&sc->sc_txq[i],
48559be82a42SAdrian Chadd 				    axq_q_s);
48569be82a42SAdrian Chadd 				if (bf_last != NULL) {
48579be82a42SAdrian Chadd 					ath_hal_gettxdesclinkptr(ah,
48589be82a42SAdrian Chadd 					    bf_last->bf_lastds,
48599be82a42SAdrian Chadd 					    &sc->sc_txq[i].axq_link);
48609be82a42SAdrian Chadd 				} else {
48618328d6e4SAdrian Chadd 					sc->sc_txq[i].axq_link = NULL;
48629be82a42SAdrian Chadd 				}
48638328d6e4SAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
48648328d6e4SAdrian Chadd 			} else
4865c42a7b7eSSam Leffler 				ath_tx_draintxq(sc, &sc->sc_txq[i]);
4866ef27340cSAdrian Chadd 		}
4867ef27340cSAdrian Chadd 	}
48684a3ac3fcSSam Leffler #ifdef ATH_DEBUG
48694a3ac3fcSSam Leffler 	if (sc->sc_debug & ATH_DEBUG_RESET) {
48706b349e5aSAdrian Chadd 		struct ath_buf *bf = TAILQ_FIRST(&sc->sc_bbuf);
48714a3ac3fcSSam Leffler 		if (bf != NULL && bf->bf_m != NULL) {
48726902009eSSam Leffler 			ath_printtxbuf(sc, bf, sc->sc_bhalq, 0,
48736edf1dc7SAdrian Chadd 				ath_hal_txprocdesc(ah, bf->bf_lastds,
487465f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
4875e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ifp->if_l2com,
4876e40b6ab1SSam Leffler 			    mtod(bf->bf_m, const uint8_t *), bf->bf_m->m_len,
4877e40b6ab1SSam Leffler 			    0, -1);
48784a3ac3fcSSam Leffler 		}
48794a3ac3fcSSam Leffler 	}
48804a3ac3fcSSam Leffler #endif /* ATH_DEBUG */
4881e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
488213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4883e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
48842e986da5SSam Leffler 	sc->sc_wd_timer = 0;
48855591b213SSam Leffler }
48865591b213SSam Leffler 
48875591b213SSam Leffler /*
4888c42a7b7eSSam Leffler  * Update internal state after a channel change.
4889c42a7b7eSSam Leffler  */
4890c42a7b7eSSam Leffler static void
4891c42a7b7eSSam Leffler ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
4892c42a7b7eSSam Leffler {
4893c42a7b7eSSam Leffler 	enum ieee80211_phymode mode;
4894c42a7b7eSSam Leffler 
4895c42a7b7eSSam Leffler 	/*
4896c42a7b7eSSam Leffler 	 * Change channels and update the h/w rate map
4897c42a7b7eSSam Leffler 	 * if we're switching; e.g. 11a to 11b/g.
4898c42a7b7eSSam Leffler 	 */
489968e8e04eSSam Leffler 	mode = ieee80211_chan2mode(chan);
4900c42a7b7eSSam Leffler 	if (mode != sc->sc_curmode)
4901c42a7b7eSSam Leffler 		ath_setcurmode(sc, mode);
490259efa8b5SSam Leffler 	sc->sc_curchan = chan;
4903c42a7b7eSSam Leffler }
4904c42a7b7eSSam Leffler 
4905c42a7b7eSSam Leffler /*
49065591b213SSam Leffler  * Set/change channels.  If the channel is really being changed,
49074fa8d4efSDaniel Eischen  * it's done by resetting the chip.  To accomplish this we must
49085591b213SSam Leffler  * first cleanup any pending DMA, then restart stuff after a la
49095591b213SSam Leffler  * ath_init.
49105591b213SSam Leffler  */
49115591b213SSam Leffler static int
49125591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
49135591b213SSam Leffler {
4914b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
4915b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
49165591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4917ef27340cSAdrian Chadd 	int ret = 0;
4918ef27340cSAdrian Chadd 
4919ef27340cSAdrian Chadd 	/* Treat this as an interface reset */
4920d52f7132SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
4921d52f7132SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
4922d52f7132SAdrian Chadd 
4923d52f7132SAdrian Chadd 	/* (Try to) stop TX/RX from occuring */
4924d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
4925d52f7132SAdrian Chadd 
4926ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4927904e385eSAdrian Chadd 
4928904e385eSAdrian Chadd 	/* Stop new RX/TX/interrupt completion */
4929ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
4930ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
4931ef27340cSAdrian Chadd 		    __func__);
4932ee321975SAdrian Chadd 	}
4933904e385eSAdrian Chadd 
4934904e385eSAdrian Chadd 	ath_hal_intrset(ah, 0);
4935904e385eSAdrian Chadd 
4936904e385eSAdrian Chadd 	/* Stop pending RX/TX completion */
4937904e385eSAdrian Chadd 	ath_txrx_stop_locked(sc);
4938904e385eSAdrian Chadd 
4939ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4940c42a7b7eSSam Leffler 
494159efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz, flags 0x%x)\n",
494259efa8b5SSam Leffler 	    __func__, ieee80211_chan2ieee(ic, chan),
494359efa8b5SSam Leffler 	    chan->ic_freq, chan->ic_flags);
494459efa8b5SSam Leffler 	if (chan != sc->sc_curchan) {
4945c42a7b7eSSam Leffler 		HAL_STATUS status;
49465591b213SSam Leffler 		/*
49475591b213SSam Leffler 		 * To switch channels clear any pending DMA operations;
49485591b213SSam Leffler 		 * wait long enough for the RX fifo to drain, reset the
49495591b213SSam Leffler 		 * hardware at the new frequency, and then re-enable
49505591b213SSam Leffler 		 * the relevant bits of the h/w.
49515591b213SSam Leffler 		 */
4952ef27340cSAdrian Chadd #if 0
49535591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
4954ef27340cSAdrian Chadd #endif
49559a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* turn off frame recv */
49569a842e8bSAdrian Chadd 		/*
49579a842e8bSAdrian Chadd 		 * First, handle completed TX/RX frames.
49589a842e8bSAdrian Chadd 		 */
4959f8cc9b09SAdrian Chadd 		ath_rx_flush(sc);
49609a842e8bSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_NOLOSS);
49619a842e8bSAdrian Chadd 		/*
49629a842e8bSAdrian Chadd 		 * Next, flush the non-scheduled frames.
49639a842e8bSAdrian Chadd 		 */
4964517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_FULL);	/* clear pending tx frames */
49659a842e8bSAdrian Chadd 
49666322256bSAdrian Chadd 		ath_update_chainmasks(sc, chan);
49676322256bSAdrian Chadd 		ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
49686322256bSAdrian Chadd 		    sc->sc_cur_rxchainmask);
496959efa8b5SSam Leffler 		if (!ath_hal_reset(ah, sc->sc_opmode, chan, AH_TRUE, &status)) {
4970b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to reset "
497179649302SGavin Atkinson 			    "channel %u (%u MHz, flags 0x%x), hal status %u\n",
497259efa8b5SSam Leffler 			    __func__, ieee80211_chan2ieee(ic, chan),
497359efa8b5SSam Leffler 			    chan->ic_freq, chan->ic_flags, status);
4974ef27340cSAdrian Chadd 			ret = EIO;
4975ef27340cSAdrian Chadd 			goto finish;
49765591b213SSam Leffler 		}
4977c59005e9SSam Leffler 		sc->sc_diversity = ath_hal_getdiversity(ah);
4978c42a7b7eSSam Leffler 
497948237774SAdrian Chadd 		/* Let DFS at it in case it's a DFS channel */
4980398bca2eSAdrian Chadd 		ath_dfs_radar_enable(sc, chan);
498148237774SAdrian Chadd 
49829af351f9SAdrian Chadd 		/* Let spectral at in case spectral is enabled */
49839af351f9SAdrian Chadd 		ath_spectral_enable(sc, chan);
49849af351f9SAdrian Chadd 
49855591b213SSam Leffler 		/*
4986b70f530bSAdrian Chadd 		 * Let bluetooth coexistence at in case it's needed for this
4987b70f530bSAdrian Chadd 		 * channel
4988b70f530bSAdrian Chadd 		 */
4989b70f530bSAdrian Chadd 		ath_btcoex_enable(sc, ic->ic_curchan);
4990b70f530bSAdrian Chadd 
4991b70f530bSAdrian Chadd 		/*
4992dd6a574eSAdrian Chadd 		 * If we're doing TDMA, enforce the TXOP limitation for chips
4993dd6a574eSAdrian Chadd 		 * that support it.
4994dd6a574eSAdrian Chadd 		 */
4995dd6a574eSAdrian Chadd 		if (sc->sc_hasenforcetxop && sc->sc_tdma)
4996dd6a574eSAdrian Chadd 			ath_hal_setenforcetxop(sc->sc_ah, 1);
4997dd6a574eSAdrian Chadd 		else
4998dd6a574eSAdrian Chadd 			ath_hal_setenforcetxop(sc->sc_ah, 0);
4999dd6a574eSAdrian Chadd 
5000dd6a574eSAdrian Chadd 		/*
50015591b213SSam Leffler 		 * Re-enable rx framework.
50025591b213SSam Leffler 		 */
50035591b213SSam Leffler 		if (ath_startrecv(sc) != 0) {
5004b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to restart recv logic\n",
5005b032f27cSSam Leffler 			    __func__);
5006ef27340cSAdrian Chadd 			ret = EIO;
5007ef27340cSAdrian Chadd 			goto finish;
50085591b213SSam Leffler 		}
50095591b213SSam Leffler 
50105591b213SSam Leffler 		/*
50115591b213SSam Leffler 		 * Change channels and update the h/w rate map
50125591b213SSam Leffler 		 * if we're switching; e.g. 11a to 11b/g.
50135591b213SSam Leffler 		 */
5014c42a7b7eSSam Leffler 		ath_chan_change(sc, chan);
50150a915fadSSam Leffler 
50160a915fadSSam Leffler 		/*
50172fd9aabbSAdrian Chadd 		 * Reset clears the beacon timers; reset them
50182fd9aabbSAdrian Chadd 		 * here if needed.
50192fd9aabbSAdrian Chadd 		 */
50202fd9aabbSAdrian Chadd 		if (sc->sc_beacons) {		/* restart beacons */
50212fd9aabbSAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
50222fd9aabbSAdrian Chadd 			if (sc->sc_tdma)
50232fd9aabbSAdrian Chadd 				ath_tdma_config(sc, NULL);
50242fd9aabbSAdrian Chadd 			else
50252fd9aabbSAdrian Chadd #endif
50262fd9aabbSAdrian Chadd 			ath_beacon_config(sc, NULL);
50272fd9aabbSAdrian Chadd 		}
50282fd9aabbSAdrian Chadd 
50292fd9aabbSAdrian Chadd 		/*
50300a915fadSSam Leffler 		 * Re-enable interrupts.
50310a915fadSSam Leffler 		 */
5032e78719adSAdrian Chadd #if 0
50330a915fadSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
5034ef27340cSAdrian Chadd #endif
50355591b213SSam Leffler 	}
5036ef27340cSAdrian Chadd 
5037ef27340cSAdrian Chadd finish:
5038ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5039ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
5040ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
5041ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
5042ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5043ef27340cSAdrian Chadd 
5044e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
5045ef27340cSAdrian Chadd 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5046e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
5047ef27340cSAdrian Chadd 	ath_txrx_start(sc);
5048ef27340cSAdrian Chadd 	/* XXX ath_start? */
5049ef27340cSAdrian Chadd 
5050ef27340cSAdrian Chadd 	return ret;
50515591b213SSam Leffler }
50525591b213SSam Leffler 
50535591b213SSam Leffler /*
50545591b213SSam Leffler  * Periodically recalibrate the PHY to account
50555591b213SSam Leffler  * for temperature/environment changes.
50565591b213SSam Leffler  */
50575591b213SSam Leffler static void
50585591b213SSam Leffler ath_calibrate(void *arg)
50595591b213SSam Leffler {
50605591b213SSam Leffler 	struct ath_softc *sc = arg;
50615591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
50622dc7fcc4SSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
50638d91de92SSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
5064943e37a1SAdrian Chadd 	HAL_BOOL longCal, isCalDone = AH_TRUE;
5065a108ab63SAdrian Chadd 	HAL_BOOL aniCal, shortCal = AH_FALSE;
50662dc7fcc4SSam Leffler 	int nextcal;
50675591b213SSam Leffler 
50688d91de92SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SCAN)	/* defer, off channel */
50698d91de92SSam Leffler 		goto restart;
50702dc7fcc4SSam Leffler 	longCal = (ticks - sc->sc_lastlongcal >= ath_longcalinterval*hz);
5071a108ab63SAdrian Chadd 	aniCal = (ticks - sc->sc_lastani >= ath_anicalinterval*hz/1000);
5072a108ab63SAdrian Chadd 	if (sc->sc_doresetcal)
5073a108ab63SAdrian Chadd 		shortCal = (ticks - sc->sc_lastshortcal >= ath_shortcalinterval*hz/1000);
5074a108ab63SAdrian Chadd 
5075a108ab63SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: shortCal=%d; longCal=%d; aniCal=%d\n", __func__, shortCal, longCal, aniCal);
5076a108ab63SAdrian Chadd 	if (aniCal) {
5077a108ab63SAdrian Chadd 		sc->sc_stats.ast_ani_cal++;
5078a108ab63SAdrian Chadd 		sc->sc_lastani = ticks;
5079a108ab63SAdrian Chadd 		ath_hal_ani_poll(ah, sc->sc_curchan);
5080a108ab63SAdrian Chadd 	}
5081a108ab63SAdrian Chadd 
50822dc7fcc4SSam Leffler 	if (longCal) {
50835591b213SSam Leffler 		sc->sc_stats.ast_per_cal++;
50848197f57eSAdrian Chadd 		sc->sc_lastlongcal = ticks;
50855591b213SSam Leffler 		if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
50865591b213SSam Leffler 			/*
50875591b213SSam Leffler 			 * Rfgain is out of bounds, reset the chip
50885591b213SSam Leffler 			 * to load new gain values.
50895591b213SSam Leffler 			 */
5090370572d9SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
5091370572d9SSam Leffler 				"%s: rfgain change\n", __func__);
50925591b213SSam Leffler 			sc->sc_stats.ast_per_rfgain++;
5093ef27340cSAdrian Chadd 			sc->sc_resetcal = 0;
5094ef27340cSAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
5095d52f7132SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
5096d52f7132SAdrian Chadd 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
5097ef27340cSAdrian Chadd 			return;
50985591b213SSam Leffler 		}
50992dc7fcc4SSam Leffler 		/*
51002dc7fcc4SSam Leffler 		 * If this long cal is after an idle period, then
51012dc7fcc4SSam Leffler 		 * reset the data collection state so we start fresh.
51022dc7fcc4SSam Leffler 		 */
51032dc7fcc4SSam Leffler 		if (sc->sc_resetcal) {
510459efa8b5SSam Leffler 			(void) ath_hal_calreset(ah, sc->sc_curchan);
51052dc7fcc4SSam Leffler 			sc->sc_lastcalreset = ticks;
5106a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
51072dc7fcc4SSam Leffler 			sc->sc_resetcal = 0;
5108a108ab63SAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
51092dc7fcc4SSam Leffler 		}
51102dc7fcc4SSam Leffler 	}
5111a108ab63SAdrian Chadd 
5112a108ab63SAdrian Chadd 	/* Only call if we're doing a short/long cal, not for ANI calibration */
5113a108ab63SAdrian Chadd 	if (shortCal || longCal) {
5114943e37a1SAdrian Chadd 		isCalDone = AH_FALSE;
511559efa8b5SSam Leffler 		if (ath_hal_calibrateN(ah, sc->sc_curchan, longCal, &isCalDone)) {
51162dc7fcc4SSam Leffler 			if (longCal) {
51172dc7fcc4SSam Leffler 				/*
51182dc7fcc4SSam Leffler 				 * Calibrate noise floor data again in case of change.
51192dc7fcc4SSam Leffler 				 */
51202dc7fcc4SSam Leffler 				ath_hal_process_noisefloor(ah);
51212dc7fcc4SSam Leffler 			}
51222dc7fcc4SSam Leffler 		} else {
5123c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
5124c42a7b7eSSam Leffler 				"%s: calibration of channel %u failed\n",
512559efa8b5SSam Leffler 				__func__, sc->sc_curchan->ic_freq);
51265591b213SSam Leffler 			sc->sc_stats.ast_per_calfail++;
51275591b213SSam Leffler 		}
5128a108ab63SAdrian Chadd 		if (shortCal)
5129a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
5130a108ab63SAdrian Chadd 	}
51312dc7fcc4SSam Leffler 	if (!isCalDone) {
51328d91de92SSam Leffler restart:
51337b0c77ecSSam Leffler 		/*
51342dc7fcc4SSam Leffler 		 * Use a shorter interval to potentially collect multiple
51352dc7fcc4SSam Leffler 		 * data samples required to complete calibration.  Once
51362dc7fcc4SSam Leffler 		 * we're told the work is done we drop back to a longer
51372dc7fcc4SSam Leffler 		 * interval between requests.  We're more aggressive doing
51382dc7fcc4SSam Leffler 		 * work when operating as an AP to improve operation right
51392dc7fcc4SSam Leffler 		 * after startup.
51407b0c77ecSSam Leffler 		 */
5141a108ab63SAdrian Chadd 		sc->sc_lastshortcal = ticks;
5142a108ab63SAdrian Chadd 		nextcal = ath_shortcalinterval*hz/1000;
51432dc7fcc4SSam Leffler 		if (sc->sc_opmode != HAL_M_HOSTAP)
51442dc7fcc4SSam Leffler 			nextcal *= 10;
5145a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_TRUE;
51462dc7fcc4SSam Leffler 	} else {
5147a108ab63SAdrian Chadd 		/* nextcal should be the shortest time for next event */
51482dc7fcc4SSam Leffler 		nextcal = ath_longcalinterval*hz;
51492dc7fcc4SSam Leffler 		if (sc->sc_lastcalreset == 0)
51502dc7fcc4SSam Leffler 			sc->sc_lastcalreset = sc->sc_lastlongcal;
51512dc7fcc4SSam Leffler 		else if (ticks - sc->sc_lastcalreset >= ath_resetcalinterval*hz)
51522dc7fcc4SSam Leffler 			sc->sc_resetcal = 1;	/* setup reset next trip */
5153a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_FALSE;
5154bd5a9920SSam Leffler 	}
5155a108ab63SAdrian Chadd 	/* ANI calibration may occur more often than short/long/resetcal */
5156a108ab63SAdrian Chadd 	if (ath_anicalinterval > 0)
5157a108ab63SAdrian Chadd 		nextcal = MIN(nextcal, ath_anicalinterval*hz/1000);
5158bd5a9920SSam Leffler 
51592dc7fcc4SSam Leffler 	if (nextcal != 0) {
51602dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: next +%u (%sisCalDone)\n",
51612dc7fcc4SSam Leffler 		    __func__, nextcal, isCalDone ? "" : "!");
51622dc7fcc4SSam Leffler 		callout_reset(&sc->sc_cal_ch, nextcal, ath_calibrate, sc);
51632dc7fcc4SSam Leffler 	} else {
51642dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: calibration disabled\n",
51652dc7fcc4SSam Leffler 		    __func__);
51662dc7fcc4SSam Leffler 		/* NB: don't rearm timer */
51672dc7fcc4SSam Leffler 	}
51685591b213SSam Leffler }
51695591b213SSam Leffler 
517068e8e04eSSam Leffler static void
517168e8e04eSSam Leffler ath_scan_start(struct ieee80211com *ic)
517268e8e04eSSam Leffler {
517368e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
517468e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
517568e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
517668e8e04eSSam Leffler 	u_int32_t rfilt;
517768e8e04eSSam Leffler 
517868e8e04eSSam Leffler 	/* XXX calibration timer? */
517968e8e04eSSam Leffler 
5180c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
518168e8e04eSSam Leffler 	sc->sc_scanning = 1;
518268e8e04eSSam Leffler 	sc->sc_syncbeacon = 0;
518368e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5184c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
5185c98cefc5SAdrian Chadd 
5186c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
518768e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
518868e8e04eSSam Leffler 	ath_hal_setassocid(ah, ifp->if_broadcastaddr, 0);
5189c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
519068e8e04eSSam Leffler 
519168e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0\n",
519268e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(ifp->if_broadcastaddr));
519368e8e04eSSam Leffler }
519468e8e04eSSam Leffler 
519568e8e04eSSam Leffler static void
519668e8e04eSSam Leffler ath_scan_end(struct ieee80211com *ic)
519768e8e04eSSam Leffler {
519868e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
519968e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
520068e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
520168e8e04eSSam Leffler 	u_int32_t rfilt;
520268e8e04eSSam Leffler 
5203c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
520468e8e04eSSam Leffler 	sc->sc_scanning = 0;
520568e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5206c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
5207c98cefc5SAdrian Chadd 
5208c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
520968e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
521068e8e04eSSam Leffler 	ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
521168e8e04eSSam Leffler 
521268e8e04eSSam Leffler 	ath_hal_process_noisefloor(ah);
5213c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
521468e8e04eSSam Leffler 
521568e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
521668e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(sc->sc_curbssid),
521768e8e04eSSam Leffler 		 sc->sc_curaid);
521868e8e04eSSam Leffler }
521968e8e04eSSam Leffler 
5220fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
5221e7200579SAdrian Chadd /*
5222e7200579SAdrian Chadd  * For now, just do a channel change.
5223e7200579SAdrian Chadd  *
5224e7200579SAdrian Chadd  * Later, we'll go through the hard slog of suspending tx/rx, changing rate
5225e7200579SAdrian Chadd  * control state and resetting the hardware without dropping frames out
5226e7200579SAdrian Chadd  * of the queue.
5227e7200579SAdrian Chadd  *
5228e7200579SAdrian Chadd  * The unfortunate trouble here is making absolutely sure that the
5229e7200579SAdrian Chadd  * channel width change has propagated enough so the hardware
5230e7200579SAdrian Chadd  * absolutely isn't handed bogus frames for it's current operating
5231e7200579SAdrian Chadd  * mode. (Eg, 40MHz frames in 20MHz mode.) Since TX and RX can and
5232e7200579SAdrian Chadd  * does occur in parallel, we need to make certain we've blocked
5233e7200579SAdrian Chadd  * any further ongoing TX (and RX, that can cause raw TX)
5234e7200579SAdrian Chadd  * before we do this.
5235e7200579SAdrian Chadd  */
5236e7200579SAdrian Chadd static void
5237e7200579SAdrian Chadd ath_update_chw(struct ieee80211com *ic)
5238e7200579SAdrian Chadd {
5239e7200579SAdrian Chadd 	struct ifnet *ifp = ic->ic_ifp;
5240e7200579SAdrian Chadd 	struct ath_softc *sc = ifp->if_softc;
5241e7200579SAdrian Chadd 
5242e7200579SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: called\n", __func__);
5243e7200579SAdrian Chadd 	ath_set_channel(ic);
5244e7200579SAdrian Chadd }
5245fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
5246e7200579SAdrian Chadd 
524768e8e04eSSam Leffler static void
524868e8e04eSSam Leffler ath_set_channel(struct ieee80211com *ic)
524968e8e04eSSam Leffler {
525068e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
525168e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
525268e8e04eSSam Leffler 
525368e8e04eSSam Leffler 	(void) ath_chan_set(sc, ic->ic_curchan);
525468e8e04eSSam Leffler 	/*
525568e8e04eSSam Leffler 	 * If we are returning to our bss channel then mark state
525668e8e04eSSam Leffler 	 * so the next recv'd beacon's tsf will be used to sync the
525768e8e04eSSam Leffler 	 * beacon timers.  Note that since we only hear beacons in
525868e8e04eSSam Leffler 	 * sta/ibss mode this has no effect in other operating modes.
525968e8e04eSSam Leffler 	 */
5260a887b1e3SAdrian Chadd 	ATH_LOCK(sc);
526168e8e04eSSam Leffler 	if (!sc->sc_scanning && ic->ic_curchan == ic->ic_bsschan)
526268e8e04eSSam Leffler 		sc->sc_syncbeacon = 1;
5263a887b1e3SAdrian Chadd 	ATH_UNLOCK(sc);
526468e8e04eSSam Leffler }
526568e8e04eSSam Leffler 
5266b032f27cSSam Leffler /*
5267b032f27cSSam Leffler  * Walk the vap list and check if there any vap's in RUN state.
5268b032f27cSSam Leffler  */
52695591b213SSam Leffler static int
5270b032f27cSSam Leffler ath_isanyrunningvaps(struct ieee80211vap *this)
52715591b213SSam Leffler {
5272b032f27cSSam Leffler 	struct ieee80211com *ic = this->iv_ic;
5273b032f27cSSam Leffler 	struct ieee80211vap *vap;
5274b032f27cSSam Leffler 
5275b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
5276b032f27cSSam Leffler 
5277b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
5278309a3e45SSam Leffler 		if (vap != this && vap->iv_state >= IEEE80211_S_RUN)
5279b032f27cSSam Leffler 			return 1;
5280b032f27cSSam Leffler 	}
5281b032f27cSSam Leffler 	return 0;
5282b032f27cSSam Leffler }
5283b032f27cSSam Leffler 
5284b032f27cSSam Leffler static int
5285b032f27cSSam Leffler ath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
5286b032f27cSSam Leffler {
5287b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
5288b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5289b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
529045bbf62fSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5291b032f27cSSam Leffler 	struct ieee80211_node *ni = NULL;
529268e8e04eSSam Leffler 	int i, error, stamode;
52935591b213SSam Leffler 	u_int32_t rfilt;
5294f52efb6dSAdrian Chadd 	int csa_run_transition = 0;
5295a74ebfe5SAdrian Chadd 
52965591b213SSam Leffler 	static const HAL_LED_STATE leds[] = {
52975591b213SSam Leffler 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
52985591b213SSam Leffler 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
52995591b213SSam Leffler 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
53005591b213SSam Leffler 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
530177d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CAC */
53025591b213SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
530377d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CSA */
530477d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_SLEEP */
53055591b213SSam Leffler 	};
53065591b213SSam Leffler 
5307c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
5308b032f27cSSam Leffler 		ieee80211_state_name[vap->iv_state],
5309c42a7b7eSSam Leffler 		ieee80211_state_name[nstate]);
53105591b213SSam Leffler 
5311107fdf96SAdrian Chadd 	/*
5312107fdf96SAdrian Chadd 	 * net80211 _should_ have the comlock asserted at this point.
5313107fdf96SAdrian Chadd 	 * There are some comments around the calls to vap->iv_newstate
5314107fdf96SAdrian Chadd 	 * which indicate that it (newstate) may end up dropping the
5315107fdf96SAdrian Chadd 	 * lock.  This and the subsequent lock assert check after newstate
5316107fdf96SAdrian Chadd 	 * are an attempt to catch these and figure out how/why.
5317107fdf96SAdrian Chadd 	 */
5318107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
5319107fdf96SAdrian Chadd 
5320f52efb6dSAdrian Chadd 	if (vap->iv_state == IEEE80211_S_CSA && nstate == IEEE80211_S_RUN)
5321f52efb6dSAdrian Chadd 		csa_run_transition = 1;
5322f52efb6dSAdrian Chadd 
53232e986da5SSam Leffler 	callout_drain(&sc->sc_cal_ch);
53245591b213SSam Leffler 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
53255591b213SSam Leffler 
5326b032f27cSSam Leffler 	if (nstate == IEEE80211_S_SCAN) {
532758769f58SSam Leffler 		/*
5328b032f27cSSam Leffler 		 * Scanning: turn off beacon miss and don't beacon.
5329b032f27cSSam Leffler 		 * Mark beacon state so when we reach RUN state we'll
5330b032f27cSSam Leffler 		 * [re]setup beacons.  Unblock the task q thread so
5331b032f27cSSam Leffler 		 * deferred interrupt processing is done.
533258769f58SSam Leffler 		 */
5333b032f27cSSam Leffler 		ath_hal_intrset(ah,
5334b032f27cSSam Leffler 		    sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
53355591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5336b032f27cSSam Leffler 		sc->sc_beacons = 0;
5337b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
53385591b213SSam Leffler 	}
53395591b213SSam Leffler 
534080767531SAdrian Chadd 	ni = ieee80211_ref_node(vap->iv_bss);
534168e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5342b032f27cSSam Leffler 	stamode = (vap->iv_opmode == IEEE80211_M_STA ||
53437b916f89SSam Leffler 		   vap->iv_opmode == IEEE80211_M_AHDEMO ||
5344b032f27cSSam Leffler 		   vap->iv_opmode == IEEE80211_M_IBSS);
534568e8e04eSSam Leffler 	if (stamode && nstate == IEEE80211_S_RUN) {
534668e8e04eSSam Leffler 		sc->sc_curaid = ni->ni_associd;
534768e8e04eSSam Leffler 		IEEE80211_ADDR_COPY(sc->sc_curbssid, ni->ni_bssid);
5348b032f27cSSam Leffler 		ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
5349b032f27cSSam Leffler 	}
535068e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
5351b032f27cSSam Leffler 	   __func__, rfilt, ether_sprintf(sc->sc_curbssid), sc->sc_curaid);
535268e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
535368e8e04eSSam Leffler 
5354b032f27cSSam Leffler 	/* XXX is this to restore keycache on resume? */
5355b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_STA &&
5356b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY)) {
53575591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
53585591b213SSam Leffler 			if (ath_hal_keyisvalid(ah, i))
535968e8e04eSSam Leffler 				ath_hal_keysetmac(ah, i, ni->ni_bssid);
53605591b213SSam Leffler 	}
5361b032f27cSSam Leffler 
5362b032f27cSSam Leffler 	/*
5363b032f27cSSam Leffler 	 * Invoke the parent method to do net80211 work.
5364b032f27cSSam Leffler 	 */
5365b032f27cSSam Leffler 	error = avp->av_newstate(vap, nstate, arg);
5366b032f27cSSam Leffler 	if (error != 0)
5367b032f27cSSam Leffler 		goto bad;
5368c42a7b7eSSam Leffler 
5369107fdf96SAdrian Chadd 	/*
5370107fdf96SAdrian Chadd 	 * See above: ensure av_newstate() doesn't drop the lock
5371107fdf96SAdrian Chadd 	 * on us.
5372107fdf96SAdrian Chadd 	 */
5373107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
5374107fdf96SAdrian Chadd 
537568e8e04eSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
5376b032f27cSSam Leffler 		/* NB: collect bss node again, it may have changed */
537780767531SAdrian Chadd 		ieee80211_free_node(ni);
537880767531SAdrian Chadd 		ni = ieee80211_ref_node(vap->iv_bss);
53795591b213SSam Leffler 
5380b032f27cSSam Leffler 		DPRINTF(sc, ATH_DEBUG_STATE,
5381b032f27cSSam Leffler 		    "%s(RUN): iv_flags 0x%08x bintvl %d bssid %s "
5382b032f27cSSam Leffler 		    "capinfo 0x%04x chan %d\n", __func__,
5383b032f27cSSam Leffler 		    vap->iv_flags, ni->ni_intval, ether_sprintf(ni->ni_bssid),
5384b032f27cSSam Leffler 		    ni->ni_capinfo, ieee80211_chan2ieee(ic, ic->ic_curchan));
5385b032f27cSSam Leffler 
5386b032f27cSSam Leffler 		switch (vap->iv_opmode) {
5387584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
538810ad9a77SSam Leffler 		case IEEE80211_M_AHDEMO:
538910ad9a77SSam Leffler 			if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
539010ad9a77SSam Leffler 				break;
539110ad9a77SSam Leffler 			/* fall thru... */
539210ad9a77SSam Leffler #endif
5393e8fd88a3SSam Leffler 		case IEEE80211_M_HOSTAP:
5394e8fd88a3SSam Leffler 		case IEEE80211_M_IBSS:
539559aa14a9SRui Paulo 		case IEEE80211_M_MBSS:
53965591b213SSam Leffler 			/*
5397e8fd88a3SSam Leffler 			 * Allocate and setup the beacon frame.
5398e8fd88a3SSam Leffler 			 *
5399f818612bSSam Leffler 			 * Stop any previous beacon DMA.  This may be
5400f818612bSSam Leffler 			 * necessary, for example, when an ibss merge
5401f818612bSSam Leffler 			 * causes reconfiguration; there will be a state
5402f818612bSSam Leffler 			 * transition from RUN->RUN that means we may
5403f818612bSSam Leffler 			 * be called with beacon transmission active.
5404f818612bSSam Leffler 			 */
5405f818612bSSam Leffler 			ath_hal_stoptxdma(ah, sc->sc_bhalq);
5406b032f27cSSam Leffler 
54075591b213SSam Leffler 			error = ath_beacon_alloc(sc, ni);
54085591b213SSam Leffler 			if (error != 0)
54095591b213SSam Leffler 				goto bad;
54107a04dc27SSam Leffler 			/*
541180d939bfSSam Leffler 			 * If joining an adhoc network defer beacon timer
541280d939bfSSam Leffler 			 * configuration to the next beacon frame so we
541380d939bfSSam Leffler 			 * have a current TSF to use.  Otherwise we're
5414b032f27cSSam Leffler 			 * starting an ibss/bss so there's no need to delay;
5415b032f27cSSam Leffler 			 * if this is the first vap moving to RUN state, then
5416b032f27cSSam Leffler 			 * beacon state needs to be [re]configured.
54177a04dc27SSam Leffler 			 */
5418b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_IBSS &&
5419b032f27cSSam Leffler 			    ni->ni_tstamp.tsf != 0) {
542080d939bfSSam Leffler 				sc->sc_syncbeacon = 1;
5421b032f27cSSam Leffler 			} else if (!sc->sc_beacons) {
5422584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
542310ad9a77SSam Leffler 				if (vap->iv_caps & IEEE80211_C_TDMA)
542410ad9a77SSam Leffler 					ath_tdma_config(sc, vap);
542510ad9a77SSam Leffler 				else
542610ad9a77SSam Leffler #endif
5427b032f27cSSam Leffler 					ath_beacon_config(sc, vap);
5428b032f27cSSam Leffler 				sc->sc_beacons = 1;
5429b032f27cSSam Leffler 			}
5430e8fd88a3SSam Leffler 			break;
5431e8fd88a3SSam Leffler 		case IEEE80211_M_STA:
5432e8fd88a3SSam Leffler 			/*
543380d939bfSSam Leffler 			 * Defer beacon timer configuration to the next
543480d939bfSSam Leffler 			 * beacon frame so we have a current TSF to use
543580d939bfSSam Leffler 			 * (any TSF collected when scanning is likely old).
5436f52efb6dSAdrian Chadd 			 * However if it's due to a CSA -> RUN transition,
5437f52efb6dSAdrian Chadd 			 * force a beacon update so we pick up a lack of
5438f52efb6dSAdrian Chadd 			 * beacons from an AP in CAC and thus force a
5439f52efb6dSAdrian Chadd 			 * scan.
5440a74ebfe5SAdrian Chadd 			 *
5441a74ebfe5SAdrian Chadd 			 * And, there's also corner cases here where
5442a74ebfe5SAdrian Chadd 			 * after a scan, the AP may have disappeared.
5443a74ebfe5SAdrian Chadd 			 * In that case, we may not receive an actual
5444a74ebfe5SAdrian Chadd 			 * beacon to update the beacon timer and thus we
5445a74ebfe5SAdrian Chadd 			 * won't get notified of the missing beacons.
54467a04dc27SSam Leffler 			 */
544780d939bfSSam Leffler 			sc->sc_syncbeacon = 1;
5448a74ebfe5SAdrian Chadd #if 0
5449f52efb6dSAdrian Chadd 			if (csa_run_transition)
5450a74ebfe5SAdrian Chadd #endif
5451f52efb6dSAdrian Chadd 				ath_beacon_config(sc, vap);
5452a74ebfe5SAdrian Chadd 
5453a74ebfe5SAdrian Chadd 			/*
5454a74ebfe5SAdrian Chadd 			 * PR: kern/175227
5455a74ebfe5SAdrian Chadd 			 *
5456a74ebfe5SAdrian Chadd 			 * Reconfigure beacons during reset; as otherwise
5457a74ebfe5SAdrian Chadd 			 * we won't get the beacon timers reprogrammed
5458a74ebfe5SAdrian Chadd 			 * after a reset and thus we won't pick up a
5459a74ebfe5SAdrian Chadd 			 * beacon miss interrupt.
5460a74ebfe5SAdrian Chadd 			 *
5461a74ebfe5SAdrian Chadd 			 * Hopefully we'll see a beacon before the BMISS
5462a74ebfe5SAdrian Chadd 			 * timer fires (too often), leading to a STA
5463a74ebfe5SAdrian Chadd 			 * disassociation.
5464a74ebfe5SAdrian Chadd 			 */
5465a74ebfe5SAdrian Chadd 			sc->sc_beacons = 1;
5466e8fd88a3SSam Leffler 			break;
5467b032f27cSSam Leffler 		case IEEE80211_M_MONITOR:
5468b032f27cSSam Leffler 			/*
5469b032f27cSSam Leffler 			 * Monitor mode vaps have only INIT->RUN and RUN->RUN
5470b032f27cSSam Leffler 			 * transitions so we must re-enable interrupts here to
5471b032f27cSSam Leffler 			 * handle the case of a single monitor mode vap.
5472b032f27cSSam Leffler 			 */
5473b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask);
5474b032f27cSSam Leffler 			break;
5475b032f27cSSam Leffler 		case IEEE80211_M_WDS:
5476b032f27cSSam Leffler 			break;
5477e8fd88a3SSam Leffler 		default:
5478e8fd88a3SSam Leffler 			break;
54795591b213SSam Leffler 		}
54805591b213SSam Leffler 		/*
54817b0c77ecSSam Leffler 		 * Let the hal process statistics collected during a
54827b0c77ecSSam Leffler 		 * scan so it can provide calibrated noise floor data.
54837b0c77ecSSam Leffler 		 */
54847b0c77ecSSam Leffler 		ath_hal_process_noisefloor(ah);
54857b0c77ecSSam Leffler 		/*
5486ffa2cab6SSam Leffler 		 * Reset rssi stats; maybe not the best place...
5487ffa2cab6SSam Leffler 		 */
5488ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
5489ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
5490ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
549145bbf62fSSam Leffler 		/*
5492b032f27cSSam Leffler 		 * Finally, start any timers and the task q thread
5493b032f27cSSam Leffler 		 * (in case we didn't go through SCAN state).
549445bbf62fSSam Leffler 		 */
54952dc7fcc4SSam Leffler 		if (ath_longcalinterval != 0) {
5496c42a7b7eSSam Leffler 			/* start periodic recalibration timer */
54972dc7fcc4SSam Leffler 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
54982dc7fcc4SSam Leffler 		} else {
54992dc7fcc4SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
55002dc7fcc4SSam Leffler 			    "%s: calibration disabled\n", __func__);
5501c42a7b7eSSam Leffler 		}
5502b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
5503b032f27cSSam Leffler 	} else if (nstate == IEEE80211_S_INIT) {
5504b032f27cSSam Leffler 		/*
5505b032f27cSSam Leffler 		 * If there are no vaps left in RUN state then
5506b032f27cSSam Leffler 		 * shutdown host/driver operation:
5507b032f27cSSam Leffler 		 * o disable interrupts
5508b032f27cSSam Leffler 		 * o disable the task queue thread
5509b032f27cSSam Leffler 		 * o mark beacon processing as stopped
5510b032f27cSSam Leffler 		 */
5511b032f27cSSam Leffler 		if (!ath_isanyrunningvaps(vap)) {
5512b032f27cSSam Leffler 			sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5513b032f27cSSam Leffler 			/* disable interrupts  */
5514b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL);
5515b032f27cSSam Leffler 			taskqueue_block(sc->sc_tq);
5516b032f27cSSam Leffler 			sc->sc_beacons = 0;
5517b032f27cSSam Leffler 		}
5518584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
551910ad9a77SSam Leffler 		ath_hal_setcca(ah, AH_TRUE);
552010ad9a77SSam Leffler #endif
5521b032f27cSSam Leffler 	}
55225591b213SSam Leffler bad:
552380767531SAdrian Chadd 	ieee80211_free_node(ni);
55245591b213SSam Leffler 	return error;
55255591b213SSam Leffler }
55265591b213SSam Leffler 
55275591b213SSam Leffler /*
5528e8fd88a3SSam Leffler  * Allocate a key cache slot to the station so we can
5529e8fd88a3SSam Leffler  * setup a mapping from key index to node. The key cache
5530e8fd88a3SSam Leffler  * slot is needed for managing antenna state and for
5531e8fd88a3SSam Leffler  * compression when stations do not use crypto.  We do
5532e8fd88a3SSam Leffler  * it uniliaterally here; if crypto is employed this slot
5533e8fd88a3SSam Leffler  * will be reassigned.
5534e8fd88a3SSam Leffler  */
5535e8fd88a3SSam Leffler static void
5536e8fd88a3SSam Leffler ath_setup_stationkey(struct ieee80211_node *ni)
5537e8fd88a3SSam Leffler {
5538b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
5539b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
5540c1225b52SSam Leffler 	ieee80211_keyix keyix, rxkeyix;
5541e8fd88a3SSam Leffler 
554280767531SAdrian Chadd 	/* XXX should take a locked ref to vap->iv_bss */
5543b032f27cSSam Leffler 	if (!ath_key_alloc(vap, &ni->ni_ucastkey, &keyix, &rxkeyix)) {
5544e8fd88a3SSam Leffler 		/*
5545e8fd88a3SSam Leffler 		 * Key cache is full; we'll fall back to doing
5546e8fd88a3SSam Leffler 		 * the more expensive lookup in software.  Note
5547e8fd88a3SSam Leffler 		 * this also means no h/w compression.
5548e8fd88a3SSam Leffler 		 */
5549e8fd88a3SSam Leffler 		/* XXX msg+statistic */
5550e8fd88a3SSam Leffler 	} else {
5551c1225b52SSam Leffler 		/* XXX locking? */
5552e8fd88a3SSam Leffler 		ni->ni_ucastkey.wk_keyix = keyix;
5553c1225b52SSam Leffler 		ni->ni_ucastkey.wk_rxkeyix = rxkeyix;
555433052833SSam Leffler 		/* NB: must mark device key to get called back on delete */
555533052833SSam Leffler 		ni->ni_ucastkey.wk_flags |= IEEE80211_KEY_DEVKEY;
5556d3ac945bSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_ucastkey.wk_macaddr, ni->ni_macaddr);
5557e8fd88a3SSam Leffler 		/* NB: this will create a pass-thru key entry */
555855c7b877SAdrian Chadd 		ath_keyset(sc, vap, &ni->ni_ucastkey, vap->iv_bss);
5559e8fd88a3SSam Leffler 	}
5560e8fd88a3SSam Leffler }
5561e8fd88a3SSam Leffler 
5562e8fd88a3SSam Leffler /*
55635591b213SSam Leffler  * Setup driver-specific state for a newly associated node.
55645591b213SSam Leffler  * Note that we're called also on a re-associate, the isnew
55655591b213SSam Leffler  * param tells us if this is the first time or not.
55665591b213SSam Leffler  */
55675591b213SSam Leffler static void
5568e9962332SSam Leffler ath_newassoc(struct ieee80211_node *ni, int isnew)
55695591b213SSam Leffler {
5570b032f27cSSam Leffler 	struct ath_node *an = ATH_NODE(ni);
5571b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
5572b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
5573c62362cbSSam Leffler 	const struct ieee80211_txparam *tp = ni->ni_txparms;
55745591b213SSam Leffler 
5575ab06fdf2SSam Leffler 	an->an_mcastrix = ath_tx_findrix(sc, tp->mcastrate);
5576ab06fdf2SSam Leffler 	an->an_mgmtrix = ath_tx_findrix(sc, tp->mgmtrate);
5577b032f27cSSam Leffler 
5578b032f27cSSam Leffler 	ath_rate_newassoc(sc, an, isnew);
557932da86a0SAdrian Chadd 
5580e8fd88a3SSam Leffler 	if (isnew &&
5581b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey &&
5582b032f27cSSam Leffler 	    ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE)
5583e8fd88a3SSam Leffler 		ath_setup_stationkey(ni);
55844bed2b67SAdrian Chadd 
55854bed2b67SAdrian Chadd 	/*
55864bed2b67SAdrian Chadd 	 * If we're reassociating, make sure that any paused queues
55874bed2b67SAdrian Chadd 	 * get unpaused.
55884bed2b67SAdrian Chadd 	 *
55894bed2b67SAdrian Chadd 	 * Now, we may hvae frames in the hardware queue for this node.
55904bed2b67SAdrian Chadd 	 * So if we are reassociating and there are frames in the queue,
55914bed2b67SAdrian Chadd 	 * we need to go through the cleanup path to ensure that they're
55924bed2b67SAdrian Chadd 	 * marked as non-aggregate.
55934bed2b67SAdrian Chadd 	 */
55944bed2b67SAdrian Chadd 	if (! isnew) {
559532da86a0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE,
55964bed2b67SAdrian Chadd 		    "%s: %6D: reassoc; is_powersave=%d\n",
55974bed2b67SAdrian Chadd 		    __func__,
55984bed2b67SAdrian Chadd 		    ni->ni_macaddr,
55994bed2b67SAdrian Chadd 		    ":",
56004bed2b67SAdrian Chadd 		    an->an_is_powersave);
56014bed2b67SAdrian Chadd 
56024bed2b67SAdrian Chadd 		/* XXX for now, we can't hold the lock across assoc */
56034bed2b67SAdrian Chadd 		ath_tx_node_reassoc(sc, an);
56044bed2b67SAdrian Chadd 
56054bed2b67SAdrian Chadd 		/* XXX for now, we can't hold the lock across wakeup */
56064bed2b67SAdrian Chadd 		if (an->an_is_powersave)
56074bed2b67SAdrian Chadd 			ath_tx_node_wakeup(sc, an);
56084bed2b67SAdrian Chadd 	}
5609e8fd88a3SSam Leffler }
56105591b213SSam Leffler 
56115591b213SSam Leffler static int
561259efa8b5SSam Leffler ath_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *reg,
5613b032f27cSSam Leffler 	int nchans, struct ieee80211_channel chans[])
5614b032f27cSSam Leffler {
5615b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5616b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
561759efa8b5SSam Leffler 	HAL_STATUS status;
5618b032f27cSSam Leffler 
5619033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
562059efa8b5SSam Leffler 	    "%s: rd %u cc %u location %c%s\n",
562159efa8b5SSam Leffler 	    __func__, reg->regdomain, reg->country, reg->location,
562259efa8b5SSam Leffler 	    reg->ecm ? " ecm" : "");
5623033022a9SSam Leffler 
562459efa8b5SSam Leffler 	status = ath_hal_set_channels(ah, chans, nchans,
562559efa8b5SSam Leffler 	    reg->country, reg->regdomain);
562659efa8b5SSam Leffler 	if (status != HAL_OK) {
562759efa8b5SSam Leffler 		DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: failed, status %u\n",
562859efa8b5SSam Leffler 		    __func__, status);
562959efa8b5SSam Leffler 		return EINVAL;		/* XXX */
5630b032f27cSSam Leffler 	}
56318db87e40SAdrian Chadd 
5632b032f27cSSam Leffler 	return 0;
5633b032f27cSSam Leffler }
5634b032f27cSSam Leffler 
5635b032f27cSSam Leffler static void
5636b032f27cSSam Leffler ath_getradiocaps(struct ieee80211com *ic,
56375fe9f044SSam Leffler 	int maxchans, int *nchans, struct ieee80211_channel chans[])
5638b032f27cSSam Leffler {
5639b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5640b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5641b032f27cSSam Leffler 
564259efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: use rd %u cc %d\n",
564359efa8b5SSam Leffler 	    __func__, SKU_DEBUG, CTRY_DEFAULT);
5644033022a9SSam Leffler 
564559efa8b5SSam Leffler 	/* XXX check return */
564659efa8b5SSam Leffler 	(void) ath_hal_getchannels(ah, chans, maxchans, nchans,
564759efa8b5SSam Leffler 	    HAL_MODE_ALL, CTRY_DEFAULT, SKU_DEBUG, AH_TRUE);
5648033022a9SSam Leffler 
5649b032f27cSSam Leffler }
5650b032f27cSSam Leffler 
5651b032f27cSSam Leffler static int
5652b032f27cSSam Leffler ath_getchannels(struct ath_softc *sc)
5653b032f27cSSam Leffler {
5654b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
5655b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
5656b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
565759efa8b5SSam Leffler 	HAL_STATUS status;
5658b032f27cSSam Leffler 
5659b032f27cSSam Leffler 	/*
566059efa8b5SSam Leffler 	 * Collect channel set based on EEPROM contents.
5661b032f27cSSam Leffler 	 */
566259efa8b5SSam Leffler 	status = ath_hal_init_channels(ah, ic->ic_channels, IEEE80211_CHAN_MAX,
566359efa8b5SSam Leffler 	    &ic->ic_nchans, HAL_MODE_ALL, CTRY_DEFAULT, SKU_NONE, AH_TRUE);
566459efa8b5SSam Leffler 	if (status != HAL_OK) {
566559efa8b5SSam Leffler 		if_printf(ifp, "%s: unable to collect channel list from hal, "
566659efa8b5SSam Leffler 		    "status %d\n", __func__, status);
566759efa8b5SSam Leffler 		return EINVAL;
566859efa8b5SSam Leffler 	}
5669ca876918SSam Leffler 	(void) ath_hal_getregdomain(ah, &sc->sc_eerd);
5670ca876918SSam Leffler 	ath_hal_getcountrycode(ah, &sc->sc_eecc);	/* NB: cannot fail */
567159efa8b5SSam Leffler 	/* XXX map Atheros sku's to net80211 SKU's */
567259efa8b5SSam Leffler 	/* XXX net80211 types too small */
567359efa8b5SSam Leffler 	ic->ic_regdomain.regdomain = (uint16_t) sc->sc_eerd;
567459efa8b5SSam Leffler 	ic->ic_regdomain.country = (uint16_t) sc->sc_eecc;
567559efa8b5SSam Leffler 	ic->ic_regdomain.isocc[0] = ' ';	/* XXX don't know */
567659efa8b5SSam Leffler 	ic->ic_regdomain.isocc[1] = ' ';
567759efa8b5SSam Leffler 
5678b032f27cSSam Leffler 	ic->ic_regdomain.ecm = 1;
5679b032f27cSSam Leffler 	ic->ic_regdomain.location = 'I';
5680033022a9SSam Leffler 
5681033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
568259efa8b5SSam Leffler 	    "%s: eeprom rd %u cc %u (mapped rd %u cc %u) location %c%s\n",
5683033022a9SSam Leffler 	    __func__, sc->sc_eerd, sc->sc_eecc,
5684033022a9SSam Leffler 	    ic->ic_regdomain.regdomain, ic->ic_regdomain.country,
568559efa8b5SSam Leffler 	    ic->ic_regdomain.location, ic->ic_regdomain.ecm ? " ecm" : "");
56865591b213SSam Leffler 	return 0;
56875591b213SSam Leffler }
56885591b213SSam Leffler 
56896c4612b9SSam Leffler static int
56906c4612b9SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode)
56916c4612b9SSam Leffler {
56926c4612b9SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
56936c4612b9SSam Leffler 	const HAL_RATE_TABLE *rt;
56946c4612b9SSam Leffler 
56956c4612b9SSam Leffler 	switch (mode) {
56966c4612b9SSam Leffler 	case IEEE80211_MODE_11A:
56976c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A);
56986c4612b9SSam Leffler 		break;
5699724c193aSSam Leffler 	case IEEE80211_MODE_HALF:
5700aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_HALF_RATE);
5701aaa70f2fSSam Leffler 		break;
5702724c193aSSam Leffler 	case IEEE80211_MODE_QUARTER:
5703aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_QUARTER_RATE);
5704aaa70f2fSSam Leffler 		break;
57056c4612b9SSam Leffler 	case IEEE80211_MODE_11B:
57066c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11B);
57076c4612b9SSam Leffler 		break;
57086c4612b9SSam Leffler 	case IEEE80211_MODE_11G:
57096c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11G);
57106c4612b9SSam Leffler 		break;
57116c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_A:
571268e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108A);
57136c4612b9SSam Leffler 		break;
57146c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_G:
57156c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108G);
57166c4612b9SSam Leffler 		break;
571768e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
571868e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_TURBO);
571968e8e04eSSam Leffler 		break;
572068e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
572168e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NA_HT20);
572268e8e04eSSam Leffler 		break;
572368e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
572468e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NG_HT20);
572568e8e04eSSam Leffler 		break;
57266c4612b9SSam Leffler 	default:
57276c4612b9SSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
57286c4612b9SSam Leffler 			__func__, mode);
57296c4612b9SSam Leffler 		return 0;
57306c4612b9SSam Leffler 	}
57316c4612b9SSam Leffler 	sc->sc_rates[mode] = rt;
5732aaa70f2fSSam Leffler 	return (rt != NULL);
57335591b213SSam Leffler }
57345591b213SSam Leffler 
57355591b213SSam Leffler static void
57365591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
57375591b213SSam Leffler {
57383e50ec2cSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
57393e50ec2cSSam Leffler 	/* NB: on/off times from the Atheros NDIS driver, w/ permission */
57403e50ec2cSSam Leffler 	static const struct {
57413e50ec2cSSam Leffler 		u_int		rate;		/* tx/rx 802.11 rate */
57423e50ec2cSSam Leffler 		u_int16_t	timeOn;		/* LED on time (ms) */
57433e50ec2cSSam Leffler 		u_int16_t	timeOff;	/* LED off time (ms) */
57443e50ec2cSSam Leffler 	} blinkrates[] = {
57453e50ec2cSSam Leffler 		{ 108,  40,  10 },
57463e50ec2cSSam Leffler 		{  96,  44,  11 },
57473e50ec2cSSam Leffler 		{  72,  50,  13 },
57483e50ec2cSSam Leffler 		{  48,  57,  14 },
57493e50ec2cSSam Leffler 		{  36,  67,  16 },
57503e50ec2cSSam Leffler 		{  24,  80,  20 },
57513e50ec2cSSam Leffler 		{  22, 100,  25 },
57523e50ec2cSSam Leffler 		{  18, 133,  34 },
57533e50ec2cSSam Leffler 		{  12, 160,  40 },
57543e50ec2cSSam Leffler 		{  10, 200,  50 },
57553e50ec2cSSam Leffler 		{   6, 240,  58 },
57563e50ec2cSSam Leffler 		{   4, 267,  66 },
57573e50ec2cSSam Leffler 		{   2, 400, 100 },
57583e50ec2cSSam Leffler 		{   0, 500, 130 },
5759724c193aSSam Leffler 		/* XXX half/quarter rates */
57603e50ec2cSSam Leffler 	};
57615591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
57623e50ec2cSSam Leffler 	int i, j;
57635591b213SSam Leffler 
57645591b213SSam Leffler 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
57655591b213SSam Leffler 	rt = sc->sc_rates[mode];
57665591b213SSam Leffler 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
5767180f268dSSam Leffler 	for (i = 0; i < rt->rateCount; i++) {
5768180f268dSSam Leffler 		uint8_t ieeerate = rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
5769180f268dSSam Leffler 		if (rt->info[i].phy != IEEE80211_T_HT)
5770180f268dSSam Leffler 			sc->sc_rixmap[ieeerate] = i;
5771180f268dSSam Leffler 		else
5772180f268dSSam Leffler 			sc->sc_rixmap[ieeerate | IEEE80211_RATE_MCS] = i;
5773180f268dSSam Leffler 	}
57741b1a8e41SSam Leffler 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
577546d4d74cSSam Leffler 	for (i = 0; i < N(sc->sc_hwmap); i++) {
577646d4d74cSSam Leffler 		if (i >= rt->rateCount) {
57773e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledon = (500 * hz) / 1000;
57783e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledoff = (130 * hz) / 1000;
577916b4851aSSam Leffler 			continue;
57803e50ec2cSSam Leffler 		}
57813e50ec2cSSam Leffler 		sc->sc_hwmap[i].ieeerate =
578246d4d74cSSam Leffler 			rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
578346d4d74cSSam Leffler 		if (rt->info[i].phy == IEEE80211_T_HT)
578426041a14SSam Leffler 			sc->sc_hwmap[i].ieeerate |= IEEE80211_RATE_MCS;
5785d3be6f5bSSam Leffler 		sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
578646d4d74cSSam Leffler 		if (rt->info[i].shortPreamble ||
578746d4d74cSSam Leffler 		    rt->info[i].phy == IEEE80211_T_OFDM)
5788d3be6f5bSSam Leffler 			sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE;
57895463c4a4SSam Leffler 		sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags;
57903e50ec2cSSam Leffler 		for (j = 0; j < N(blinkrates)-1; j++)
57913e50ec2cSSam Leffler 			if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate)
57923e50ec2cSSam Leffler 				break;
57933e50ec2cSSam Leffler 		/* NB: this uses the last entry if the rate isn't found */
57943e50ec2cSSam Leffler 		/* XXX beware of overlow */
57953e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000;
57963e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000;
5797c42a7b7eSSam Leffler 	}
57985591b213SSam Leffler 	sc->sc_currates = rt;
57995591b213SSam Leffler 	sc->sc_curmode = mode;
58005591b213SSam Leffler 	/*
5801c42a7b7eSSam Leffler 	 * All protection frames are transmited at 2Mb/s for
5802c42a7b7eSSam Leffler 	 * 11g, otherwise at 1Mb/s.
58035591b213SSam Leffler 	 */
5804913a1ba1SSam Leffler 	if (mode == IEEE80211_MODE_11G)
5805ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*2);
5806913a1ba1SSam Leffler 	else
5807ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*1);
58084fa8d4efSDaniel Eischen 	/* NB: caller is responsible for resetting rate control state */
58093e50ec2cSSam Leffler #undef N
58105591b213SSam Leffler }
58115591b213SSam Leffler 
5812c42a7b7eSSam Leffler static void
58132e986da5SSam Leffler ath_watchdog(void *arg)
5814c42a7b7eSSam Leffler {
58152e986da5SSam Leffler 	struct ath_softc *sc = arg;
5816ef27340cSAdrian Chadd 	int do_reset = 0;
5817c42a7b7eSSam Leffler 
58182e986da5SSam Leffler 	if (sc->sc_wd_timer != 0 && --sc->sc_wd_timer == 0) {
58192e986da5SSam Leffler 		struct ifnet *ifp = sc->sc_ifp;
5820459bc4f0SSam Leffler 		uint32_t hangs;
5821459bc4f0SSam Leffler 
5822459bc4f0SSam Leffler 		if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) &&
5823459bc4f0SSam Leffler 		    hangs != 0) {
5824459bc4f0SSam Leffler 			if_printf(ifp, "%s hang detected (0x%x)\n",
5825459bc4f0SSam Leffler 			    hangs & 0xff ? "bb" : "mac", hangs);
5826459bc4f0SSam Leffler 		} else
5827c42a7b7eSSam Leffler 			if_printf(ifp, "device timeout\n");
5828ef27340cSAdrian Chadd 		do_reset = 1;
5829c42a7b7eSSam Leffler 		ifp->if_oerrors++;
5830c42a7b7eSSam Leffler 		sc->sc_stats.ast_watchdog++;
5831c42a7b7eSSam Leffler 	}
5832ef27340cSAdrian Chadd 
5833ef27340cSAdrian Chadd 	/*
5834ef27340cSAdrian Chadd 	 * We can't hold the lock across the ath_reset() call.
5835d52f7132SAdrian Chadd 	 *
5836d52f7132SAdrian Chadd 	 * And since this routine can't hold a lock and sleep,
5837d52f7132SAdrian Chadd 	 * do the reset deferred.
5838ef27340cSAdrian Chadd 	 */
5839ef27340cSAdrian Chadd 	if (do_reset) {
5840d52f7132SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
5841ef27340cSAdrian Chadd 	}
5842ef27340cSAdrian Chadd 
58432e986da5SSam Leffler 	callout_schedule(&sc->sc_wd_ch, hz);
5844c42a7b7eSSam Leffler }
5845c42a7b7eSSam Leffler 
5846b8f2a853SAdrian Chadd /*
5847b8f2a853SAdrian Chadd  * Fetch the rate control statistics for the given node.
5848b8f2a853SAdrian Chadd  */
5849b8f2a853SAdrian Chadd static int
5850b8f2a853SAdrian Chadd ath_ioctl_ratestats(struct ath_softc *sc, struct ath_rateioctl *rs)
5851b8f2a853SAdrian Chadd {
5852b8f2a853SAdrian Chadd 	struct ath_node *an;
5853b8f2a853SAdrian Chadd 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
5854b8f2a853SAdrian Chadd 	struct ieee80211_node *ni;
5855b8f2a853SAdrian Chadd 	int error = 0;
5856b8f2a853SAdrian Chadd 
5857b8f2a853SAdrian Chadd 	/* Perform a lookup on the given node */
5858b8f2a853SAdrian Chadd 	ni = ieee80211_find_node(&ic->ic_sta, rs->is_u.macaddr);
5859b8f2a853SAdrian Chadd 	if (ni == NULL) {
5860b8f2a853SAdrian Chadd 		error = EINVAL;
5861b8f2a853SAdrian Chadd 		goto bad;
5862b8f2a853SAdrian Chadd 	}
5863b8f2a853SAdrian Chadd 
5864b8f2a853SAdrian Chadd 	/* Lock the ath_node */
5865b8f2a853SAdrian Chadd 	an = ATH_NODE(ni);
5866b8f2a853SAdrian Chadd 	ATH_NODE_LOCK(an);
5867b8f2a853SAdrian Chadd 
5868b8f2a853SAdrian Chadd 	/* Fetch the rate control stats for this node */
5869b8f2a853SAdrian Chadd 	error = ath_rate_fetch_node_stats(sc, an, rs);
5870b8f2a853SAdrian Chadd 
5871b8f2a853SAdrian Chadd 	/* No matter what happens here, just drop through */
5872b8f2a853SAdrian Chadd 
5873b8f2a853SAdrian Chadd 	/* Unlock the ath_node */
5874b8f2a853SAdrian Chadd 	ATH_NODE_UNLOCK(an);
5875b8f2a853SAdrian Chadd 
5876b8f2a853SAdrian Chadd 	/* Unref the node */
5877b8f2a853SAdrian Chadd 	ieee80211_node_decref(ni);
5878b8f2a853SAdrian Chadd 
5879b8f2a853SAdrian Chadd bad:
5880b8f2a853SAdrian Chadd 	return (error);
5881b8f2a853SAdrian Chadd }
5882b8f2a853SAdrian Chadd 
5883a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
5884c42a7b7eSSam Leffler /*
5885c42a7b7eSSam Leffler  * Diagnostic interface to the HAL.  This is used by various
5886c42a7b7eSSam Leffler  * tools to do things like retrieve register contents for
5887c42a7b7eSSam Leffler  * debugging.  The mechanism is intentionally opaque so that
5888c42a7b7eSSam Leffler  * it can change frequently w/o concern for compatiblity.
5889c42a7b7eSSam Leffler  */
5890c42a7b7eSSam Leffler static int
5891c42a7b7eSSam Leffler ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
5892c42a7b7eSSam Leffler {
5893c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5894c42a7b7eSSam Leffler 	u_int id = ad->ad_id & ATH_DIAG_ID;
5895c42a7b7eSSam Leffler 	void *indata = NULL;
5896c42a7b7eSSam Leffler 	void *outdata = NULL;
5897c42a7b7eSSam Leffler 	u_int32_t insize = ad->ad_in_size;
5898c42a7b7eSSam Leffler 	u_int32_t outsize = ad->ad_out_size;
5899c42a7b7eSSam Leffler 	int error = 0;
5900c42a7b7eSSam Leffler 
5901c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_IN) {
5902c42a7b7eSSam Leffler 		/*
5903c42a7b7eSSam Leffler 		 * Copy in data.
5904c42a7b7eSSam Leffler 		 */
5905c42a7b7eSSam Leffler 		indata = malloc(insize, M_TEMP, M_NOWAIT);
5906c42a7b7eSSam Leffler 		if (indata == NULL) {
5907c42a7b7eSSam Leffler 			error = ENOMEM;
5908c42a7b7eSSam Leffler 			goto bad;
5909c42a7b7eSSam Leffler 		}
5910c42a7b7eSSam Leffler 		error = copyin(ad->ad_in_data, indata, insize);
5911c42a7b7eSSam Leffler 		if (error)
5912c42a7b7eSSam Leffler 			goto bad;
5913c42a7b7eSSam Leffler 	}
5914c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_DYN) {
5915c42a7b7eSSam Leffler 		/*
5916c42a7b7eSSam Leffler 		 * Allocate a buffer for the results (otherwise the HAL
5917c42a7b7eSSam Leffler 		 * returns a pointer to a buffer where we can read the
5918c42a7b7eSSam Leffler 		 * results).  Note that we depend on the HAL leaving this
5919c42a7b7eSSam Leffler 		 * pointer for us to use below in reclaiming the buffer;
5920c42a7b7eSSam Leffler 		 * may want to be more defensive.
5921c42a7b7eSSam Leffler 		 */
5922c42a7b7eSSam Leffler 		outdata = malloc(outsize, M_TEMP, M_NOWAIT);
5923c42a7b7eSSam Leffler 		if (outdata == NULL) {
5924c42a7b7eSSam Leffler 			error = ENOMEM;
5925c42a7b7eSSam Leffler 			goto bad;
5926c42a7b7eSSam Leffler 		}
5927c42a7b7eSSam Leffler 	}
5928c42a7b7eSSam Leffler 	if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) {
5929c42a7b7eSSam Leffler 		if (outsize < ad->ad_out_size)
5930c42a7b7eSSam Leffler 			ad->ad_out_size = outsize;
5931c42a7b7eSSam Leffler 		if (outdata != NULL)
5932c42a7b7eSSam Leffler 			error = copyout(outdata, ad->ad_out_data,
5933c42a7b7eSSam Leffler 					ad->ad_out_size);
5934c42a7b7eSSam Leffler 	} else {
5935c42a7b7eSSam Leffler 		error = EINVAL;
5936c42a7b7eSSam Leffler 	}
5937c42a7b7eSSam Leffler bad:
5938c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
5939c42a7b7eSSam Leffler 		free(indata, M_TEMP);
5940c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
5941c42a7b7eSSam Leffler 		free(outdata, M_TEMP);
5942c42a7b7eSSam Leffler 	return error;
5943c42a7b7eSSam Leffler }
5944a585a9a1SSam Leffler #endif /* ATH_DIAGAPI */
5945c42a7b7eSSam Leffler 
5946c42a7b7eSSam Leffler static int
5947c42a7b7eSSam Leffler ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
5948c42a7b7eSSam Leffler {
5949c42a7b7eSSam Leffler #define	IS_RUNNING(ifp) \
595013f4c340SRobert Watson 	((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
5951c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
5952b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
5953c42a7b7eSSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
595484784be1SSam Leffler 	const HAL_RATE_TABLE *rt;
5955c42a7b7eSSam Leffler 	int error = 0;
5956c42a7b7eSSam Leffler 
5957c42a7b7eSSam Leffler 	switch (cmd) {
5958c42a7b7eSSam Leffler 	case SIOCSIFFLAGS:
595931a8c1edSAndrew Thompson 		ATH_LOCK(sc);
5960c42a7b7eSSam Leffler 		if (IS_RUNNING(ifp)) {
5961c42a7b7eSSam Leffler 			/*
5962c42a7b7eSSam Leffler 			 * To avoid rescanning another access point,
5963c42a7b7eSSam Leffler 			 * do not call ath_init() here.  Instead,
5964c42a7b7eSSam Leffler 			 * only reflect promisc mode settings.
5965c42a7b7eSSam Leffler 			 */
5966c42a7b7eSSam Leffler 			ath_mode_init(sc);
5967c42a7b7eSSam Leffler 		} else if (ifp->if_flags & IFF_UP) {
5968c42a7b7eSSam Leffler 			/*
5969c42a7b7eSSam Leffler 			 * Beware of being called during attach/detach
5970c42a7b7eSSam Leffler 			 * to reset promiscuous mode.  In that case we
5971c42a7b7eSSam Leffler 			 * will still be marked UP but not RUNNING.
5972c42a7b7eSSam Leffler 			 * However trying to re-init the interface
5973c42a7b7eSSam Leffler 			 * is the wrong thing to do as we've already
5974c42a7b7eSSam Leffler 			 * torn down much of our state.  There's
5975c42a7b7eSSam Leffler 			 * probably a better way to deal with this.
5976c42a7b7eSSam Leffler 			 */
5977b032f27cSSam Leffler 			if (!sc->sc_invalid)
5978fc74a9f9SBrooks Davis 				ath_init(sc);	/* XXX lose error */
5979d3ac945bSSam Leffler 		} else {
5980c42a7b7eSSam Leffler 			ath_stop_locked(ifp);
5981d3ac945bSSam Leffler #ifdef notyet
5982d3ac945bSSam Leffler 			/* XXX must wakeup in places like ath_vap_delete */
5983d3ac945bSSam Leffler 			if (!sc->sc_invalid)
5984d3ac945bSSam Leffler 				ath_hal_setpower(sc->sc_ah, HAL_PM_FULL_SLEEP);
5985d3ac945bSSam Leffler #endif
5986d3ac945bSSam Leffler 		}
598731a8c1edSAndrew Thompson 		ATH_UNLOCK(sc);
5988c42a7b7eSSam Leffler 		break;
5989b032f27cSSam Leffler 	case SIOCGIFMEDIA:
5990b032f27cSSam Leffler 	case SIOCSIFMEDIA:
5991b032f27cSSam Leffler 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
5992b032f27cSSam Leffler 		break;
5993c42a7b7eSSam Leffler 	case SIOCGATHSTATS:
5994c42a7b7eSSam Leffler 		/* NB: embed these numbers to get a consistent view */
5995c42a7b7eSSam Leffler 		sc->sc_stats.ast_tx_packets = ifp->if_opackets;
5996c42a7b7eSSam Leffler 		sc->sc_stats.ast_rx_packets = ifp->if_ipackets;
599784784be1SSam Leffler 		sc->sc_stats.ast_tx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgtxrssi);
599884784be1SSam Leffler 		sc->sc_stats.ast_rx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgrssi);
5999584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
600010ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsfadjp = TDMA_AVG(sc->sc_avgtsfdeltap);
600110ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsfadjm = TDMA_AVG(sc->sc_avgtsfdeltam);
600210ad9a77SSam Leffler #endif
600384784be1SSam Leffler 		rt = sc->sc_currates;
600446d4d74cSSam Leffler 		sc->sc_stats.ast_tx_rate =
600546d4d74cSSam Leffler 		    rt->info[sc->sc_txrix].dot11Rate &~ IEEE80211_RATE_BASIC;
60066aa113fdSAdrian Chadd 		if (rt->info[sc->sc_txrix].phy & IEEE80211_T_HT)
60076aa113fdSAdrian Chadd 			sc->sc_stats.ast_tx_rate |= IEEE80211_RATE_MCS;
6008c42a7b7eSSam Leffler 		return copyout(&sc->sc_stats,
6009c42a7b7eSSam Leffler 		    ifr->ifr_data, sizeof (sc->sc_stats));
601094fe37d2SAdrian Chadd 	case SIOCGATHAGSTATS:
601194fe37d2SAdrian Chadd 		return copyout(&sc->sc_aggr_stats,
601294fe37d2SAdrian Chadd 		    ifr->ifr_data, sizeof (sc->sc_aggr_stats));
60133fc21fedSSam Leffler 	case SIOCZATHSTATS:
60143fc21fedSSam Leffler 		error = priv_check(curthread, PRIV_DRIVER);
60159467e3f3SAdrian Chadd 		if (error == 0) {
60163fc21fedSSam Leffler 			memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
601741b6b507SAdrian Chadd 			memset(&sc->sc_aggr_stats, 0,
601841b6b507SAdrian Chadd 			    sizeof(sc->sc_aggr_stats));
60199467e3f3SAdrian Chadd 			memset(&sc->sc_intr_stats, 0,
60209467e3f3SAdrian Chadd 			    sizeof(sc->sc_intr_stats));
60219467e3f3SAdrian Chadd 		}
60223fc21fedSSam Leffler 		break;
6023a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
6024c42a7b7eSSam Leffler 	case SIOCGATHDIAG:
6025c42a7b7eSSam Leffler 		error = ath_ioctl_diag(sc, (struct ath_diag *) ifr);
6026c42a7b7eSSam Leffler 		break;
6027f51c84eaSAdrian Chadd 	case SIOCGATHPHYERR:
6028f51c84eaSAdrian Chadd 		error = ath_ioctl_phyerr(sc,(struct ath_diag*) ifr);
6029f51c84eaSAdrian Chadd 		break;
6030a585a9a1SSam Leffler #endif
60319af351f9SAdrian Chadd 	case SIOCGATHSPECTRAL:
60329af351f9SAdrian Chadd 		error = ath_ioctl_spectral(sc,(struct ath_diag*) ifr);
60339af351f9SAdrian Chadd 		break;
6034b8f2a853SAdrian Chadd 	case SIOCGATHNODERATESTATS:
6035b8f2a853SAdrian Chadd 		error = ath_ioctl_ratestats(sc, (struct ath_rateioctl *) ifr);
6036b8f2a853SAdrian Chadd 		break;
603731a8c1edSAndrew Thompson 	case SIOCGIFADDR:
6038b032f27cSSam Leffler 		error = ether_ioctl(ifp, cmd, data);
6039c42a7b7eSSam Leffler 		break;
604031a8c1edSAndrew Thompson 	default:
604131a8c1edSAndrew Thompson 		error = EINVAL;
604231a8c1edSAndrew Thompson 		break;
6043c42a7b7eSSam Leffler 	}
6044c42a7b7eSSam Leffler 	return error;
6045a614e076SSam Leffler #undef IS_RUNNING
6046c42a7b7eSSam Leffler }
6047c42a7b7eSSam Leffler 
6048c42a7b7eSSam Leffler /*
6049c42a7b7eSSam Leffler  * Announce various information on device/driver attach.
6050c42a7b7eSSam Leffler  */
6051c42a7b7eSSam Leffler static void
6052c42a7b7eSSam Leffler ath_announce(struct ath_softc *sc)
6053c42a7b7eSSam Leffler {
6054fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
6055c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6056c42a7b7eSSam Leffler 
6057498657cfSSam Leffler 	if_printf(ifp, "AR%s mac %d.%d RF%s phy %d.%d\n",
6058498657cfSSam Leffler 		ath_hal_mac_name(ah), ah->ah_macVersion, ah->ah_macRev,
6059498657cfSSam Leffler 		ath_hal_rf_name(ah), ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
606046a924c4SAdrian Chadd 	if_printf(ifp, "2GHz radio: 0x%.4x; 5GHz radio: 0x%.4x\n",
606146a924c4SAdrian Chadd 		ah->ah_analog2GhzRev, ah->ah_analog5GhzRev);
6062c42a7b7eSSam Leffler 	if (bootverbose) {
6063c42a7b7eSSam Leffler 		int i;
6064c42a7b7eSSam Leffler 		for (i = 0; i <= WME_AC_VO; i++) {
6065c42a7b7eSSam Leffler 			struct ath_txq *txq = sc->sc_ac2q[i];
6066c42a7b7eSSam Leffler 			if_printf(ifp, "Use hw queue %u for %s traffic\n",
6067c42a7b7eSSam Leffler 				txq->axq_qnum, ieee80211_wme_acnames[i]);
6068c42a7b7eSSam Leffler 		}
6069c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for CAB traffic\n",
6070c42a7b7eSSam Leffler 			sc->sc_cabq->axq_qnum);
6071c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for beacons\n", sc->sc_bhalq);
6072c42a7b7eSSam Leffler 	}
6073e2d787faSSam Leffler 	if (ath_rxbuf != ATH_RXBUF)
6074e2d787faSSam Leffler 		if_printf(ifp, "using %u rx buffers\n", ath_rxbuf);
6075e2d787faSSam Leffler 	if (ath_txbuf != ATH_TXBUF)
6076e2d787faSSam Leffler 		if_printf(ifp, "using %u tx buffers\n", ath_txbuf);
60779ac01d39SRui Paulo 	if (sc->sc_mcastkey && bootverbose)
60789ac01d39SRui Paulo 		if_printf(ifp, "using multicast key search\n");
6079c42a7b7eSSam Leffler }
608010ad9a77SSam Leffler 
608148237774SAdrian Chadd static void
608248237774SAdrian Chadd ath_dfs_tasklet(void *p, int npending)
608348237774SAdrian Chadd {
608448237774SAdrian Chadd 	struct ath_softc *sc = (struct ath_softc *) p;
608548237774SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
608648237774SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
608748237774SAdrian Chadd 
608848237774SAdrian Chadd 	/*
608948237774SAdrian Chadd 	 * If previous processing has found a radar event,
609048237774SAdrian Chadd 	 * signal this to the net80211 layer to begin DFS
609148237774SAdrian Chadd 	 * processing.
609248237774SAdrian Chadd 	 */
609348237774SAdrian Chadd 	if (ath_dfs_process_radar_event(sc, sc->sc_curchan)) {
609448237774SAdrian Chadd 		/* DFS event found, initiate channel change */
609506fc4a10SAdrian Chadd 		/*
609606fc4a10SAdrian Chadd 		 * XXX doesn't currently tell us whether the event
609706fc4a10SAdrian Chadd 		 * XXX was found in the primary or extension
609806fc4a10SAdrian Chadd 		 * XXX channel!
609906fc4a10SAdrian Chadd 		 */
610006fc4a10SAdrian Chadd 		IEEE80211_LOCK(ic);
610148237774SAdrian Chadd 		ieee80211_dfs_notify_radar(ic, sc->sc_curchan);
610206fc4a10SAdrian Chadd 		IEEE80211_UNLOCK(ic);
610348237774SAdrian Chadd 	}
610448237774SAdrian Chadd }
610548237774SAdrian Chadd 
61060eb81626SAdrian Chadd /*
61070eb81626SAdrian Chadd  * Enable/disable power save.  This must be called with
61080eb81626SAdrian Chadd  * no TX driver locks currently held, so it should only
61090eb81626SAdrian Chadd  * be called from the RX path (which doesn't hold any
61100eb81626SAdrian Chadd  * TX driver locks.)
61110eb81626SAdrian Chadd  */
61120eb81626SAdrian Chadd static void
61130eb81626SAdrian Chadd ath_node_powersave(struct ieee80211_node *ni, int enable)
61140eb81626SAdrian Chadd {
6115bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
61160eb81626SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
61170eb81626SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
61180eb81626SAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
61190eb81626SAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
61200eb81626SAdrian Chadd 
61210eb81626SAdrian Chadd 	/* XXX and no TXQ locks should be held here */
61220eb81626SAdrian Chadd 
61239b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, "%s: %6D: enable=%d\n",
61249b48fb4bSAdrian Chadd 	    __func__,
61259b48fb4bSAdrian Chadd 	    ni->ni_macaddr,
61269b48fb4bSAdrian Chadd 	    ":",
61279b48fb4bSAdrian Chadd 	    !! enable);
61280eb81626SAdrian Chadd 
61290eb81626SAdrian Chadd 	/* Suspend or resume software queue handling */
61300eb81626SAdrian Chadd 	if (enable)
61310eb81626SAdrian Chadd 		ath_tx_node_sleep(sc, an);
61320eb81626SAdrian Chadd 	else
61330eb81626SAdrian Chadd 		ath_tx_node_wakeup(sc, an);
61340eb81626SAdrian Chadd 
61350eb81626SAdrian Chadd 	/* Update net80211 state */
61360eb81626SAdrian Chadd 	avp->av_node_ps(ni, enable);
6137bdbb6e5bSAdrian Chadd #else
6138bdbb6e5bSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6139bdbb6e5bSAdrian Chadd 
6140bdbb6e5bSAdrian Chadd 	/* Update net80211 state */
6141bdbb6e5bSAdrian Chadd 	avp->av_node_ps(ni, enable);
6142bdbb6e5bSAdrian Chadd #endif/* ATH_SW_PSQ */
61430eb81626SAdrian Chadd }
61440eb81626SAdrian Chadd 
6145548a605dSAdrian Chadd /*
6146548a605dSAdrian Chadd  * Notification from net80211 that the powersave queue state has
6147548a605dSAdrian Chadd  * changed.
6148548a605dSAdrian Chadd  *
6149548a605dSAdrian Chadd  * Since the software queue also may have some frames:
6150548a605dSAdrian Chadd  *
6151548a605dSAdrian Chadd  * + if the node software queue has frames and the TID state
6152548a605dSAdrian Chadd  *   is 0, we set the TIM;
6153548a605dSAdrian Chadd  * + if the node and the stack are both empty, we clear the TIM bit.
6154548a605dSAdrian Chadd  * + If the stack tries to set the bit, always set it.
6155548a605dSAdrian Chadd  * + If the stack tries to clear the bit, only clear it if the
6156548a605dSAdrian Chadd  *   software queue in question is also cleared.
6157548a605dSAdrian Chadd  *
6158548a605dSAdrian Chadd  * TODO: this is called during node teardown; so let's ensure this
6159548a605dSAdrian Chadd  * is all correctly handled and that the TIM bit is cleared.
6160548a605dSAdrian Chadd  * It may be that the node flush is called _AFTER_ the net80211
6161548a605dSAdrian Chadd  * stack clears the TIM.
6162548a605dSAdrian Chadd  *
6163548a605dSAdrian Chadd  * Here is the racy part.  Since it's possible >1 concurrent,
6164548a605dSAdrian Chadd  * overlapping TXes will appear complete with a TX completion in
6165548a605dSAdrian Chadd  * another thread, it's possible that the concurrent TIM calls will
6166548a605dSAdrian Chadd  * clash.  We can't hold the node lock here because setting the
6167548a605dSAdrian Chadd  * TIM grabs the net80211 comlock and this may cause a LOR.
6168548a605dSAdrian Chadd  * The solution is either to totally serialise _everything_ at
6169548a605dSAdrian Chadd  * this point (ie, all TX, completion and any reset/flush go into
6170548a605dSAdrian Chadd  * one taskqueue) or a new "ath TIM lock" needs to be created that
6171548a605dSAdrian Chadd  * just wraps the driver state change and this call to avp->av_set_tim().
6172548a605dSAdrian Chadd  *
6173548a605dSAdrian Chadd  * The same race exists in the net80211 power save queue handling
6174548a605dSAdrian Chadd  * as well.  Since multiple transmitting threads may queue frames
6175548a605dSAdrian Chadd  * into the driver, as well as ps-poll and the driver transmitting
6176548a605dSAdrian Chadd  * frames (and thus clearing the psq), it's quite possible that
6177548a605dSAdrian Chadd  * a packet entering the PSQ and a ps-poll being handled will
6178548a605dSAdrian Chadd  * race, causing the TIM to be cleared and not re-set.
6179548a605dSAdrian Chadd  */
6180548a605dSAdrian Chadd static int
6181548a605dSAdrian Chadd ath_node_set_tim(struct ieee80211_node *ni, int enable)
6182548a605dSAdrian Chadd {
6183bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
6184548a605dSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
6185548a605dSAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
6186548a605dSAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
6187548a605dSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6188548a605dSAdrian Chadd 	int changed = 0;
6189548a605dSAdrian Chadd 
61904bed2b67SAdrian Chadd 	ATH_TX_LOCK(sc);
6191548a605dSAdrian Chadd 	an->an_stack_psq = enable;
6192548a605dSAdrian Chadd 
6193548a605dSAdrian Chadd 	/*
6194548a605dSAdrian Chadd 	 * This will get called for all operating modes,
6195548a605dSAdrian Chadd 	 * even if avp->av_set_tim is unset.
6196548a605dSAdrian Chadd 	 * It's currently set for hostap/ibss modes; but
6197548a605dSAdrian Chadd 	 * the same infrastructure is used for both STA
6198548a605dSAdrian Chadd 	 * and AP/IBSS node power save.
6199548a605dSAdrian Chadd 	 */
6200548a605dSAdrian Chadd 	if (avp->av_set_tim == NULL) {
62014bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6202548a605dSAdrian Chadd 		return (0);
6203548a605dSAdrian Chadd 	}
6204548a605dSAdrian Chadd 
6205548a605dSAdrian Chadd 	/*
6206548a605dSAdrian Chadd 	 * If setting the bit, always set it here.
6207548a605dSAdrian Chadd 	 * If clearing the bit, only clear it if the
6208548a605dSAdrian Chadd 	 * software queue is also empty.
6209548a605dSAdrian Chadd 	 *
6210548a605dSAdrian Chadd 	 * If the node has left power save, just clear the TIM
6211548a605dSAdrian Chadd 	 * bit regardless of the state of the power save queue.
6212548a605dSAdrian Chadd 	 *
6213548a605dSAdrian Chadd 	 * XXX TODO: although atomics are used, it's quite possible
6214548a605dSAdrian Chadd 	 * that a race will occur between this and setting/clearing
6215548a605dSAdrian Chadd 	 * in another thread.  TX completion will occur always in
6216548a605dSAdrian Chadd 	 * one thread, however setting/clearing the TIM bit can come
6217548a605dSAdrian Chadd 	 * from a variety of different process contexts!
6218548a605dSAdrian Chadd 	 */
6219548a605dSAdrian Chadd 	if (enable && an->an_tim_set == 1) {
6220548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
62219b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, tim_set=1, ignoring\n",
62229b48fb4bSAdrian Chadd 		    __func__,
62239b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
62249b48fb4bSAdrian Chadd 		    ":",
62259b48fb4bSAdrian Chadd 		    enable);
62264bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6227548a605dSAdrian Chadd 	} else if (enable) {
6228548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
62299b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, enabling TIM\n",
62309b48fb4bSAdrian Chadd 		    __func__,
62319b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
62329b48fb4bSAdrian Chadd 		    ":",
62339b48fb4bSAdrian Chadd 		    enable);
6234548a605dSAdrian Chadd 		an->an_tim_set = 1;
62354bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6236548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6237ba83edd4SAdrian Chadd 	} else if (an->an_swq_depth == 0) {
6238548a605dSAdrian Chadd 		/* disable */
6239548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
62409b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_swq_depth == 0, disabling\n",
62419b48fb4bSAdrian Chadd 		    __func__,
62429b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
62439b48fb4bSAdrian Chadd 		    ":",
62449b48fb4bSAdrian Chadd 		    enable);
6245548a605dSAdrian Chadd 		an->an_tim_set = 0;
62464bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6247548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6248548a605dSAdrian Chadd 	} else if (! an->an_is_powersave) {
6249548a605dSAdrian Chadd 		/*
6250548a605dSAdrian Chadd 		 * disable regardless; the node isn't in powersave now
6251548a605dSAdrian Chadd 		 */
6252548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
62539b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_pwrsave=0, disabling\n",
62549b48fb4bSAdrian Chadd 		    __func__,
62559b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
62569b48fb4bSAdrian Chadd 		    ":",
62579b48fb4bSAdrian Chadd 		    enable);
6258548a605dSAdrian Chadd 		an->an_tim_set = 0;
62594bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6260548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6261548a605dSAdrian Chadd 	} else {
6262548a605dSAdrian Chadd 		/*
6263548a605dSAdrian Chadd 		 * psq disable, node is currently in powersave, node
6264548a605dSAdrian Chadd 		 * software queue isn't empty, so don't clear the TIM bit
6265548a605dSAdrian Chadd 		 * for now.
6266548a605dSAdrian Chadd 		 */
62674bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6268548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
62699b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_swq_depth > 0, ignoring\n",
62709b48fb4bSAdrian Chadd 		    __func__,
62719b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
62729b48fb4bSAdrian Chadd 		    ":",
62739b48fb4bSAdrian Chadd 		    enable);
6274548a605dSAdrian Chadd 		changed = 0;
6275548a605dSAdrian Chadd 	}
6276548a605dSAdrian Chadd 
6277548a605dSAdrian Chadd 	return (changed);
6278bdbb6e5bSAdrian Chadd #else
6279bdbb6e5bSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6280bdbb6e5bSAdrian Chadd 
628160328038SAdrian Chadd 	/*
6282661c81c3SBaptiste Daroussin 	 * Some operating modes don't set av_set_tim(), so don't
628360328038SAdrian Chadd 	 * update it here.
628460328038SAdrian Chadd 	 */
628560328038SAdrian Chadd 	if (avp->av_set_tim == NULL)
628660328038SAdrian Chadd 		return (0);
628760328038SAdrian Chadd 
6288bdbb6e5bSAdrian Chadd 	return (avp->av_set_tim(ni, enable));
6289bdbb6e5bSAdrian Chadd #endif /* ATH_SW_PSQ */
6290548a605dSAdrian Chadd }
6291548a605dSAdrian Chadd 
6292548a605dSAdrian Chadd /*
6293548a605dSAdrian Chadd  * Set or update the TIM from the software queue.
6294548a605dSAdrian Chadd  *
6295548a605dSAdrian Chadd  * Check the software queue depth before attempting to do lock
6296548a605dSAdrian Chadd  * anything; that avoids trying to obtain the lock.  Then,
6297548a605dSAdrian Chadd  * re-check afterwards to ensure nothing has changed in the
6298548a605dSAdrian Chadd  * meantime.
6299548a605dSAdrian Chadd  *
6300548a605dSAdrian Chadd  * set:   This is designed to be called from the TX path, after
6301548a605dSAdrian Chadd  *        a frame has been queued; to see if the swq > 0.
6302548a605dSAdrian Chadd  *
6303548a605dSAdrian Chadd  * clear: This is designed to be called from the buffer completion point
6304548a605dSAdrian Chadd  *        (right now it's ath_tx_default_comp()) where the state of
6305548a605dSAdrian Chadd  *        a software queue has changed.
6306548a605dSAdrian Chadd  *
6307548a605dSAdrian Chadd  * It makes sense to place it at buffer free / completion rather
6308548a605dSAdrian Chadd  * than after each software queue operation, as there's no real
6309548a605dSAdrian Chadd  * point in churning the TIM bit as the last frames in the software
6310548a605dSAdrian Chadd  * queue are transmitted.  If they fail and we retry them, we'd
6311548a605dSAdrian Chadd  * just be setting the TIM bit again anyway.
6312548a605dSAdrian Chadd  */
6313548a605dSAdrian Chadd void
6314548a605dSAdrian Chadd ath_tx_update_tim(struct ath_softc *sc, struct ieee80211_node *ni,
6315548a605dSAdrian Chadd      int enable)
6316548a605dSAdrian Chadd {
6317bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
6318548a605dSAdrian Chadd 	struct ath_node *an;
6319548a605dSAdrian Chadd 	struct ath_vap *avp;
6320548a605dSAdrian Chadd 
6321548a605dSAdrian Chadd 	/* Don't do this for broadcast/etc frames */
6322548a605dSAdrian Chadd 	if (ni == NULL)
6323548a605dSAdrian Chadd 		return;
6324548a605dSAdrian Chadd 
6325548a605dSAdrian Chadd 	an = ATH_NODE(ni);
6326548a605dSAdrian Chadd 	avp = ATH_VAP(ni->ni_vap);
6327548a605dSAdrian Chadd 
6328548a605dSAdrian Chadd 	/*
6329548a605dSAdrian Chadd 	 * And for operating modes without the TIM handler set, let's
6330548a605dSAdrian Chadd 	 * just skip those.
6331548a605dSAdrian Chadd 	 */
6332548a605dSAdrian Chadd 	if (avp->av_set_tim == NULL)
6333548a605dSAdrian Chadd 		return;
6334548a605dSAdrian Chadd 
63354bed2b67SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
6336548a605dSAdrian Chadd 
6337548a605dSAdrian Chadd 	if (enable) {
6338548a605dSAdrian Chadd 		if (an->an_is_powersave &&
6339548a605dSAdrian Chadd 		    an->an_tim_set == 0 &&
6340ba83edd4SAdrian Chadd 		    an->an_swq_depth != 0) {
6341548a605dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
63429b48fb4bSAdrian Chadd 			    "%s: %6D: swq_depth>0, tim_set=0, set!\n",
63439b48fb4bSAdrian Chadd 			    __func__,
63449b48fb4bSAdrian Chadd 			    ni->ni_macaddr,
63459b48fb4bSAdrian Chadd 			    ":");
6346548a605dSAdrian Chadd 			an->an_tim_set = 1;
6347548a605dSAdrian Chadd 			(void) avp->av_set_tim(ni, 1);
6348548a605dSAdrian Chadd 		}
6349548a605dSAdrian Chadd 	} else {
6350548a605dSAdrian Chadd 		/*
6351548a605dSAdrian Chadd 		 * Don't bother grabbing the lock unless the queue is empty.
6352548a605dSAdrian Chadd 		 */
6353ba83edd4SAdrian Chadd 		if (&an->an_swq_depth != 0)
6354548a605dSAdrian Chadd 			return;
6355548a605dSAdrian Chadd 
6356548a605dSAdrian Chadd 		if (an->an_is_powersave &&
6357548a605dSAdrian Chadd 		    an->an_stack_psq == 0 &&
6358548a605dSAdrian Chadd 		    an->an_tim_set == 1 &&
6359ba83edd4SAdrian Chadd 		    an->an_swq_depth == 0) {
6360548a605dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
636122a3aee6SAdrian Chadd 			    "%s: %6D: swq_depth=0, tim_set=1, psq_set=0,"
6362548a605dSAdrian Chadd 			    " clear!\n",
636322a3aee6SAdrian Chadd 			    __func__,
636422a3aee6SAdrian Chadd 			    ni->ni_macaddr,
636522a3aee6SAdrian Chadd 			    ":");
6366548a605dSAdrian Chadd 			an->an_tim_set = 0;
6367548a605dSAdrian Chadd 			(void) avp->av_set_tim(ni, 0);
6368548a605dSAdrian Chadd 		}
6369548a605dSAdrian Chadd 	}
6370bdbb6e5bSAdrian Chadd #else
6371bdbb6e5bSAdrian Chadd 	return;
6372bdbb6e5bSAdrian Chadd #endif	/* ATH_SW_PSQ */
6373548a605dSAdrian Chadd }
63740eb81626SAdrian Chadd 
637522a3aee6SAdrian Chadd /*
637622a3aee6SAdrian Chadd  * Received a ps-poll frame from net80211.
637722a3aee6SAdrian Chadd  *
637822a3aee6SAdrian Chadd  * Here we get a chance to serve out a software-queued frame ourselves
637922a3aee6SAdrian Chadd  * before we punt it to net80211 to transmit us one itself - either
638022a3aee6SAdrian Chadd  * because there's traffic in the net80211 psq, or a NULL frame to
638122a3aee6SAdrian Chadd  * indicate there's nothing else.
638222a3aee6SAdrian Chadd  */
638322a3aee6SAdrian Chadd static void
638422a3aee6SAdrian Chadd ath_node_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m)
638522a3aee6SAdrian Chadd {
638622a3aee6SAdrian Chadd #ifdef	ATH_SW_PSQ
638722a3aee6SAdrian Chadd 	struct ath_node *an;
638822a3aee6SAdrian Chadd 	struct ath_vap *avp;
638922a3aee6SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
639022a3aee6SAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
639122a3aee6SAdrian Chadd 	int tid;
639222a3aee6SAdrian Chadd 
639322a3aee6SAdrian Chadd 	/* Just paranoia */
639422a3aee6SAdrian Chadd 	if (ni == NULL)
639522a3aee6SAdrian Chadd 		return;
639622a3aee6SAdrian Chadd 
639722a3aee6SAdrian Chadd 	/*
639822a3aee6SAdrian Chadd 	 * Unassociated (temporary node) station.
639922a3aee6SAdrian Chadd 	 */
640022a3aee6SAdrian Chadd 	if (ni->ni_associd == 0)
640122a3aee6SAdrian Chadd 		return;
640222a3aee6SAdrian Chadd 
640322a3aee6SAdrian Chadd 	/*
640422a3aee6SAdrian Chadd 	 * We do have an active node, so let's begin looking into it.
640522a3aee6SAdrian Chadd 	 */
640622a3aee6SAdrian Chadd 	an = ATH_NODE(ni);
640722a3aee6SAdrian Chadd 	avp = ATH_VAP(ni->ni_vap);
640822a3aee6SAdrian Chadd 
640922a3aee6SAdrian Chadd 	/*
641022a3aee6SAdrian Chadd 	 * For now, we just call the original ps-poll method.
641122a3aee6SAdrian Chadd 	 * Once we're ready to flip this on:
641222a3aee6SAdrian Chadd 	 *
641322a3aee6SAdrian Chadd 	 * + Set leak to 1, as no matter what we're going to have
641422a3aee6SAdrian Chadd 	 *   to send a frame;
641522a3aee6SAdrian Chadd 	 * + Check the software queue and if there's something in it,
641622a3aee6SAdrian Chadd 	 *   schedule the highest TID thas has traffic from this node.
641722a3aee6SAdrian Chadd 	 *   Then make sure we schedule the software scheduler to
641822a3aee6SAdrian Chadd 	 *   run so it picks up said frame.
641922a3aee6SAdrian Chadd 	 *
642022a3aee6SAdrian Chadd 	 * That way whatever happens, we'll at least send _a_ frame
642122a3aee6SAdrian Chadd 	 * to the given node.
642222a3aee6SAdrian Chadd 	 *
642322a3aee6SAdrian Chadd 	 * Again, yes, it's crappy QoS if the node has multiple
642422a3aee6SAdrian Chadd 	 * TIDs worth of traffic - but let's get it working first
642522a3aee6SAdrian Chadd 	 * before we optimise it.
642622a3aee6SAdrian Chadd 	 *
642722a3aee6SAdrian Chadd 	 * Also yes, there's definitely latency here - we're not
642822a3aee6SAdrian Chadd 	 * direct dispatching to the hardware in this path (and
642922a3aee6SAdrian Chadd 	 * we're likely being called from the packet receive path,
643022a3aee6SAdrian Chadd 	 * so going back into TX may be a little hairy!) but again
643122a3aee6SAdrian Chadd 	 * I'd like to get this working first before optimising
643222a3aee6SAdrian Chadd 	 * turn-around time.
643322a3aee6SAdrian Chadd 	 */
643422a3aee6SAdrian Chadd 
643522a3aee6SAdrian Chadd 	ATH_TX_LOCK(sc);
643622a3aee6SAdrian Chadd 
643722a3aee6SAdrian Chadd 	/*
643822a3aee6SAdrian Chadd 	 * Legacy - we're called and the node isn't asleep.
643922a3aee6SAdrian Chadd 	 * Immediately punt.
644022a3aee6SAdrian Chadd 	 */
644122a3aee6SAdrian Chadd 	if (! an->an_is_powersave) {
644222a3aee6SAdrian Chadd 		device_printf(sc->sc_dev,
644322a3aee6SAdrian Chadd 		    "%s: %6D: not in powersave?\n",
644422a3aee6SAdrian Chadd 		    __func__,
644522a3aee6SAdrian Chadd 		    ni->ni_macaddr,
644622a3aee6SAdrian Chadd 		    ":");
644722a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
644822a3aee6SAdrian Chadd 		avp->av_recv_pspoll(ni, m);
644922a3aee6SAdrian Chadd 		return;
645022a3aee6SAdrian Chadd 	}
645122a3aee6SAdrian Chadd 
645222a3aee6SAdrian Chadd 	/*
645322a3aee6SAdrian Chadd 	 * We're in powersave.
645422a3aee6SAdrian Chadd 	 *
645522a3aee6SAdrian Chadd 	 * Leak a frame.
645622a3aee6SAdrian Chadd 	 */
645722a3aee6SAdrian Chadd 	an->an_leak_count = 1;
645822a3aee6SAdrian Chadd 
645922a3aee6SAdrian Chadd 	/*
646022a3aee6SAdrian Chadd 	 * Now, if there's no frames in the node, just punt to
646122a3aee6SAdrian Chadd 	 * recv_pspoll.
646222a3aee6SAdrian Chadd 	 *
646322a3aee6SAdrian Chadd 	 * Don't bother checking if the TIM bit is set, we really
646422a3aee6SAdrian Chadd 	 * only care if there are any frames here!
646522a3aee6SAdrian Chadd 	 */
646622a3aee6SAdrian Chadd 	if (an->an_swq_depth == 0) {
646722a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
646822a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
646922a3aee6SAdrian Chadd 		    "%s: %6D: SWQ empty; punting to net80211\n",
647022a3aee6SAdrian Chadd 		    __func__,
647122a3aee6SAdrian Chadd 		    ni->ni_macaddr,
647222a3aee6SAdrian Chadd 		    ":");
647322a3aee6SAdrian Chadd 		avp->av_recv_pspoll(ni, m);
647422a3aee6SAdrian Chadd 		return;
647522a3aee6SAdrian Chadd 	}
647622a3aee6SAdrian Chadd 
647722a3aee6SAdrian Chadd 	/*
647822a3aee6SAdrian Chadd 	 * Ok, let's schedule the highest TID that has traffic
647922a3aee6SAdrian Chadd 	 * and then schedule something.
648022a3aee6SAdrian Chadd 	 */
648122a3aee6SAdrian Chadd 	for (tid = IEEE80211_TID_SIZE - 1; tid >= 0; tid--) {
648222a3aee6SAdrian Chadd 		struct ath_tid *atid = &an->an_tid[tid];
648322a3aee6SAdrian Chadd 		/*
648422a3aee6SAdrian Chadd 		 * No frames? Skip.
648522a3aee6SAdrian Chadd 		 */
648622a3aee6SAdrian Chadd 		if (atid->axq_depth == 0)
648722a3aee6SAdrian Chadd 			continue;
648822a3aee6SAdrian Chadd 		ath_tx_tid_sched(sc, atid);
648922a3aee6SAdrian Chadd 		/*
649022a3aee6SAdrian Chadd 		 * XXX we could do a direct call to the TXQ
649122a3aee6SAdrian Chadd 		 * scheduler code here to optimise latency
649222a3aee6SAdrian Chadd 		 * at the expense of a REALLY deep callstack.
649322a3aee6SAdrian Chadd 		 */
649422a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
649522a3aee6SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_txqtask);
649622a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
649722a3aee6SAdrian Chadd 		    "%s: %6D: leaking frame to TID %d\n",
649822a3aee6SAdrian Chadd 		    __func__,
649922a3aee6SAdrian Chadd 		    ni->ni_macaddr,
650022a3aee6SAdrian Chadd 		    ":",
650122a3aee6SAdrian Chadd 		    tid);
650222a3aee6SAdrian Chadd 		return;
650322a3aee6SAdrian Chadd 	}
650422a3aee6SAdrian Chadd 
650522a3aee6SAdrian Chadd 	ATH_TX_UNLOCK(sc);
650622a3aee6SAdrian Chadd 
650722a3aee6SAdrian Chadd 	/*
650822a3aee6SAdrian Chadd 	 * XXX nothing in the TIDs at this point? Eek.
650922a3aee6SAdrian Chadd 	 */
651022a3aee6SAdrian Chadd 	device_printf(sc->sc_dev, "%s: %6D: TIDs empty, but ath_node showed traffic?!\n",
651122a3aee6SAdrian Chadd 	    __func__,
651222a3aee6SAdrian Chadd 	    ni->ni_macaddr,
651322a3aee6SAdrian Chadd 	    ":");
651422a3aee6SAdrian Chadd 	avp->av_recv_pspoll(ni, m);
651522a3aee6SAdrian Chadd #else
651622a3aee6SAdrian Chadd 	avp->av_recv_pspoll(ni, m);
651722a3aee6SAdrian Chadd #endif	/* ATH_SW_PSQ */
651822a3aee6SAdrian Chadd }
651922a3aee6SAdrian Chadd 
6520dba9c859SAdrian Chadd MODULE_VERSION(if_ath, 1);
6521dba9c859SAdrian Chadd MODULE_DEPEND(if_ath, wlan, 1, 1, 1);          /* 802.11 media layer */
652258816f3fSAdrian Chadd #if	defined(IEEE80211_ALQ) || defined(AH_DEBUG_ALQ)
652358816f3fSAdrian Chadd MODULE_DEPEND(if_ath, alq, 1, 1, 1);
652458816f3fSAdrian Chadd #endif
6525