xref: /freebsd/sys/dev/ath/if_ath.c (revision 7d567ed66fb29a6edef06b69485e5e427aec626d)
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>
7676039bc8SGleb Smirnoff #include <net/if_var.h>
775591b213SSam Leffler #include <net/if_dl.h>
785591b213SSam Leffler #include <net/if_media.h>
79fc74a9f9SBrooks Davis #include <net/if_types.h>
805591b213SSam Leffler #include <net/if_arp.h>
815591b213SSam Leffler #include <net/ethernet.h>
825591b213SSam Leffler #include <net/if_llc.h>
835591b213SSam Leffler 
845591b213SSam Leffler #include <net80211/ieee80211_var.h>
8559efa8b5SSam Leffler #include <net80211/ieee80211_regdomain.h>
86339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
87339ccfb3SSam Leffler #include <net80211/ieee80211_superg.h>
88339ccfb3SSam Leffler #endif
89584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
9010ad9a77SSam Leffler #include <net80211/ieee80211_tdma.h>
9110ad9a77SSam Leffler #endif
925591b213SSam Leffler 
935591b213SSam Leffler #include <net/bpf.h>
945591b213SSam Leffler 
955591b213SSam Leffler #ifdef INET
965591b213SSam Leffler #include <netinet/in.h>
975591b213SSam Leffler #include <netinet/if_ether.h>
985591b213SSam Leffler #endif
995591b213SSam Leffler 
1005591b213SSam Leffler #include <dev/ath/if_athvar.h>
10133644623SSam Leffler #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
1020dbe9289SAdrian Chadd #include <dev/ath/ath_hal/ah_diagcodes.h>
1035591b213SSam Leffler 
1045bc8125aSAdrian Chadd #include <dev/ath/if_ath_debug.h>
105b8e788a5SAdrian Chadd #include <dev/ath/if_ath_misc.h>
106e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_tsf.h>
107b8e788a5SAdrian Chadd #include <dev/ath/if_ath_tx.h>
1086079fdbeSAdrian Chadd #include <dev/ath/if_ath_sysctl.h>
109c65ee21dSAdrian Chadd #include <dev/ath/if_ath_led.h>
110d2d7a00aSAdrian Chadd #include <dev/ath/if_ath_keycache.h>
111e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_rx.h>
112f8cc9b09SAdrian Chadd #include <dev/ath/if_ath_rx_edma.h>
1133fdfc330SAdrian Chadd #include <dev/ath/if_ath_tx_edma.h>
114a35dae8dSAdrian Chadd #include <dev/ath/if_ath_beacon.h>
115b70f530bSAdrian Chadd #include <dev/ath/if_ath_btcoex.h>
1169af351f9SAdrian Chadd #include <dev/ath/if_ath_spectral.h>
117216ca234SAdrian Chadd #include <dev/ath/if_ath_lna_div.h>
11848237774SAdrian Chadd #include <dev/ath/if_athdfs.h>
1195bc8125aSAdrian Chadd 
12086e07743SSam Leffler #ifdef ATH_TX99_DIAG
12186e07743SSam Leffler #include <dev/ath/ath_tx99/ath_tx99.h>
12286e07743SSam Leffler #endif
12386e07743SSam Leffler 
12489d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
125bdbb6e5bSAdrian Chadd #include <dev/ath/if_ath_alq.h>
126bdbb6e5bSAdrian Chadd #endif
127bdbb6e5bSAdrian Chadd 
128bdbb6e5bSAdrian Chadd /*
129bdbb6e5bSAdrian Chadd  * Only enable this if you're working on PS-POLL support.
130bdbb6e5bSAdrian Chadd  */
13122a3aee6SAdrian Chadd #define	ATH_SW_PSQ
132bdbb6e5bSAdrian Chadd 
133b032f27cSSam Leffler /*
134b032f27cSSam Leffler  * ATH_BCBUF determines the number of vap's that can transmit
135b032f27cSSam Leffler  * beacons and also (currently) the number of vap's that can
136b032f27cSSam Leffler  * have unique mac addresses/bssid.  When staggering beacons
137b032f27cSSam Leffler  * 4 is probably a good max as otherwise the beacons become
138b032f27cSSam Leffler  * very closely spaced and there is limited time for cab q traffic
139b032f27cSSam Leffler  * to go out.  You can burst beacons instead but that is not good
140b032f27cSSam Leffler  * for stations in power save and at some point you really want
141b032f27cSSam Leffler  * another radio (and channel).
142b032f27cSSam Leffler  *
143b032f27cSSam Leffler  * The limit on the number of mac addresses is tied to our use of
144b032f27cSSam Leffler  * the U/L bit and tracking addresses in a byte; it would be
145b032f27cSSam Leffler  * worthwhile to allow more for applications like proxy sta.
146b032f27cSSam Leffler  */
147b032f27cSSam Leffler CTASSERT(ATH_BCBUF <= 8);
148b032f27cSSam Leffler 
149b032f27cSSam Leffler static struct ieee80211vap *ath_vap_create(struct ieee80211com *,
150fcd9500fSBernhard Schmidt 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
151fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN],
152fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN]);
153b032f27cSSam Leffler static void	ath_vap_delete(struct ieee80211vap *);
1545591b213SSam Leffler static void	ath_init(void *);
155c42a7b7eSSam Leffler static void	ath_stop_locked(struct ifnet *);
1565591b213SSam Leffler static void	ath_stop(struct ifnet *);
157b032f27cSSam Leffler static int	ath_reset_vap(struct ieee80211vap *, u_long);
158cd7dffd0SAdrian Chadd static int	ath_transmit(struct ifnet *ifp, struct mbuf *m);
159cd7dffd0SAdrian Chadd static void	ath_qflush(struct ifnet *ifp);
1605591b213SSam Leffler static int	ath_media_change(struct ifnet *);
1612e986da5SSam Leffler static void	ath_watchdog(void *);
1625591b213SSam Leffler static int	ath_ioctl(struct ifnet *, u_long, caddr_t);
1635591b213SSam Leffler static void	ath_fatal_proc(void *, int);
164b032f27cSSam Leffler static void	ath_bmiss_vap(struct ieee80211vap *);
1655591b213SSam Leffler static void	ath_bmiss_proc(void *, int);
166b032f27cSSam Leffler static void	ath_key_update_begin(struct ieee80211vap *);
167b032f27cSSam Leffler static void	ath_key_update_end(struct ieee80211vap *);
168b032f27cSSam Leffler static void	ath_update_mcast(struct ifnet *);
169b032f27cSSam Leffler static void	ath_update_promisc(struct ifnet *);
170c42a7b7eSSam Leffler static void	ath_updateslot(struct ifnet *);
171c42a7b7eSSam Leffler static void	ath_bstuck_proc(void *, int);
172d52f7132SAdrian Chadd static void	ath_reset_proc(void *, int);
1735591b213SSam Leffler static int	ath_desc_alloc(struct ath_softc *);
1745591b213SSam Leffler static void	ath_desc_free(struct ath_softc *);
17538c208f8SSam Leffler static struct ieee80211_node *ath_node_alloc(struct ieee80211vap *,
17638c208f8SSam Leffler 			const uint8_t [IEEE80211_ADDR_LEN]);
1774afa805eSAdrian Chadd static void	ath_node_cleanup(struct ieee80211_node *);
178c42a7b7eSSam Leffler static void	ath_node_free(struct ieee80211_node *);
17968e8e04eSSam Leffler static void	ath_node_getsignal(const struct ieee80211_node *,
18068e8e04eSSam Leffler 			int8_t *, int8_t *);
181622b3fd2SSam Leffler static void	ath_txq_init(struct ath_softc *sc, struct ath_txq *, int);
182c42a7b7eSSam Leffler static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype);
183c42a7b7eSSam Leffler static int	ath_tx_setup(struct ath_softc *, int, int);
184c42a7b7eSSam Leffler static void	ath_tx_cleanupq(struct ath_softc *, struct ath_txq *);
185c42a7b7eSSam Leffler static void	ath_tx_cleanup(struct ath_softc *);
186788e6aa9SAdrian Chadd static int	ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq,
187788e6aa9SAdrian Chadd 		    int dosched);
188c42a7b7eSSam Leffler static void	ath_tx_proc_q0(void *, int);
189c42a7b7eSSam Leffler static void	ath_tx_proc_q0123(void *, int);
1905591b213SSam Leffler static void	ath_tx_proc(void *, int);
19103e9308fSAdrian Chadd static void	ath_txq_sched_tasklet(void *, int);
1925591b213SSam Leffler static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
193c42a7b7eSSam Leffler static void	ath_chan_change(struct ath_softc *, struct ieee80211_channel *);
19468e8e04eSSam Leffler static void	ath_scan_start(struct ieee80211com *);
19568e8e04eSSam Leffler static void	ath_scan_end(struct ieee80211com *);
19668e8e04eSSam Leffler static void	ath_set_channel(struct ieee80211com *);
197fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
198e7200579SAdrian Chadd static void	ath_update_chw(struct ieee80211com *);
199fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
2005591b213SSam Leffler static void	ath_calibrate(void *);
201b032f27cSSam Leffler static int	ath_newstate(struct ieee80211vap *, enum ieee80211_state, int);
202e8fd88a3SSam Leffler static void	ath_setup_stationkey(struct ieee80211_node *);
203e9962332SSam Leffler static void	ath_newassoc(struct ieee80211_node *, int);
204b032f27cSSam Leffler static int	ath_setregdomain(struct ieee80211com *,
205b032f27cSSam Leffler 		    struct ieee80211_regdomain *, int,
206b032f27cSSam Leffler 		    struct ieee80211_channel []);
2075fe9f044SSam Leffler static void	ath_getradiocaps(struct ieee80211com *, int, int *,
208b032f27cSSam Leffler 		    struct ieee80211_channel []);
209b032f27cSSam Leffler static int	ath_getchannels(struct ath_softc *);
2105591b213SSam Leffler 
211c42a7b7eSSam Leffler static int	ath_rate_setup(struct ath_softc *, u_int mode);
2125591b213SSam Leffler static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
213c42a7b7eSSam Leffler 
214c42a7b7eSSam Leffler static void	ath_announce(struct ath_softc *);
2155591b213SSam Leffler 
21648237774SAdrian Chadd static void	ath_dfs_tasklet(void *, int);
2170eb81626SAdrian Chadd static void	ath_node_powersave(struct ieee80211_node *, int);
218548a605dSAdrian Chadd static int	ath_node_set_tim(struct ieee80211_node *, int);
21922a3aee6SAdrian Chadd static void	ath_node_recv_pspoll(struct ieee80211_node *, struct mbuf *);
22048237774SAdrian Chadd 
221584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
222a35dae8dSAdrian Chadd #include <dev/ath/if_ath_tdma.h>
223a35dae8dSAdrian Chadd #endif
22410ad9a77SSam Leffler 
2255591b213SSam Leffler SYSCTL_DECL(_hw_ath);
2265591b213SSam Leffler 
2275591b213SSam Leffler /* XXX validate sysctl values */
2282dc7fcc4SSam Leffler static	int ath_longcalinterval = 30;		/* long cals every 30 secs */
2292dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, longcal, CTLFLAG_RW, &ath_longcalinterval,
2302dc7fcc4SSam Leffler 	    0, "long chip calibration interval (secs)");
2312dc7fcc4SSam Leffler static	int ath_shortcalinterval = 100;		/* short cals every 100 ms */
2322dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, shortcal, CTLFLAG_RW, &ath_shortcalinterval,
2332dc7fcc4SSam Leffler 	    0, "short chip calibration interval (msecs)");
2342dc7fcc4SSam Leffler static	int ath_resetcalinterval = 20*60;	/* reset cal state 20 mins */
2352dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, resetcal, CTLFLAG_RW, &ath_resetcalinterval,
2362dc7fcc4SSam Leffler 	    0, "reset chip calibration results (secs)");
237a108ab63SAdrian Chadd static	int ath_anicalinterval = 100;		/* ANI calibration - 100 msec */
238a108ab63SAdrian Chadd SYSCTL_INT(_hw_ath, OID_AUTO, anical, CTLFLAG_RW, &ath_anicalinterval,
239a108ab63SAdrian Chadd 	    0, "ANI calibration (msecs)");
2405591b213SSam Leffler 
2413d184db2SAdrian Chadd int ath_rxbuf = ATH_RXBUF;		/* # rx buffers to allocate */
242aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RW, &ath_rxbuf,
243e2d787faSSam Leffler 	    0, "rx buffers allocated");
244e2d787faSSam Leffler TUNABLE_INT("hw.ath.rxbuf", &ath_rxbuf);
2453d184db2SAdrian Chadd int ath_txbuf = ATH_TXBUF;		/* # tx buffers to allocate */
246aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RW, &ath_txbuf,
247e2d787faSSam Leffler 	    0, "tx buffers allocated");
248e2d787faSSam Leffler TUNABLE_INT("hw.ath.txbuf", &ath_txbuf);
2493d184db2SAdrian Chadd int ath_txbuf_mgmt = ATH_MGMT_TXBUF;	/* # mgmt tx buffers to allocate */
250af33d486SAdrian Chadd SYSCTL_INT(_hw_ath, OID_AUTO, txbuf_mgmt, CTLFLAG_RW, &ath_txbuf_mgmt,
251af33d486SAdrian Chadd 	    0, "tx (mgmt) buffers allocated");
252af33d486SAdrian Chadd TUNABLE_INT("hw.ath.txbuf_mgmt", &ath_txbuf_mgmt);
253e2d787faSSam Leffler 
254a35dae8dSAdrian Chadd int ath_bstuck_threshold = 4;		/* max missed beacons */
255a32ac9d3SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, bstuck, CTLFLAG_RW, &ath_bstuck_threshold,
256a32ac9d3SSam Leffler 	    0, "max missed beacon xmits before chip reset");
257a32ac9d3SSam Leffler 
2586b349e5aSAdrian Chadd MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
259c42a7b7eSSam Leffler 
260f8418db5SAdrian Chadd void
261f8418db5SAdrian Chadd ath_legacy_attach_comp_func(struct ath_softc *sc)
262f8418db5SAdrian Chadd {
263f8418db5SAdrian Chadd 
264f8418db5SAdrian Chadd 	/*
265f8418db5SAdrian Chadd 	 * Special case certain configurations.  Note the
266f8418db5SAdrian Chadd 	 * CAB queue is handled by these specially so don't
267f8418db5SAdrian Chadd 	 * include them when checking the txq setup mask.
268f8418db5SAdrian Chadd 	 */
269f8418db5SAdrian Chadd 	switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
270f8418db5SAdrian Chadd 	case 0x01:
271f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
272f8418db5SAdrian Chadd 		break;
273f8418db5SAdrian Chadd 	case 0x0f:
274f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
275f8418db5SAdrian Chadd 		break;
276f8418db5SAdrian Chadd 	default:
277f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
278f8418db5SAdrian Chadd 		break;
279f8418db5SAdrian Chadd 	}
280f8418db5SAdrian Chadd }
281f8418db5SAdrian Chadd 
282f5c30c4eSAdrian Chadd /*
283f5c30c4eSAdrian Chadd  * Set the target power mode.
284f5c30c4eSAdrian Chadd  *
285f5c30c4eSAdrian Chadd  * If this is called during a point in time where
286f5c30c4eSAdrian Chadd  * the hardware is being programmed elsewhere, it will
287f5c30c4eSAdrian Chadd  * simply store it away and update it when all current
288f5c30c4eSAdrian Chadd  * uses of the hardware are completed.
289f5c30c4eSAdrian Chadd  */
290f5c30c4eSAdrian Chadd void
291f5c30c4eSAdrian Chadd _ath_power_setpower(struct ath_softc *sc, int power_state, const char *file, int line)
292f5c30c4eSAdrian Chadd {
293f5c30c4eSAdrian Chadd 	ATH_LOCK_ASSERT(sc);
294f5c30c4eSAdrian Chadd 
295f5c30c4eSAdrian Chadd 	sc->sc_target_powerstate = power_state;
296f5c30c4eSAdrian Chadd 
297f5c30c4eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) state=%d, refcnt=%d\n",
298f5c30c4eSAdrian Chadd 	    __func__,
299f5c30c4eSAdrian Chadd 	    file,
300f5c30c4eSAdrian Chadd 	    line,
301f5c30c4eSAdrian Chadd 	    power_state,
302f5c30c4eSAdrian Chadd 	    sc->sc_powersave_refcnt);
303f5c30c4eSAdrian Chadd 
304f5c30c4eSAdrian Chadd 	if (sc->sc_powersave_refcnt == 0 &&
305f5c30c4eSAdrian Chadd 	    power_state != sc->sc_cur_powerstate) {
306f5c30c4eSAdrian Chadd 		sc->sc_cur_powerstate = power_state;
307f5c30c4eSAdrian Chadd 		ath_hal_setpower(sc->sc_ah, power_state);
308*7d567ed6SAdrian Chadd 
309*7d567ed6SAdrian Chadd 		/*
310*7d567ed6SAdrian Chadd 		 * If the NIC is force-awake, then set the
311*7d567ed6SAdrian Chadd 		 * self-gen frame state appropriately.
312*7d567ed6SAdrian Chadd 		 *
313*7d567ed6SAdrian Chadd 		 * If the nic is in network sleep or full-sleep,
314*7d567ed6SAdrian Chadd 		 * we let the above call leave the self-gen
315*7d567ed6SAdrian Chadd 		 * state as "sleep".
316*7d567ed6SAdrian Chadd 		 */
317*7d567ed6SAdrian Chadd 		if (sc->sc_cur_powerstate == HAL_PM_AWAKE &&
318*7d567ed6SAdrian Chadd 		    sc->sc_target_selfgen_state != HAL_PM_AWAKE) {
319*7d567ed6SAdrian Chadd 			ath_hal_setselfgenpower(sc->sc_ah,
320*7d567ed6SAdrian Chadd 			    sc->sc_target_selfgen_state);
321*7d567ed6SAdrian Chadd 		}
322*7d567ed6SAdrian Chadd 	}
323*7d567ed6SAdrian Chadd }
324*7d567ed6SAdrian Chadd 
325*7d567ed6SAdrian Chadd /*
326*7d567ed6SAdrian Chadd  * Set the current self-generated frames state.
327*7d567ed6SAdrian Chadd  *
328*7d567ed6SAdrian Chadd  * This is separate from the target power mode.  The chip may be
329*7d567ed6SAdrian Chadd  * awake but the desired state is "sleep", so frames sent to the
330*7d567ed6SAdrian Chadd  * destination has PWRMGT=1 in the 802.11 header.  The NIC also
331*7d567ed6SAdrian Chadd  * needs to know to set PWRMGT=1 in self-generated frames.
332*7d567ed6SAdrian Chadd  */
333*7d567ed6SAdrian Chadd void
334*7d567ed6SAdrian Chadd _ath_power_set_selfgen(struct ath_softc *sc, int power_state, const char *file, int line)
335*7d567ed6SAdrian Chadd {
336*7d567ed6SAdrian Chadd 
337*7d567ed6SAdrian Chadd 	ATH_LOCK_ASSERT(sc);
338*7d567ed6SAdrian Chadd 
339*7d567ed6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) state=%d, refcnt=%d\n",
340*7d567ed6SAdrian Chadd 	    __func__,
341*7d567ed6SAdrian Chadd 	    file,
342*7d567ed6SAdrian Chadd 	    line,
343*7d567ed6SAdrian Chadd 	    power_state,
344*7d567ed6SAdrian Chadd 	    sc->sc_target_selfgen_state);
345*7d567ed6SAdrian Chadd 
346*7d567ed6SAdrian Chadd 	sc->sc_target_selfgen_state = power_state;
347*7d567ed6SAdrian Chadd 
348*7d567ed6SAdrian Chadd 	/*
349*7d567ed6SAdrian Chadd 	 * If the NIC is force-awake, then set the power state.
350*7d567ed6SAdrian Chadd 	 * Network-state and full-sleep will already transition it to
351*7d567ed6SAdrian Chadd 	 * mark self-gen frames as sleeping - and we can't
352*7d567ed6SAdrian Chadd 	 * guarantee the NIC is awake to program the self-gen frame
353*7d567ed6SAdrian Chadd 	 * setting anyway.
354*7d567ed6SAdrian Chadd 	 */
355*7d567ed6SAdrian Chadd 	if (sc->sc_cur_powerstate == HAL_PM_AWAKE) {
356*7d567ed6SAdrian Chadd 		ath_hal_setselfgenpower(sc->sc_ah, power_state);
357f5c30c4eSAdrian Chadd 	}
358f5c30c4eSAdrian Chadd }
359f5c30c4eSAdrian Chadd 
360f5c30c4eSAdrian Chadd /*
361f5c30c4eSAdrian Chadd  * Set the hardware power mode and take a reference.
362f5c30c4eSAdrian Chadd  *
363f5c30c4eSAdrian Chadd  * This doesn't update the target power mode in the driver;
364f5c30c4eSAdrian Chadd  * it just updates the hardware power state.
365f5c30c4eSAdrian Chadd  *
366f5c30c4eSAdrian Chadd  * XXX it should only ever force the hardware awake; it should
367f5c30c4eSAdrian Chadd  * never be called to set it asleep.
368f5c30c4eSAdrian Chadd  */
369f5c30c4eSAdrian Chadd void
370f5c30c4eSAdrian Chadd _ath_power_set_power_state(struct ath_softc *sc, int power_state, const char *file, int line)
371f5c30c4eSAdrian Chadd {
372f5c30c4eSAdrian Chadd 	ATH_LOCK_ASSERT(sc);
373f5c30c4eSAdrian Chadd 
374f5c30c4eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) state=%d, refcnt=%d\n",
375f5c30c4eSAdrian Chadd 	    __func__,
376f5c30c4eSAdrian Chadd 	    file,
377f5c30c4eSAdrian Chadd 	    line,
378f5c30c4eSAdrian Chadd 	    power_state,
379f5c30c4eSAdrian Chadd 	    sc->sc_powersave_refcnt);
380f5c30c4eSAdrian Chadd 
381f5c30c4eSAdrian Chadd 	sc->sc_powersave_refcnt++;
382f5c30c4eSAdrian Chadd 
383f5c30c4eSAdrian Chadd 	if (power_state != sc->sc_cur_powerstate) {
384f5c30c4eSAdrian Chadd 		ath_hal_setpower(sc->sc_ah, power_state);
385f5c30c4eSAdrian Chadd 		sc->sc_cur_powerstate = power_state;
386*7d567ed6SAdrian Chadd 
387*7d567ed6SAdrian Chadd 		/*
388*7d567ed6SAdrian Chadd 		 * Adjust the self-gen powerstate if appropriate.
389*7d567ed6SAdrian Chadd 		 */
390*7d567ed6SAdrian Chadd 		if (sc->sc_cur_powerstate == HAL_PM_AWAKE &&
391*7d567ed6SAdrian Chadd 		    sc->sc_target_selfgen_state != HAL_PM_AWAKE) {
392*7d567ed6SAdrian Chadd 			ath_hal_setselfgenpower(sc->sc_ah,
393*7d567ed6SAdrian Chadd 			    sc->sc_target_selfgen_state);
394*7d567ed6SAdrian Chadd 		}
395*7d567ed6SAdrian Chadd 
396f5c30c4eSAdrian Chadd 	}
397f5c30c4eSAdrian Chadd }
398f5c30c4eSAdrian Chadd 
399f5c30c4eSAdrian Chadd /*
400f5c30c4eSAdrian Chadd  * Restore the power save mode to what it once was.
401f5c30c4eSAdrian Chadd  *
402f5c30c4eSAdrian Chadd  * This will decrement the reference counter and once it hits
403f5c30c4eSAdrian Chadd  * zero, it'll restore the powersave state.
404f5c30c4eSAdrian Chadd  */
405f5c30c4eSAdrian Chadd void
406f5c30c4eSAdrian Chadd _ath_power_restore_power_state(struct ath_softc *sc, const char *file, int line)
407f5c30c4eSAdrian Chadd {
408f5c30c4eSAdrian Chadd 
409f5c30c4eSAdrian Chadd 	ATH_LOCK_ASSERT(sc);
410f5c30c4eSAdrian Chadd 
411f5c30c4eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) refcnt=%d, target state=%d\n",
412f5c30c4eSAdrian Chadd 	    __func__,
413f5c30c4eSAdrian Chadd 	    file,
414f5c30c4eSAdrian Chadd 	    line,
415f5c30c4eSAdrian Chadd 	    sc->sc_powersave_refcnt,
416f5c30c4eSAdrian Chadd 	    sc->sc_target_powerstate);
417f5c30c4eSAdrian Chadd 
418f5c30c4eSAdrian Chadd 	if (sc->sc_powersave_refcnt == 0)
419f5c30c4eSAdrian Chadd 		device_printf(sc->sc_dev, "%s: refcnt=0?\n", __func__);
420f5c30c4eSAdrian Chadd 	else
421f5c30c4eSAdrian Chadd 		sc->sc_powersave_refcnt--;
422f5c30c4eSAdrian Chadd 
423f5c30c4eSAdrian Chadd 	if (sc->sc_powersave_refcnt == 0 &&
424f5c30c4eSAdrian Chadd 	    sc->sc_target_powerstate != sc->sc_cur_powerstate) {
425f5c30c4eSAdrian Chadd 		sc->sc_cur_powerstate = sc->sc_target_powerstate;
426f5c30c4eSAdrian Chadd 		ath_hal_setpower(sc->sc_ah, sc->sc_target_powerstate);
427f5c30c4eSAdrian Chadd 	}
428*7d567ed6SAdrian Chadd 
429*7d567ed6SAdrian Chadd 	/*
430*7d567ed6SAdrian Chadd 	 * Adjust the self-gen powerstate if appropriate.
431*7d567ed6SAdrian Chadd 	 */
432*7d567ed6SAdrian Chadd 	if (sc->sc_cur_powerstate == HAL_PM_AWAKE &&
433*7d567ed6SAdrian Chadd 	    sc->sc_target_selfgen_state != HAL_PM_AWAKE) {
434*7d567ed6SAdrian Chadd 		ath_hal_setselfgenpower(sc->sc_ah,
435*7d567ed6SAdrian Chadd 		    sc->sc_target_selfgen_state);
436*7d567ed6SAdrian Chadd 	}
437*7d567ed6SAdrian Chadd 
438f5c30c4eSAdrian Chadd }
439f5c30c4eSAdrian Chadd 
44067397d39SAdrian Chadd #define	HAL_MODE_HT20 (HAL_MODE_11NG_HT20 | HAL_MODE_11NA_HT20)
44167397d39SAdrian Chadd #define	HAL_MODE_HT40 \
44267397d39SAdrian Chadd 	(HAL_MODE_11NG_HT40PLUS | HAL_MODE_11NG_HT40MINUS | \
44367397d39SAdrian Chadd 	HAL_MODE_11NA_HT40PLUS | HAL_MODE_11NA_HT40MINUS)
4445591b213SSam Leffler int
4455591b213SSam Leffler ath_attach(u_int16_t devid, struct ath_softc *sc)
4465591b213SSam Leffler {
447fc74a9f9SBrooks Davis 	struct ifnet *ifp;
448b032f27cSSam Leffler 	struct ieee80211com *ic;
449fc74a9f9SBrooks Davis 	struct ath_hal *ah = NULL;
4505591b213SSam Leffler 	HAL_STATUS status;
451c42a7b7eSSam Leffler 	int error = 0, i;
452411373ebSSam Leffler 	u_int wmodes;
45329aca940SSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
454a865860dSAdrian Chadd 	int rx_chainmask, tx_chainmask;
4555591b213SSam Leffler 
456c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
4575591b213SSam Leffler 
458a93c5097SAdrian Chadd 	CURVNET_SET(vnet0);
459b032f27cSSam Leffler 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
460fc74a9f9SBrooks Davis 	if (ifp == NULL) {
461fc74a9f9SBrooks Davis 		device_printf(sc->sc_dev, "can not if_alloc()\n");
462fc74a9f9SBrooks Davis 		error = ENOSPC;
463bb327d28SAdrian Chadd 		CURVNET_RESTORE();
464fc74a9f9SBrooks Davis 		goto bad;
465fc74a9f9SBrooks Davis 	}
466b032f27cSSam Leffler 	ic = ifp->if_l2com;
467fc74a9f9SBrooks Davis 
4685591b213SSam Leffler 	/* set these up early for if_printf use */
4699bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(sc->sc_dev),
4709bf40edeSBrooks Davis 		device_get_unit(sc->sc_dev));
471a93c5097SAdrian Chadd 	CURVNET_RESTORE();
4725591b213SSam Leffler 
4737e97436bSAdrian Chadd 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh,
4747e97436bSAdrian Chadd 	    sc->sc_eepromdata, &status);
4755591b213SSam Leffler 	if (ah == NULL) {
4765591b213SSam Leffler 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
4775591b213SSam Leffler 			status);
4785591b213SSam Leffler 		error = ENXIO;
4795591b213SSam Leffler 		goto bad;
4805591b213SSam Leffler 	}
4815591b213SSam Leffler 	sc->sc_ah = ah;
482b58b3803SSam Leffler 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
4833297be13SSam Leffler #ifdef	ATH_DEBUG
4843297be13SSam Leffler 	sc->sc_debug = ath_debug;
4853297be13SSam Leffler #endif
4865591b213SSam Leffler 
4875591b213SSam Leffler 	/*
488f8cc9b09SAdrian Chadd 	 * Setup the DMA/EDMA functions based on the current
489f8cc9b09SAdrian Chadd 	 * hardware support.
490f8cc9b09SAdrian Chadd 	 *
491f8cc9b09SAdrian Chadd 	 * This is required before the descriptors are allocated.
492f8cc9b09SAdrian Chadd 	 */
4933d184db2SAdrian Chadd 	if (ath_hal_hasedma(sc->sc_ah)) {
4943d184db2SAdrian Chadd 		sc->sc_isedma = 1;
495f8cc9b09SAdrian Chadd 		ath_recv_setup_edma(sc);
4963fdfc330SAdrian Chadd 		ath_xmit_setup_edma(sc);
4973fdfc330SAdrian Chadd 	} else {
498f8cc9b09SAdrian Chadd 		ath_recv_setup_legacy(sc);
4993fdfc330SAdrian Chadd 		ath_xmit_setup_legacy(sc);
5003fdfc330SAdrian Chadd 	}
501f8cc9b09SAdrian Chadd 
502f5c30c4eSAdrian Chadd 	if (ath_hal_hasmybeacon(sc->sc_ah)) {
503f5c30c4eSAdrian Chadd 		sc->sc_do_mybeacon = 1;
504f5c30c4eSAdrian Chadd 	}
505f5c30c4eSAdrian Chadd 
506f8cc9b09SAdrian Chadd 	/*
507c42a7b7eSSam Leffler 	 * Check if the MAC has multi-rate retry support.
508c42a7b7eSSam Leffler 	 * We do this by trying to setup a fake extended
509c42a7b7eSSam Leffler 	 * descriptor.  MAC's that don't have support will
510c42a7b7eSSam Leffler 	 * return false w/o doing anything.  MAC's that do
511c42a7b7eSSam Leffler 	 * support it will return true w/o doing anything.
512c42a7b7eSSam Leffler 	 */
513c42a7b7eSSam Leffler 	sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
514c42a7b7eSSam Leffler 
515c42a7b7eSSam Leffler 	/*
516c42a7b7eSSam Leffler 	 * Check if the device has hardware counters for PHY
517c42a7b7eSSam Leffler 	 * errors.  If so we need to enable the MIB interrupt
518c42a7b7eSSam Leffler 	 * so we can act on stat triggers.
519c42a7b7eSSam Leffler 	 */
520c42a7b7eSSam Leffler 	if (ath_hal_hwphycounters(ah))
521c42a7b7eSSam Leffler 		sc->sc_needmib = 1;
522c42a7b7eSSam Leffler 
523c42a7b7eSSam Leffler 	/*
524c42a7b7eSSam Leffler 	 * Get the hardware key cache size.
525c42a7b7eSSam Leffler 	 */
526c42a7b7eSSam Leffler 	sc->sc_keymax = ath_hal_keycachesize(ah);
527e8fd88a3SSam Leffler 	if (sc->sc_keymax > ATH_KEYMAX) {
528e8fd88a3SSam Leffler 		if_printf(ifp, "Warning, using only %u of %u key cache slots\n",
529e8fd88a3SSam Leffler 			ATH_KEYMAX, sc->sc_keymax);
530e8fd88a3SSam Leffler 		sc->sc_keymax = ATH_KEYMAX;
531c42a7b7eSSam Leffler 	}
532c42a7b7eSSam Leffler 	/*
533c42a7b7eSSam Leffler 	 * Reset the key cache since some parts do not
534c42a7b7eSSam Leffler 	 * reset the contents on initial power up.
535c42a7b7eSSam Leffler 	 */
536c42a7b7eSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
537c42a7b7eSSam Leffler 		ath_hal_keyreset(ah, i);
538c42a7b7eSSam Leffler 
539c42a7b7eSSam Leffler 	/*
540b032f27cSSam Leffler 	 * Collect the default channel list.
5415591b213SSam Leffler 	 */
542b032f27cSSam Leffler 	error = ath_getchannels(sc);
5435591b213SSam Leffler 	if (error != 0)
5445591b213SSam Leffler 		goto bad;
5455591b213SSam Leffler 
5465591b213SSam Leffler 	/*
5475591b213SSam Leffler 	 * Setup rate tables for all potential media types.
5485591b213SSam Leffler 	 */
5495591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11A);
5505591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11B);
5515591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11G);
552c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
553c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
55468e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_STURBO_A);
55568e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NA);
55668e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NG);
557724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_HALF);
558724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_QUARTER);
559aaa70f2fSSam Leffler 
560c42a7b7eSSam Leffler 	/* NB: setup here so ath_rate_update is happy */
561c42a7b7eSSam Leffler 	ath_setcurmode(sc, IEEE80211_MODE_11A);
5625591b213SSam Leffler 
563c42a7b7eSSam Leffler 	/*
5643fdfc330SAdrian Chadd 	 * Allocate TX descriptors and populate the lists.
565c42a7b7eSSam Leffler 	 */
5665591b213SSam Leffler 	error = ath_desc_alloc(sc);
5675591b213SSam Leffler 	if (error != 0) {
5683fdfc330SAdrian Chadd 		if_printf(ifp, "failed to allocate TX descriptors: %d\n",
5693fdfc330SAdrian Chadd 		    error);
5703fdfc330SAdrian Chadd 		goto bad;
5713fdfc330SAdrian Chadd 	}
5723fdfc330SAdrian Chadd 	error = ath_txdma_setup(sc);
5733fdfc330SAdrian Chadd 	if (error != 0) {
5743fdfc330SAdrian Chadd 		if_printf(ifp, "failed to allocate TX descriptors: %d\n",
5753fdfc330SAdrian Chadd 		    error);
5765591b213SSam Leffler 		goto bad;
5775591b213SSam Leffler 	}
5783d184db2SAdrian Chadd 
5793fdfc330SAdrian Chadd 	/*
5803fdfc330SAdrian Chadd 	 * Allocate RX descriptors and populate the lists.
5813fdfc330SAdrian Chadd 	 */
5823d184db2SAdrian Chadd 	error = ath_rxdma_setup(sc);
5833d184db2SAdrian Chadd 	if (error != 0) {
5843d184db2SAdrian Chadd 		if_printf(ifp, "failed to allocate RX descriptors: %d\n",
5853d184db2SAdrian Chadd 		    error);
5863d184db2SAdrian Chadd 		goto bad;
5873d184db2SAdrian Chadd 	}
5883d184db2SAdrian Chadd 
5892e986da5SSam Leffler 	callout_init_mtx(&sc->sc_cal_ch, &sc->sc_mtx, 0);
5902e986da5SSam Leffler 	callout_init_mtx(&sc->sc_wd_ch, &sc->sc_mtx, 0);
5915591b213SSam Leffler 
592f0b2a0beSSam Leffler 	ATH_TXBUF_LOCK_INIT(sc);
5935591b213SSam Leffler 
5940bbf5441SSam Leffler 	sc->sc_tq = taskqueue_create("ath_taskq", M_NOWAIT,
5950bbf5441SSam Leffler 		taskqueue_thread_enqueue, &sc->sc_tq);
5960bbf5441SSam Leffler 	taskqueue_start_threads(&sc->sc_tq, 1, PI_NET,
5970bbf5441SSam Leffler 		"%s taskq", ifp->if_xname);
5980bbf5441SSam Leffler 
599f8cc9b09SAdrian Chadd 	TASK_INIT(&sc->sc_rxtask, 0, sc->sc_rx.recv_tasklet, sc);
6005591b213SSam Leffler 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
601c42a7b7eSSam Leffler 	TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc);
602d52f7132SAdrian Chadd 	TASK_INIT(&sc->sc_resettask,0, ath_reset_proc, sc);
60303e9308fSAdrian Chadd 	TASK_INIT(&sc->sc_txqtask, 0, ath_txq_sched_tasklet, sc);
604f846cf42SAdrian Chadd 	TASK_INIT(&sc->sc_fataltask, 0, ath_fatal_proc, sc);
6055591b213SSam Leffler 
6065591b213SSam Leffler 	/*
607c42a7b7eSSam Leffler 	 * Allocate hardware transmit queues: one queue for
608c42a7b7eSSam Leffler 	 * beacon frames and one data queue for each QoS
6094fa8d4efSDaniel Eischen 	 * priority.  Note that the hal handles resetting
610c42a7b7eSSam Leffler 	 * these queues at the needed time.
611c42a7b7eSSam Leffler 	 *
612c42a7b7eSSam Leffler 	 * XXX PS-Poll
6135591b213SSam Leffler 	 */
614e1252ce1SAdrian Chadd 	sc->sc_bhalq = ath_beaconq_setup(sc);
6155591b213SSam Leffler 	if (sc->sc_bhalq == (u_int) -1) {
6165591b213SSam Leffler 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
617c42a7b7eSSam Leffler 		error = EIO;
618b28b4653SSam Leffler 		goto bad2;
6195591b213SSam Leffler 	}
620c42a7b7eSSam Leffler 	sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
621c42a7b7eSSam Leffler 	if (sc->sc_cabq == NULL) {
622c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup CAB xmit queue!\n");
623c42a7b7eSSam Leffler 		error = EIO;
624c42a7b7eSSam Leffler 		goto bad2;
625c42a7b7eSSam Leffler 	}
626c42a7b7eSSam Leffler 	/* NB: insure BK queue is the lowest priority h/w queue */
627c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
628c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup xmit queue for %s traffic!\n",
629c42a7b7eSSam Leffler 			ieee80211_wme_acnames[WME_AC_BK]);
630c42a7b7eSSam Leffler 		error = EIO;
631c42a7b7eSSam Leffler 		goto bad2;
632c42a7b7eSSam Leffler 	}
633c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
634c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
635c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
636c42a7b7eSSam Leffler 		/*
637c42a7b7eSSam Leffler 		 * Not enough hardware tx queues to properly do WME;
638c42a7b7eSSam Leffler 		 * just punt and assign them all to the same h/w queue.
639c42a7b7eSSam Leffler 		 * We could do a better job of this if, for example,
640c42a7b7eSSam Leffler 		 * we allocate queues when we switch from station to
641c42a7b7eSSam Leffler 		 * AP mode.
642c42a7b7eSSam Leffler 		 */
643c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_VI] != NULL)
644c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
645c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_BE] != NULL)
646c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
647c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
648c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
649c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
650c42a7b7eSSam Leffler 	}
651c42a7b7eSSam Leffler 
652c42a7b7eSSam Leffler 	/*
653f8418db5SAdrian Chadd 	 * Attach the TX completion function.
654f8418db5SAdrian Chadd 	 *
655f8418db5SAdrian Chadd 	 * The non-EDMA chips may have some special case optimisations;
656f8418db5SAdrian Chadd 	 * this method gives everyone a chance to attach cleanly.
657c42a7b7eSSam Leffler 	 */
658f8418db5SAdrian Chadd 	sc->sc_tx.xmit_attach_comp_func(sc);
659c42a7b7eSSam Leffler 
660c42a7b7eSSam Leffler 	/*
661c42a7b7eSSam Leffler 	 * Setup rate control.  Some rate control modules
662c42a7b7eSSam Leffler 	 * call back to change the anntena state so expose
663c42a7b7eSSam Leffler 	 * the necessary entry points.
664c42a7b7eSSam Leffler 	 * XXX maybe belongs in struct ath_ratectrl?
665c42a7b7eSSam Leffler 	 */
666c42a7b7eSSam Leffler 	sc->sc_setdefantenna = ath_setdefantenna;
667c42a7b7eSSam Leffler 	sc->sc_rc = ath_rate_attach(sc);
668c42a7b7eSSam Leffler 	if (sc->sc_rc == NULL) {
669c42a7b7eSSam Leffler 		error = EIO;
670c42a7b7eSSam Leffler 		goto bad2;
671c42a7b7eSSam Leffler 	}
672c42a7b7eSSam Leffler 
67348237774SAdrian Chadd 	/* Attach DFS module */
67448237774SAdrian Chadd 	if (! ath_dfs_attach(sc)) {
6757e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
6767e97436bSAdrian Chadd 		    "%s: unable to attach DFS\n", __func__);
67748237774SAdrian Chadd 		error = EIO;
67848237774SAdrian Chadd 		goto bad2;
67948237774SAdrian Chadd 	}
68048237774SAdrian Chadd 
6819af351f9SAdrian Chadd 	/* Attach spectral module */
6829af351f9SAdrian Chadd 	if (ath_spectral_attach(sc) < 0) {
6839af351f9SAdrian Chadd 		device_printf(sc->sc_dev,
6849af351f9SAdrian Chadd 		    "%s: unable to attach spectral\n", __func__);
6859af351f9SAdrian Chadd 		error = EIO;
6869af351f9SAdrian Chadd 		goto bad2;
6879af351f9SAdrian Chadd 	}
6889af351f9SAdrian Chadd 
689b70f530bSAdrian Chadd 	/* Attach bluetooth coexistence module */
690b70f530bSAdrian Chadd 	if (ath_btcoex_attach(sc) < 0) {
691b70f530bSAdrian Chadd 		device_printf(sc->sc_dev,
692b70f530bSAdrian Chadd 		    "%s: unable to attach bluetooth coexistence\n", __func__);
693b70f530bSAdrian Chadd 		error = EIO;
694b70f530bSAdrian Chadd 		goto bad2;
695b70f530bSAdrian Chadd 	}
696b70f530bSAdrian Chadd 
697216ca234SAdrian Chadd 	/* Attach LNA diversity module */
698216ca234SAdrian Chadd 	if (ath_lna_div_attach(sc) < 0) {
699216ca234SAdrian Chadd 		device_printf(sc->sc_dev,
700216ca234SAdrian Chadd 		    "%s: unable to attach LNA diversity\n", __func__);
701216ca234SAdrian Chadd 		error = EIO;
702216ca234SAdrian Chadd 		goto bad2;
703216ca234SAdrian Chadd 	}
704216ca234SAdrian Chadd 
70548237774SAdrian Chadd 	/* Start DFS processing tasklet */
70648237774SAdrian Chadd 	TASK_INIT(&sc->sc_dfstask, 0, ath_dfs_tasklet, sc);
70748237774SAdrian Chadd 
7083440495aSAdrian Chadd 	/* Configure LED state */
7093e50ec2cSSam Leffler 	sc->sc_blinking = 0;
710c42a7b7eSSam Leffler 	sc->sc_ledstate = 1;
7113e50ec2cSSam Leffler 	sc->sc_ledon = 0;			/* low true */
7123e50ec2cSSam Leffler 	sc->sc_ledidle = (2700*hz)/1000;	/* 2.7sec */
7133e50ec2cSSam Leffler 	callout_init(&sc->sc_ledtimer, CALLOUT_MPSAFE);
7143440495aSAdrian Chadd 
7153440495aSAdrian Chadd 	/*
7163440495aSAdrian Chadd 	 * Don't setup hardware-based blinking.
7173440495aSAdrian Chadd 	 *
7183440495aSAdrian Chadd 	 * Although some NICs may have this configured in the
7193440495aSAdrian Chadd 	 * default reset register values, the user may wish
7203440495aSAdrian Chadd 	 * to alter which pins have which function.
7213440495aSAdrian Chadd 	 *
7223440495aSAdrian Chadd 	 * The reference driver attaches the MAC network LED to GPIO1 and
7233440495aSAdrian Chadd 	 * the MAC power LED to GPIO2.  However, the DWA-552 cardbus
7243440495aSAdrian Chadd 	 * NIC has these reversed.
7253440495aSAdrian Chadd 	 */
7263440495aSAdrian Chadd 	sc->sc_hardled = (1 == 0);
7273440495aSAdrian Chadd 	sc->sc_led_net_pin = -1;
7283440495aSAdrian Chadd 	sc->sc_led_pwr_pin = -1;
729c42a7b7eSSam Leffler 	/*
730c42a7b7eSSam Leffler 	 * Auto-enable soft led processing for IBM cards and for
731c42a7b7eSSam Leffler 	 * 5211 minipci cards.  Users can also manually enable/disable
732c42a7b7eSSam Leffler 	 * support with a sysctl.
733c42a7b7eSSam Leffler 	 */
734c42a7b7eSSam Leffler 	sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
7356558ffd9SAdrian Chadd 	ath_led_config(sc);
736a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
7375591b213SSam Leffler 
7385591b213SSam Leffler 	ifp->if_softc = sc;
7395591b213SSam Leffler 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
740cd7dffd0SAdrian Chadd 	ifp->if_transmit = ath_transmit;
741cd7dffd0SAdrian Chadd 	ifp->if_qflush = ath_qflush;
7425591b213SSam Leffler 	ifp->if_ioctl = ath_ioctl;
7435591b213SSam Leffler 	ifp->if_init = ath_init;
744e50d35e6SMaxim Sobolev 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
745e50d35e6SMaxim Sobolev 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
746154b8df2SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
7475591b213SSam Leffler 
748c42a7b7eSSam Leffler 	ic->ic_ifp = ifp;
7495591b213SSam Leffler 	/* XXX not right but it's not used anywhere important */
7505591b213SSam Leffler 	ic->ic_phytype = IEEE80211_T_OFDM;
7515591b213SSam Leffler 	ic->ic_opmode = IEEE80211_M_STA;
752c42a7b7eSSam Leffler 	ic->ic_caps =
753c43feedeSSam Leffler 		  IEEE80211_C_STA		/* station mode */
754c43feedeSSam Leffler 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
755fe32c3efSSam Leffler 		| IEEE80211_C_HOSTAP		/* hostap mode */
756fe32c3efSSam Leffler 		| IEEE80211_C_MONITOR		/* monitor mode */
7577a04dc27SSam Leffler 		| IEEE80211_C_AHDEMO		/* adhoc demo mode */
758b032f27cSSam Leffler 		| IEEE80211_C_WDS		/* 4-address traffic works */
75959aa14a9SRui Paulo 		| IEEE80211_C_MBSS		/* mesh point link mode */
760fe32c3efSSam Leffler 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
761c42a7b7eSSam Leffler 		| IEEE80211_C_SHSLOT		/* short slot time supported */
762c42a7b7eSSam Leffler 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
7633b324f57SAdrian Chadd #ifndef	ATH_ENABLE_11N
76468e8e04eSSam Leffler 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
7653b324f57SAdrian Chadd #endif
76668e8e04eSSam Leffler 		| IEEE80211_C_TXFRAG		/* handle tx frags */
76710dc8de4SAdrian Chadd #ifdef	ATH_ENABLE_DFS
7687e97436bSAdrian Chadd 		| IEEE80211_C_DFS		/* Enable radar detection */
76910dc8de4SAdrian Chadd #endif
770f5c30c4eSAdrian Chadd 		| IEEE80211_C_PMGT		/* Station side power mgmt */
771f5c30c4eSAdrian Chadd 		| IEEE80211_C_SWSLEEP
77201e7e035SSam Leffler 		;
773c42a7b7eSSam Leffler 	/*
774c42a7b7eSSam Leffler 	 * Query the hal to figure out h/w crypto support.
775c42a7b7eSSam Leffler 	 */
776c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
777b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
778c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
779b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_OCB;
780c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
781b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_CCM;
782c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
783b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_CKIP;
784c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
785b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIP;
786c42a7b7eSSam Leffler 		/*
787c42a7b7eSSam Leffler 		 * Check if h/w does the MIC and/or whether the
788c42a7b7eSSam Leffler 		 * separate key cache entries are required to
789c42a7b7eSSam Leffler 		 * handle both tx+rx MIC keys.
790c42a7b7eSSam Leffler 		 */
791c42a7b7eSSam Leffler 		if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
792b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
7935901d2d3SSam Leffler 		/*
7945901d2d3SSam Leffler 		 * If the h/w supports storing tx+rx MIC keys
7955901d2d3SSam Leffler 		 * in one cache slot automatically enable use.
7965901d2d3SSam Leffler 		 */
7975901d2d3SSam Leffler 		if (ath_hal_hastkipsplit(ah) ||
7985901d2d3SSam Leffler 		    !ath_hal_settkipsplit(ah, AH_FALSE))
799c42a7b7eSSam Leffler 			sc->sc_splitmic = 1;
800b032f27cSSam Leffler 		/*
801b032f27cSSam Leffler 		 * If the h/w can do TKIP MIC together with WME then
802b032f27cSSam Leffler 		 * we use it; otherwise we force the MIC to be done
803b032f27cSSam Leffler 		 * in software by the net80211 layer.
804b032f27cSSam Leffler 		 */
805b032f27cSSam Leffler 		if (ath_hal_haswmetkipmic(ah))
806b032f27cSSam Leffler 			sc->sc_wmetkipmic = 1;
807c42a7b7eSSam Leffler 	}
808e8fd88a3SSam Leffler 	sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR);
8099ac01d39SRui Paulo 	/*
8101ac5dac2SRui Paulo 	 * Check for multicast key search support.
8119ac01d39SRui Paulo 	 */
8129ac01d39SRui Paulo 	if (ath_hal_hasmcastkeysearch(sc->sc_ah) &&
8139ac01d39SRui Paulo 	    !ath_hal_getmcastkeysearch(sc->sc_ah)) {
8149ac01d39SRui Paulo 		ath_hal_setmcastkeysearch(sc->sc_ah, 1);
8159ac01d39SRui Paulo 	}
816e8fd88a3SSam Leffler 	sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah);
817c42a7b7eSSam Leffler 	/*
8185901d2d3SSam Leffler 	 * Mark key cache slots associated with global keys
8195901d2d3SSam Leffler 	 * as in use.  If we knew TKIP was not to be used we
8205901d2d3SSam Leffler 	 * could leave the +32, +64, and +32+64 slots free.
8215901d2d3SSam Leffler 	 */
8225901d2d3SSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
8235901d2d3SSam Leffler 		setbit(sc->sc_keymap, i);
8245901d2d3SSam Leffler 		setbit(sc->sc_keymap, i+64);
8255901d2d3SSam Leffler 		if (sc->sc_splitmic) {
8265901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32);
8275901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32+64);
8285901d2d3SSam Leffler 		}
8295901d2d3SSam Leffler 	}
8305901d2d3SSam Leffler 	/*
831c42a7b7eSSam Leffler 	 * TPC support can be done either with a global cap or
832c42a7b7eSSam Leffler 	 * per-packet support.  The latter is not available on
833c42a7b7eSSam Leffler 	 * all parts.  We're a bit pedantic here as all parts
834c42a7b7eSSam Leffler 	 * support a global cap.
835c42a7b7eSSam Leffler 	 */
836c59005e9SSam Leffler 	if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah))
837c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TXPMGT;
838c42a7b7eSSam Leffler 
839c42a7b7eSSam Leffler 	/*
840c42a7b7eSSam Leffler 	 * Mark WME capability only if we have sufficient
841c42a7b7eSSam Leffler 	 * hardware queues to do proper priority scheduling.
842c42a7b7eSSam Leffler 	 */
843c42a7b7eSSam Leffler 	if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
844c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_WME;
845c42a7b7eSSam Leffler 	/*
846e8fd88a3SSam Leffler 	 * Check for misc other capabilities.
847c42a7b7eSSam Leffler 	 */
848c42a7b7eSSam Leffler 	if (ath_hal_hasbursting(ah))
849c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_BURST;
850b032f27cSSam Leffler 	sc->sc_hasbmask = ath_hal_hasbssidmask(ah);
85159aa14a9SRui Paulo 	sc->sc_hasbmatch = ath_hal_hasbssidmatch(ah);
852b032f27cSSam Leffler 	sc->sc_hastsfadd = ath_hal_hastsfadjust(ah);
8538a2a6beeSAdrian Chadd 	sc->sc_rxslink = ath_hal_self_linked_final_rxdesc(ah);
854fc4de9b7SAdrian Chadd 	sc->sc_rxtsf32 = ath_hal_has_long_rxdesc_tsf(ah);
855dd6a574eSAdrian Chadd 	sc->sc_hasenforcetxop = ath_hal_hasenforcetxop(ah);
8563df7a8abSAdrian Chadd 	sc->sc_rx_lnamixer = ath_hal_hasrxlnamixer(ah);
857216ca234SAdrian Chadd 	sc->sc_hasdivcomb = ath_hal_hasdivantcomb(ah);
858216ca234SAdrian Chadd 
85968e8e04eSSam Leffler 	if (ath_hal_hasfastframes(ah))
86068e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_FF;
86159efa8b5SSam Leffler 	wmodes = ath_hal_getwirelessmodes(ah);
862411373ebSSam Leffler 	if (wmodes & (HAL_MODE_108G|HAL_MODE_TURBO))
86368e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TURBOP;
864584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
86510ad9a77SSam Leffler 	if (ath_hal_macversion(ah) > 0x78) {
86610ad9a77SSam Leffler 		ic->ic_caps |= IEEE80211_C_TDMA; /* capable of TDMA */
86710ad9a77SSam Leffler 		ic->ic_tdma_update = ath_tdma_update;
86810ad9a77SSam Leffler 	}
86910ad9a77SSam Leffler #endif
87067397d39SAdrian Chadd 
87167397d39SAdrian Chadd 	/*
8729c85ff91SAdrian Chadd 	 * TODO: enforce that at least this many frames are available
8739c85ff91SAdrian Chadd 	 * in the txbuf list before allowing data frames (raw or
8749c85ff91SAdrian Chadd 	 * otherwise) to be transmitted.
8759c85ff91SAdrian Chadd 	 */
8769c85ff91SAdrian Chadd 	sc->sc_txq_data_minfree = 10;
8779c85ff91SAdrian Chadd 	/*
8789c85ff91SAdrian Chadd 	 * Leave this as default to maintain legacy behaviour.
8799c85ff91SAdrian Chadd 	 * Shortening the cabq/mcastq may end up causing some
8809c85ff91SAdrian Chadd 	 * undesirable behaviour.
8819c85ff91SAdrian Chadd 	 */
8829c85ff91SAdrian Chadd 	sc->sc_txq_mcastq_maxdepth = ath_txbuf;
8839c85ff91SAdrian Chadd 
8847dcb2beaSAdrian Chadd 	/*
88522a3aee6SAdrian Chadd 	 * How deep can the node software TX queue get whilst it's asleep.
88622a3aee6SAdrian Chadd 	 */
88722a3aee6SAdrian Chadd 	sc->sc_txq_node_psq_maxdepth = 16;
88822a3aee6SAdrian Chadd 
88922a3aee6SAdrian Chadd 	/*
8907dcb2beaSAdrian Chadd 	 * Default the maximum queue depth for a given node
8917dcb2beaSAdrian Chadd 	 * to 1/4'th the TX buffers, or 64, whichever
8927dcb2beaSAdrian Chadd 	 * is larger.
8937dcb2beaSAdrian Chadd 	 */
8947dcb2beaSAdrian Chadd 	sc->sc_txq_node_maxdepth = MAX(64, ath_txbuf / 4);
8957dcb2beaSAdrian Chadd 
896b837332dSAdrian Chadd 	/* Enable CABQ by default */
897b837332dSAdrian Chadd 	sc->sc_cabq_enable = 1;
898b837332dSAdrian Chadd 
8999c85ff91SAdrian Chadd 	/*
900a865860dSAdrian Chadd 	 * Allow the TX and RX chainmasks to be overridden by
901a865860dSAdrian Chadd 	 * environment variables and/or device.hints.
902a865860dSAdrian Chadd 	 *
903a865860dSAdrian Chadd 	 * This must be done early - before the hardware is
904a865860dSAdrian Chadd 	 * calibrated or before the 802.11n stream calculation
905a865860dSAdrian Chadd 	 * is done.
906a865860dSAdrian Chadd 	 */
907a865860dSAdrian Chadd 	if (resource_int_value(device_get_name(sc->sc_dev),
908a865860dSAdrian Chadd 	    device_get_unit(sc->sc_dev), "rx_chainmask",
909a865860dSAdrian Chadd 	    &rx_chainmask) == 0) {
910a865860dSAdrian Chadd 		device_printf(sc->sc_dev, "Setting RX chainmask to 0x%x\n",
911a865860dSAdrian Chadd 		    rx_chainmask);
912a865860dSAdrian Chadd 		(void) ath_hal_setrxchainmask(sc->sc_ah, rx_chainmask);
913a865860dSAdrian Chadd 	}
914a865860dSAdrian Chadd 	if (resource_int_value(device_get_name(sc->sc_dev),
915a865860dSAdrian Chadd 	    device_get_unit(sc->sc_dev), "tx_chainmask",
916a865860dSAdrian Chadd 	    &tx_chainmask) == 0) {
917a865860dSAdrian Chadd 		device_printf(sc->sc_dev, "Setting TX chainmask to 0x%x\n",
918a865860dSAdrian Chadd 		    tx_chainmask);
919dc8552d5SAdrian Chadd 		(void) ath_hal_settxchainmask(sc->sc_ah, tx_chainmask);
920a865860dSAdrian Chadd 	}
921a865860dSAdrian Chadd 
922af017101SAdrian Chadd 	/*
923ff5b5634SAdrian Chadd 	 * Query the TX/RX chainmask configuration.
924ff5b5634SAdrian Chadd 	 *
925ff5b5634SAdrian Chadd 	 * This is only relevant for 11n devices.
926ff5b5634SAdrian Chadd 	 */
927ff5b5634SAdrian Chadd 	ath_hal_getrxchainmask(ah, &sc->sc_rxchainmask);
928ff5b5634SAdrian Chadd 	ath_hal_gettxchainmask(ah, &sc->sc_txchainmask);
929ff5b5634SAdrian Chadd 
930ff5b5634SAdrian Chadd 	/*
931af017101SAdrian Chadd 	 * Disable MRR with protected frames by default.
932af017101SAdrian Chadd 	 * Only 802.11n series NICs can handle this.
933af017101SAdrian Chadd 	 */
934af017101SAdrian Chadd 	sc->sc_mrrprot = 0;	/* XXX should be a capability */
935af017101SAdrian Chadd 
9365540369bSAdrian Chadd 	/*
9375540369bSAdrian Chadd 	 * Query the enterprise mode information the HAL.
9385540369bSAdrian Chadd 	 */
9395540369bSAdrian Chadd 	if (ath_hal_getcapability(ah, HAL_CAP_ENTERPRISE_MODE, 0,
9405540369bSAdrian Chadd 	    &sc->sc_ent_cfg) == HAL_OK)
9415540369bSAdrian Chadd 		sc->sc_use_ent = 1;
9425540369bSAdrian Chadd 
9438fd67f92SAdrian Chadd #ifdef	ATH_ENABLE_11N
94467397d39SAdrian Chadd 	/*
94567397d39SAdrian Chadd 	 * Query HT capabilities
94667397d39SAdrian Chadd 	 */
94767397d39SAdrian Chadd 	if (ath_hal_getcapability(ah, HAL_CAP_HT, 0, NULL) == HAL_OK &&
94867397d39SAdrian Chadd 	    (wmodes & (HAL_MODE_HT20 | HAL_MODE_HT40))) {
9496f4fb2d8SAdrian Chadd 		uint32_t rxs, txs;
95067397d39SAdrian Chadd 
95167397d39SAdrian Chadd 		device_printf(sc->sc_dev, "[HT] enabling HT modes\n");
952af017101SAdrian Chadd 
953af017101SAdrian Chadd 		sc->sc_mrrprot = 1;	/* XXX should be a capability */
954af017101SAdrian Chadd 
95567397d39SAdrian Chadd 		ic->ic_htcaps = IEEE80211_HTC_HT	/* HT operation */
95667397d39SAdrian Chadd 			    | IEEE80211_HTC_AMPDU	/* A-MPDU tx/rx */
95767397d39SAdrian Chadd 			    | IEEE80211_HTC_AMSDU	/* A-MSDU tx/rx */
9587e97436bSAdrian Chadd 			    | IEEE80211_HTCAP_MAXAMSDU_3839
9597e97436bSAdrian Chadd 			    				/* max A-MSDU length */
96067397d39SAdrian Chadd 			    | IEEE80211_HTCAP_SMPS_OFF;	/* SM power save off */
96167397d39SAdrian Chadd 			;
96267397d39SAdrian Chadd 
96376355edbSAdrian Chadd 		/*
96476355edbSAdrian Chadd 		 * Enable short-GI for HT20 only if the hardware
96576355edbSAdrian Chadd 		 * advertises support.
96676355edbSAdrian Chadd 		 * Notably, anything earlier than the AR9287 doesn't.
96776355edbSAdrian Chadd 		 */
96876355edbSAdrian Chadd 		if ((ath_hal_getcapability(ah,
96976355edbSAdrian Chadd 		    HAL_CAP_HT20_SGI, 0, NULL) == HAL_OK) &&
97076355edbSAdrian Chadd 		    (wmodes & HAL_MODE_HT20)) {
97176355edbSAdrian Chadd 			device_printf(sc->sc_dev,
97276355edbSAdrian Chadd 			    "[HT] enabling short-GI in 20MHz mode\n");
97376355edbSAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
97476355edbSAdrian Chadd 		}
97576355edbSAdrian Chadd 
97667397d39SAdrian Chadd 		if (wmodes & HAL_MODE_HT40)
97767397d39SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40
97867397d39SAdrian Chadd 			    |  IEEE80211_HTCAP_SHORTGI40;
97967397d39SAdrian Chadd 
98067397d39SAdrian Chadd 		/*
9817e97436bSAdrian Chadd 		 * TX/RX streams need to be taken into account when
9827e97436bSAdrian Chadd 		 * negotiating which MCS rates it'll receive and
98367397d39SAdrian Chadd 		 * what MCS rates are available for TX.
98467397d39SAdrian Chadd 		 */
98554517070SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 0, &txs);
98654517070SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 1, &rxs);
98767397d39SAdrian Chadd 		ic->ic_txstream = txs;
98867397d39SAdrian Chadd 		ic->ic_rxstream = rxs;
98967397d39SAdrian Chadd 
9906606ba81SAdrian Chadd 		/*
9916606ba81SAdrian Chadd 		 * Setup TX and RX STBC based on what the HAL allows and
9926606ba81SAdrian Chadd 		 * the currently configured chainmask set.
9936606ba81SAdrian Chadd 		 * Ie - don't enable STBC TX if only one chain is enabled.
9946606ba81SAdrian Chadd 		 * STBC RX is fine on a single RX chain; it just won't
9956606ba81SAdrian Chadd 		 * provide any real benefit.
9966606ba81SAdrian Chadd 		 */
9976606ba81SAdrian Chadd 		if (ath_hal_getcapability(ah, HAL_CAP_RX_STBC, 0,
9986606ba81SAdrian Chadd 		    NULL) == HAL_OK) {
9996606ba81SAdrian Chadd 			sc->sc_rx_stbc = 1;
10006606ba81SAdrian Chadd 			device_printf(sc->sc_dev,
10016606ba81SAdrian Chadd 			    "[HT] 1 stream STBC receive enabled\n");
10026606ba81SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_1STREAM;
10036606ba81SAdrian Chadd 		}
10046606ba81SAdrian Chadd 		if (txs > 1 && ath_hal_getcapability(ah, HAL_CAP_TX_STBC, 0,
10056606ba81SAdrian Chadd 		    NULL) == HAL_OK) {
10066606ba81SAdrian Chadd 			sc->sc_tx_stbc = 1;
10076606ba81SAdrian Chadd 			device_printf(sc->sc_dev,
10086606ba81SAdrian Chadd 			    "[HT] 1 stream STBC transmit enabled\n");
10096606ba81SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
10106606ba81SAdrian Chadd 		}
10116606ba81SAdrian Chadd 
1012ce656facSAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_RTS_AGGR_LIMIT, 1,
1013ce656facSAdrian Chadd 		    &sc->sc_rts_aggr_limit);
1014ce656facSAdrian Chadd 		if (sc->sc_rts_aggr_limit != (64 * 1024))
1015ce656facSAdrian Chadd 			device_printf(sc->sc_dev,
1016ce656facSAdrian Chadd 			    "[HT] RTS aggregates limited to %d KiB\n",
1017ce656facSAdrian Chadd 			    sc->sc_rts_aggr_limit / 1024);
1018ce656facSAdrian Chadd 
10197e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
10207e97436bSAdrian Chadd 		    "[HT] %d RX streams; %d TX streams\n", rxs, txs);
102167397d39SAdrian Chadd 	}
102267397d39SAdrian Chadd #endif
102367397d39SAdrian Chadd 
1024c42a7b7eSSam Leffler 	/*
1025f8aa9fd5SAdrian Chadd 	 * Initial aggregation settings.
1026f8aa9fd5SAdrian Chadd 	 */
102772910f03SAdrian Chadd 	sc->sc_hwq_limit_aggr = ATH_AGGR_MIN_QDEPTH;
102872910f03SAdrian Chadd 	sc->sc_hwq_limit_nonaggr = ATH_NONAGGR_MIN_QDEPTH;
1029f8aa9fd5SAdrian Chadd 	sc->sc_tid_hwq_lo = ATH_AGGR_SCHED_LOW;
1030f8aa9fd5SAdrian Chadd 	sc->sc_tid_hwq_hi = ATH_AGGR_SCHED_HIGH;
10314a502c33SAdrian Chadd 	sc->sc_aggr_limit = ATH_AGGR_MAXSIZE;
1032a54ecf78SAdrian Chadd 	sc->sc_delim_min_pad = 0;
1033f8aa9fd5SAdrian Chadd 
1034f8aa9fd5SAdrian Chadd 	/*
1035ddbe3036SAdrian Chadd 	 * Check if the hardware requires PCI register serialisation.
1036ddbe3036SAdrian Chadd 	 * Some of the Owl based MACs require this.
1037ddbe3036SAdrian Chadd 	 */
1038ddbe3036SAdrian Chadd 	if (mp_ncpus > 1 &&
1039ddbe3036SAdrian Chadd 	    ath_hal_getcapability(ah, HAL_CAP_SERIALISE_WAR,
1040ddbe3036SAdrian Chadd 	     0, NULL) == HAL_OK) {
1041ddbe3036SAdrian Chadd 		sc->sc_ah->ah_config.ah_serialise_reg_war = 1;
10427e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
10437e97436bSAdrian Chadd 		    "Enabling register serialisation\n");
1044ddbe3036SAdrian Chadd 	}
1045ddbe3036SAdrian Chadd 
1046ddbe3036SAdrian Chadd 	/*
1047f0db652cSAdrian Chadd 	 * Initialise the deferred completed RX buffer list.
1048f0db652cSAdrian Chadd 	 */
10495d4dedadSAdrian Chadd 	TAILQ_INIT(&sc->sc_rx_rxlist[HAL_RX_QUEUE_HP]);
10505d4dedadSAdrian Chadd 	TAILQ_INIT(&sc->sc_rx_rxlist[HAL_RX_QUEUE_LP]);
1051f0db652cSAdrian Chadd 
1052f0db652cSAdrian Chadd 	/*
1053c42a7b7eSSam Leffler 	 * Indicate we need the 802.11 header padded to a
1054c42a7b7eSSam Leffler 	 * 32-bit boundary for 4-address and QoS frames.
1055c42a7b7eSSam Leffler 	 */
1056c42a7b7eSSam Leffler 	ic->ic_flags |= IEEE80211_F_DATAPAD;
1057c42a7b7eSSam Leffler 
1058c42a7b7eSSam Leffler 	/*
1059c42a7b7eSSam Leffler 	 * Query the hal about antenna support.
1060c42a7b7eSSam Leffler 	 */
1061c42a7b7eSSam Leffler 	sc->sc_defant = ath_hal_getdefantenna(ah);
1062c42a7b7eSSam Leffler 
1063c42a7b7eSSam Leffler 	/*
1064c42a7b7eSSam Leffler 	 * Not all chips have the VEOL support we want to
1065c42a7b7eSSam Leffler 	 * use with IBSS beacons; check here for it.
1066c42a7b7eSSam Leffler 	 */
1067c42a7b7eSSam Leffler 	sc->sc_hasveol = ath_hal_hasveol(ah);
10685591b213SSam Leffler 
10695591b213SSam Leffler 	/* get mac address from hardware */
107029aca940SSam Leffler 	ath_hal_getmac(ah, macaddr);
1071b032f27cSSam Leffler 	if (sc->sc_hasbmask)
1072b032f27cSSam Leffler 		ath_hal_getbssidmask(ah, sc->sc_hwbssidmask);
10735591b213SSam Leffler 
1074b032f27cSSam Leffler 	/* NB: used to size node table key mapping array */
1075b032f27cSSam Leffler 	ic->ic_max_keyix = sc->sc_keymax;
10765591b213SSam Leffler 	/* call MI attach routine. */
107729aca940SSam Leffler 	ieee80211_ifattach(ic, macaddr);
1078b032f27cSSam Leffler 	ic->ic_setregdomain = ath_setregdomain;
1079b032f27cSSam Leffler 	ic->ic_getradiocaps = ath_getradiocaps;
1080b032f27cSSam Leffler 	sc->sc_opmode = HAL_M_STA;
1081b032f27cSSam Leffler 
10825591b213SSam Leffler 	/* override default methods */
1083b032f27cSSam Leffler 	ic->ic_newassoc = ath_newassoc;
1084b032f27cSSam Leffler 	ic->ic_updateslot = ath_updateslot;
1085b032f27cSSam Leffler 	ic->ic_wme.wme_update = ath_wme_update;
1086b032f27cSSam Leffler 	ic->ic_vap_create = ath_vap_create;
1087b032f27cSSam Leffler 	ic->ic_vap_delete = ath_vap_delete;
1088b032f27cSSam Leffler 	ic->ic_raw_xmit = ath_raw_xmit;
1089b032f27cSSam Leffler 	ic->ic_update_mcast = ath_update_mcast;
1090b032f27cSSam Leffler 	ic->ic_update_promisc = ath_update_promisc;
10915591b213SSam Leffler 	ic->ic_node_alloc = ath_node_alloc;
10921e774079SSam Leffler 	sc->sc_node_free = ic->ic_node_free;
10935591b213SSam Leffler 	ic->ic_node_free = ath_node_free;
10944afa805eSAdrian Chadd 	sc->sc_node_cleanup = ic->ic_node_cleanup;
10954afa805eSAdrian Chadd 	ic->ic_node_cleanup = ath_node_cleanup;
109668e8e04eSSam Leffler 	ic->ic_node_getsignal = ath_node_getsignal;
109768e8e04eSSam Leffler 	ic->ic_scan_start = ath_scan_start;
109868e8e04eSSam Leffler 	ic->ic_scan_end = ath_scan_end;
109968e8e04eSSam Leffler 	ic->ic_set_channel = ath_set_channel;
1100fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
1101eb6f0de0SAdrian Chadd 	/* 802.11n specific - but just override anyway */
1102eb6f0de0SAdrian Chadd 	sc->sc_addba_request = ic->ic_addba_request;
1103eb6f0de0SAdrian Chadd 	sc->sc_addba_response = ic->ic_addba_response;
1104eb6f0de0SAdrian Chadd 	sc->sc_addba_stop = ic->ic_addba_stop;
1105eb6f0de0SAdrian Chadd 	sc->sc_bar_response = ic->ic_bar_response;
1106eb6f0de0SAdrian Chadd 	sc->sc_addba_response_timeout = ic->ic_addba_response_timeout;
1107eb6f0de0SAdrian Chadd 
1108eb6f0de0SAdrian Chadd 	ic->ic_addba_request = ath_addba_request;
1109eb6f0de0SAdrian Chadd 	ic->ic_addba_response = ath_addba_response;
1110eb6f0de0SAdrian Chadd 	ic->ic_addba_response_timeout = ath_addba_response_timeout;
1111eb6f0de0SAdrian Chadd 	ic->ic_addba_stop = ath_addba_stop;
1112eb6f0de0SAdrian Chadd 	ic->ic_bar_response = ath_bar_response;
1113eb6f0de0SAdrian Chadd 
1114fdd72b4aSAdrian Chadd 	ic->ic_update_chw = ath_update_chw;
1115fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
1116fdd72b4aSAdrian Chadd 
1117e1b5ab97SAdrian Chadd #ifdef	ATH_ENABLE_RADIOTAP_VENDOR_EXT
1118e1b5ab97SAdrian Chadd 	/*
1119e1b5ab97SAdrian Chadd 	 * There's one vendor bitmap entry in the RX radiotap
1120e1b5ab97SAdrian Chadd 	 * header; make sure that's taken into account.
1121e1b5ab97SAdrian Chadd 	 */
1122e1b5ab97SAdrian Chadd 	ieee80211_radiotap_attachv(ic,
1123e1b5ab97SAdrian Chadd 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th), 0,
1124e1b5ab97SAdrian Chadd 		ATH_TX_RADIOTAP_PRESENT,
1125e1b5ab97SAdrian Chadd 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th), 1,
1126e1b5ab97SAdrian Chadd 		ATH_RX_RADIOTAP_PRESENT);
1127e1b5ab97SAdrian Chadd #else
1128e1b5ab97SAdrian Chadd 	/*
1129e1b5ab97SAdrian Chadd 	 * No vendor bitmap/extensions are present.
1130e1b5ab97SAdrian Chadd 	 */
11315463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
11325463c4a4SSam Leffler 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
11335463c4a4SSam Leffler 		ATH_TX_RADIOTAP_PRESENT,
11345463c4a4SSam Leffler 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
11355463c4a4SSam Leffler 		ATH_RX_RADIOTAP_PRESENT);
1136e1b5ab97SAdrian Chadd #endif	/* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
11375463c4a4SSam Leffler 
11384866e6c2SSam Leffler 	/*
1139bdbb6e5bSAdrian Chadd 	 * Setup the ALQ logging if required
1140bdbb6e5bSAdrian Chadd 	 */
114189d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
1142bdbb6e5bSAdrian Chadd 	if_ath_alq_init(&sc->sc_alq, device_get_nameunit(sc->sc_dev));
1143bb327d28SAdrian Chadd 	if_ath_alq_setcfg(&sc->sc_alq,
1144bb327d28SAdrian Chadd 	    sc->sc_ah->ah_macVersion,
1145bb327d28SAdrian Chadd 	    sc->sc_ah->ah_macRev,
1146bb327d28SAdrian Chadd 	    sc->sc_ah->ah_phyRev,
1147bb327d28SAdrian Chadd 	    sc->sc_ah->ah_magic);
1148bdbb6e5bSAdrian Chadd #endif
1149bdbb6e5bSAdrian Chadd 
1150bdbb6e5bSAdrian Chadd 	/*
11514866e6c2SSam Leffler 	 * Setup dynamic sysctl's now that country code and
11524866e6c2SSam Leffler 	 * regdomain are available from the hal.
11534866e6c2SSam Leffler 	 */
11544866e6c2SSam Leffler 	ath_sysctlattach(sc);
1155e8dabfbeSAdrian Chadd 	ath_sysctl_stats_attach(sc);
115637931a35SAdrian Chadd 	ath_sysctl_hal_attach(sc);
115773454c73SSam Leffler 
1158c42a7b7eSSam Leffler 	if (bootverbose)
1159c42a7b7eSSam Leffler 		ieee80211_announce(ic);
1160c42a7b7eSSam Leffler 	ath_announce(sc);
1161f5c30c4eSAdrian Chadd 
1162f5c30c4eSAdrian Chadd 	/*
1163f5c30c4eSAdrian Chadd 	 * Put it to sleep for now.
1164f5c30c4eSAdrian Chadd 	 */
1165f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1166f5c30c4eSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_FULL_SLEEP);
1167f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1168f5c30c4eSAdrian Chadd 
11695591b213SSam Leffler 	return 0;
1170b28b4653SSam Leffler bad2:
1171c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
1172b28b4653SSam Leffler 	ath_desc_free(sc);
11733fdfc330SAdrian Chadd 	ath_txdma_teardown(sc);
11743d184db2SAdrian Chadd 	ath_rxdma_teardown(sc);
11755591b213SSam Leffler bad:
11765591b213SSam Leffler 	if (ah)
11775591b213SSam Leffler 		ath_hal_detach(ah);
11788bf40208SAdrian Chadd 
11798bf40208SAdrian Chadd 	/*
11808bf40208SAdrian Chadd 	 * To work around scoping issues with CURVNET_SET/CURVNET_RESTORE..
11818bf40208SAdrian Chadd 	 */
11828bf40208SAdrian Chadd 	if (ifp != NULL && ifp->if_vnet) {
1183a93c5097SAdrian Chadd 		CURVNET_SET(ifp->if_vnet);
1184fc74a9f9SBrooks Davis 		if_free(ifp);
1185a93c5097SAdrian Chadd 		CURVNET_RESTORE();
11868bf40208SAdrian Chadd 	} else if (ifp != NULL)
11878bf40208SAdrian Chadd 		if_free(ifp);
11885591b213SSam Leffler 	sc->sc_invalid = 1;
11895591b213SSam Leffler 	return error;
11905591b213SSam Leffler }
11915591b213SSam Leffler 
11925591b213SSam Leffler int
11935591b213SSam Leffler ath_detach(struct ath_softc *sc)
11945591b213SSam Leffler {
1195fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
11965591b213SSam Leffler 
1197c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1198c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
11995591b213SSam Leffler 
1200c42a7b7eSSam Leffler 	/*
1201c42a7b7eSSam Leffler 	 * NB: the order of these is important:
120271b85077SSam Leffler 	 * o stop the chip so no more interrupts will fire
1203c42a7b7eSSam Leffler 	 * o call the 802.11 layer before detaching the hal to
1204c42a7b7eSSam Leffler 	 *   insure callbacks into the driver to delete global
1205c42a7b7eSSam Leffler 	 *   key cache entries can be handled
120671b85077SSam Leffler 	 * o free the taskqueue which drains any pending tasks
1207c42a7b7eSSam Leffler 	 * o reclaim the tx queue data structures after calling
1208c42a7b7eSSam Leffler 	 *   the 802.11 layer as we'll get called back to reclaim
1209c42a7b7eSSam Leffler 	 *   node state and potentially want to use them
1210c42a7b7eSSam Leffler 	 * o to cleanup the tx queues the hal is called, so detach
1211c42a7b7eSSam Leffler 	 *   it last
1212c42a7b7eSSam Leffler 	 * Other than that, it's straightforward...
1213c42a7b7eSSam Leffler 	 */
1214f5c30c4eSAdrian Chadd 
1215f5c30c4eSAdrian Chadd 	/*
1216f5c30c4eSAdrian Chadd 	 * XXX Wake the hardware up first.  ath_stop() will still
1217f5c30c4eSAdrian Chadd 	 * wake it up first, but I'd rather do it here just to
1218f5c30c4eSAdrian Chadd 	 * ensure it's awake.
1219f5c30c4eSAdrian Chadd 	 */
1220f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1221f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1222f5c30c4eSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_AWAKE);
1223f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1224f5c30c4eSAdrian Chadd 
1225f5c30c4eSAdrian Chadd 	/*
1226f5c30c4eSAdrian Chadd 	 * Stop things cleanly.
1227f5c30c4eSAdrian Chadd 	 */
122871b85077SSam Leffler 	ath_stop(ifp);
1229f5c30c4eSAdrian Chadd 
1230b032f27cSSam Leffler 	ieee80211_ifdetach(ifp->if_l2com);
123171b85077SSam Leffler 	taskqueue_free(sc->sc_tq);
123286e07743SSam Leffler #ifdef ATH_TX99_DIAG
123386e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
123486e07743SSam Leffler 		sc->sc_tx99->detach(sc->sc_tx99);
123586e07743SSam Leffler #endif
1236c42a7b7eSSam Leffler 	ath_rate_detach(sc->sc_rc);
123789d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
1238bdbb6e5bSAdrian Chadd 	if_ath_alq_tidyup(&sc->sc_alq);
1239bdbb6e5bSAdrian Chadd #endif
1240216ca234SAdrian Chadd 	ath_lna_div_detach(sc);
1241b70f530bSAdrian Chadd 	ath_btcoex_detach(sc);
12429af351f9SAdrian Chadd 	ath_spectral_detach(sc);
124348237774SAdrian Chadd 	ath_dfs_detach(sc);
12445591b213SSam Leffler 	ath_desc_free(sc);
12454bf404eaSAdrian Chadd 	ath_txdma_teardown(sc);
12463d184db2SAdrian Chadd 	ath_rxdma_teardown(sc);
1247c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
124871b85077SSam Leffler 	ath_hal_detach(sc->sc_ah);	/* NB: sets chip in full sleep */
1249a93c5097SAdrian Chadd 
1250a93c5097SAdrian Chadd 	CURVNET_SET(ifp->if_vnet);
1251c4c6f08fSRuslan Ermilov 	if_free(ifp);
1252a93c5097SAdrian Chadd 	CURVNET_RESTORE();
1253f0b2a0beSSam Leffler 
12545591b213SSam Leffler 	return 0;
12555591b213SSam Leffler }
12565591b213SSam Leffler 
1257b032f27cSSam Leffler /*
1258b032f27cSSam Leffler  * MAC address handling for multiple BSS on the same radio.
1259b032f27cSSam Leffler  * The first vap uses the MAC address from the EEPROM.  For
1260b032f27cSSam Leffler  * subsequent vap's we set the U/L bit (bit 1) in the MAC
1261b032f27cSSam Leffler  * address and use the next six bits as an index.
1262b032f27cSSam Leffler  */
1263b032f27cSSam Leffler static void
1264b032f27cSSam Leffler assign_address(struct ath_softc *sc, uint8_t mac[IEEE80211_ADDR_LEN], int clone)
1265b032f27cSSam Leffler {
1266b032f27cSSam Leffler 	int i;
1267b032f27cSSam Leffler 
1268b032f27cSSam Leffler 	if (clone && sc->sc_hasbmask) {
1269b032f27cSSam Leffler 		/* NB: we only do this if h/w supports multiple bssid */
1270b032f27cSSam Leffler 		for (i = 0; i < 8; i++)
1271b032f27cSSam Leffler 			if ((sc->sc_bssidmask & (1<<i)) == 0)
1272b032f27cSSam Leffler 				break;
1273b032f27cSSam Leffler 		if (i != 0)
1274b032f27cSSam Leffler 			mac[0] |= (i << 2)|0x2;
1275b032f27cSSam Leffler 	} else
1276b032f27cSSam Leffler 		i = 0;
1277b032f27cSSam Leffler 	sc->sc_bssidmask |= 1<<i;
1278b032f27cSSam Leffler 	sc->sc_hwbssidmask[0] &= ~mac[0];
1279b032f27cSSam Leffler 	if (i == 0)
1280b032f27cSSam Leffler 		sc->sc_nbssid0++;
1281b032f27cSSam Leffler }
1282b032f27cSSam Leffler 
1283b032f27cSSam Leffler static void
1284b032f27cSSam Leffler reclaim_address(struct ath_softc *sc, const uint8_t mac[IEEE80211_ADDR_LEN])
1285b032f27cSSam Leffler {
1286b032f27cSSam Leffler 	int i = mac[0] >> 2;
1287b032f27cSSam Leffler 	uint8_t mask;
1288b032f27cSSam Leffler 
1289b032f27cSSam Leffler 	if (i != 0 || --sc->sc_nbssid0 == 0) {
1290b032f27cSSam Leffler 		sc->sc_bssidmask &= ~(1<<i);
1291b032f27cSSam Leffler 		/* recalculate bssid mask from remaining addresses */
1292b032f27cSSam Leffler 		mask = 0xff;
1293b032f27cSSam Leffler 		for (i = 1; i < 8; i++)
1294b032f27cSSam Leffler 			if (sc->sc_bssidmask & (1<<i))
1295b032f27cSSam Leffler 				mask &= ~((i<<2)|0x2);
1296b032f27cSSam Leffler 		sc->sc_hwbssidmask[0] |= mask;
1297b032f27cSSam Leffler 	}
1298b032f27cSSam Leffler }
1299b032f27cSSam Leffler 
1300b032f27cSSam Leffler /*
1301b032f27cSSam Leffler  * Assign a beacon xmit slot.  We try to space out
1302b032f27cSSam Leffler  * assignments so when beacons are staggered the
1303b032f27cSSam Leffler  * traffic coming out of the cab q has maximal time
1304b032f27cSSam Leffler  * to go out before the next beacon is scheduled.
1305b032f27cSSam Leffler  */
1306b032f27cSSam Leffler static int
1307b032f27cSSam Leffler assign_bslot(struct ath_softc *sc)
1308b032f27cSSam Leffler {
1309b032f27cSSam Leffler 	u_int slot, free;
1310b032f27cSSam Leffler 
1311b032f27cSSam Leffler 	free = 0;
1312b032f27cSSam Leffler 	for (slot = 0; slot < ATH_BCBUF; slot++)
1313b032f27cSSam Leffler 		if (sc->sc_bslot[slot] == NULL) {
1314b032f27cSSam Leffler 			if (sc->sc_bslot[(slot+1)%ATH_BCBUF] == NULL &&
1315b032f27cSSam Leffler 			    sc->sc_bslot[(slot-1)%ATH_BCBUF] == NULL)
1316b032f27cSSam Leffler 				return slot;
1317b032f27cSSam Leffler 			free = slot;
1318b032f27cSSam Leffler 			/* NB: keep looking for a double slot */
1319b032f27cSSam Leffler 		}
1320b032f27cSSam Leffler 	return free;
1321b032f27cSSam Leffler }
1322b032f27cSSam Leffler 
1323b032f27cSSam Leffler static struct ieee80211vap *
1324fcd9500fSBernhard Schmidt ath_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
1325fcd9500fSBernhard Schmidt     enum ieee80211_opmode opmode, int flags,
1326b032f27cSSam Leffler     const uint8_t bssid[IEEE80211_ADDR_LEN],
1327b032f27cSSam Leffler     const uint8_t mac0[IEEE80211_ADDR_LEN])
1328b032f27cSSam Leffler {
1329b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
1330b032f27cSSam Leffler 	struct ath_vap *avp;
1331b032f27cSSam Leffler 	struct ieee80211vap *vap;
1332b032f27cSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN];
1333fcd9500fSBernhard Schmidt 	int needbeacon, error;
1334fcd9500fSBernhard Schmidt 	enum ieee80211_opmode ic_opmode;
1335b032f27cSSam Leffler 
1336b032f27cSSam Leffler 	avp = (struct ath_vap *) malloc(sizeof(struct ath_vap),
1337b032f27cSSam Leffler 	    M_80211_VAP, M_WAITOK | M_ZERO);
1338b032f27cSSam Leffler 	needbeacon = 0;
1339b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(mac, mac0);
1340b032f27cSSam Leffler 
1341b032f27cSSam Leffler 	ATH_LOCK(sc);
1342a8962181SSam Leffler 	ic_opmode = opmode;		/* default to opmode of new vap */
1343b032f27cSSam Leffler 	switch (opmode) {
1344b032f27cSSam Leffler 	case IEEE80211_M_STA:
1345a8962181SSam Leffler 		if (sc->sc_nstavaps != 0) {	/* XXX only 1 for now */
1346b032f27cSSam Leffler 			device_printf(sc->sc_dev, "only 1 sta vap supported\n");
1347b032f27cSSam Leffler 			goto bad;
1348b032f27cSSam Leffler 		}
1349b032f27cSSam Leffler 		if (sc->sc_nvaps) {
1350b032f27cSSam Leffler 			/*
1351a8962181SSam Leffler 			 * With multiple vaps we must fall back
1352a8962181SSam Leffler 			 * to s/w beacon miss handling.
1353b032f27cSSam Leffler 			 */
1354b032f27cSSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
1355b032f27cSSam Leffler 		}
1356a8962181SSam Leffler 		if (flags & IEEE80211_CLONE_NOBEACONS) {
1357a8962181SSam Leffler 			/*
1358a8962181SSam Leffler 			 * Station mode w/o beacons are implemented w/ AP mode.
1359a8962181SSam Leffler 			 */
1360b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
1361a8962181SSam Leffler 		}
1362b032f27cSSam Leffler 		break;
1363b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1364b032f27cSSam Leffler 		if (sc->sc_nvaps != 0) {	/* XXX only 1 for now */
1365b032f27cSSam Leffler 			device_printf(sc->sc_dev,
1366b032f27cSSam Leffler 			    "only 1 ibss vap supported\n");
1367b032f27cSSam Leffler 			goto bad;
1368b032f27cSSam Leffler 		}
1369b032f27cSSam Leffler 		needbeacon = 1;
1370b032f27cSSam Leffler 		break;
1371b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1372584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
137310ad9a77SSam Leffler 		if (flags & IEEE80211_CLONE_TDMA) {
1374a8962181SSam Leffler 			if (sc->sc_nvaps != 0) {
1375a8962181SSam Leffler 				device_printf(sc->sc_dev,
1376a8962181SSam Leffler 				    "only 1 tdma vap supported\n");
1377a8962181SSam Leffler 				goto bad;
1378a8962181SSam Leffler 			}
137910ad9a77SSam Leffler 			needbeacon = 1;
138010ad9a77SSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
138110ad9a77SSam Leffler 		}
1382b032f27cSSam Leffler 		/* fall thru... */
138310ad9a77SSam Leffler #endif
1384b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1385b032f27cSSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode != opmode) {
1386a8962181SSam Leffler 			/*
1387a8962181SSam Leffler 			 * Adopt existing mode.  Adding a monitor or ahdemo
1388a8962181SSam Leffler 			 * vap to an existing configuration is of dubious
1389a8962181SSam Leffler 			 * value but should be ok.
1390a8962181SSam Leffler 			 */
1391b032f27cSSam Leffler 			/* XXX not right for monitor mode */
1392b032f27cSSam Leffler 			ic_opmode = ic->ic_opmode;
1393a8962181SSam Leffler 		}
1394b032f27cSSam Leffler 		break;
1395b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
139659aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1397b032f27cSSam Leffler 		needbeacon = 1;
1398a8962181SSam Leffler 		break;
1399b032f27cSSam Leffler 	case IEEE80211_M_WDS:
1400a8962181SSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode == IEEE80211_M_STA) {
1401b032f27cSSam Leffler 			device_printf(sc->sc_dev,
1402b032f27cSSam Leffler 			    "wds not supported in sta mode\n");
1403b032f27cSSam Leffler 			goto bad;
1404b032f27cSSam Leffler 		}
1405b032f27cSSam Leffler 		/*
1406b032f27cSSam Leffler 		 * Silently remove any request for a unique
1407b032f27cSSam Leffler 		 * bssid; WDS vap's always share the local
1408b032f27cSSam Leffler 		 * mac address.
1409b032f27cSSam Leffler 		 */
1410b032f27cSSam Leffler 		flags &= ~IEEE80211_CLONE_BSSID;
1411a8962181SSam Leffler 		if (sc->sc_nvaps == 0)
1412b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
1413a8962181SSam Leffler 		else
1414a8962181SSam Leffler 			ic_opmode = ic->ic_opmode;
14157d261891SRui Paulo 		break;
1416b032f27cSSam Leffler 	default:
1417b032f27cSSam Leffler 		device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
1418b032f27cSSam Leffler 		goto bad;
1419b032f27cSSam Leffler 	}
1420b032f27cSSam Leffler 	/*
1421b032f27cSSam Leffler 	 * Check that a beacon buffer is available; the code below assumes it.
1422b032f27cSSam Leffler 	 */
14236b349e5aSAdrian Chadd 	if (needbeacon & TAILQ_EMPTY(&sc->sc_bbuf)) {
1424b032f27cSSam Leffler 		device_printf(sc->sc_dev, "no beacon buffer available\n");
1425b032f27cSSam Leffler 		goto bad;
1426b032f27cSSam Leffler 	}
1427b032f27cSSam Leffler 
1428b032f27cSSam Leffler 	/* STA, AHDEMO? */
142959aa14a9SRui Paulo 	if (opmode == IEEE80211_M_HOSTAP || opmode == IEEE80211_M_MBSS) {
1430b032f27cSSam Leffler 		assign_address(sc, mac, flags & IEEE80211_CLONE_BSSID);
1431b032f27cSSam Leffler 		ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1432b032f27cSSam Leffler 	}
1433b032f27cSSam Leffler 
1434b032f27cSSam Leffler 	vap = &avp->av_vap;
1435b032f27cSSam Leffler 	/* XXX can't hold mutex across if_alloc */
1436b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1437b032f27cSSam Leffler 	error = ieee80211_vap_setup(ic, vap, name, unit, opmode, flags,
1438b032f27cSSam Leffler 	    bssid, mac);
1439b032f27cSSam Leffler 	ATH_LOCK(sc);
1440b032f27cSSam Leffler 	if (error != 0) {
1441b032f27cSSam Leffler 		device_printf(sc->sc_dev, "%s: error %d creating vap\n",
1442b032f27cSSam Leffler 		    __func__, error);
1443b032f27cSSam Leffler 		goto bad2;
1444b032f27cSSam Leffler 	}
1445b032f27cSSam Leffler 
1446b032f27cSSam Leffler 	/* h/w crypto support */
1447b032f27cSSam Leffler 	vap->iv_key_alloc = ath_key_alloc;
1448b032f27cSSam Leffler 	vap->iv_key_delete = ath_key_delete;
1449b032f27cSSam Leffler 	vap->iv_key_set = ath_key_set;
1450b032f27cSSam Leffler 	vap->iv_key_update_begin = ath_key_update_begin;
1451b032f27cSSam Leffler 	vap->iv_key_update_end = ath_key_update_end;
1452b032f27cSSam Leffler 
1453b032f27cSSam Leffler 	/* override various methods */
1454b032f27cSSam Leffler 	avp->av_recv_mgmt = vap->iv_recv_mgmt;
1455b032f27cSSam Leffler 	vap->iv_recv_mgmt = ath_recv_mgmt;
1456b032f27cSSam Leffler 	vap->iv_reset = ath_reset_vap;
1457b032f27cSSam Leffler 	vap->iv_update_beacon = ath_beacon_update;
1458b032f27cSSam Leffler 	avp->av_newstate = vap->iv_newstate;
1459b032f27cSSam Leffler 	vap->iv_newstate = ath_newstate;
1460b032f27cSSam Leffler 	avp->av_bmiss = vap->iv_bmiss;
1461b032f27cSSam Leffler 	vap->iv_bmiss = ath_bmiss_vap;
1462b032f27cSSam Leffler 
14630eb81626SAdrian Chadd 	avp->av_node_ps = vap->iv_node_ps;
14640eb81626SAdrian Chadd 	vap->iv_node_ps = ath_node_powersave;
14650eb81626SAdrian Chadd 
1466548a605dSAdrian Chadd 	avp->av_set_tim = vap->iv_set_tim;
1467548a605dSAdrian Chadd 	vap->iv_set_tim = ath_node_set_tim;
1468548a605dSAdrian Chadd 
146922a3aee6SAdrian Chadd 	avp->av_recv_pspoll = vap->iv_recv_pspoll;
147022a3aee6SAdrian Chadd 	vap->iv_recv_pspoll = ath_node_recv_pspoll;
147122a3aee6SAdrian Chadd 
14729be25f4aSAdrian Chadd 	/* Set default parameters */
14739be25f4aSAdrian Chadd 
14749be25f4aSAdrian Chadd 	/*
14759be25f4aSAdrian Chadd 	 * Anything earlier than some AR9300 series MACs don't
14769be25f4aSAdrian Chadd 	 * support a smaller MPDU density.
14779be25f4aSAdrian Chadd 	 */
14789be25f4aSAdrian Chadd 	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_8;
14799be25f4aSAdrian Chadd 	/*
14809be25f4aSAdrian Chadd 	 * All NICs can handle the maximum size, however
14819be25f4aSAdrian Chadd 	 * AR5416 based MACs can only TX aggregates w/ RTS
14829be25f4aSAdrian Chadd 	 * protection when the total aggregate size is <= 8k.
14839be25f4aSAdrian Chadd 	 * However, for now that's enforced by the TX path.
14849be25f4aSAdrian Chadd 	 */
14859be25f4aSAdrian Chadd 	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K;
14869be25f4aSAdrian Chadd 
1487b032f27cSSam Leffler 	avp->av_bslot = -1;
1488b032f27cSSam Leffler 	if (needbeacon) {
1489b032f27cSSam Leffler 		/*
1490b032f27cSSam Leffler 		 * Allocate beacon state and setup the q for buffered
1491b032f27cSSam Leffler 		 * multicast frames.  We know a beacon buffer is
1492b032f27cSSam Leffler 		 * available because we checked above.
1493b032f27cSSam Leffler 		 */
14946b349e5aSAdrian Chadd 		avp->av_bcbuf = TAILQ_FIRST(&sc->sc_bbuf);
14956b349e5aSAdrian Chadd 		TAILQ_REMOVE(&sc->sc_bbuf, avp->av_bcbuf, bf_list);
1496b032f27cSSam Leffler 		if (opmode != IEEE80211_M_IBSS || !sc->sc_hasveol) {
1497b032f27cSSam Leffler 			/*
1498b032f27cSSam Leffler 			 * Assign the vap to a beacon xmit slot.  As above
1499b032f27cSSam Leffler 			 * this cannot fail to find a free one.
1500b032f27cSSam Leffler 			 */
1501b032f27cSSam Leffler 			avp->av_bslot = assign_bslot(sc);
1502b032f27cSSam Leffler 			KASSERT(sc->sc_bslot[avp->av_bslot] == NULL,
1503b032f27cSSam Leffler 			    ("beacon slot %u not empty", avp->av_bslot));
1504b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = vap;
1505b032f27cSSam Leffler 			sc->sc_nbcnvaps++;
1506b032f27cSSam Leffler 		}
1507b032f27cSSam Leffler 		if (sc->sc_hastsfadd && sc->sc_nbcnvaps > 0) {
1508b032f27cSSam Leffler 			/*
1509b032f27cSSam Leffler 			 * Multple vaps are to transmit beacons and we
1510b032f27cSSam Leffler 			 * have h/w support for TSF adjusting; enable
1511b032f27cSSam Leffler 			 * use of staggered beacons.
1512b032f27cSSam Leffler 			 */
1513b032f27cSSam Leffler 			sc->sc_stagbeacons = 1;
1514b032f27cSSam Leffler 		}
1515b032f27cSSam Leffler 		ath_txq_init(sc, &avp->av_mcastq, ATH_TXQ_SWQ);
1516b032f27cSSam Leffler 	}
1517b032f27cSSam Leffler 
1518b032f27cSSam Leffler 	ic->ic_opmode = ic_opmode;
1519b032f27cSSam Leffler 	if (opmode != IEEE80211_M_WDS) {
1520b032f27cSSam Leffler 		sc->sc_nvaps++;
1521b032f27cSSam Leffler 		if (opmode == IEEE80211_M_STA)
1522b032f27cSSam Leffler 			sc->sc_nstavaps++;
1523fe0dd789SSam Leffler 		if (opmode == IEEE80211_M_MBSS)
1524fe0dd789SSam Leffler 			sc->sc_nmeshvaps++;
1525b032f27cSSam Leffler 	}
1526b032f27cSSam Leffler 	switch (ic_opmode) {
1527b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1528b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_IBSS;
1529b032f27cSSam Leffler 		break;
1530b032f27cSSam Leffler 	case IEEE80211_M_STA:
1531b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_STA;
1532b032f27cSSam Leffler 		break;
1533b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1534584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
153510ad9a77SSam Leffler 		if (vap->iv_caps & IEEE80211_C_TDMA) {
153610ad9a77SSam Leffler 			sc->sc_tdma = 1;
153710ad9a77SSam Leffler 			/* NB: disable tsf adjust */
153810ad9a77SSam Leffler 			sc->sc_stagbeacons = 0;
153910ad9a77SSam Leffler 		}
154010ad9a77SSam Leffler 		/*
154110ad9a77SSam Leffler 		 * NB: adhoc demo mode is a pseudo mode; to the hal it's
154210ad9a77SSam Leffler 		 * just ap mode.
154310ad9a77SSam Leffler 		 */
154410ad9a77SSam Leffler 		/* fall thru... */
154510ad9a77SSam Leffler #endif
1546b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
154759aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1548b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_HOSTAP;
1549b032f27cSSam Leffler 		break;
1550b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1551b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_MONITOR;
1552b032f27cSSam Leffler 		break;
1553b032f27cSSam Leffler 	default:
1554b032f27cSSam Leffler 		/* XXX should not happen */
1555b032f27cSSam Leffler 		break;
1556b032f27cSSam Leffler 	}
1557b032f27cSSam Leffler 	if (sc->sc_hastsfadd) {
1558b032f27cSSam Leffler 		/*
1559b032f27cSSam Leffler 		 * Configure whether or not TSF adjust should be done.
1560b032f27cSSam Leffler 		 */
1561b032f27cSSam Leffler 		ath_hal_settsfadjust(sc->sc_ah, sc->sc_stagbeacons);
1562b032f27cSSam Leffler 	}
156310ad9a77SSam Leffler 	if (flags & IEEE80211_CLONE_NOBEACONS) {
156410ad9a77SSam Leffler 		/*
156510ad9a77SSam Leffler 		 * Enable s/w beacon miss handling.
156610ad9a77SSam Leffler 		 */
156710ad9a77SSam Leffler 		sc->sc_swbmiss = 1;
156810ad9a77SSam Leffler 	}
1569b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1570b032f27cSSam Leffler 
1571b032f27cSSam Leffler 	/* complete setup */
1572b032f27cSSam Leffler 	ieee80211_vap_attach(vap, ath_media_change, ieee80211_media_status);
1573b032f27cSSam Leffler 	return vap;
1574b032f27cSSam Leffler bad2:
1575b032f27cSSam Leffler 	reclaim_address(sc, mac);
1576b032f27cSSam Leffler 	ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1577b032f27cSSam Leffler bad:
1578b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1579b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1580b032f27cSSam Leffler 	return NULL;
1581b032f27cSSam Leffler }
1582b032f27cSSam Leffler 
1583b032f27cSSam Leffler static void
1584b032f27cSSam Leffler ath_vap_delete(struct ieee80211vap *vap)
1585b032f27cSSam Leffler {
1586b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1587b032f27cSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1588b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1589b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1590b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
1591b032f27cSSam Leffler 
1592f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1593f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1594f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1595f5c30c4eSAdrian Chadd 
1596f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
1597b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1598b032f27cSSam Leffler 		/*
1599b032f27cSSam Leffler 		 * Quiesce the hardware while we remove the vap.  In
1600b032f27cSSam Leffler 		 * particular we need to reclaim all references to
1601b032f27cSSam Leffler 		 * the vap state by any frames pending on the tx queues.
1602b032f27cSSam Leffler 		 */
1603b032f27cSSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
1604517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);		/* stop hw xmit side */
1605517526efSAdrian Chadd 		/* XXX Do all frames from all vaps/nodes need draining here? */
16069a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* stop recv side */
1607b032f27cSSam Leffler 	}
1608b032f27cSSam Leffler 
1609f5c30c4eSAdrian Chadd 	/* .. leave the hardware awake for now. */
1610f5c30c4eSAdrian Chadd 
1611b032f27cSSam Leffler 	ieee80211_vap_detach(vap);
161216d4de92SAdrian Chadd 
161316d4de92SAdrian Chadd 	/*
161416d4de92SAdrian Chadd 	 * XXX Danger Will Robinson! Danger!
161516d4de92SAdrian Chadd 	 *
161616d4de92SAdrian Chadd 	 * Because ieee80211_vap_detach() can queue a frame (the station
161716d4de92SAdrian Chadd 	 * diassociate message?) after we've drained the TXQ and
161816d4de92SAdrian Chadd 	 * flushed the software TXQ, we will end up with a frame queued
161916d4de92SAdrian Chadd 	 * to a node whose vap is about to be freed.
162016d4de92SAdrian Chadd 	 *
162116d4de92SAdrian Chadd 	 * To work around this, flush the hardware/software again.
162216d4de92SAdrian Chadd 	 * This may be racy - the ath task may be running and the packet
162316d4de92SAdrian Chadd 	 * may be being scheduled between sw->hw txq. Tsk.
162416d4de92SAdrian Chadd 	 *
162516d4de92SAdrian Chadd 	 * TODO: figure out why a new node gets allocated somewhere around
162616d4de92SAdrian Chadd 	 * here (after the ath_tx_swq() call; and after an ath_stop_locked()
162716d4de92SAdrian Chadd 	 * call!)
162816d4de92SAdrian Chadd 	 */
162916d4de92SAdrian Chadd 
163016d4de92SAdrian Chadd 	ath_draintxq(sc, ATH_RESET_DEFAULT);
163116d4de92SAdrian Chadd 
1632b032f27cSSam Leffler 	ATH_LOCK(sc);
1633b032f27cSSam Leffler 	/*
1634b032f27cSSam Leffler 	 * Reclaim beacon state.  Note this must be done before
1635b032f27cSSam Leffler 	 * the vap instance is reclaimed as we may have a reference
1636b032f27cSSam Leffler 	 * to it in the buffer for the beacon frame.
1637b032f27cSSam Leffler 	 */
1638b032f27cSSam Leffler 	if (avp->av_bcbuf != NULL) {
1639b032f27cSSam Leffler 		if (avp->av_bslot != -1) {
1640b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = NULL;
1641b032f27cSSam Leffler 			sc->sc_nbcnvaps--;
1642b032f27cSSam Leffler 		}
1643b032f27cSSam Leffler 		ath_beacon_return(sc, avp->av_bcbuf);
1644b032f27cSSam Leffler 		avp->av_bcbuf = NULL;
1645b032f27cSSam Leffler 		if (sc->sc_nbcnvaps == 0) {
1646b032f27cSSam Leffler 			sc->sc_stagbeacons = 0;
1647b032f27cSSam Leffler 			if (sc->sc_hastsfadd)
1648b032f27cSSam Leffler 				ath_hal_settsfadjust(sc->sc_ah, 0);
1649b032f27cSSam Leffler 		}
1650b032f27cSSam Leffler 		/*
1651b032f27cSSam Leffler 		 * Reclaim any pending mcast frames for the vap.
1652b032f27cSSam Leffler 		 */
1653b032f27cSSam Leffler 		ath_tx_draintxq(sc, &avp->av_mcastq);
1654b032f27cSSam Leffler 	}
1655b032f27cSSam Leffler 	/*
1656b032f27cSSam Leffler 	 * Update bookkeeping.
1657b032f27cSSam Leffler 	 */
1658b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
1659b032f27cSSam Leffler 		sc->sc_nstavaps--;
1660b032f27cSSam Leffler 		if (sc->sc_nstavaps == 0 && sc->sc_swbmiss)
1661b032f27cSSam Leffler 			sc->sc_swbmiss = 0;
166259aa14a9SRui Paulo 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
166359aa14a9SRui Paulo 	    vap->iv_opmode == IEEE80211_M_MBSS) {
1664b032f27cSSam Leffler 		reclaim_address(sc, vap->iv_myaddr);
1665b032f27cSSam Leffler 		ath_hal_setbssidmask(ah, sc->sc_hwbssidmask);
1666fe0dd789SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_MBSS)
1667fe0dd789SSam Leffler 			sc->sc_nmeshvaps--;
1668b032f27cSSam Leffler 	}
1669b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_WDS)
1670b032f27cSSam Leffler 		sc->sc_nvaps--;
1671584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
167210ad9a77SSam Leffler 	/* TDMA operation ceases when the last vap is destroyed */
167310ad9a77SSam Leffler 	if (sc->sc_tdma && sc->sc_nvaps == 0) {
167410ad9a77SSam Leffler 		sc->sc_tdma = 0;
167510ad9a77SSam Leffler 		sc->sc_swbmiss = 0;
167610ad9a77SSam Leffler 	}
167710ad9a77SSam Leffler #endif
1678b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1679b032f27cSSam Leffler 
1680b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1681b032f27cSSam Leffler 		/*
1682b032f27cSSam Leffler 		 * Restart rx+tx machines if still running (RUNNING will
1683b032f27cSSam Leffler 		 * be reset if we just destroyed the last vap).
1684b032f27cSSam Leffler 		 */
1685b032f27cSSam Leffler 		if (ath_startrecv(sc) != 0)
1686b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to restart recv logic\n",
1687b032f27cSSam Leffler 			    __func__);
1688c89b957aSSam Leffler 		if (sc->sc_beacons) {		/* restart beacons */
1689c89b957aSSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
1690c89b957aSSam Leffler 			if (sc->sc_tdma)
1691c89b957aSSam Leffler 				ath_tdma_config(sc, NULL);
1692c89b957aSSam Leffler 			else
1693c89b957aSSam Leffler #endif
1694b032f27cSSam Leffler 				ath_beacon_config(sc, NULL);
1695c89b957aSSam Leffler 		}
1696b032f27cSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
1697b032f27cSSam Leffler 	}
1698f5c30c4eSAdrian Chadd 
1699f5c30c4eSAdrian Chadd 	/* Ok, let the hardware asleep. */
1700f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
170116d4de92SAdrian Chadd 	ATH_UNLOCK(sc);
1702b032f27cSSam Leffler }
1703b032f27cSSam Leffler 
17045591b213SSam Leffler void
17055591b213SSam Leffler ath_suspend(struct ath_softc *sc)
17065591b213SSam Leffler {
1707fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1708d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
17095591b213SSam Leffler 
1710c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1711c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
17125591b213SSam Leffler 
1713d3ac945bSSam Leffler 	sc->sc_resume_up = (ifp->if_flags & IFF_UP) != 0;
1714d1328898SAdrian Chadd 
1715d3ac945bSSam Leffler 	ieee80211_suspend_all(ic);
1716d3ac945bSSam Leffler 	/*
1717d3ac945bSSam Leffler 	 * NB: don't worry about putting the chip in low power
1718d3ac945bSSam Leffler 	 * mode; pci will power off our socket on suspend and
1719f29b8b7fSWarner Losh 	 * CardBus detaches the device.
1720d3ac945bSSam Leffler 	 */
1721d73df6d5SAdrian Chadd 
1722ae2a0aa4SAdrian Chadd 	/*
1723ae2a0aa4SAdrian Chadd 	 * XXX ensure none of the taskqueues are running
1724ae2a0aa4SAdrian Chadd 	 * XXX ensure sc_invalid is 1
1725ae2a0aa4SAdrian Chadd 	 * XXX ensure the calibration callout is disabled
1726ae2a0aa4SAdrian Chadd 	 */
1727ae2a0aa4SAdrian Chadd 
1728ae2a0aa4SAdrian Chadd 	/* Disable the PCIe PHY, complete with workarounds */
1729ae2a0aa4SAdrian Chadd 	ath_hal_enablepcie(sc->sc_ah, 1, 1);
1730d3ac945bSSam Leffler }
1731d3ac945bSSam Leffler 
1732d3ac945bSSam Leffler /*
1733d3ac945bSSam Leffler  * Reset the key cache since some parts do not reset the
1734d3ac945bSSam Leffler  * contents on resume.  First we clear all entries, then
1735d3ac945bSSam Leffler  * re-load keys that the 802.11 layer assumes are setup
1736d3ac945bSSam Leffler  * in h/w.
1737d3ac945bSSam Leffler  */
1738d3ac945bSSam Leffler static void
1739d3ac945bSSam Leffler ath_reset_keycache(struct ath_softc *sc)
1740d3ac945bSSam Leffler {
1741d3ac945bSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1742d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1743d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1744d3ac945bSSam Leffler 	int i;
1745d3ac945bSSam Leffler 
1746f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1747f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1748d3ac945bSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
1749d3ac945bSSam Leffler 		ath_hal_keyreset(ah, i);
1750f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
1751f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1752d3ac945bSSam Leffler 	ieee80211_crypto_reload_keys(ic);
17535591b213SSam Leffler }
17545591b213SSam Leffler 
17556322256bSAdrian Chadd /*
17566322256bSAdrian Chadd  * Fetch the current chainmask configuration based on the current
17576322256bSAdrian Chadd  * operating channel and options.
17586322256bSAdrian Chadd  */
17596322256bSAdrian Chadd static void
17606322256bSAdrian Chadd ath_update_chainmasks(struct ath_softc *sc, struct ieee80211_channel *chan)
17616322256bSAdrian Chadd {
17626322256bSAdrian Chadd 
17636322256bSAdrian Chadd 	/*
17646322256bSAdrian Chadd 	 * Set TX chainmask to the currently configured chainmask;
17656322256bSAdrian Chadd 	 * the TX chainmask depends upon the current operating mode.
17666322256bSAdrian Chadd 	 */
17676322256bSAdrian Chadd 	sc->sc_cur_rxchainmask = sc->sc_rxchainmask;
17686322256bSAdrian Chadd 	if (IEEE80211_IS_CHAN_HT(chan)) {
17696322256bSAdrian Chadd 		sc->sc_cur_txchainmask = sc->sc_txchainmask;
17706322256bSAdrian Chadd 	} else {
17716322256bSAdrian Chadd 		sc->sc_cur_txchainmask = 1;
17726322256bSAdrian Chadd 	}
17737904f516SAdrian Chadd 
17747904f516SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
17757904f516SAdrian Chadd 	    "%s: TX chainmask is now 0x%x, RX is now 0x%x\n",
17767904f516SAdrian Chadd 	    __func__,
17777904f516SAdrian Chadd 	    sc->sc_cur_txchainmask,
17787904f516SAdrian Chadd 	    sc->sc_cur_rxchainmask);
17796322256bSAdrian Chadd }
17806322256bSAdrian Chadd 
17815591b213SSam Leffler void
17825591b213SSam Leffler ath_resume(struct ath_softc *sc)
17835591b213SSam Leffler {
1784fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1785d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1786d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1787d3ac945bSSam Leffler 	HAL_STATUS status;
17885591b213SSam Leffler 
1789c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1790c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
17915591b213SSam Leffler 
1792d73df6d5SAdrian Chadd 	/* Re-enable PCIe, re-enable the PCIe bus */
1793ae2a0aa4SAdrian Chadd 	ath_hal_enablepcie(ah, 0, 0);
1794d73df6d5SAdrian Chadd 
1795d3ac945bSSam Leffler 	/*
1796d3ac945bSSam Leffler 	 * Must reset the chip before we reload the
1797d3ac945bSSam Leffler 	 * keycache as we were powered down on suspend.
1798d3ac945bSSam Leffler 	 */
17996322256bSAdrian Chadd 	ath_update_chainmasks(sc,
18006322256bSAdrian Chadd 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan);
18016322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
18026322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
1803f5c30c4eSAdrian Chadd 
1804f5c30c4eSAdrian Chadd 	/* Ensure we set the current power state to on */
1805f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1806*7d567ed6SAdrian Chadd 	ath_power_setselfgen(sc, HAL_PM_AWAKE);
1807f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1808f5c30c4eSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_AWAKE);
1809f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1810f5c30c4eSAdrian Chadd 
1811054d7b69SSam Leffler 	ath_hal_reset(ah, sc->sc_opmode,
1812054d7b69SSam Leffler 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan,
1813054d7b69SSam Leffler 	    AH_FALSE, &status);
1814d3ac945bSSam Leffler 	ath_reset_keycache(sc);
18157e5eb44dSAdrian Chadd 
18167e5eb44dSAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
18177e5eb44dSAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
18187e5eb44dSAdrian Chadd 
18199af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
18209af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
18219af351f9SAdrian Chadd 
1822dd6a574eSAdrian Chadd 	/*
1823b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
1824b70f530bSAdrian Chadd 	 */
1825b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
1826b70f530bSAdrian Chadd 
1827b70f530bSAdrian Chadd 	/*
1828dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
1829dd6a574eSAdrian Chadd 	 * support it.
1830dd6a574eSAdrian Chadd 	 */
1831dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
1832dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
1833dd6a574eSAdrian Chadd 	else
1834dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
1835dd6a574eSAdrian Chadd 
1836a497cd88SAdrian Chadd 	/* Restore the LED configuration */
1837a497cd88SAdrian Chadd 	ath_led_config(sc);
1838a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
1839a497cd88SAdrian Chadd 
1840d1328898SAdrian Chadd 	if (sc->sc_resume_up)
1841021a0db5SAdrian Chadd 		ieee80211_resume_all(ic);
18422fd9aabbSAdrian Chadd 
1843f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1844f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
1845f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1846f5c30c4eSAdrian Chadd 
18472fd9aabbSAdrian Chadd 	/* XXX beacons ? */
18486b59f5e3SSam Leffler }
18495591b213SSam Leffler 
18505591b213SSam Leffler void
18515591b213SSam Leffler ath_shutdown(struct ath_softc *sc)
18525591b213SSam Leffler {
1853fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
18545591b213SSam Leffler 
1855c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1856c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
18575591b213SSam Leffler 
18585591b213SSam Leffler 	ath_stop(ifp);
1859d3ac945bSSam Leffler 	/* NB: no point powering down chip as we're about to reboot */
18605591b213SSam Leffler }
18615591b213SSam Leffler 
1862c42a7b7eSSam Leffler /*
1863c42a7b7eSSam Leffler  * Interrupt handler.  Most of the actual processing is deferred.
1864c42a7b7eSSam Leffler  */
18655591b213SSam Leffler void
18665591b213SSam Leffler ath_intr(void *arg)
18675591b213SSam Leffler {
18685591b213SSam Leffler 	struct ath_softc *sc = arg;
1869fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
18705591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
18716f5fe81eSAdrian Chadd 	HAL_INT status = 0;
18728f939e79SAdrian Chadd 	uint32_t txqs;
18735591b213SSam Leffler 
1874ef27340cSAdrian Chadd 	/*
1875ef27340cSAdrian Chadd 	 * If we're inside a reset path, just print a warning and
1876ef27340cSAdrian Chadd 	 * clear the ISR. The reset routine will finish it for us.
1877ef27340cSAdrian Chadd 	 */
1878ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
1879ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt) {
1880ef27340cSAdrian Chadd 		HAL_INT status;
1881ef27340cSAdrian Chadd 		ath_hal_getisr(ah, &status);	/* clear ISR */
1882ef27340cSAdrian Chadd 		ath_hal_intrset(ah, 0);		/* disable further intr's */
1883ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_ANY,
1884ef27340cSAdrian Chadd 		    "%s: in reset, ignoring: status=0x%x\n",
1885ef27340cSAdrian Chadd 		    __func__, status);
1886ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
1887ef27340cSAdrian Chadd 		return;
1888ef27340cSAdrian Chadd 	}
1889ef27340cSAdrian Chadd 
18905591b213SSam Leffler 	if (sc->sc_invalid) {
18915591b213SSam Leffler 		/*
1892b58b3803SSam Leffler 		 * The hardware is not ready/present, don't touch anything.
1893b58b3803SSam Leffler 		 * Note this can happen early on if the IRQ is shared.
18945591b213SSam Leffler 		 */
1895c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
1896ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
18975591b213SSam Leffler 		return;
18985591b213SSam Leffler 	}
1899ef27340cSAdrian Chadd 	if (!ath_hal_intrpend(ah)) {		/* shared irq, not for us */
1900ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
1901fdd758d4SSam Leffler 		return;
1902ef27340cSAdrian Chadd 	}
1903ef27340cSAdrian Chadd 
1904f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1905f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1906f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1907f5c30c4eSAdrian Chadd 
190868e8e04eSSam Leffler 	if ((ifp->if_flags & IFF_UP) == 0 ||
190968e8e04eSSam Leffler 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
191068e8e04eSSam Leffler 		HAL_INT status;
191168e8e04eSSam Leffler 
1912c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1913c42a7b7eSSam Leffler 			__func__, ifp->if_flags);
19145591b213SSam Leffler 		ath_hal_getisr(ah, &status);	/* clear ISR */
19155591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable further intr's */
1916ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
1917f5c30c4eSAdrian Chadd 
1918f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
1919f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
1920f5c30c4eSAdrian Chadd 		ATH_UNLOCK(sc);
19215591b213SSam Leffler 		return;
19225591b213SSam Leffler 	}
1923ef27340cSAdrian Chadd 
1924c42a7b7eSSam Leffler 	/*
1925c42a7b7eSSam Leffler 	 * Figure out the reason(s) for the interrupt.  Note
1926c42a7b7eSSam Leffler 	 * that the hal returns a pseudo-ISR that may include
1927c42a7b7eSSam Leffler 	 * bits we haven't explicitly enabled so we mask the
1928c42a7b7eSSam Leffler 	 * value to insure we only process bits we requested.
1929c42a7b7eSSam Leffler 	 */
19305591b213SSam Leffler 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
1931c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
193203682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 1, "ath_intr: mask=0x%.8x", status);
1933a26f3327SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
1934a26f3327SAdrian Chadd 	if_ath_alq_post_intr(&sc->sc_alq, status, ah->ah_intrstate,
1935a26f3327SAdrian Chadd 	    ah->ah_syncstate);
1936a26f3327SAdrian Chadd #endif	/* ATH_DEBUG_ALQ */
193731fdf3d6SAdrian Chadd #ifdef	ATH_KTR_INTR_DEBUG
193803682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 5,
1939f52d3452SAdrian Chadd 	    "ath_intr: ISR=0x%.8x, ISR_S0=0x%.8x, ISR_S1=0x%.8x, ISR_S2=0x%.8x, ISR_S5=0x%.8x",
1940f52d3452SAdrian Chadd 	    ah->ah_intrstate[0],
1941f52d3452SAdrian Chadd 	    ah->ah_intrstate[1],
1942f52d3452SAdrian Chadd 	    ah->ah_intrstate[2],
1943f52d3452SAdrian Chadd 	    ah->ah_intrstate[3],
1944f52d3452SAdrian Chadd 	    ah->ah_intrstate[6]);
194531fdf3d6SAdrian Chadd #endif
19469467e3f3SAdrian Chadd 
19479467e3f3SAdrian Chadd 	/* Squirrel away SYNC interrupt debugging */
19489467e3f3SAdrian Chadd 	if (ah->ah_syncstate != 0) {
19499467e3f3SAdrian Chadd 		int i;
19509467e3f3SAdrian Chadd 		for (i = 0; i < 32; i++)
19519467e3f3SAdrian Chadd 			if (ah->ah_syncstate & (i << i))
19529467e3f3SAdrian Chadd 				sc->sc_intr_stats.sync_intr[i]++;
19539467e3f3SAdrian Chadd 	}
19549467e3f3SAdrian Chadd 
1955ecddff40SSam Leffler 	status &= sc->sc_imask;			/* discard unasked for bits */
19566f5fe81eSAdrian Chadd 
19576f5fe81eSAdrian Chadd 	/* Short-circuit un-handled interrupts */
1958ef27340cSAdrian Chadd 	if (status == 0x0) {
1959ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
1960f5c30c4eSAdrian Chadd 
1961f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
1962f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
1963f5c30c4eSAdrian Chadd 		ATH_UNLOCK(sc);
1964f5c30c4eSAdrian Chadd 
19656f5fe81eSAdrian Chadd 		return;
1966ef27340cSAdrian Chadd 	}
19676f5fe81eSAdrian Chadd 
1968ef27340cSAdrian Chadd 	/*
1969ef27340cSAdrian Chadd 	 * Take a note that we're inside the interrupt handler, so
1970ef27340cSAdrian Chadd 	 * the reset routines know to wait.
1971ef27340cSAdrian Chadd 	 */
1972ef27340cSAdrian Chadd 	sc->sc_intr_cnt++;
1973ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
1974ef27340cSAdrian Chadd 
1975ef27340cSAdrian Chadd 	/*
1976ef27340cSAdrian Chadd 	 * Handle the interrupt. We won't run concurrent with the reset
1977ef27340cSAdrian Chadd 	 * or channel change routines as they'll wait for sc_intr_cnt
1978ef27340cSAdrian Chadd 	 * to be 0 before continuing.
1979ef27340cSAdrian Chadd 	 */
19805591b213SSam Leffler 	if (status & HAL_INT_FATAL) {
19815591b213SSam Leffler 		sc->sc_stats.ast_hardware++;
19825591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
1983f846cf42SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_fataltask);
19845591b213SSam Leffler 	} else {
1985c42a7b7eSSam Leffler 		if (status & HAL_INT_SWBA) {
1986c42a7b7eSSam Leffler 			/*
1987c42a7b7eSSam Leffler 			 * Software beacon alert--time to send a beacon.
1988c42a7b7eSSam Leffler 			 * Handle beacon transmission directly; deferring
1989c42a7b7eSSam Leffler 			 * this is too slow to meet timing constraints
1990c42a7b7eSSam Leffler 			 * under load.
1991c42a7b7eSSam Leffler 			 */
1992584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
199310ad9a77SSam Leffler 			if (sc->sc_tdma) {
199410ad9a77SSam Leffler 				if (sc->sc_tdmaswba == 0) {
199510ad9a77SSam Leffler 					struct ieee80211com *ic = ifp->if_l2com;
199610ad9a77SSam Leffler 					struct ieee80211vap *vap =
199710ad9a77SSam Leffler 					    TAILQ_FIRST(&ic->ic_vaps);
199810ad9a77SSam Leffler 					ath_tdma_beacon_send(sc, vap);
199910ad9a77SSam Leffler 					sc->sc_tdmaswba =
200010ad9a77SSam Leffler 					    vap->iv_tdma->tdma_bintval;
200110ad9a77SSam Leffler 				} else
200210ad9a77SSam Leffler 					sc->sc_tdmaswba--;
200310ad9a77SSam Leffler 			} else
200410ad9a77SSam Leffler #endif
2005339ccfb3SSam Leffler 			{
2006c42a7b7eSSam Leffler 				ath_beacon_proc(sc, 0);
2007339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
2008339ccfb3SSam Leffler 				/*
2009339ccfb3SSam Leffler 				 * Schedule the rx taskq in case there's no
2010339ccfb3SSam Leffler 				 * traffic so any frames held on the staging
2011339ccfb3SSam Leffler 				 * queue are aged and potentially flushed.
2012339ccfb3SSam Leffler 				 */
2013f0db652cSAdrian Chadd 				sc->sc_rx.recv_sched(sc, 1);
2014339ccfb3SSam Leffler #endif
2015339ccfb3SSam Leffler 			}
2016c42a7b7eSSam Leffler 		}
20175591b213SSam Leffler 		if (status & HAL_INT_RXEOL) {
20188f939e79SAdrian Chadd 			int imask;
201903682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXEOL");
2020ef27340cSAdrian Chadd 			ATH_PCU_LOCK(sc);
20215591b213SSam Leffler 			/*
20225591b213SSam Leffler 			 * NB: the hardware should re-read the link when
20235591b213SSam Leffler 			 *     RXE bit is written, but it doesn't work at
20245591b213SSam Leffler 			 *     least on older hardware revs.
20255591b213SSam Leffler 			 */
20265591b213SSam Leffler 			sc->sc_stats.ast_rxeol++;
202773f895fcSAdrian Chadd 			/*
202873f895fcSAdrian Chadd 			 * Disable RXEOL/RXORN - prevent an interrupt
202973f895fcSAdrian Chadd 			 * storm until the PCU logic can be reset.
20301fdadc0fSAdrian Chadd 			 * In case the interface is reset some other
20311fdadc0fSAdrian Chadd 			 * way before "sc_kickpcu" is called, don't
20321fdadc0fSAdrian Chadd 			 * modify sc_imask - that way if it is reset
20331fdadc0fSAdrian Chadd 			 * by a call to ath_reset() somehow, the
20341fdadc0fSAdrian Chadd 			 * interrupt mask will be correctly reprogrammed.
203573f895fcSAdrian Chadd 			 */
20368f939e79SAdrian Chadd 			imask = sc->sc_imask;
20371fdadc0fSAdrian Chadd 			imask &= ~(HAL_INT_RXEOL | HAL_INT_RXORN);
20381fdadc0fSAdrian Chadd 			ath_hal_intrset(ah, imask);
20391fdadc0fSAdrian Chadd 			/*
20408f939e79SAdrian Chadd 			 * Only blank sc_rxlink if we've not yet kicked
20418f939e79SAdrian Chadd 			 * the PCU.
20428f939e79SAdrian Chadd 			 *
20438f939e79SAdrian Chadd 			 * This isn't entirely correct - the correct solution
20448f939e79SAdrian Chadd 			 * would be to have a PCU lock and engage that for
20458f939e79SAdrian Chadd 			 * the duration of the PCU fiddling; which would include
20468f939e79SAdrian Chadd 			 * running the RX process. Otherwise we could end up
20478f939e79SAdrian Chadd 			 * messing up the RX descriptor chain and making the
20488f939e79SAdrian Chadd 			 * RX desc list much shorter.
20498f939e79SAdrian Chadd 			 */
20508f939e79SAdrian Chadd 			if (! sc->sc_kickpcu)
20518f939e79SAdrian Chadd 				sc->sc_rxlink = NULL;
20528f939e79SAdrian Chadd 			sc->sc_kickpcu = 1;
2053f0db652cSAdrian Chadd 			ATH_PCU_UNLOCK(sc);
20548f939e79SAdrian Chadd 			/*
20551fdadc0fSAdrian Chadd 			 * Enqueue an RX proc, to handled whatever
20561fdadc0fSAdrian Chadd 			 * is in the RX queue.
20571fdadc0fSAdrian Chadd 			 * This will then kick the PCU.
20581fdadc0fSAdrian Chadd 			 */
2059f0db652cSAdrian Chadd 			sc->sc_rx.recv_sched(sc, 1);
20605591b213SSam Leffler 		}
20615591b213SSam Leffler 		if (status & HAL_INT_TXURN) {
20625591b213SSam Leffler 			sc->sc_stats.ast_txurn++;
20635591b213SSam Leffler 			/* bump tx trigger level */
20645591b213SSam Leffler 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
20655591b213SSam Leffler 		}
2066bcbb08ceSAdrian Chadd 		/*
2067bcbb08ceSAdrian Chadd 		 * Handle both the legacy and RX EDMA interrupt bits.
2068bcbb08ceSAdrian Chadd 		 * Note that HAL_INT_RXLP is also HAL_INT_RXDESC.
2069bcbb08ceSAdrian Chadd 		 */
2070bcbb08ceSAdrian Chadd 		if (status & (HAL_INT_RX | HAL_INT_RXHP | HAL_INT_RXLP)) {
20718f939e79SAdrian Chadd 			sc->sc_stats.ast_rx_intr++;
2072f0db652cSAdrian Chadd 			sc->sc_rx.recv_sched(sc, 1);
20738f939e79SAdrian Chadd 		}
20748f939e79SAdrian Chadd 		if (status & HAL_INT_TX) {
20758f939e79SAdrian Chadd 			sc->sc_stats.ast_tx_intr++;
20768f939e79SAdrian Chadd 			/*
20778f939e79SAdrian Chadd 			 * Grab all the currently set bits in the HAL txq bitmap
20788f939e79SAdrian Chadd 			 * and blank them. This is the only place we should be
20798f939e79SAdrian Chadd 			 * doing this.
20808f939e79SAdrian Chadd 			 */
2081bad98824SAdrian Chadd 			if (! sc->sc_isedma) {
2082ef27340cSAdrian Chadd 				ATH_PCU_LOCK(sc);
20838f939e79SAdrian Chadd 				txqs = 0xffffffff;
20848f939e79SAdrian Chadd 				ath_hal_gettxintrtxqs(sc->sc_ah, &txqs);
208503682514SAdrian Chadd 				ATH_KTR(sc, ATH_KTR_INTERRUPTS, 3,
208603682514SAdrian Chadd 				    "ath_intr: TX; txqs=0x%08x, txq_active was 0x%08x, now 0x%08x",
208703682514SAdrian Chadd 				    txqs,
208803682514SAdrian Chadd 				    sc->sc_txq_active,
208903682514SAdrian Chadd 				    sc->sc_txq_active | txqs);
20908f939e79SAdrian Chadd 				sc->sc_txq_active |= txqs;
2091ef27340cSAdrian Chadd 				ATH_PCU_UNLOCK(sc);
20928f939e79SAdrian Chadd 			}
2093bad98824SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask);
2094bad98824SAdrian Chadd 		}
20955591b213SSam Leffler 		if (status & HAL_INT_BMISS) {
20965591b213SSam Leffler 			sc->sc_stats.ast_bmiss++;
20970bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask);
20985591b213SSam Leffler 		}
20996ad02dbaSAdrian Chadd 		if (status & HAL_INT_GTT)
21006ad02dbaSAdrian Chadd 			sc->sc_stats.ast_tx_timeout++;
21015594f5c0SAdrian Chadd 		if (status & HAL_INT_CST)
21025594f5c0SAdrian Chadd 			sc->sc_stats.ast_tx_cst++;
2103c42a7b7eSSam Leffler 		if (status & HAL_INT_MIB) {
2104c42a7b7eSSam Leffler 			sc->sc_stats.ast_mib++;
2105ef27340cSAdrian Chadd 			ATH_PCU_LOCK(sc);
2106c42a7b7eSSam Leffler 			/*
2107c42a7b7eSSam Leffler 			 * Disable interrupts until we service the MIB
2108c42a7b7eSSam Leffler 			 * interrupt; otherwise it will continue to fire.
2109c42a7b7eSSam Leffler 			 */
2110c42a7b7eSSam Leffler 			ath_hal_intrset(ah, 0);
2111c42a7b7eSSam Leffler 			/*
2112c42a7b7eSSam Leffler 			 * Let the hal handle the event.  We assume it will
2113c42a7b7eSSam Leffler 			 * clear whatever condition caused the interrupt.
2114c42a7b7eSSam Leffler 			 */
2115ffa2cab6SSam Leffler 			ath_hal_mibevent(ah, &sc->sc_halstats);
21168f939e79SAdrian Chadd 			/*
21178f939e79SAdrian Chadd 			 * Don't reset the interrupt if we've just
21188f939e79SAdrian Chadd 			 * kicked the PCU, or we may get a nested
21198f939e79SAdrian Chadd 			 * RXEOL before the rxproc has had a chance
21208f939e79SAdrian Chadd 			 * to run.
21218f939e79SAdrian Chadd 			 */
21228f939e79SAdrian Chadd 			if (sc->sc_kickpcu == 0)
2123c42a7b7eSSam Leffler 				ath_hal_intrset(ah, sc->sc_imask);
2124ef27340cSAdrian Chadd 			ATH_PCU_UNLOCK(sc);
2125c42a7b7eSSam Leffler 		}
21269c4fc1e8SSam Leffler 		if (status & HAL_INT_RXORN) {
21279c4fc1e8SSam Leffler 			/* NB: hal marks HAL_INT_FATAL when RXORN is fatal */
212803682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXORN");
21299c4fc1e8SSam Leffler 			sc->sc_stats.ast_rxorn++;
21309c4fc1e8SSam Leffler 		}
2131f5c30c4eSAdrian Chadd 		if (status & HAL_INT_TSFOOR) {
2132f5c30c4eSAdrian Chadd 			device_printf(sc->sc_dev, "%s: TSFOOR\n", __func__);
2133f5c30c4eSAdrian Chadd 			sc->sc_syncbeacon = 1;
2134f5c30c4eSAdrian Chadd 		}
21355591b213SSam Leffler 	}
2136ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2137ef27340cSAdrian Chadd 	sc->sc_intr_cnt--;
2138ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2139f5c30c4eSAdrian Chadd 
2140f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2141f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2142f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
21435591b213SSam Leffler }
21445591b213SSam Leffler 
21455591b213SSam Leffler static void
21465591b213SSam Leffler ath_fatal_proc(void *arg, int pending)
21475591b213SSam Leffler {
21485591b213SSam Leffler 	struct ath_softc *sc = arg;
2149fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
215016c8acaaSSam Leffler 	u_int32_t *state;
215116c8acaaSSam Leffler 	u_int32_t len;
215268e8e04eSSam Leffler 	void *sp;
21535591b213SSam Leffler 
2154c42a7b7eSSam Leffler 	if_printf(ifp, "hardware error; resetting\n");
215516c8acaaSSam Leffler 	/*
215616c8acaaSSam Leffler 	 * Fatal errors are unrecoverable.  Typically these
215716c8acaaSSam Leffler 	 * are caused by DMA errors.  Collect h/w state from
215816c8acaaSSam Leffler 	 * the hal so we can diagnose what's going on.
215916c8acaaSSam Leffler 	 */
216068e8e04eSSam Leffler 	if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) {
216116c8acaaSSam Leffler 		KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len));
216268e8e04eSSam Leffler 		state = sp;
216316c8acaaSSam Leffler 		if_printf(ifp, "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n",
216416c8acaaSSam Leffler 		    state[0], state[1] , state[2], state[3],
216516c8acaaSSam Leffler 		    state[4], state[5]);
216616c8acaaSSam Leffler 	}
2167517526efSAdrian Chadd 	ath_reset(ifp, ATH_RESET_NOLOSS);
21685591b213SSam Leffler }
21695591b213SSam Leffler 
21705591b213SSam Leffler static void
2171b032f27cSSam Leffler ath_bmiss_vap(struct ieee80211vap *vap)
21725591b213SSam Leffler {
2173f5c30c4eSAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
2174f5c30c4eSAdrian Chadd 
217559fbb257SSam Leffler 	/*
217659fbb257SSam Leffler 	 * Workaround phantom bmiss interrupts by sanity-checking
217759fbb257SSam Leffler 	 * the time of our last rx'd frame.  If it is within the
217859fbb257SSam Leffler 	 * beacon miss interval then ignore the interrupt.  If it's
217959fbb257SSam Leffler 	 * truly a bmiss we'll get another interrupt soon and that'll
218059fbb257SSam Leffler 	 * be dispatched up for processing.  Note this applies only
218159fbb257SSam Leffler 	 * for h/w beacon miss events.
218259fbb257SSam Leffler 	 */
2183f5c30c4eSAdrian Chadd 
2184f5c30c4eSAdrian Chadd 	/*
2185f5c30c4eSAdrian Chadd 	 * XXX TODO: Just read the TSF during the interrupt path;
2186f5c30c4eSAdrian Chadd 	 * that way we don't have to wake up again just to read it
2187f5c30c4eSAdrian Chadd 	 * again.
2188f5c30c4eSAdrian Chadd 	 */
2189f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2190f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2191f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2192f5c30c4eSAdrian Chadd 
219359fbb257SSam Leffler 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
2194a7ace843SSam Leffler 		struct ifnet *ifp = vap->iv_ic->ic_ifp;
2195a7ace843SSam Leffler 		struct ath_softc *sc = ifp->if_softc;
2196d7736e13SSam Leffler 		u_int64_t lastrx = sc->sc_lastrx;
2197d7736e13SSam Leffler 		u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah);
219880767531SAdrian Chadd 		/* XXX should take a locked ref to iv_bss */
2199d7736e13SSam Leffler 		u_int bmisstimeout =
2200b032f27cSSam Leffler 			vap->iv_bmissthreshold * vap->iv_bss->ni_intval * 1024;
2201d7736e13SSam Leffler 
2202d7736e13SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2203d7736e13SSam Leffler 		    "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n",
2204d7736e13SSam Leffler 		    __func__, (unsigned long long) tsf,
2205d7736e13SSam Leffler 		    (unsigned long long)(tsf - lastrx),
2206d7736e13SSam Leffler 		    (unsigned long long) lastrx, bmisstimeout);
220759fbb257SSam Leffler 
220859fbb257SSam Leffler 		if (tsf - lastrx <= bmisstimeout) {
2209d7736e13SSam Leffler 			sc->sc_stats.ast_bmiss_phantom++;
2210f5c30c4eSAdrian Chadd 
2211f5c30c4eSAdrian Chadd 			ATH_LOCK(sc);
2212f5c30c4eSAdrian Chadd 			ath_power_restore_power_state(sc);
2213f5c30c4eSAdrian Chadd 			ATH_UNLOCK(sc);
2214f5c30c4eSAdrian Chadd 
221559fbb257SSam Leffler 			return;
221659fbb257SSam Leffler 		}
221759fbb257SSam Leffler 	}
2218f5c30c4eSAdrian Chadd 
2219f5c30c4eSAdrian Chadd 	/*
2220f5c30c4eSAdrian Chadd 	 * There's no need to keep the hardware awake during the call
2221f5c30c4eSAdrian Chadd 	 * to av_bmiss().
2222f5c30c4eSAdrian Chadd 	 */
2223f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2224f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2225f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2226f5c30c4eSAdrian Chadd 
2227f5c30c4eSAdrian Chadd 	/*
2228f5c30c4eSAdrian Chadd 	 * Attempt to force a beacon resync.
2229f5c30c4eSAdrian Chadd 	 */
2230f5c30c4eSAdrian Chadd 	sc->sc_syncbeacon = 1;
2231f5c30c4eSAdrian Chadd 
223259fbb257SSam Leffler 	ATH_VAP(vap)->av_bmiss(vap);
2233e585d188SSam Leffler }
2234b032f27cSSam Leffler 
2235f5c30c4eSAdrian Chadd /* XXX this needs a force wakeup! */
2236b837332dSAdrian Chadd int
2237459bc4f0SSam Leffler ath_hal_gethangstate(struct ath_hal *ah, uint32_t mask, uint32_t *hangs)
2238459bc4f0SSam Leffler {
2239459bc4f0SSam Leffler 	uint32_t rsize;
2240459bc4f0SSam Leffler 	void *sp;
2241459bc4f0SSam Leffler 
224225c96056SAdrian Chadd 	if (!ath_hal_getdiagstate(ah, HAL_DIAG_CHECK_HANGS, &mask, sizeof(mask), &sp, &rsize))
2243459bc4f0SSam Leffler 		return 0;
2244459bc4f0SSam Leffler 	KASSERT(rsize == sizeof(uint32_t), ("resultsize %u", rsize));
2245459bc4f0SSam Leffler 	*hangs = *(uint32_t *)sp;
2246459bc4f0SSam Leffler 	return 1;
2247459bc4f0SSam Leffler }
2248459bc4f0SSam Leffler 
2249b032f27cSSam Leffler static void
2250b032f27cSSam Leffler ath_bmiss_proc(void *arg, int pending)
2251b032f27cSSam Leffler {
2252b032f27cSSam Leffler 	struct ath_softc *sc = arg;
2253b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
2254459bc4f0SSam Leffler 	uint32_t hangs;
2255b032f27cSSam Leffler 
2256b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
2257459bc4f0SSam Leffler 
2258f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2259f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2260f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2261f5c30c4eSAdrian Chadd 
2262f5c30c4eSAdrian Chadd 	ath_beacon_miss(sc);
2263f5c30c4eSAdrian Chadd 
2264a74ebfe5SAdrian Chadd 	/*
2265a74ebfe5SAdrian Chadd 	 * Do a reset upon any becaon miss event.
2266a74ebfe5SAdrian Chadd 	 *
2267a74ebfe5SAdrian Chadd 	 * It may be a non-recognised RX clear hang which needs a reset
2268a74ebfe5SAdrian Chadd 	 * to clear.
2269a74ebfe5SAdrian Chadd 	 */
2270459bc4f0SSam Leffler 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) {
2271517526efSAdrian Chadd 		ath_reset(ifp, ATH_RESET_NOLOSS);
2272a74ebfe5SAdrian Chadd 		if_printf(ifp, "bb hang detected (0x%x), resetting\n", hangs);
2273a74ebfe5SAdrian Chadd 	} else {
2274a74ebfe5SAdrian Chadd 		ath_reset(ifp, ATH_RESET_NOLOSS);
2275b032f27cSSam Leffler 		ieee80211_beacon_miss(ifp->if_l2com);
22765591b213SSam Leffler 	}
2277f5c30c4eSAdrian Chadd 
2278f5c30c4eSAdrian Chadd 	/* Force a beacon resync, in case they've drifted */
2279f5c30c4eSAdrian Chadd 	sc->sc_syncbeacon = 1;
2280f5c30c4eSAdrian Chadd 
2281f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2282f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2283f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2284a74ebfe5SAdrian Chadd }
22855591b213SSam Leffler 
2286724c193aSSam Leffler /*
2287b032f27cSSam Leffler  * Handle TKIP MIC setup to deal hardware that doesn't do MIC
2288b032f27cSSam Leffler  * calcs together with WME.  If necessary disable the crypto
2289b032f27cSSam Leffler  * hardware and mark the 802.11 state so keys will be setup
2290b032f27cSSam Leffler  * with the MIC work done in software.
2291b032f27cSSam Leffler  */
2292b032f27cSSam Leffler static void
2293b032f27cSSam Leffler ath_settkipmic(struct ath_softc *sc)
2294b032f27cSSam Leffler {
2295b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
2296b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
2297b032f27cSSam Leffler 
2298b032f27cSSam Leffler 	if ((ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIP) && !sc->sc_wmetkipmic) {
2299b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WME) {
2300b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_FALSE);
2301b032f27cSSam Leffler 			ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC;
2302b032f27cSSam Leffler 		} else {
2303b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_TRUE);
2304b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
2305b032f27cSSam Leffler 		}
2306b032f27cSSam Leffler 	}
2307b032f27cSSam Leffler }
2308b032f27cSSam Leffler 
23095591b213SSam Leffler static void
23105591b213SSam Leffler ath_init(void *arg)
23115591b213SSam Leffler {
23125591b213SSam Leffler 	struct ath_softc *sc = (struct ath_softc *) arg;
2313fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
2314b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
23155591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
23165591b213SSam Leffler 	HAL_STATUS status;
23175591b213SSam Leffler 
2318c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
2319c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
23205591b213SSam Leffler 
2321f0b2a0beSSam Leffler 	ATH_LOCK(sc);
23225591b213SSam Leffler 	/*
2323f5c30c4eSAdrian Chadd 	 * Force the sleep state awake.
2324f5c30c4eSAdrian Chadd 	 */
2325*7d567ed6SAdrian Chadd 	ath_power_setselfgen(sc, HAL_PM_AWAKE);
2326f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2327f5c30c4eSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_AWAKE);
2328f5c30c4eSAdrian Chadd 
2329f5c30c4eSAdrian Chadd 	/*
23305591b213SSam Leffler 	 * Stop anything previously setup.  This is safe
23315591b213SSam Leffler 	 * whether this is the first time through or not.
23325591b213SSam Leffler 	 */
2333c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
23345591b213SSam Leffler 
23355591b213SSam Leffler 	/*
23365591b213SSam Leffler 	 * The basic interface to setting the hardware in a good
23375591b213SSam Leffler 	 * state is ``reset''.  On return the hardware is known to
23385591b213SSam Leffler 	 * be powered up and with interrupts disabled.  This must
23395591b213SSam Leffler 	 * be followed by initialization of the appropriate bits
23405591b213SSam Leffler 	 * and then setup of the interrupt mask.
23415591b213SSam Leffler 	 */
2342b032f27cSSam Leffler 	ath_settkipmic(sc);
23436322256bSAdrian Chadd 	ath_update_chainmasks(sc, ic->ic_curchan);
23446322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
23456322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
2346f5c30c4eSAdrian Chadd 
234759efa8b5SSam Leffler 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_FALSE, &status)) {
23485591b213SSam Leffler 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
23495591b213SSam Leffler 			status);
2350b032f27cSSam Leffler 		ATH_UNLOCK(sc);
2351b032f27cSSam Leffler 		return;
23525591b213SSam Leffler 	}
2353b032f27cSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
23545591b213SSam Leffler 
235548237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
235648237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
235748237774SAdrian Chadd 
23589af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
23599af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
23609af351f9SAdrian Chadd 
23615591b213SSam Leffler 	/*
2362b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
2363b70f530bSAdrian Chadd 	 */
2364b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
2365b70f530bSAdrian Chadd 
2366b70f530bSAdrian Chadd 	/*
2367dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
2368dd6a574eSAdrian Chadd 	 * support it.
2369dd6a574eSAdrian Chadd 	 */
2370dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
2371dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
2372dd6a574eSAdrian Chadd 	else
2373dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
2374dd6a574eSAdrian Chadd 
2375dd6a574eSAdrian Chadd 	/*
2376c59005e9SSam Leffler 	 * Likewise this is set during reset so update
2377c59005e9SSam Leffler 	 * state cached in the driver.
2378c59005e9SSam Leffler 	 */
2379c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
23802dc7fcc4SSam Leffler 	sc->sc_lastlongcal = 0;
23812dc7fcc4SSam Leffler 	sc->sc_resetcal = 1;
23822dc7fcc4SSam Leffler 	sc->sc_lastcalreset = 0;
2383a108ab63SAdrian Chadd 	sc->sc_lastani = 0;
2384a108ab63SAdrian Chadd 	sc->sc_lastshortcal = 0;
2385a108ab63SAdrian Chadd 	sc->sc_doresetcal = AH_FALSE;
23862fd9aabbSAdrian Chadd 	/*
23872fd9aabbSAdrian Chadd 	 * Beacon timers were cleared here; give ath_newstate()
23882fd9aabbSAdrian Chadd 	 * a hint that the beacon timers should be poked when
23892fd9aabbSAdrian Chadd 	 * things transition to the RUN state.
23902fd9aabbSAdrian Chadd 	 */
23912fd9aabbSAdrian Chadd 	sc->sc_beacons = 0;
2392c42a7b7eSSam Leffler 
2393c42a7b7eSSam Leffler 	/*
23945591b213SSam Leffler 	 * Setup the hardware after reset: the key cache
23955591b213SSam Leffler 	 * is filled as needed and the receive engine is
23965591b213SSam Leffler 	 * set going.  Frame transmit is handled entirely
23975591b213SSam Leffler 	 * in the frame output path; there's nothing to do
23985591b213SSam Leffler 	 * here except setup the interrupt mask.
23995591b213SSam Leffler 	 */
24005591b213SSam Leffler 	if (ath_startrecv(sc) != 0) {
24015591b213SSam Leffler 		if_printf(ifp, "unable to start recv logic\n");
2402f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
2403b032f27cSSam Leffler 		ATH_UNLOCK(sc);
2404b032f27cSSam Leffler 		return;
24055591b213SSam Leffler 	}
24065591b213SSam Leffler 
24075591b213SSam Leffler 	/*
24085591b213SSam Leffler 	 * Enable interrupts.
24095591b213SSam Leffler 	 */
24105591b213SSam Leffler 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
24115591b213SSam Leffler 		  | HAL_INT_RXEOL | HAL_INT_RXORN
241269930f87SAdrian Chadd 		  | HAL_INT_TXURN
24135591b213SSam Leffler 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
2414bcbb08ceSAdrian Chadd 
2415bcbb08ceSAdrian Chadd 	/*
2416bcbb08ceSAdrian Chadd 	 * Enable RX EDMA bits.  Note these overlap with
2417bcbb08ceSAdrian Chadd 	 * HAL_INT_RX and HAL_INT_RXDESC respectively.
2418bcbb08ceSAdrian Chadd 	 */
2419bcbb08ceSAdrian Chadd 	if (sc->sc_isedma)
2420bcbb08ceSAdrian Chadd 		sc->sc_imask |= (HAL_INT_RXHP | HAL_INT_RXLP);
2421bcbb08ceSAdrian Chadd 
2422c42a7b7eSSam Leffler 	/*
2423c42a7b7eSSam Leffler 	 * Enable MIB interrupts when there are hardware phy counters.
2424c42a7b7eSSam Leffler 	 * Note we only do this (at the moment) for station mode.
2425c42a7b7eSSam Leffler 	 */
2426c42a7b7eSSam Leffler 	if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
2427c42a7b7eSSam Leffler 		sc->sc_imask |= HAL_INT_MIB;
24285591b213SSam Leffler 
2429f5c30c4eSAdrian Chadd 	/*
2430f5c30c4eSAdrian Chadd 	 * XXX add capability for this.
2431f5c30c4eSAdrian Chadd 	 *
2432f5c30c4eSAdrian Chadd 	 * If we're in STA mode (and maybe IBSS?) then register for
2433f5c30c4eSAdrian Chadd 	 * TSFOOR interrupts.
2434f5c30c4eSAdrian Chadd 	 */
2435f5c30c4eSAdrian Chadd 	if (ic->ic_opmode == IEEE80211_M_STA)
2436f5c30c4eSAdrian Chadd 		sc->sc_imask |= HAL_INT_TSFOOR;
2437f5c30c4eSAdrian Chadd 
24385594f5c0SAdrian Chadd 	/* Enable global TX timeout and carrier sense timeout if available */
24396ad02dbaSAdrian Chadd 	if (ath_hal_gtxto_supported(ah))
24403788ebedSAdrian Chadd 		sc->sc_imask |= HAL_INT_GTT;
2441d0a0ebc6SAdrian Chadd 
2442d0a0ebc6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: imask=0x%x\n",
2443d0a0ebc6SAdrian Chadd 		__func__, sc->sc_imask);
24446ad02dbaSAdrian Chadd 
244513f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
24462e986da5SSam Leffler 	callout_reset(&sc->sc_wd_ch, hz, ath_watchdog, sc);
2447b032f27cSSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
24485591b213SSam Leffler 
2449f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2450b032f27cSSam Leffler 	ATH_UNLOCK(sc);
2451b032f27cSSam Leffler 
245286e07743SSam Leffler #ifdef ATH_TX99_DIAG
245386e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
245486e07743SSam Leffler 		sc->sc_tx99->start(sc->sc_tx99);
245586e07743SSam Leffler 	else
245686e07743SSam Leffler #endif
2457b032f27cSSam Leffler 	ieee80211_start_all(ic);		/* start all vap's */
24585591b213SSam Leffler }
24595591b213SSam Leffler 
24605591b213SSam Leffler static void
2461c42a7b7eSSam Leffler ath_stop_locked(struct ifnet *ifp)
24625591b213SSam Leffler {
24635591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
24645591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
24655591b213SSam Leffler 
2466c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
2467c42a7b7eSSam Leffler 		__func__, sc->sc_invalid, ifp->if_flags);
24685591b213SSam Leffler 
2469c42a7b7eSSam Leffler 	ATH_LOCK_ASSERT(sc);
2470f5c30c4eSAdrian Chadd 
2471f5c30c4eSAdrian Chadd 	/*
2472f5c30c4eSAdrian Chadd 	 * Wake the hardware up before fiddling with it.
2473f5c30c4eSAdrian Chadd 	 */
2474f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2475f5c30c4eSAdrian Chadd 
247613f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
24775591b213SSam Leffler 		/*
24785591b213SSam Leffler 		 * Shutdown the hardware and driver:
2479c42a7b7eSSam Leffler 		 *    reset 802.11 state machine
24805591b213SSam Leffler 		 *    turn off timers
2481c42a7b7eSSam Leffler 		 *    disable interrupts
2482c42a7b7eSSam Leffler 		 *    turn off the radio
24835591b213SSam Leffler 		 *    clear transmit machinery
24845591b213SSam Leffler 		 *    clear receive machinery
24855591b213SSam Leffler 		 *    drain and release tx queues
24865591b213SSam Leffler 		 *    reclaim beacon resources
24875591b213SSam Leffler 		 *    power down hardware
24885591b213SSam Leffler 		 *
24895591b213SSam Leffler 		 * Note that some of this work is not possible if the
24905591b213SSam Leffler 		 * hardware is gone (invalid).
24915591b213SSam Leffler 		 */
249286e07743SSam Leffler #ifdef ATH_TX99_DIAG
249386e07743SSam Leffler 		if (sc->sc_tx99 != NULL)
249486e07743SSam Leffler 			sc->sc_tx99->stop(sc->sc_tx99);
249586e07743SSam Leffler #endif
24962e986da5SSam Leffler 		callout_stop(&sc->sc_wd_ch);
24972e986da5SSam Leffler 		sc->sc_wd_timer = 0;
249813f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2499c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
25003e50ec2cSSam Leffler 			if (sc->sc_softled) {
25013e50ec2cSSam Leffler 				callout_stop(&sc->sc_ledtimer);
25023e50ec2cSSam Leffler 				ath_hal_gpioset(ah, sc->sc_ledpin,
25033e50ec2cSSam Leffler 					!sc->sc_ledon);
25043e50ec2cSSam Leffler 				sc->sc_blinking = 0;
25053e50ec2cSSam Leffler 			}
25065591b213SSam Leffler 			ath_hal_intrset(ah, 0);
2507c42a7b7eSSam Leffler 		}
2508517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);
2509c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
25109a842e8bSAdrian Chadd 			ath_stoprecv(sc, 1);
2511c42a7b7eSSam Leffler 			ath_hal_phydisable(ah);
2512c42a7b7eSSam Leffler 		} else
25135591b213SSam Leffler 			sc->sc_rxlink = NULL;
2514b032f27cSSam Leffler 		ath_beacon_free(sc);	/* XXX not needed */
2515c42a7b7eSSam Leffler 	}
2516f5c30c4eSAdrian Chadd 
2517f5c30c4eSAdrian Chadd 	/* And now, restore the current power state */
2518f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2519c42a7b7eSSam Leffler }
2520c42a7b7eSSam Leffler 
2521f5c30c4eSAdrian Chadd /*
2522f5c30c4eSAdrian Chadd  * Wait until all pending TX/RX has completed.
2523f5c30c4eSAdrian Chadd  *
2524f5c30c4eSAdrian Chadd  * This waits until all existing transmit, receive and interrupts
2525f5c30c4eSAdrian Chadd  * have completed.  It's assumed that the caller has first
2526f5c30c4eSAdrian Chadd  * grabbed the reset lock so it doesn't try to do overlapping
2527f5c30c4eSAdrian Chadd  * chip resets.
2528f5c30c4eSAdrian Chadd  */
2529f5c30c4eSAdrian Chadd #define	MAX_TXRX_ITERATIONS	100
2530ef27340cSAdrian Chadd static void
253121008bf1SAdrian Chadd ath_txrx_stop_locked(struct ath_softc *sc)
2532ef27340cSAdrian Chadd {
2533ef27340cSAdrian Chadd 	int i = MAX_TXRX_ITERATIONS;
2534ef27340cSAdrian Chadd 
2535ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
253621008bf1SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
253721008bf1SAdrian Chadd 
2538ef27340cSAdrian Chadd 	/*
2539ef27340cSAdrian Chadd 	 * Sleep until all the pending operations have completed.
2540ef27340cSAdrian Chadd 	 *
2541ef27340cSAdrian Chadd 	 * The caller must ensure that reset has been incremented
2542ef27340cSAdrian Chadd 	 * or the pending operations may continue being queued.
2543ef27340cSAdrian Chadd 	 */
2544ef27340cSAdrian Chadd 	while (sc->sc_rxproc_cnt || sc->sc_txproc_cnt ||
2545ef27340cSAdrian Chadd 	    sc->sc_txstart_cnt || sc->sc_intr_cnt) {
2546ef27340cSAdrian Chadd 		if (i <= 0)
2547ef27340cSAdrian Chadd 			break;
2548f5c30c4eSAdrian Chadd 		msleep(sc, &sc->sc_pcu_mtx, 0, "ath_txrx_stop",
2549f5c30c4eSAdrian Chadd 		    msecs_to_ticks(10));
2550ef27340cSAdrian Chadd 		i--;
2551ef27340cSAdrian Chadd 	}
2552ef27340cSAdrian Chadd 
2553ef27340cSAdrian Chadd 	if (i <= 0)
2554ef27340cSAdrian Chadd 		device_printf(sc->sc_dev,
2555ef27340cSAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2556ef27340cSAdrian Chadd 		    __func__, MAX_TXRX_ITERATIONS);
2557ef27340cSAdrian Chadd }
2558ef27340cSAdrian Chadd #undef	MAX_TXRX_ITERATIONS
2559ef27340cSAdrian Chadd 
2560e78719adSAdrian Chadd #if 0
2561ef27340cSAdrian Chadd static void
256221008bf1SAdrian Chadd ath_txrx_stop(struct ath_softc *sc)
256321008bf1SAdrian Chadd {
256421008bf1SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
256521008bf1SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
256621008bf1SAdrian Chadd 
256721008bf1SAdrian Chadd 	ATH_PCU_LOCK(sc);
256821008bf1SAdrian Chadd 	ath_txrx_stop_locked(sc);
256921008bf1SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
257021008bf1SAdrian Chadd }
2571e78719adSAdrian Chadd #endif
257221008bf1SAdrian Chadd 
257321008bf1SAdrian Chadd static void
2574ef27340cSAdrian Chadd ath_txrx_start(struct ath_softc *sc)
2575ef27340cSAdrian Chadd {
2576ef27340cSAdrian Chadd 
2577ef27340cSAdrian Chadd 	taskqueue_unblock(sc->sc_tq);
2578ef27340cSAdrian Chadd }
2579ef27340cSAdrian Chadd 
2580ee321975SAdrian Chadd /*
2581ee321975SAdrian Chadd  * Grab the reset lock, and wait around until noone else
2582ee321975SAdrian Chadd  * is trying to do anything with it.
2583ee321975SAdrian Chadd  *
2584ee321975SAdrian Chadd  * This is totally horrible but we can't hold this lock for
2585ee321975SAdrian Chadd  * long enough to do TX/RX or we end up with net80211/ip stack
2586ee321975SAdrian Chadd  * LORs and eventual deadlock.
2587ee321975SAdrian Chadd  *
2588ee321975SAdrian Chadd  * "dowait" signals whether to spin, waiting for the reset
2589ee321975SAdrian Chadd  * lock count to reach 0. This should (for now) only be used
2590ee321975SAdrian Chadd  * during the reset path, as the rest of the code may not
2591ee321975SAdrian Chadd  * be locking-reentrant enough to behave correctly.
2592ee321975SAdrian Chadd  *
2593ee321975SAdrian Chadd  * Another, cleaner way should be found to serialise all of
2594ee321975SAdrian Chadd  * these operations.
2595ee321975SAdrian Chadd  */
2596f5c30c4eSAdrian Chadd #define	MAX_RESET_ITERATIONS	25
2597ee321975SAdrian Chadd static int
2598ee321975SAdrian Chadd ath_reset_grablock(struct ath_softc *sc, int dowait)
2599ee321975SAdrian Chadd {
2600ee321975SAdrian Chadd 	int w = 0;
2601ee321975SAdrian Chadd 	int i = MAX_RESET_ITERATIONS;
2602ee321975SAdrian Chadd 
2603ee321975SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
2604ee321975SAdrian Chadd 	do {
2605ee321975SAdrian Chadd 		if (sc->sc_inreset_cnt == 0) {
2606ee321975SAdrian Chadd 			w = 1;
2607ee321975SAdrian Chadd 			break;
2608ee321975SAdrian Chadd 		}
2609ee321975SAdrian Chadd 		if (dowait == 0) {
2610ee321975SAdrian Chadd 			w = 0;
2611ee321975SAdrian Chadd 			break;
2612ee321975SAdrian Chadd 		}
2613ee321975SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2614f5c30c4eSAdrian Chadd 		/*
2615f5c30c4eSAdrian Chadd 		 * 1 tick is likely not enough time for long calibrations
2616f5c30c4eSAdrian Chadd 		 * to complete.  So we should wait quite a while.
2617f5c30c4eSAdrian Chadd 		 */
2618f5c30c4eSAdrian Chadd 		pause("ath_reset_grablock", msecs_to_ticks(100));
2619ee321975SAdrian Chadd 		i--;
2620ee321975SAdrian Chadd 		ATH_PCU_LOCK(sc);
2621ee321975SAdrian Chadd 	} while (i > 0);
2622ee321975SAdrian Chadd 
2623ee321975SAdrian Chadd 	/*
2624ee321975SAdrian Chadd 	 * We always increment the refcounter, regardless
2625ee321975SAdrian Chadd 	 * of whether we succeeded to get it in an exclusive
2626ee321975SAdrian Chadd 	 * way.
2627ee321975SAdrian Chadd 	 */
2628ee321975SAdrian Chadd 	sc->sc_inreset_cnt++;
2629ee321975SAdrian Chadd 
2630ee321975SAdrian Chadd 	if (i <= 0)
2631ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2632ee321975SAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2633ee321975SAdrian Chadd 		    __func__, MAX_RESET_ITERATIONS);
2634ee321975SAdrian Chadd 
2635ee321975SAdrian Chadd 	if (w == 0)
2636ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2637ee321975SAdrian Chadd 		    "%s: warning, recursive reset path!\n",
2638ee321975SAdrian Chadd 		    __func__);
2639ee321975SAdrian Chadd 
2640ee321975SAdrian Chadd 	return w;
2641ee321975SAdrian Chadd }
2642ee321975SAdrian Chadd #undef MAX_RESET_ITERATIONS
2643ee321975SAdrian Chadd 
2644ee321975SAdrian Chadd /*
2645ee321975SAdrian Chadd  * XXX TODO: write ath_reset_releaselock
2646ee321975SAdrian Chadd  */
2647ee321975SAdrian Chadd 
2648c42a7b7eSSam Leffler static void
2649c42a7b7eSSam Leffler ath_stop(struct ifnet *ifp)
2650c42a7b7eSSam Leffler {
2651c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2652c42a7b7eSSam Leffler 
2653c42a7b7eSSam Leffler 	ATH_LOCK(sc);
2654c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
2655f0b2a0beSSam Leffler 	ATH_UNLOCK(sc);
26565591b213SSam Leffler }
26575591b213SSam Leffler 
26585591b213SSam Leffler /*
26595591b213SSam Leffler  * Reset the hardware w/o losing operational state.  This is
26605591b213SSam Leffler  * basically a more efficient way of doing ath_stop, ath_init,
26615591b213SSam Leffler  * followed by state transitions to the current 802.11
2662c42a7b7eSSam Leffler  * operational state.  Used to recover from various errors and
2663c42a7b7eSSam Leffler  * to reset or reload hardware state.
26645591b213SSam Leffler  */
26656079fdbeSAdrian Chadd int
2666517526efSAdrian Chadd ath_reset(struct ifnet *ifp, ATH_RESET_TYPE reset_type)
26675591b213SSam Leffler {
2668c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2669b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
26705591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
26715591b213SSam Leffler 	HAL_STATUS status;
2672ef27340cSAdrian Chadd 	int i;
26735591b213SSam Leffler 
2674f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
267516d4de92SAdrian Chadd 
2676ee321975SAdrian Chadd 	/* Ensure ATH_LOCK isn't held; ath_rx_proc can't be locked */
2677ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
2678ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
2679ef27340cSAdrian Chadd 
2680d52f7132SAdrian Chadd 	/* Try to (stop any further TX/RX from occuring */
2681d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
2682d52f7132SAdrian Chadd 
2683f5c30c4eSAdrian Chadd 	/*
2684f5c30c4eSAdrian Chadd 	 * Wake the hardware up.
2685f5c30c4eSAdrian Chadd 	 */
2686f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2687f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2688f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2689f5c30c4eSAdrian Chadd 
2690ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2691904e385eSAdrian Chadd 
2692904e385eSAdrian Chadd 	/*
2693904e385eSAdrian Chadd 	 * Grab the reset lock before TX/RX is stopped.
2694904e385eSAdrian Chadd 	 *
2695904e385eSAdrian Chadd 	 * This is needed to ensure that when the TX/RX actually does finish,
2696904e385eSAdrian Chadd 	 * no further TX/RX/reset runs in parallel with this.
2697904e385eSAdrian Chadd 	 */
2698ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
2699ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
2700ef27340cSAdrian Chadd 		    __func__);
2701ef27340cSAdrian Chadd 	}
2702904e385eSAdrian Chadd 
2703904e385eSAdrian Chadd 	/* disable interrupts */
2704904e385eSAdrian Chadd 	ath_hal_intrset(ah, 0);
2705904e385eSAdrian Chadd 
2706904e385eSAdrian Chadd 	/*
2707904e385eSAdrian Chadd 	 * Now, ensure that any in progress TX/RX completes before we
2708904e385eSAdrian Chadd 	 * continue.
2709904e385eSAdrian Chadd 	 */
2710904e385eSAdrian Chadd 	ath_txrx_stop_locked(sc);
2711904e385eSAdrian Chadd 
2712ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2713ef27340cSAdrian Chadd 
2714f52d3452SAdrian Chadd 	/*
27159a842e8bSAdrian Chadd 	 * Should now wait for pending TX/RX to complete
27169a842e8bSAdrian Chadd 	 * and block future ones from occuring. This needs to be
27179a842e8bSAdrian Chadd 	 * done before the TX queue is drained.
2718f52d3452SAdrian Chadd 	 */
2719ef27340cSAdrian Chadd 	ath_draintxq(sc, reset_type);	/* stop xmit side */
2720ef27340cSAdrian Chadd 
2721ef27340cSAdrian Chadd 	/*
2722ef27340cSAdrian Chadd 	 * Regardless of whether we're doing a no-loss flush or
2723ef27340cSAdrian Chadd 	 * not, stop the PCU and handle what's in the RX queue.
2724ef27340cSAdrian Chadd 	 * That way frames aren't dropped which shouldn't be.
2725ef27340cSAdrian Chadd 	 */
27269a842e8bSAdrian Chadd 	ath_stoprecv(sc, (reset_type != ATH_RESET_NOLOSS));
2727f8cc9b09SAdrian Chadd 	ath_rx_flush(sc);
2728ef27340cSAdrian Chadd 
2729b032f27cSSam Leffler 	ath_settkipmic(sc);		/* configure TKIP MIC handling */
27305591b213SSam Leffler 	/* NB: indicate channel change so we do a full reset */
27316322256bSAdrian Chadd 	ath_update_chainmasks(sc, ic->ic_curchan);
27326322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
27336322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
273459efa8b5SSam Leffler 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_TRUE, &status))
27355591b213SSam Leffler 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
27365591b213SSam Leffler 			__func__, status);
2737c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
273848237774SAdrian Chadd 
273948237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
274048237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
274148237774SAdrian Chadd 
27429af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
27439af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
27449af351f9SAdrian Chadd 
2745dd6a574eSAdrian Chadd 	/*
2746b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
2747b70f530bSAdrian Chadd 	 */
2748b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
2749b70f530bSAdrian Chadd 
2750b70f530bSAdrian Chadd 	/*
2751dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
2752dd6a574eSAdrian Chadd 	 * support it.
2753dd6a574eSAdrian Chadd 	 */
2754dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
2755dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
2756dd6a574eSAdrian Chadd 	else
2757dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
2758dd6a574eSAdrian Chadd 
275968e8e04eSSam Leffler 	if (ath_startrecv(sc) != 0)	/* restart recv */
276068e8e04eSSam Leffler 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
2761c42a7b7eSSam Leffler 	/*
2762c42a7b7eSSam Leffler 	 * We may be doing a reset in response to an ioctl
2763c42a7b7eSSam Leffler 	 * that changes the channel so update any state that
2764c42a7b7eSSam Leffler 	 * might change as a result.
2765c42a7b7eSSam Leffler 	 */
2766724c193aSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
2767c89b957aSSam Leffler 	if (sc->sc_beacons) {		/* restart beacons */
2768584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
276910ad9a77SSam Leffler 		if (sc->sc_tdma)
277010ad9a77SSam Leffler 			ath_tdma_config(sc, NULL);
277110ad9a77SSam Leffler 		else
277210ad9a77SSam Leffler #endif
2773c89b957aSSam Leffler 			ath_beacon_config(sc, NULL);
277410ad9a77SSam Leffler 	}
2775c42a7b7eSSam Leffler 
2776ef27340cSAdrian Chadd 	/*
2777ef27340cSAdrian Chadd 	 * Release the reset lock and re-enable interrupts here.
2778ef27340cSAdrian Chadd 	 * If an interrupt was being processed in ath_intr(),
2779ef27340cSAdrian Chadd 	 * it would disable interrupts at this point. So we have
2780ef27340cSAdrian Chadd 	 * to atomically enable interrupts and decrement the
2781ef27340cSAdrian Chadd 	 * reset counter - this way ath_intr() doesn't end up
2782ef27340cSAdrian Chadd 	 * disabling interrupts without a corresponding enable
2783ef27340cSAdrian Chadd 	 * in the rest or channel change path.
2784f5c30c4eSAdrian Chadd 	 *
2785f5c30c4eSAdrian Chadd 	 * Grab the TX reference in case we need to transmit.
2786f5c30c4eSAdrian Chadd 	 * That way a parallel transmit doesn't.
2787ef27340cSAdrian Chadd 	 */
2788ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2789ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
2790f5c30c4eSAdrian Chadd 	sc->sc_txstart_cnt++;
2791ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
2792ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
2793ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2794ef27340cSAdrian Chadd 
2795ef27340cSAdrian Chadd 	/*
2796ef27340cSAdrian Chadd 	 * TX and RX can be started here. If it were started with
2797ef27340cSAdrian Chadd 	 * sc_inreset_cnt > 0, the TX and RX path would abort.
2798ef27340cSAdrian Chadd 	 * Thus if this is a nested call through the reset or
2799ef27340cSAdrian Chadd 	 * channel change code, TX completion will occur but
2800ef27340cSAdrian Chadd 	 * RX completion and ath_start / ath_tx_start will not
2801ef27340cSAdrian Chadd 	 * run.
2802ef27340cSAdrian Chadd 	 */
2803ef27340cSAdrian Chadd 
2804ef27340cSAdrian Chadd 	/* Restart TX/RX as needed */
2805ef27340cSAdrian Chadd 	ath_txrx_start(sc);
2806ef27340cSAdrian Chadd 
2807f5c30c4eSAdrian Chadd 	/* XXX TODO: we need to hold the tx refcount here! */
2808f5c30c4eSAdrian Chadd 
2809375307d4SAdrian Chadd 	/* Restart TX completion and pending TX */
2810ef27340cSAdrian Chadd 	if (reset_type == ATH_RESET_NOLOSS) {
2811ef27340cSAdrian Chadd 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
2812ef27340cSAdrian Chadd 			if (ATH_TXQ_SETUP(sc, i)) {
2813b837332dSAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
2814ef27340cSAdrian Chadd 				ath_txq_restart_dma(sc, &sc->sc_txq[i]);
2815b837332dSAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
2816b837332dSAdrian Chadd 
2817b837332dSAdrian Chadd 				ATH_TX_LOCK(sc);
2818ef27340cSAdrian Chadd 				ath_txq_sched(sc, &sc->sc_txq[i]);
2819375307d4SAdrian Chadd 				ATH_TX_UNLOCK(sc);
2820ef27340cSAdrian Chadd 			}
2821b837332dSAdrian Chadd 		}
2822b837332dSAdrian Chadd 	}
2823ef27340cSAdrian Chadd 
2824ef27340cSAdrian Chadd 	/*
2825ef27340cSAdrian Chadd 	 * This may have been set during an ath_start() call which
2826ef27340cSAdrian Chadd 	 * set this once it detected a concurrent TX was going on.
2827ef27340cSAdrian Chadd 	 * So, clear it.
2828ef27340cSAdrian Chadd 	 */
2829e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
2830ef27340cSAdrian Chadd 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2831e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
2832ef27340cSAdrian Chadd 
2833f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2834f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2835f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2836f5c30c4eSAdrian Chadd 
2837f5c30c4eSAdrian Chadd 	ATH_PCU_LOCK(sc);
2838f5c30c4eSAdrian Chadd 	sc->sc_txstart_cnt--;
2839f5c30c4eSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2840f5c30c4eSAdrian Chadd 
2841ef27340cSAdrian Chadd 	/* Handle any frames in the TX queue */
2842ef27340cSAdrian Chadd 	/*
2843ef27340cSAdrian Chadd 	 * XXX should this be done by the caller, rather than
2844ef27340cSAdrian Chadd 	 * ath_reset() ?
2845ef27340cSAdrian Chadd 	 */
28468e739394SAdrian Chadd 	ath_tx_kick(sc);		/* restart xmit */
2847c42a7b7eSSam Leffler 	return 0;
28485591b213SSam Leffler }
28495591b213SSam Leffler 
285068e8e04eSSam Leffler static int
2851b032f27cSSam Leffler ath_reset_vap(struct ieee80211vap *vap, u_long cmd)
2852b032f27cSSam Leffler {
28534b54a231SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
28544b54a231SSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
28554b54a231SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
28564b54a231SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
28574b54a231SSam Leffler 
28584b54a231SSam Leffler 	switch (cmd) {
28594b54a231SSam Leffler 	case IEEE80211_IOC_TXPOWER:
28604b54a231SSam Leffler 		/*
28614b54a231SSam Leffler 		 * If per-packet TPC is enabled, then we have nothing
28624b54a231SSam Leffler 		 * to do; otherwise we need to force the global limit.
28634b54a231SSam Leffler 		 * All this can happen directly; no need to reset.
28644b54a231SSam Leffler 		 */
28654b54a231SSam Leffler 		if (!ath_hal_gettpc(ah))
28664b54a231SSam Leffler 			ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
28674b54a231SSam Leffler 		return 0;
28684b54a231SSam Leffler 	}
2869517526efSAdrian Chadd 	/* XXX? Full or NOLOSS? */
2870517526efSAdrian Chadd 	return ath_reset(ifp, ATH_RESET_FULL);
2871b032f27cSSam Leffler }
2872b032f27cSSam Leffler 
2873b8e788a5SAdrian Chadd struct ath_buf *
2874af33d486SAdrian Chadd _ath_getbuf_locked(struct ath_softc *sc, ath_buf_type_t btype)
287510ad9a77SSam Leffler {
287610ad9a77SSam Leffler 	struct ath_buf *bf;
287710ad9a77SSam Leffler 
287810ad9a77SSam Leffler 	ATH_TXBUF_LOCK_ASSERT(sc);
287910ad9a77SSam Leffler 
2880af33d486SAdrian Chadd 	if (btype == ATH_BUFTYPE_MGMT)
2881af33d486SAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_txbuf_mgmt);
2882af33d486SAdrian Chadd 	else
28836b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_txbuf);
2884af33d486SAdrian Chadd 
2885e346b073SAdrian Chadd 	if (bf == NULL) {
2886e346b073SAdrian Chadd 		sc->sc_stats.ast_tx_getnobuf++;
2887e346b073SAdrian Chadd 	} else {
2888e346b073SAdrian Chadd 		if (bf->bf_flags & ATH_BUF_BUSY) {
2889e346b073SAdrian Chadd 			sc->sc_stats.ast_tx_getbusybuf++;
2890e346b073SAdrian Chadd 			bf = NULL;
2891e346b073SAdrian Chadd 		}
2892e346b073SAdrian Chadd 	}
2893e346b073SAdrian Chadd 
2894af33d486SAdrian Chadd 	if (bf != NULL && (bf->bf_flags & ATH_BUF_BUSY) == 0) {
2895af33d486SAdrian Chadd 		if (btype == ATH_BUFTYPE_MGMT)
2896af33d486SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_txbuf_mgmt, bf, bf_list);
289723ced6c1SAdrian Chadd 		else {
2898af33d486SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
289923ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt--;
290023ced6c1SAdrian Chadd 
290123ced6c1SAdrian Chadd 			/*
290223ced6c1SAdrian Chadd 			 * This shuldn't happen; however just to be
290323ced6c1SAdrian Chadd 			 * safe print a warning and fudge the txbuf
290423ced6c1SAdrian Chadd 			 * count.
290523ced6c1SAdrian Chadd 			 */
290623ced6c1SAdrian Chadd 			if (sc->sc_txbuf_cnt < 0) {
290723ced6c1SAdrian Chadd 				device_printf(sc->sc_dev,
290823ced6c1SAdrian Chadd 				    "%s: sc_txbuf_cnt < 0?\n",
290923ced6c1SAdrian Chadd 				    __func__);
291023ced6c1SAdrian Chadd 				sc->sc_txbuf_cnt = 0;
291123ced6c1SAdrian Chadd 			}
291223ced6c1SAdrian Chadd 		}
2913af33d486SAdrian Chadd 	} else
291410ad9a77SSam Leffler 		bf = NULL;
2915e346b073SAdrian Chadd 
291610ad9a77SSam Leffler 	if (bf == NULL) {
2917af33d486SAdrian Chadd 		/* XXX should check which list, mgmt or otherwise */
291810ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: %s\n", __func__,
29196b349e5aSAdrian Chadd 		    TAILQ_FIRST(&sc->sc_txbuf) == NULL ?
292010ad9a77SSam Leffler 			"out of xmit buffers" : "xmit buffer busy");
2921e346b073SAdrian Chadd 		return NULL;
292210ad9a77SSam Leffler 	}
2923e346b073SAdrian Chadd 
2924af33d486SAdrian Chadd 	/* XXX TODO: should do this at buffer list initialisation */
2925af33d486SAdrian Chadd 	/* XXX (then, ensure the buffer has the right flag set) */
29263feffbd7SAdrian Chadd 	bf->bf_flags = 0;
2927af33d486SAdrian Chadd 	if (btype == ATH_BUFTYPE_MGMT)
2928af33d486SAdrian Chadd 		bf->bf_flags |= ATH_BUF_MGMT;
2929af33d486SAdrian Chadd 	else
2930af33d486SAdrian Chadd 		bf->bf_flags &= (~ATH_BUF_MGMT);
2931af33d486SAdrian Chadd 
2932e346b073SAdrian Chadd 	/* Valid bf here; clear some basic fields */
2933e346b073SAdrian Chadd 	bf->bf_next = NULL;	/* XXX just to be sure */
2934e346b073SAdrian Chadd 	bf->bf_last = NULL;	/* XXX again, just to be sure */
2935e346b073SAdrian Chadd 	bf->bf_comp = NULL;	/* XXX again, just to be sure */
2936e346b073SAdrian Chadd 	bzero(&bf->bf_state, sizeof(bf->bf_state));
2937e346b073SAdrian Chadd 
293885bf9bc3SAdrian Chadd 	/*
293985bf9bc3SAdrian Chadd 	 * Track the descriptor ID only if doing EDMA
294085bf9bc3SAdrian Chadd 	 */
294185bf9bc3SAdrian Chadd 	if (sc->sc_isedma) {
294285bf9bc3SAdrian Chadd 		bf->bf_descid = sc->sc_txbuf_descid;
294385bf9bc3SAdrian Chadd 		sc->sc_txbuf_descid++;
294485bf9bc3SAdrian Chadd 	}
294585bf9bc3SAdrian Chadd 
294610ad9a77SSam Leffler 	return bf;
294710ad9a77SSam Leffler }
294810ad9a77SSam Leffler 
2949e346b073SAdrian Chadd /*
2950e346b073SAdrian Chadd  * When retrying a software frame, buffers marked ATH_BUF_BUSY
2951e346b073SAdrian Chadd  * can't be thrown back on the queue as they could still be
2952e346b073SAdrian Chadd  * in use by the hardware.
2953e346b073SAdrian Chadd  *
2954e346b073SAdrian Chadd  * This duplicates the buffer, or returns NULL.
2955e346b073SAdrian Chadd  *
2956e346b073SAdrian Chadd  * The descriptor is also copied but the link pointers and
2957e346b073SAdrian Chadd  * the DMA segments aren't copied; this frame should thus
2958e346b073SAdrian Chadd  * be again passed through the descriptor setup/chain routines
2959e346b073SAdrian Chadd  * so the link is correct.
2960e346b073SAdrian Chadd  *
2961e346b073SAdrian Chadd  * The caller must free the buffer using ath_freebuf().
2962e346b073SAdrian Chadd  */
2963e346b073SAdrian Chadd struct ath_buf *
29643f3a5dbdSAdrian Chadd ath_buf_clone(struct ath_softc *sc, struct ath_buf *bf)
2965e346b073SAdrian Chadd {
2966e346b073SAdrian Chadd 	struct ath_buf *tbf;
2967e346b073SAdrian Chadd 
2968af33d486SAdrian Chadd 	tbf = ath_getbuf(sc,
2969af33d486SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_MGMT) ?
2970af33d486SAdrian Chadd 	     ATH_BUFTYPE_MGMT : ATH_BUFTYPE_NORMAL);
2971e346b073SAdrian Chadd 	if (tbf == NULL)
2972e346b073SAdrian Chadd 		return NULL;	/* XXX failure? Why? */
2973e346b073SAdrian Chadd 
2974e346b073SAdrian Chadd 	/* Copy basics */
2975e346b073SAdrian Chadd 	tbf->bf_next = NULL;
2976e346b073SAdrian Chadd 	tbf->bf_nseg = bf->bf_nseg;
29773feffbd7SAdrian Chadd 	tbf->bf_flags = bf->bf_flags & ATH_BUF_FLAGS_CLONE;
2978e346b073SAdrian Chadd 	tbf->bf_status = bf->bf_status;
2979e346b073SAdrian Chadd 	tbf->bf_m = bf->bf_m;
2980e346b073SAdrian Chadd 	tbf->bf_node = bf->bf_node;
2981f5c30c4eSAdrian Chadd 	KASSERT((bf->bf_node != NULL), ("%s: bf_node=NULL!", __func__));
2982e346b073SAdrian Chadd 	/* will be setup by the chain/setup function */
2983e346b073SAdrian Chadd 	tbf->bf_lastds = NULL;
2984e346b073SAdrian Chadd 	/* for now, last == self */
2985e346b073SAdrian Chadd 	tbf->bf_last = tbf;
2986e346b073SAdrian Chadd 	tbf->bf_comp = bf->bf_comp;
2987e346b073SAdrian Chadd 
2988e346b073SAdrian Chadd 	/* NOTE: DMA segments will be setup by the setup/chain functions */
2989e346b073SAdrian Chadd 
2990e346b073SAdrian Chadd 	/* The caller has to re-init the descriptor + links */
2991e346b073SAdrian Chadd 
29923f3a5dbdSAdrian Chadd 	/*
29933f3a5dbdSAdrian Chadd 	 * Free the DMA mapping here, before we NULL the mbuf.
29943f3a5dbdSAdrian Chadd 	 * We must only call bus_dmamap_unload() once per mbuf chain
29953f3a5dbdSAdrian Chadd 	 * or behaviour is undefined.
29963f3a5dbdSAdrian Chadd 	 */
29973f3a5dbdSAdrian Chadd 	if (bf->bf_m != NULL) {
29983f3a5dbdSAdrian Chadd 		/*
29993f3a5dbdSAdrian Chadd 		 * XXX is this POSTWRITE call required?
30003f3a5dbdSAdrian Chadd 		 */
30013f3a5dbdSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
30023f3a5dbdSAdrian Chadd 		    BUS_DMASYNC_POSTWRITE);
30033f3a5dbdSAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
30043f3a5dbdSAdrian Chadd 	}
30053f3a5dbdSAdrian Chadd 
30063f3a5dbdSAdrian Chadd 	bf->bf_m = NULL;
30073f3a5dbdSAdrian Chadd 	bf->bf_node = NULL;
30083f3a5dbdSAdrian Chadd 
3009e346b073SAdrian Chadd 	/* Copy state */
3010e346b073SAdrian Chadd 	memcpy(&tbf->bf_state, &bf->bf_state, sizeof(bf->bf_state));
3011e346b073SAdrian Chadd 
3012e346b073SAdrian Chadd 	return tbf;
3013e346b073SAdrian Chadd }
3014e346b073SAdrian Chadd 
3015b8e788a5SAdrian Chadd struct ath_buf *
3016af33d486SAdrian Chadd ath_getbuf(struct ath_softc *sc, ath_buf_type_t btype)
301710ad9a77SSam Leffler {
301810ad9a77SSam Leffler 	struct ath_buf *bf;
301910ad9a77SSam Leffler 
302010ad9a77SSam Leffler 	ATH_TXBUF_LOCK(sc);
3021af33d486SAdrian Chadd 	bf = _ath_getbuf_locked(sc, btype);
3022af33d486SAdrian Chadd 	/*
3023af33d486SAdrian Chadd 	 * If a mgmt buffer was requested but we're out of those,
3024af33d486SAdrian Chadd 	 * try requesting a normal one.
3025af33d486SAdrian Chadd 	 */
3026af33d486SAdrian Chadd 	if (bf == NULL && btype == ATH_BUFTYPE_MGMT)
3027af33d486SAdrian Chadd 		bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL);
3028e4e7938aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
302910ad9a77SSam Leffler 	if (bf == NULL) {
303010ad9a77SSam Leffler 		struct ifnet *ifp = sc->sc_ifp;
303110ad9a77SSam Leffler 
303210ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: stop queue\n", __func__);
303310ad9a77SSam Leffler 		sc->sc_stats.ast_tx_qstop++;
3034e4e7938aSAdrian Chadd 		IF_LOCK(&ifp->if_snd);
303510ad9a77SSam Leffler 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3036e4e7938aSAdrian Chadd 		IF_UNLOCK(&ifp->if_snd);
303710ad9a77SSam Leffler 	}
303810ad9a77SSam Leffler 	return bf;
303910ad9a77SSam Leffler }
304010ad9a77SSam Leffler 
30418e739394SAdrian Chadd static void
3042cd7dffd0SAdrian Chadd ath_qflush(struct ifnet *ifp)
30435591b213SSam Leffler {
30445591b213SSam Leffler 
3045cd7dffd0SAdrian Chadd 	/* XXX TODO */
30468e739394SAdrian Chadd }
30478e739394SAdrian Chadd 
30487dcb2beaSAdrian Chadd /*
3049cd7dffd0SAdrian Chadd  * Transmit a single frame.
3050cd7dffd0SAdrian Chadd  *
3051cd7dffd0SAdrian Chadd  * net80211 will free the node reference if the transmit
3052cd7dffd0SAdrian Chadd  * fails, so don't free the node reference here.
30537dcb2beaSAdrian Chadd  */
3054cd7dffd0SAdrian Chadd static int
3055cd7dffd0SAdrian Chadd ath_transmit(struct ifnet *ifp, struct mbuf *m)
3056cd7dffd0SAdrian Chadd {
3057cd7dffd0SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
3058cd7dffd0SAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3059cd7dffd0SAdrian Chadd 	struct ieee80211_node *ni;
3060cd7dffd0SAdrian Chadd 	struct mbuf *next;
3061cd7dffd0SAdrian Chadd 	struct ath_buf *bf;
3062cd7dffd0SAdrian Chadd 	ath_bufhead frags;
3063cd7dffd0SAdrian Chadd 	int retval = 0;
3064cd7dffd0SAdrian Chadd 
3065cd7dffd0SAdrian Chadd 	/*
3066cd7dffd0SAdrian Chadd 	 * Tell the reset path that we're currently transmitting.
3067cd7dffd0SAdrian Chadd 	 */
3068cd7dffd0SAdrian Chadd 	ATH_PCU_LOCK(sc);
3069cd7dffd0SAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
307083bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
3071cd7dffd0SAdrian Chadd 		    "%s: sc_inreset_cnt > 0; bailing\n", __func__);
3072cd7dffd0SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
3073cd7dffd0SAdrian Chadd 		IF_LOCK(&ifp->if_snd);
3074cd7dffd0SAdrian Chadd 		sc->sc_stats.ast_tx_qstop++;
3075cd7dffd0SAdrian Chadd 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3076cd7dffd0SAdrian Chadd 		IF_UNLOCK(&ifp->if_snd);
3077cd7dffd0SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TX, 0, "ath_start_task: OACTIVE, finish");
3078cd7dffd0SAdrian Chadd 		return (ENOBUFS);	/* XXX should be EINVAL or? */
3079cd7dffd0SAdrian Chadd 	}
3080cd7dffd0SAdrian Chadd 	sc->sc_txstart_cnt++;
3081cd7dffd0SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3082cd7dffd0SAdrian Chadd 
3083f5c30c4eSAdrian Chadd 	/* Wake the hardware up already */
3084f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3085f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3086f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3087f5c30c4eSAdrian Chadd 
3088cd7dffd0SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_transmit: start");
3089cd7dffd0SAdrian Chadd 	/*
3090cd7dffd0SAdrian Chadd 	 * Grab the TX lock - it's ok to do this here; we haven't
3091cd7dffd0SAdrian Chadd 	 * yet started transmitting.
3092cd7dffd0SAdrian Chadd 	 */
3093cd7dffd0SAdrian Chadd 	ATH_TX_LOCK(sc);
3094cd7dffd0SAdrian Chadd 
3095cd7dffd0SAdrian Chadd 	/*
3096cd7dffd0SAdrian Chadd 	 * Node reference, if there's one.
3097cd7dffd0SAdrian Chadd 	 */
30987dcb2beaSAdrian Chadd 	ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
30997dcb2beaSAdrian Chadd 
31007dcb2beaSAdrian Chadd 	/*
31017dcb2beaSAdrian Chadd 	 * Enforce how deep a node queue can get.
31027dcb2beaSAdrian Chadd 	 *
31037dcb2beaSAdrian Chadd 	 * XXX it would be nicer if we kept an mbuf queue per
31047dcb2beaSAdrian Chadd 	 * node and only whacked them into ath_bufs when we
31057dcb2beaSAdrian Chadd 	 * are ready to schedule some traffic from them.
31067dcb2beaSAdrian Chadd 	 * .. that may come later.
31077dcb2beaSAdrian Chadd 	 *
31087dcb2beaSAdrian Chadd 	 * XXX we should also track the per-node hardware queue
31097dcb2beaSAdrian Chadd 	 * depth so it is easy to limit the _SUM_ of the swq and
31107dcb2beaSAdrian Chadd 	 * hwq frames.  Since we only schedule two HWQ frames
31117dcb2beaSAdrian Chadd 	 * at a time, this should be OK for now.
31127dcb2beaSAdrian Chadd 	 */
31137dcb2beaSAdrian Chadd 	if ((!(m->m_flags & M_EAPOL)) &&
31147dcb2beaSAdrian Chadd 	    (ATH_NODE(ni)->an_swq_depth > sc->sc_txq_node_maxdepth)) {
31157dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nodeq_overflow++;
31167dcb2beaSAdrian Chadd 		m_freem(m);
31177dcb2beaSAdrian Chadd 		m = NULL;
3118cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
3119cd7dffd0SAdrian Chadd 		goto finish;
31207dcb2beaSAdrian Chadd 	}
31217dcb2beaSAdrian Chadd 
31227dcb2beaSAdrian Chadd 	/*
31237dcb2beaSAdrian Chadd 	 * Check how many TX buffers are available.
31247dcb2beaSAdrian Chadd 	 *
31257dcb2beaSAdrian Chadd 	 * If this is for non-EAPOL traffic, just leave some
31267dcb2beaSAdrian Chadd 	 * space free in order for buffer cloning and raw
31277dcb2beaSAdrian Chadd 	 * frame transmission to occur.
31287dcb2beaSAdrian Chadd 	 *
31297dcb2beaSAdrian Chadd 	 * If it's for EAPOL traffic, ignore this for now.
31307dcb2beaSAdrian Chadd 	 * Management traffic will be sent via the raw transmit
31317dcb2beaSAdrian Chadd 	 * method which bypasses this check.
31327dcb2beaSAdrian Chadd 	 *
31337dcb2beaSAdrian Chadd 	 * This is needed to ensure that EAPOL frames during
31347dcb2beaSAdrian Chadd 	 * (re) keying have a chance to go out.
31357dcb2beaSAdrian Chadd 	 *
31367dcb2beaSAdrian Chadd 	 * See kern/138379 for more information.
31377dcb2beaSAdrian Chadd 	 */
31387dcb2beaSAdrian Chadd 	if ((!(m->m_flags & M_EAPOL)) &&
31397dcb2beaSAdrian Chadd 	    (sc->sc_txbuf_cnt <= sc->sc_txq_data_minfree)) {
31407dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
3141cd7dffd0SAdrian Chadd 		m_freem(m);
31427dcb2beaSAdrian Chadd 		m = NULL;
3143cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
3144cd7dffd0SAdrian Chadd 		goto finish;
314523ced6c1SAdrian Chadd 	}
314623ced6c1SAdrian Chadd 
31475591b213SSam Leffler 	/*
31485591b213SSam Leffler 	 * Grab a TX buffer and associated resources.
31497dcb2beaSAdrian Chadd 	 *
31507dcb2beaSAdrian Chadd 	 * If it's an EAPOL frame, allocate a MGMT ath_buf.
31517dcb2beaSAdrian Chadd 	 * That way even with temporary buffer exhaustion due to
31527dcb2beaSAdrian Chadd 	 * the data path doesn't leave us without the ability
31537dcb2beaSAdrian Chadd 	 * to transmit management frames.
31547dcb2beaSAdrian Chadd 	 *
31557dcb2beaSAdrian Chadd 	 * Otherwise allocate a normal buffer.
31565591b213SSam Leffler 	 */
31577dcb2beaSAdrian Chadd 	if (m->m_flags & M_EAPOL)
31587dcb2beaSAdrian Chadd 		bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT);
31597dcb2beaSAdrian Chadd 	else
3160af33d486SAdrian Chadd 		bf = ath_getbuf(sc, ATH_BUFTYPE_NORMAL);
31611a85141aSAdrian Chadd 
31627dcb2beaSAdrian Chadd 	if (bf == NULL) {
31637dcb2beaSAdrian Chadd 		/*
3164cd7dffd0SAdrian Chadd 		 * If we failed to allocate a buffer, fail.
31657dcb2beaSAdrian Chadd 		 *
31667dcb2beaSAdrian Chadd 		 * We shouldn't fail normally, due to the check
31677dcb2beaSAdrian Chadd 		 * above.
31687dcb2beaSAdrian Chadd 		 */
31697dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
31707dcb2beaSAdrian Chadd 		IF_LOCK(&ifp->if_snd);
31717dcb2beaSAdrian Chadd 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
31727dcb2beaSAdrian Chadd 		IF_UNLOCK(&ifp->if_snd);
3173cd7dffd0SAdrian Chadd 		m_freem(m);
31747dcb2beaSAdrian Chadd 		m = NULL;
3175cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
3176cd7dffd0SAdrian Chadd 		goto finish;
3177b032f27cSSam Leffler 	}
31787dcb2beaSAdrian Chadd 
3179cd7dffd0SAdrian Chadd 	/*
3180cd7dffd0SAdrian Chadd 	 * At this point we have a buffer; so we need to free it
3181cd7dffd0SAdrian Chadd 	 * if we hit any error conditions.
3182cd7dffd0SAdrian Chadd 	 */
31837dcb2beaSAdrian Chadd 
318468e8e04eSSam Leffler 	/*
318568e8e04eSSam Leffler 	 * Check for fragmentation.  If this frame
318668e8e04eSSam Leffler 	 * has been broken up verify we have enough
318768e8e04eSSam Leffler 	 * buffers to send all the fragments so all
318868e8e04eSSam Leffler 	 * go out or none...
318968e8e04eSSam Leffler 	 */
31906b349e5aSAdrian Chadd 	TAILQ_INIT(&frags);
31911a85141aSAdrian Chadd 	if ((m->m_flags & M_FRAG) &&
31921a85141aSAdrian Chadd 	    !ath_txfrag_setup(sc, &frags, m, ni)) {
319368e8e04eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT,
319468e8e04eSSam Leffler 		    "%s: out of txfrag buffers\n", __func__);
319536c6be9aSSam Leffler 		sc->sc_stats.ast_tx_nofrag++;
31969cb93076SSam Leffler 		ifp->if_oerrors++;
31971a85141aSAdrian Chadd 		ath_freetx(m);
319868e8e04eSSam Leffler 		goto bad;
319968e8e04eSSam Leffler 	}
3200cd7dffd0SAdrian Chadd 
3201cd7dffd0SAdrian Chadd 	/*
3202cd7dffd0SAdrian Chadd 	 * At this point if we have any TX fragments, then we will
3203cd7dffd0SAdrian Chadd 	 * have bumped the node reference once for each of those.
3204cd7dffd0SAdrian Chadd 	 */
3205cd7dffd0SAdrian Chadd 
3206cd7dffd0SAdrian Chadd 	/*
3207cd7dffd0SAdrian Chadd 	 * XXX Is there anything actually _enforcing_ that the
3208cd7dffd0SAdrian Chadd 	 * fragments are being transmitted in one hit, rather than
3209cd7dffd0SAdrian Chadd 	 * being interleaved with other transmissions on that
3210cd7dffd0SAdrian Chadd 	 * hardware queue?
3211cd7dffd0SAdrian Chadd 	 *
3212cd7dffd0SAdrian Chadd 	 * The ATH TX output lock is the only thing serialising this
3213cd7dffd0SAdrian Chadd 	 * right now.
3214cd7dffd0SAdrian Chadd 	 */
3215cd7dffd0SAdrian Chadd 
3216cd7dffd0SAdrian Chadd 	/*
3217cd7dffd0SAdrian Chadd 	 * Calculate the "next fragment" length field in ath_buf
3218cd7dffd0SAdrian Chadd 	 * in order to let the transmit path know enough about
3219cd7dffd0SAdrian Chadd 	 * what to next write to the hardware.
3220cd7dffd0SAdrian Chadd 	 */
3221cd7dffd0SAdrian Chadd 	if (m->m_flags & M_FRAG) {
3222cd7dffd0SAdrian Chadd 		struct ath_buf *fbf = bf;
3223cd7dffd0SAdrian Chadd 		struct ath_buf *n_fbf = NULL;
3224cd7dffd0SAdrian Chadd 		struct mbuf *fm = m->m_nextpkt;
3225cd7dffd0SAdrian Chadd 
3226cd7dffd0SAdrian Chadd 		/*
3227cd7dffd0SAdrian Chadd 		 * We need to walk the list of fragments and set
3228cd7dffd0SAdrian Chadd 		 * the next size to the following buffer.
3229cd7dffd0SAdrian Chadd 		 * However, the first buffer isn't in the frag
3230cd7dffd0SAdrian Chadd 		 * list, so we have to do some gymnastics here.
3231cd7dffd0SAdrian Chadd 		 */
3232cd7dffd0SAdrian Chadd 		TAILQ_FOREACH(n_fbf, &frags, bf_list) {
3233cd7dffd0SAdrian Chadd 			fbf->bf_nextfraglen = fm->m_pkthdr.len;
3234cd7dffd0SAdrian Chadd 			fbf = n_fbf;
3235cd7dffd0SAdrian Chadd 			fm = fm->m_nextpkt;
3236cd7dffd0SAdrian Chadd 		}
3237cd7dffd0SAdrian Chadd 	}
3238cd7dffd0SAdrian Chadd 
3239cd7dffd0SAdrian Chadd 	/*
3240cd7dffd0SAdrian Chadd 	 * Bump the ifp output counter.
3241cd7dffd0SAdrian Chadd 	 *
3242cd7dffd0SAdrian Chadd 	 * XXX should use atomics?
3243cd7dffd0SAdrian Chadd 	 */
32441a85141aSAdrian Chadd 	ifp->if_opackets++;
32451a85141aSAdrian Chadd nextfrag:
324668e8e04eSSam Leffler 	/*
32471a85141aSAdrian Chadd 	 * Pass the frame to the h/w for transmission.
32481a85141aSAdrian Chadd 	 * Fragmented frames have each frag chained together
32491a85141aSAdrian Chadd 	 * with m_nextpkt.  We know there are sufficient ath_buf's
32501a85141aSAdrian Chadd 	 * to send all the frags because of work done by
32511a85141aSAdrian Chadd 	 * ath_txfrag_setup.  We leave m_nextpkt set while
32521a85141aSAdrian Chadd 	 * calling ath_tx_start so it can use it to extend the
32531a85141aSAdrian Chadd 	 * the tx duration to cover the subsequent frag and
32541a85141aSAdrian Chadd 	 * so it can reclaim all the mbufs in case of an error;
32551a85141aSAdrian Chadd 	 * ath_tx_start clears m_nextpkt once it commits to
32561a85141aSAdrian Chadd 	 * handing the frame to the hardware.
3257cd7dffd0SAdrian Chadd 	 *
3258cd7dffd0SAdrian Chadd 	 * Note: if this fails, then the mbufs are freed but
3259cd7dffd0SAdrian Chadd 	 * not the node reference.
326068e8e04eSSam Leffler 	 */
32611a85141aSAdrian Chadd 	next = m->m_nextpkt;
32621a85141aSAdrian Chadd 	if (ath_tx_start(sc, ni, bf, m)) {
32635591b213SSam Leffler bad:
32641a85141aSAdrian Chadd 		ifp->if_oerrors++;
32651a85141aSAdrian Chadd reclaim:
326668e8e04eSSam Leffler 		bf->bf_m = NULL;
326768e8e04eSSam Leffler 		bf->bf_node = NULL;
3268c42a7b7eSSam Leffler 		ATH_TXBUF_LOCK(sc);
3269e1a50456SAdrian Chadd 		ath_returnbuf_head(sc, bf);
3270cd7dffd0SAdrian Chadd 		/*
3271cd7dffd0SAdrian Chadd 		 * Free the rest of the node references and
3272cd7dffd0SAdrian Chadd 		 * buffers for the fragment list.
3273cd7dffd0SAdrian Chadd 		 */
327468e8e04eSSam Leffler 		ath_txfrag_cleanup(sc, &frags, ni);
3275c42a7b7eSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
3276cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
3277cd7dffd0SAdrian Chadd 		goto finish;
32781a85141aSAdrian Chadd 	}
32791a85141aSAdrian Chadd 
3280548a605dSAdrian Chadd 	/*
3281548a605dSAdrian Chadd 	 * Check here if the node is in power save state.
3282548a605dSAdrian Chadd 	 */
3283548a605dSAdrian Chadd 	ath_tx_update_tim(sc, ni, 1);
3284548a605dSAdrian Chadd 
32851a85141aSAdrian Chadd 	if (next != NULL) {
328668e8e04eSSam Leffler 		/*
32871a85141aSAdrian Chadd 		 * Beware of state changing between frags.
32881a85141aSAdrian Chadd 		 * XXX check sta power-save state?
328968e8e04eSSam Leffler 		 */
32901a85141aSAdrian Chadd 		if (ni->ni_vap->iv_state != IEEE80211_S_RUN) {
3291c5239edbSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
32921a85141aSAdrian Chadd 			    "%s: flush fragmented packet, state %s\n",
32931a85141aSAdrian Chadd 			    __func__,
32941a85141aSAdrian Chadd 			    ieee80211_state_name[ni->ni_vap->iv_state]);
3295a91ab3c0SAdrian Chadd 			/* XXX dmamap */
32961a85141aSAdrian Chadd 			ath_freetx(next);
32971a85141aSAdrian Chadd 			goto reclaim;
3298c5239edbSAdrian Chadd 		}
32991a85141aSAdrian Chadd 		m = next;
33001a85141aSAdrian Chadd 		bf = TAILQ_FIRST(&frags);
33011a85141aSAdrian Chadd 		KASSERT(bf != NULL, ("no buf for txfrag"));
33021a85141aSAdrian Chadd 		TAILQ_REMOVE(&frags, bf, bf_list);
33031a85141aSAdrian Chadd 		goto nextfrag;
3304c5239edbSAdrian Chadd 	}
3305c5239edbSAdrian Chadd 
3306cd7dffd0SAdrian Chadd 	/*
3307cd7dffd0SAdrian Chadd 	 * Bump watchdog timer.
3308cd7dffd0SAdrian Chadd 	 */
33091a85141aSAdrian Chadd 	sc->sc_wd_timer = 5;
3310cd7dffd0SAdrian Chadd 
3311cd7dffd0SAdrian Chadd finish:
3312cd7dffd0SAdrian Chadd 	ATH_TX_UNLOCK(sc);
3313cd7dffd0SAdrian Chadd 
3314cd7dffd0SAdrian Chadd 	/*
3315cd7dffd0SAdrian Chadd 	 * Finished transmitting!
3316cd7dffd0SAdrian Chadd 	 */
3317cd7dffd0SAdrian Chadd 	ATH_PCU_LOCK(sc);
3318cd7dffd0SAdrian Chadd 	sc->sc_txstart_cnt--;
3319cd7dffd0SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3320cd7dffd0SAdrian Chadd 
3321f5c30c4eSAdrian Chadd 	/* Sleep the hardware if required */
3322f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3323f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3324f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3325f5c30c4eSAdrian Chadd 
3326cd7dffd0SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_transmit: finished");
3327cd7dffd0SAdrian Chadd 
3328cd7dffd0SAdrian Chadd 	return (retval);
33295591b213SSam Leffler }
3330cd7dffd0SAdrian Chadd 
33315591b213SSam Leffler static int
33325591b213SSam Leffler ath_media_change(struct ifnet *ifp)
33335591b213SSam Leffler {
3334b032f27cSSam Leffler 	int error = ieee80211_media_change(ifp);
3335b032f27cSSam Leffler 	/* NB: only the fixed rate can change and that doesn't need a reset */
3336b032f27cSSam Leffler 	return (error == ENETRESET ? 0 : error);
33375591b213SSam Leffler }
33385591b213SSam Leffler 
3339c42a7b7eSSam Leffler /*
3340c42a7b7eSSam Leffler  * Block/unblock tx+rx processing while a key change is done.
3341c42a7b7eSSam Leffler  * We assume the caller serializes key management operations
3342c42a7b7eSSam Leffler  * so we only need to worry about synchronization with other
3343c42a7b7eSSam Leffler  * uses that originate in the driver.
3344c42a7b7eSSam Leffler  */
3345c42a7b7eSSam Leffler static void
3346b032f27cSSam Leffler ath_key_update_begin(struct ieee80211vap *vap)
3347c42a7b7eSSam Leffler {
3348b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
3349c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
3350c42a7b7eSSam Leffler 
3351c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
3352b032f27cSSam Leffler 	taskqueue_block(sc->sc_tq);
3353c42a7b7eSSam Leffler }
3354c42a7b7eSSam Leffler 
3355c42a7b7eSSam Leffler static void
3356b032f27cSSam Leffler ath_key_update_end(struct ieee80211vap *vap)
3357c42a7b7eSSam Leffler {
3358b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
3359c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
3360c42a7b7eSSam Leffler 
3361c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
3362b032f27cSSam Leffler 	taskqueue_unblock(sc->sc_tq);
3363c42a7b7eSSam Leffler }
33645591b213SSam Leffler 
3365b032f27cSSam Leffler static void
3366b032f27cSSam Leffler ath_update_promisc(struct ifnet *ifp)
3367b032f27cSSam Leffler {
3368b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
3369b032f27cSSam Leffler 	u_int32_t rfilt;
3370b032f27cSSam Leffler 
3371b032f27cSSam Leffler 	/* configure rx filter */
3372f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3373f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3374b032f27cSSam Leffler 	rfilt = ath_calcrxfilter(sc);
3375b032f27cSSam Leffler 	ath_hal_setrxfilter(sc->sc_ah, rfilt);
3376f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3377f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3378b032f27cSSam Leffler 
3379b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x\n", __func__, rfilt);
3380b032f27cSSam Leffler }
3381b032f27cSSam Leffler 
3382b032f27cSSam Leffler static void
3383b032f27cSSam Leffler ath_update_mcast(struct ifnet *ifp)
3384b032f27cSSam Leffler {
3385b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
3386b032f27cSSam Leffler 	u_int32_t mfilt[2];
3387b032f27cSSam Leffler 
3388b032f27cSSam Leffler 	/* calculate and install multicast filter */
3389b032f27cSSam Leffler 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
3390b032f27cSSam Leffler 		struct ifmultiaddr *ifma;
3391b032f27cSSam Leffler 		/*
3392b032f27cSSam Leffler 		 * Merge multicast addresses to form the hardware filter.
3393b032f27cSSam Leffler 		 */
3394b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = 0;
3395eb956cd0SRobert Watson 		if_maddr_rlock(ifp);	/* XXX need some fiddling to remove? */
3396b032f27cSSam Leffler 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3397b032f27cSSam Leffler 			caddr_t dl;
3398b032f27cSSam Leffler 			u_int32_t val;
3399b032f27cSSam Leffler 			u_int8_t pos;
3400b032f27cSSam Leffler 
3401b032f27cSSam Leffler 			/* calculate XOR of eight 6bit values */
3402b032f27cSSam Leffler 			dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
3403b032f27cSSam Leffler 			val = LE_READ_4(dl + 0);
3404b032f27cSSam Leffler 			pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
3405b032f27cSSam Leffler 			val = LE_READ_4(dl + 3);
3406b032f27cSSam Leffler 			pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
3407b032f27cSSam Leffler 			pos &= 0x3f;
3408b032f27cSSam Leffler 			mfilt[pos / 32] |= (1 << (pos % 32));
3409b032f27cSSam Leffler 		}
3410eb956cd0SRobert Watson 		if_maddr_runlock(ifp);
3411b032f27cSSam Leffler 	} else
3412b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = ~0;
3413f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3414f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3415b032f27cSSam Leffler 	ath_hal_setmcastfilter(sc->sc_ah, mfilt[0], mfilt[1]);
3416f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3417f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3418b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: MC filter %08x:%08x\n",
3419b032f27cSSam Leffler 		__func__, mfilt[0], mfilt[1]);
34204bc0e754SSam Leffler }
34214bc0e754SSam Leffler 
3422e60c4fc2SAdrian Chadd void
34235591b213SSam Leffler ath_mode_init(struct ath_softc *sc)
34245591b213SSam Leffler {
3425fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
3426b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3427b032f27cSSam Leffler 	u_int32_t rfilt;
34285591b213SSam Leffler 
34294bc0e754SSam Leffler 	/* configure rx filter */
343068e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
34314bc0e754SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
34324bc0e754SSam Leffler 
34335591b213SSam Leffler 	/* configure operational mode */
3434c42a7b7eSSam Leffler 	ath_hal_setopmode(ah);
3435c42a7b7eSSam Leffler 
34363d184db2SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_STATE | ATH_DEBUG_MODE,
34373d184db2SAdrian Chadd 	    "%s: ah=%p, ifp=%p, if_addr=%p\n",
34383d184db2SAdrian Chadd 	    __func__,
34393d184db2SAdrian Chadd 	    ah,
34403d184db2SAdrian Chadd 	    ifp,
34413d184db2SAdrian Chadd 	    (ifp == NULL) ? NULL : ifp->if_addr);
34423d184db2SAdrian Chadd 
344329aca940SSam Leffler 	/* handle any link-level address change */
344429aca940SSam Leffler 	ath_hal_setmac(ah, IF_LLADDR(ifp));
34455591b213SSam Leffler 
34465591b213SSam Leffler 	/* calculate and install multicast filter */
3447b032f27cSSam Leffler 	ath_update_mcast(ifp);
34485591b213SSam Leffler }
34495591b213SSam Leffler 
3450c42a7b7eSSam Leffler /*
3451c42a7b7eSSam Leffler  * Set the slot time based on the current setting.
3452c42a7b7eSSam Leffler  */
3453ba5c15d9SAdrian Chadd void
3454c42a7b7eSSam Leffler ath_setslottime(struct ath_softc *sc)
3455c42a7b7eSSam Leffler {
3456b032f27cSSam Leffler 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3457c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3458aaa70f2fSSam Leffler 	u_int usec;
3459c42a7b7eSSam Leffler 
3460aaa70f2fSSam Leffler 	if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan))
3461aaa70f2fSSam Leffler 		usec = 13;
3462aaa70f2fSSam Leffler 	else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan))
3463aaa70f2fSSam Leffler 		usec = 21;
3464724c193aSSam Leffler 	else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) {
3465724c193aSSam Leffler 		/* honor short/long slot time only in 11g */
3466724c193aSSam Leffler 		/* XXX shouldn't honor on pure g or turbo g channel */
3467724c193aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
3468aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_9;
3469aaa70f2fSSam Leffler 		else
3470aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_20;
3471724c193aSSam Leffler 	} else
3472724c193aSSam Leffler 		usec = HAL_SLOT_TIME_9;
3473aaa70f2fSSam Leffler 
3474aaa70f2fSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
3475aaa70f2fSSam Leffler 	    "%s: chan %u MHz flags 0x%x %s slot, %u usec\n",
3476aaa70f2fSSam Leffler 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
3477aaa70f2fSSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec);
3478aaa70f2fSSam Leffler 
3479f5c30c4eSAdrian Chadd 	/* Wake up the hardware first before updating the slot time */
3480f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3481f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3482aaa70f2fSSam Leffler 	ath_hal_setslottime(ah, usec);
3483f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3484c42a7b7eSSam Leffler 	sc->sc_updateslot = OK;
3485f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3486c42a7b7eSSam Leffler }
3487c42a7b7eSSam Leffler 
3488c42a7b7eSSam Leffler /*
3489c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update the
3490c42a7b7eSSam Leffler  * slot time based on the current setting.
3491c42a7b7eSSam Leffler  */
3492c42a7b7eSSam Leffler static void
3493c42a7b7eSSam Leffler ath_updateslot(struct ifnet *ifp)
3494c42a7b7eSSam Leffler {
3495c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
3496b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
3497c42a7b7eSSam Leffler 
3498c42a7b7eSSam Leffler 	/*
3499c42a7b7eSSam Leffler 	 * When not coordinating the BSS, change the hardware
3500c42a7b7eSSam Leffler 	 * immediately.  For other operation we defer the change
3501c42a7b7eSSam Leffler 	 * until beacon updates have propagated to the stations.
3502f5c30c4eSAdrian Chadd 	 *
3503f5c30c4eSAdrian Chadd 	 * XXX sc_updateslot isn't changed behind a lock?
3504c42a7b7eSSam Leffler 	 */
350559aa14a9SRui Paulo 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
350659aa14a9SRui Paulo 	    ic->ic_opmode == IEEE80211_M_MBSS)
3507c42a7b7eSSam Leffler 		sc->sc_updateslot = UPDATE;
3508c42a7b7eSSam Leffler 	else
3509c42a7b7eSSam Leffler 		ath_setslottime(sc);
3510c42a7b7eSSam Leffler }
3511c42a7b7eSSam Leffler 
3512c42a7b7eSSam Leffler /*
3513622b3fd2SSam Leffler  * Append the contents of src to dst; both queues
3514622b3fd2SSam Leffler  * are assumed to be locked.
3515622b3fd2SSam Leffler  */
3516ba5c15d9SAdrian Chadd void
3517622b3fd2SSam Leffler ath_txqmove(struct ath_txq *dst, struct ath_txq *src)
3518622b3fd2SSam Leffler {
3519e86fd7a7SAdrian Chadd 
3520b837332dSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(src);
3521b837332dSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(dst);
3522b837332dSAdrian Chadd 
35236b349e5aSAdrian Chadd 	TAILQ_CONCAT(&dst->axq_q, &src->axq_q, bf_list);
3524622b3fd2SSam Leffler 	dst->axq_link = src->axq_link;
3525622b3fd2SSam Leffler 	src->axq_link = NULL;
3526622b3fd2SSam Leffler 	dst->axq_depth += src->axq_depth;
35276edf1dc7SAdrian Chadd 	dst->axq_aggr_depth += src->axq_aggr_depth;
3528622b3fd2SSam Leffler 	src->axq_depth = 0;
35296edf1dc7SAdrian Chadd 	src->axq_aggr_depth = 0;
3530622b3fd2SSam Leffler }
3531622b3fd2SSam Leffler 
3532622b3fd2SSam Leffler /*
3533d52f7132SAdrian Chadd  * Reset the hardware, with no loss.
3534d52f7132SAdrian Chadd  *
3535d52f7132SAdrian Chadd  * This can't be used for a general case reset.
3536d52f7132SAdrian Chadd  */
3537d52f7132SAdrian Chadd static void
3538d52f7132SAdrian Chadd ath_reset_proc(void *arg, int pending)
3539d52f7132SAdrian Chadd {
3540d52f7132SAdrian Chadd 	struct ath_softc *sc = arg;
3541d52f7132SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
3542d52f7132SAdrian Chadd 
3543d52f7132SAdrian Chadd #if 0
3544d52f7132SAdrian Chadd 	if_printf(ifp, "%s: resetting\n", __func__);
3545d52f7132SAdrian Chadd #endif
3546d52f7132SAdrian Chadd 	ath_reset(ifp, ATH_RESET_NOLOSS);
3547d52f7132SAdrian Chadd }
3548d52f7132SAdrian Chadd 
3549d52f7132SAdrian Chadd /*
3550c42a7b7eSSam Leffler  * Reset the hardware after detecting beacons have stopped.
3551c42a7b7eSSam Leffler  */
3552c42a7b7eSSam Leffler static void
3553c42a7b7eSSam Leffler ath_bstuck_proc(void *arg, int pending)
3554c42a7b7eSSam Leffler {
3555c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
3556fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
355716d4de92SAdrian Chadd 	uint32_t hangs = 0;
355816d4de92SAdrian Chadd 
355916d4de92SAdrian Chadd 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0)
356016d4de92SAdrian Chadd 		if_printf(ifp, "bb hang detected (0x%x)\n", hangs);
3561c42a7b7eSSam Leffler 
3562370f81faSAdrian Chadd #ifdef	ATH_DEBUG_ALQ
3563370f81faSAdrian Chadd 	if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_STUCK_BEACON))
3564370f81faSAdrian Chadd 		if_ath_alq_post(&sc->sc_alq, ATH_ALQ_STUCK_BEACON, 0, NULL);
3565370f81faSAdrian Chadd #endif
3566370f81faSAdrian Chadd 
3567c42a7b7eSSam Leffler 	if_printf(ifp, "stuck beacon; resetting (bmiss count %u)\n",
3568c42a7b7eSSam Leffler 		sc->sc_bmisscount);
3569c2e34459SSam Leffler 	sc->sc_stats.ast_bstuck++;
357016d4de92SAdrian Chadd 	/*
357116d4de92SAdrian Chadd 	 * This assumes that there's no simultaneous channel mode change
357216d4de92SAdrian Chadd 	 * occuring.
357316d4de92SAdrian Chadd 	 */
3574517526efSAdrian Chadd 	ath_reset(ifp, ATH_RESET_NOLOSS);
3575c42a7b7eSSam Leffler }
3576c42a7b7eSSam Leffler 
35775591b213SSam Leffler static void
35785591b213SSam Leffler ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
35795591b213SSam Leffler {
35805591b213SSam Leffler 	bus_addr_t *paddr = (bus_addr_t*) arg;
3581d77367bfSSam Leffler 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
35825591b213SSam Leffler 	*paddr = segs->ds_addr;
35835591b213SSam Leffler }
35845591b213SSam Leffler 
3585c9f78537SAdrian Chadd /*
3586c9f78537SAdrian Chadd  * Allocate the descriptors and appropriate DMA tag/setup.
3587c9f78537SAdrian Chadd  *
3588c9f78537SAdrian Chadd  * For some situations (eg EDMA TX completion), there isn't a requirement
3589c9f78537SAdrian Chadd  * for the ath_buf entries to be allocated.
3590c9f78537SAdrian Chadd  */
35913d184db2SAdrian Chadd int
3592c9f78537SAdrian Chadd ath_descdma_alloc_desc(struct ath_softc *sc,
3593c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head,
3594b39722d6SAdrian Chadd 	const char *name, int ds_size, int ndesc)
3595c42a7b7eSSam Leffler {
3596c42a7b7eSSam Leffler #define	DS2PHYS(_dd, _ds) \
3597c42a7b7eSSam Leffler 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
359845abcd6cSAdrian Chadd #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
359945abcd6cSAdrian Chadd 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
3600fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
3601c9f78537SAdrian Chadd 	int error;
360245abcd6cSAdrian Chadd 
36031006fc0cSAdrian Chadd 	dd->dd_descsize = ds_size;
3604c42a7b7eSSam Leffler 
36053d9b1596SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
3606b39722d6SAdrian Chadd 	    "%s: %s DMA: %u desc, %d bytes per descriptor\n",
3607b39722d6SAdrian Chadd 	    __func__, name, ndesc, dd->dd_descsize);
3608c42a7b7eSSam Leffler 
3609c42a7b7eSSam Leffler 	dd->dd_name = name;
3610b39722d6SAdrian Chadd 	dd->dd_desc_len = dd->dd_descsize * ndesc;
361145abcd6cSAdrian Chadd 
361245abcd6cSAdrian Chadd 	/*
361345abcd6cSAdrian Chadd 	 * Merlin work-around:
361445abcd6cSAdrian Chadd 	 * Descriptors that cross the 4KB boundary can't be used.
361545abcd6cSAdrian Chadd 	 * Assume one skipped descriptor per 4KB page.
361645abcd6cSAdrian Chadd 	 */
361745abcd6cSAdrian Chadd 	if (! ath_hal_split4ktrans(sc->sc_ah)) {
3618b39722d6SAdrian Chadd 		int numpages = dd->dd_desc_len / 4096;
3619b39722d6SAdrian Chadd 		dd->dd_desc_len += ds_size * numpages;
362045abcd6cSAdrian Chadd 	}
3621c42a7b7eSSam Leffler 
3622c42a7b7eSSam Leffler 	/*
3623c42a7b7eSSam Leffler 	 * Setup DMA descriptor area.
3624a91ab3c0SAdrian Chadd 	 *
3625a91ab3c0SAdrian Chadd 	 * BUS_DMA_ALLOCNOW is not used; we never use bounce
3626a91ab3c0SAdrian Chadd 	 * buffers for the descriptors themselves.
3627c42a7b7eSSam Leffler 	 */
3628c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev),	/* parent */
3629c42a7b7eSSam Leffler 		       PAGE_SIZE, 0,		/* alignment, bounds */
3630c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
3631c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR,	/* highaddr */
3632c42a7b7eSSam Leffler 		       NULL, NULL,		/* filter, filterarg */
3633c42a7b7eSSam Leffler 		       dd->dd_desc_len,		/* maxsize */
3634c42a7b7eSSam Leffler 		       1,			/* nsegments */
36356ccb8ea7SSam Leffler 		       dd->dd_desc_len,		/* maxsegsize */
3636a91ab3c0SAdrian Chadd 		       0,			/* flags */
3637c42a7b7eSSam Leffler 		       NULL,			/* lockfunc */
3638c42a7b7eSSam Leffler 		       NULL,			/* lockarg */
3639c42a7b7eSSam Leffler 		       &dd->dd_dmat);
3640c42a7b7eSSam Leffler 	if (error != 0) {
3641c42a7b7eSSam Leffler 		if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name);
3642c42a7b7eSSam Leffler 		return error;
3643c42a7b7eSSam Leffler 	}
3644c42a7b7eSSam Leffler 
3645c42a7b7eSSam Leffler 	/* allocate descriptors */
3646c42a7b7eSSam Leffler 	error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
36470553a01fSSam Leffler 				 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
36480553a01fSSam Leffler 				 &dd->dd_dmamap);
3649c42a7b7eSSam Leffler 	if (error != 0) {
3650c42a7b7eSSam Leffler 		if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
3651b39722d6SAdrian Chadd 			"error %u\n", ndesc, dd->dd_name, error);
3652c42a7b7eSSam Leffler 		goto fail1;
3653c42a7b7eSSam Leffler 	}
3654c42a7b7eSSam Leffler 
3655c42a7b7eSSam Leffler 	error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
3656c42a7b7eSSam Leffler 				dd->dd_desc, dd->dd_desc_len,
3657c42a7b7eSSam Leffler 				ath_load_cb, &dd->dd_desc_paddr,
3658c42a7b7eSSam Leffler 				BUS_DMA_NOWAIT);
3659c42a7b7eSSam Leffler 	if (error != 0) {
3660c42a7b7eSSam Leffler 		if_printf(ifp, "unable to map %s descriptors, error %u\n",
3661c42a7b7eSSam Leffler 			dd->dd_name, error);
3662c42a7b7eSSam Leffler 		goto fail2;
3663c42a7b7eSSam Leffler 	}
3664c42a7b7eSSam Leffler 
3665c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
3666c9f78537SAdrian Chadd 	    __func__, dd->dd_name, (uint8_t *) dd->dd_desc,
3667c9f78537SAdrian Chadd 	    (u_long) dd->dd_desc_len, (caddr_t) dd->dd_desc_paddr,
3668c9f78537SAdrian Chadd 	    /*XXX*/ (u_long) dd->dd_desc_len);
3669c9f78537SAdrian Chadd 
3670c9f78537SAdrian Chadd 	return (0);
3671c9f78537SAdrian Chadd 
3672c9f78537SAdrian Chadd fail2:
3673c9f78537SAdrian Chadd 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3674c9f78537SAdrian Chadd fail1:
3675c9f78537SAdrian Chadd 	bus_dma_tag_destroy(dd->dd_dmat);
3676c9f78537SAdrian Chadd 	memset(dd, 0, sizeof(*dd));
3677c9f78537SAdrian Chadd 	return error;
3678c9f78537SAdrian Chadd #undef DS2PHYS
3679c9f78537SAdrian Chadd #undef ATH_DESC_4KB_BOUND_CHECK
3680c9f78537SAdrian Chadd }
3681c9f78537SAdrian Chadd 
3682c9f78537SAdrian Chadd int
3683c9f78537SAdrian Chadd ath_descdma_setup(struct ath_softc *sc,
3684c9f78537SAdrian Chadd 	struct ath_descdma *dd, ath_bufhead *head,
3685c9f78537SAdrian Chadd 	const char *name, int ds_size, int nbuf, int ndesc)
3686c9f78537SAdrian Chadd {
3687c9f78537SAdrian Chadd #define	DS2PHYS(_dd, _ds) \
3688c9f78537SAdrian Chadd 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
3689c9f78537SAdrian Chadd #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
3690c9f78537SAdrian Chadd 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
3691c9f78537SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
3692c9f78537SAdrian Chadd 	uint8_t *ds;
3693c9f78537SAdrian Chadd 	struct ath_buf *bf;
3694c9f78537SAdrian Chadd 	int i, bsize, error;
3695c9f78537SAdrian Chadd 
3696c9f78537SAdrian Chadd 	/* Allocate descriptors */
3697c9f78537SAdrian Chadd 	error = ath_descdma_alloc_desc(sc, dd, head, name, ds_size,
3698b39722d6SAdrian Chadd 	    nbuf * ndesc);
3699c9f78537SAdrian Chadd 
3700c9f78537SAdrian Chadd 	/* Assume any errors during allocation were dealt with */
3701c9f78537SAdrian Chadd 	if (error != 0) {
3702c9f78537SAdrian Chadd 		return (error);
3703c9f78537SAdrian Chadd 	}
3704c9f78537SAdrian Chadd 
3705c9f78537SAdrian Chadd 	ds = (uint8_t *) dd->dd_desc;
3706c42a7b7eSSam Leffler 
3707ebecf802SSam Leffler 	/* allocate rx buffers */
3708c42a7b7eSSam Leffler 	bsize = sizeof(struct ath_buf) * nbuf;
3709c42a7b7eSSam Leffler 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
3710c42a7b7eSSam Leffler 	if (bf == NULL) {
3711c42a7b7eSSam Leffler 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
3712c42a7b7eSSam Leffler 			dd->dd_name, bsize);
3713c42a7b7eSSam Leffler 		goto fail3;
3714c42a7b7eSSam Leffler 	}
3715c42a7b7eSSam Leffler 	dd->dd_bufptr = bf;
3716c42a7b7eSSam Leffler 
37176b349e5aSAdrian Chadd 	TAILQ_INIT(head);
37183d9b1596SAdrian Chadd 	for (i = 0; i < nbuf; i++, bf++, ds += (ndesc * dd->dd_descsize)) {
371945abcd6cSAdrian Chadd 		bf->bf_desc = (struct ath_desc *) ds;
3720c42a7b7eSSam Leffler 		bf->bf_daddr = DS2PHYS(dd, ds);
372145abcd6cSAdrian Chadd 		if (! ath_hal_split4ktrans(sc->sc_ah)) {
372245abcd6cSAdrian Chadd 			/*
372345abcd6cSAdrian Chadd 			 * Merlin WAR: Skip descriptor addresses which
372445abcd6cSAdrian Chadd 			 * cause 4KB boundary crossing along any point
372545abcd6cSAdrian Chadd 			 * in the descriptor.
372645abcd6cSAdrian Chadd 			 */
372745abcd6cSAdrian Chadd 			 if (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr,
37287ef7f613SAdrian Chadd 			     dd->dd_descsize)) {
372945abcd6cSAdrian Chadd 				/* Start at the next page */
373045abcd6cSAdrian Chadd 				ds += 0x1000 - (bf->bf_daddr & 0xFFF);
373145abcd6cSAdrian Chadd 				bf->bf_desc = (struct ath_desc *) ds;
373245abcd6cSAdrian Chadd 				bf->bf_daddr = DS2PHYS(dd, ds);
373345abcd6cSAdrian Chadd 			}
373445abcd6cSAdrian Chadd 		}
3735c42a7b7eSSam Leffler 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
3736c42a7b7eSSam Leffler 				&bf->bf_dmamap);
3737c42a7b7eSSam Leffler 		if (error != 0) {
3738c42a7b7eSSam Leffler 			if_printf(ifp, "unable to create dmamap for %s "
3739c42a7b7eSSam Leffler 				"buffer %u, error %u\n", dd->dd_name, i, error);
3740c42a7b7eSSam Leffler 			ath_descdma_cleanup(sc, dd, head);
3741c42a7b7eSSam Leffler 			return error;
3742c42a7b7eSSam Leffler 		}
37436edf1dc7SAdrian Chadd 		bf->bf_lastds = bf->bf_desc;	/* Just an initial value */
37446b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(head, bf, bf_list);
3745c42a7b7eSSam Leffler 	}
37467ef7f613SAdrian Chadd 
37477ef7f613SAdrian Chadd 	/*
37487ef7f613SAdrian Chadd 	 * XXX TODO: ensure that ds doesn't overflow the descriptor
37497ef7f613SAdrian Chadd 	 * allocation otherwise weird stuff will occur and crash your
37507ef7f613SAdrian Chadd 	 * machine.
37517ef7f613SAdrian Chadd 	 */
3752c42a7b7eSSam Leffler 	return 0;
3753c9f78537SAdrian Chadd 	/* XXX this should likely just call ath_descdma_cleanup() */
3754c42a7b7eSSam Leffler fail3:
3755c42a7b7eSSam Leffler 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3756c42a7b7eSSam Leffler 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3757c42a7b7eSSam Leffler 	bus_dma_tag_destroy(dd->dd_dmat);
3758c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3759c42a7b7eSSam Leffler 	return error;
3760c42a7b7eSSam Leffler #undef DS2PHYS
376145abcd6cSAdrian Chadd #undef ATH_DESC_4KB_BOUND_CHECK
3762c42a7b7eSSam Leffler }
3763c42a7b7eSSam Leffler 
376439abbd9bSAdrian Chadd /*
376539abbd9bSAdrian Chadd  * Allocate ath_buf entries but no descriptor contents.
376639abbd9bSAdrian Chadd  *
376739abbd9bSAdrian Chadd  * This is for RX EDMA where the descriptors are the header part of
376839abbd9bSAdrian Chadd  * the RX buffer.
376939abbd9bSAdrian Chadd  */
377039abbd9bSAdrian Chadd int
377139abbd9bSAdrian Chadd ath_descdma_setup_rx_edma(struct ath_softc *sc,
377239abbd9bSAdrian Chadd 	struct ath_descdma *dd, ath_bufhead *head,
377339abbd9bSAdrian Chadd 	const char *name, int nbuf, int rx_status_len)
377439abbd9bSAdrian Chadd {
377539abbd9bSAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
377639abbd9bSAdrian Chadd 	struct ath_buf *bf;
377739abbd9bSAdrian Chadd 	int i, bsize, error;
377839abbd9bSAdrian Chadd 
377939abbd9bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers\n",
378039abbd9bSAdrian Chadd 	    __func__, name, nbuf);
378139abbd9bSAdrian Chadd 
378239abbd9bSAdrian Chadd 	dd->dd_name = name;
378339abbd9bSAdrian Chadd 	/*
378439abbd9bSAdrian Chadd 	 * This is (mostly) purely for show.  We're not allocating any actual
378539abbd9bSAdrian Chadd 	 * descriptors here as EDMA RX has the descriptor be part
378639abbd9bSAdrian Chadd 	 * of the RX buffer.
378739abbd9bSAdrian Chadd 	 *
378839abbd9bSAdrian Chadd 	 * However, dd_desc_len is used by ath_descdma_free() to determine
378939abbd9bSAdrian Chadd 	 * whether we have already freed this DMA mapping.
379039abbd9bSAdrian Chadd 	 */
37913d9b1596SAdrian Chadd 	dd->dd_desc_len = rx_status_len * nbuf;
37923d9b1596SAdrian Chadd 	dd->dd_descsize = rx_status_len;
379339abbd9bSAdrian Chadd 
379439abbd9bSAdrian Chadd 	/* allocate rx buffers */
379539abbd9bSAdrian Chadd 	bsize = sizeof(struct ath_buf) * nbuf;
379639abbd9bSAdrian Chadd 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
379739abbd9bSAdrian Chadd 	if (bf == NULL) {
379839abbd9bSAdrian Chadd 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
379939abbd9bSAdrian Chadd 			dd->dd_name, bsize);
3800b5b60f35SAdrian Chadd 		error = ENOMEM;
380139abbd9bSAdrian Chadd 		goto fail3;
380239abbd9bSAdrian Chadd 	}
380339abbd9bSAdrian Chadd 	dd->dd_bufptr = bf;
380439abbd9bSAdrian Chadd 
380539abbd9bSAdrian Chadd 	TAILQ_INIT(head);
380639abbd9bSAdrian Chadd 	for (i = 0; i < nbuf; i++, bf++) {
380739abbd9bSAdrian Chadd 		bf->bf_desc = NULL;
380839abbd9bSAdrian Chadd 		bf->bf_daddr = 0;
380939abbd9bSAdrian Chadd 		bf->bf_lastds = NULL;	/* Just an initial value */
381039abbd9bSAdrian Chadd 
381139abbd9bSAdrian Chadd 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
381239abbd9bSAdrian Chadd 				&bf->bf_dmamap);
381339abbd9bSAdrian Chadd 		if (error != 0) {
381439abbd9bSAdrian Chadd 			if_printf(ifp, "unable to create dmamap for %s "
381539abbd9bSAdrian Chadd 				"buffer %u, error %u\n", dd->dd_name, i, error);
381639abbd9bSAdrian Chadd 			ath_descdma_cleanup(sc, dd, head);
381739abbd9bSAdrian Chadd 			return error;
381839abbd9bSAdrian Chadd 		}
381939abbd9bSAdrian Chadd 		TAILQ_INSERT_TAIL(head, bf, bf_list);
382039abbd9bSAdrian Chadd 	}
382139abbd9bSAdrian Chadd 	return 0;
382239abbd9bSAdrian Chadd fail3:
382339abbd9bSAdrian Chadd 	memset(dd, 0, sizeof(*dd));
382439abbd9bSAdrian Chadd 	return error;
382539abbd9bSAdrian Chadd }
382639abbd9bSAdrian Chadd 
38273d184db2SAdrian Chadd void
3828c42a7b7eSSam Leffler ath_descdma_cleanup(struct ath_softc *sc,
3829c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head)
3830c42a7b7eSSam Leffler {
3831c42a7b7eSSam Leffler 	struct ath_buf *bf;
3832c42a7b7eSSam Leffler 	struct ieee80211_node *ni;
3833a91ab3c0SAdrian Chadd 	int do_warning = 0;
3834c42a7b7eSSam Leffler 
38358d467c41SAdrian Chadd 	if (dd->dd_dmamap != 0) {
3836c42a7b7eSSam Leffler 		bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3837c42a7b7eSSam Leffler 		bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3838c42a7b7eSSam Leffler 		bus_dma_tag_destroy(dd->dd_dmat);
38398d467c41SAdrian Chadd 	}
3840c42a7b7eSSam Leffler 
38419ed9f02bSAdrian Chadd 	if (head != NULL) {
38426b349e5aSAdrian Chadd 		TAILQ_FOREACH(bf, head, bf_list) {
3843c42a7b7eSSam Leffler 			if (bf->bf_m) {
3844a91ab3c0SAdrian Chadd 				/*
3845a91ab3c0SAdrian Chadd 				 * XXX warn if there's buffers here.
3846a91ab3c0SAdrian Chadd 				 * XXX it should have been freed by the
3847a91ab3c0SAdrian Chadd 				 * owner!
3848a91ab3c0SAdrian Chadd 				 */
3849a91ab3c0SAdrian Chadd 
3850a91ab3c0SAdrian Chadd 				if (do_warning == 0) {
3851a91ab3c0SAdrian Chadd 					do_warning = 1;
3852a91ab3c0SAdrian Chadd 					device_printf(sc->sc_dev,
3853a91ab3c0SAdrian Chadd 					    "%s: %s: mbuf should've been"
3854a91ab3c0SAdrian Chadd 					    " unmapped/freed!\n",
3855a91ab3c0SAdrian Chadd 					    __func__,
3856a91ab3c0SAdrian Chadd 					    dd->dd_name);
3857a91ab3c0SAdrian Chadd 				}
3858a91ab3c0SAdrian Chadd 				bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
3859a91ab3c0SAdrian Chadd 				    BUS_DMASYNC_POSTREAD);
3860a91ab3c0SAdrian Chadd 				bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3861c42a7b7eSSam Leffler 				m_freem(bf->bf_m);
3862c42a7b7eSSam Leffler 				bf->bf_m = NULL;
3863c42a7b7eSSam Leffler 			}
3864c42a7b7eSSam Leffler 			if (bf->bf_dmamap != NULL) {
3865c42a7b7eSSam Leffler 				bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
3866c42a7b7eSSam Leffler 				bf->bf_dmamap = NULL;
3867c42a7b7eSSam Leffler 			}
3868c42a7b7eSSam Leffler 			ni = bf->bf_node;
3869c42a7b7eSSam Leffler 			bf->bf_node = NULL;
3870c42a7b7eSSam Leffler 			if (ni != NULL) {
3871c42a7b7eSSam Leffler 				/*
3872c42a7b7eSSam Leffler 				 * Reclaim node reference.
3873c42a7b7eSSam Leffler 				 */
3874c42a7b7eSSam Leffler 				ieee80211_free_node(ni);
3875c42a7b7eSSam Leffler 			}
3876c42a7b7eSSam Leffler 		}
38779ed9f02bSAdrian Chadd 	}
3878c42a7b7eSSam Leffler 
38799ed9f02bSAdrian Chadd 	if (head != NULL)
38806b349e5aSAdrian Chadd 		TAILQ_INIT(head);
38819ed9f02bSAdrian Chadd 
38829ed9f02bSAdrian Chadd 	if (dd->dd_bufptr != NULL)
3883c42a7b7eSSam Leffler 		free(dd->dd_bufptr, M_ATHDEV);
3884c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3885c42a7b7eSSam Leffler }
3886c42a7b7eSSam Leffler 
3887c42a7b7eSSam Leffler static int
38885591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc)
38895591b213SSam Leffler {
3890c42a7b7eSSam Leffler 	int error;
38915591b213SSam Leffler 
3892c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
389309067b6eSAdrian Chadd 		    "tx", sc->sc_tx_desclen, ath_txbuf, ATH_MAX_SCATTER);
3894c42a7b7eSSam Leffler 	if (error != 0) {
38955591b213SSam Leffler 		return error;
3896c42a7b7eSSam Leffler 	}
389723ced6c1SAdrian Chadd 	sc->sc_txbuf_cnt = ath_txbuf;
3898c42a7b7eSSam Leffler 
3899af33d486SAdrian Chadd 	error = ath_descdma_setup(sc, &sc->sc_txdma_mgmt, &sc->sc_txbuf_mgmt,
39001006fc0cSAdrian Chadd 		    "tx_mgmt", sc->sc_tx_desclen, ath_txbuf_mgmt,
39011006fc0cSAdrian Chadd 		    ATH_TXDESC);
3902af33d486SAdrian Chadd 	if (error != 0) {
3903af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3904af33d486SAdrian Chadd 		return error;
3905af33d486SAdrian Chadd 	}
3906af33d486SAdrian Chadd 
3907af33d486SAdrian Chadd 	/*
3908af33d486SAdrian Chadd 	 * XXX mark txbuf_mgmt frames with ATH_BUF_MGMT, so the
3909af33d486SAdrian Chadd 	 * flag doesn't have to be set in ath_getbuf_locked().
3910af33d486SAdrian Chadd 	 */
3911af33d486SAdrian Chadd 
3912c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
39131006fc0cSAdrian Chadd 			"beacon", sc->sc_tx_desclen, ATH_BCBUF, 1);
3914c42a7b7eSSam Leffler 	if (error != 0) {
3915af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3916af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
3917af33d486SAdrian Chadd 		    &sc->sc_txbuf_mgmt);
3918c42a7b7eSSam Leffler 		return error;
3919c42a7b7eSSam Leffler 	}
39205591b213SSam Leffler 	return 0;
39215591b213SSam Leffler }
39225591b213SSam Leffler 
39235591b213SSam Leffler static void
39245591b213SSam Leffler ath_desc_free(struct ath_softc *sc)
39255591b213SSam Leffler {
39265591b213SSam Leffler 
3927c42a7b7eSSam Leffler 	if (sc->sc_bdma.dd_desc_len != 0)
3928c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
3929c42a7b7eSSam Leffler 	if (sc->sc_txdma.dd_desc_len != 0)
3930c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3931af33d486SAdrian Chadd 	if (sc->sc_txdma_mgmt.dd_desc_len != 0)
3932af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
3933af33d486SAdrian Chadd 		    &sc->sc_txbuf_mgmt);
39345591b213SSam Leffler }
39355591b213SSam Leffler 
39365591b213SSam Leffler static struct ieee80211_node *
393738c208f8SSam Leffler ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
39385591b213SSam Leffler {
393938c208f8SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3940c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3941c42a7b7eSSam Leffler 	const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
3942c42a7b7eSSam Leffler 	struct ath_node *an;
3943c42a7b7eSSam Leffler 
3944c42a7b7eSSam Leffler 	an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
3945c42a7b7eSSam Leffler 	if (an == NULL) {
3946c42a7b7eSSam Leffler 		/* XXX stat+msg */
3947de5af704SSam Leffler 		return NULL;
39485591b213SSam Leffler 	}
3949c42a7b7eSSam Leffler 	ath_rate_node_init(sc, an);
39505591b213SSam Leffler 
39513dd85b26SAdrian Chadd 	/* Setup the mutex - there's no associd yet so set the name to NULL */
39523dd85b26SAdrian Chadd 	snprintf(an->an_name, sizeof(an->an_name), "%s: node %p",
39533dd85b26SAdrian Chadd 	    device_get_nameunit(sc->sc_dev), an);
39543dd85b26SAdrian Chadd 	mtx_init(&an->an_mtx, an->an_name, NULL, MTX_DEF);
39553dd85b26SAdrian Chadd 
3956eb6f0de0SAdrian Chadd 	/* XXX setup ath_tid */
3957eb6f0de0SAdrian Chadd 	ath_tx_tid_init(sc, an);
3958eb6f0de0SAdrian Chadd 
39599b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__, mac, ":", an);
3960c42a7b7eSSam Leffler 	return &an->an_node;
3961c42a7b7eSSam Leffler }
3962c42a7b7eSSam Leffler 
39635591b213SSam Leffler static void
39644afa805eSAdrian Chadd ath_node_cleanup(struct ieee80211_node *ni)
39654afa805eSAdrian Chadd {
39664afa805eSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
39674afa805eSAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
39684afa805eSAdrian Chadd 
39699b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__,
39709b48fb4bSAdrian Chadd 	    ni->ni_macaddr, ":", ATH_NODE(ni));
39719b48fb4bSAdrian Chadd 
39724afa805eSAdrian Chadd 	/* Cleanup ath_tid, free unused bufs, unlink bufs in TXQ */
3973eb6f0de0SAdrian Chadd 	ath_tx_node_flush(sc, ATH_NODE(ni));
39744afa805eSAdrian Chadd 	ath_rate_node_cleanup(sc, ATH_NODE(ni));
39754afa805eSAdrian Chadd 	sc->sc_node_cleanup(ni);
39764afa805eSAdrian Chadd }
39774afa805eSAdrian Chadd 
39784afa805eSAdrian Chadd static void
3979c42a7b7eSSam Leffler ath_node_free(struct ieee80211_node *ni)
39805591b213SSam Leffler {
3981c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
3982c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
39831e774079SSam Leffler 
39849b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__,
39859b48fb4bSAdrian Chadd 	    ni->ni_macaddr, ":", ATH_NODE(ni));
39863dd85b26SAdrian Chadd 	mtx_destroy(&ATH_NODE(ni)->an_mtx);
3987c42a7b7eSSam Leffler 	sc->sc_node_free(ni);
39885591b213SSam Leffler }
39895591b213SSam Leffler 
399068e8e04eSSam Leffler static void
399168e8e04eSSam Leffler ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
399268e8e04eSSam Leffler {
399368e8e04eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
399468e8e04eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
399568e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
399668e8e04eSSam Leffler 
3997b032f27cSSam Leffler 	*rssi = ic->ic_node_getrssi(ni);
399859efa8b5SSam Leffler 	if (ni->ni_chan != IEEE80211_CHAN_ANYC)
399959efa8b5SSam Leffler 		*noise = ath_hal_getchannoise(ah, ni->ni_chan);
400059efa8b5SSam Leffler 	else
400168e8e04eSSam Leffler 		*noise = -95;		/* nominally correct */
400268e8e04eSSam Leffler }
400368e8e04eSSam Leffler 
4004c42a7b7eSSam Leffler /*
4005c42a7b7eSSam Leffler  * Set the default antenna.
4006c42a7b7eSSam Leffler  */
4007e60c4fc2SAdrian Chadd void
4008c42a7b7eSSam Leffler ath_setdefantenna(struct ath_softc *sc, u_int antenna)
4009c42a7b7eSSam Leffler {
4010c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4011c42a7b7eSSam Leffler 
4012c42a7b7eSSam Leffler 	/* XXX block beacon interrupts */
4013c42a7b7eSSam Leffler 	ath_hal_setdefantenna(ah, antenna);
4014c42a7b7eSSam Leffler 	if (sc->sc_defant != antenna)
4015c42a7b7eSSam Leffler 		sc->sc_stats.ast_ant_defswitch++;
4016c42a7b7eSSam Leffler 	sc->sc_defant = antenna;
4017c42a7b7eSSam Leffler 	sc->sc_rxotherant = 0;
4018c42a7b7eSSam Leffler }
4019c42a7b7eSSam Leffler 
40205463c4a4SSam Leffler static void
4021622b3fd2SSam Leffler ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum)
4022622b3fd2SSam Leffler {
4023622b3fd2SSam Leffler 	txq->axq_qnum = qnum;
4024339ccfb3SSam Leffler 	txq->axq_ac = 0;
4025622b3fd2SSam Leffler 	txq->axq_depth = 0;
402616d4de92SAdrian Chadd 	txq->axq_aggr_depth = 0;
4027622b3fd2SSam Leffler 	txq->axq_intrcnt = 0;
4028622b3fd2SSam Leffler 	txq->axq_link = NULL;
40296b349e5aSAdrian Chadd 	txq->axq_softc = sc;
40306b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_q);
40316b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_tidq);
40323feffbd7SAdrian Chadd 	TAILQ_INIT(&txq->fifo.axq_q);
4033b837332dSAdrian Chadd 	ATH_TXQ_LOCK_INIT(sc, txq);
4034622b3fd2SSam Leffler }
4035622b3fd2SSam Leffler 
40365591b213SSam Leffler /*
4037c42a7b7eSSam Leffler  * Setup a h/w transmit queue.
40385591b213SSam Leffler  */
4039c42a7b7eSSam Leffler static struct ath_txq *
4040c42a7b7eSSam Leffler ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
4041c42a7b7eSSam Leffler {
4042c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
4043c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4044c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
4045c42a7b7eSSam Leffler 	int qnum;
4046c42a7b7eSSam Leffler 
4047c42a7b7eSSam Leffler 	memset(&qi, 0, sizeof(qi));
4048c42a7b7eSSam Leffler 	qi.tqi_subtype = subtype;
4049c42a7b7eSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
4050c42a7b7eSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
4051c42a7b7eSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
4052c42a7b7eSSam Leffler 	/*
4053c42a7b7eSSam Leffler 	 * Enable interrupts only for EOL and DESC conditions.
4054c42a7b7eSSam Leffler 	 * We mark tx descriptors to receive a DESC interrupt
4055c42a7b7eSSam Leffler 	 * when a tx queue gets deep; otherwise waiting for the
4056c42a7b7eSSam Leffler 	 * EOL to reap descriptors.  Note that this is done to
4057c42a7b7eSSam Leffler 	 * reduce interrupt load and this only defers reaping
4058c42a7b7eSSam Leffler 	 * descriptors, never transmitting frames.  Aside from
4059c42a7b7eSSam Leffler 	 * reducing interrupts this also permits more concurrency.
4060c42a7b7eSSam Leffler 	 * The only potential downside is if the tx queue backs
4061c42a7b7eSSam Leffler 	 * up in which case the top half of the kernel may backup
4062c42a7b7eSSam Leffler 	 * due to a lack of tx descriptors.
4063c42a7b7eSSam Leffler 	 */
40646961e9edSAdrian Chadd 	if (sc->sc_isedma)
40656961e9edSAdrian Chadd 		qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE |
40666961e9edSAdrian Chadd 		    HAL_TXQ_TXOKINT_ENABLE;
40676961e9edSAdrian Chadd 	else
40686961e9edSAdrian Chadd 		qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE |
40696961e9edSAdrian Chadd 		    HAL_TXQ_TXDESCINT_ENABLE;
40706961e9edSAdrian Chadd 
4071c42a7b7eSSam Leffler 	qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
4072c42a7b7eSSam Leffler 	if (qnum == -1) {
4073c42a7b7eSSam Leffler 		/*
4074c42a7b7eSSam Leffler 		 * NB: don't print a message, this happens
4075a614e076SSam Leffler 		 * normally on parts with too few tx queues
4076c42a7b7eSSam Leffler 		 */
4077c42a7b7eSSam Leffler 		return NULL;
4078c42a7b7eSSam Leffler 	}
4079c42a7b7eSSam Leffler 	if (qnum >= N(sc->sc_txq)) {
40806891c875SPeter Wemm 		device_printf(sc->sc_dev,
40816891c875SPeter Wemm 			"hal qnum %u out of range, max %zu!\n",
4082c42a7b7eSSam Leffler 			qnum, N(sc->sc_txq));
4083c42a7b7eSSam Leffler 		ath_hal_releasetxqueue(ah, qnum);
4084c42a7b7eSSam Leffler 		return NULL;
4085c42a7b7eSSam Leffler 	}
4086c42a7b7eSSam Leffler 	if (!ATH_TXQ_SETUP(sc, qnum)) {
4087622b3fd2SSam Leffler 		ath_txq_init(sc, &sc->sc_txq[qnum], qnum);
4088c42a7b7eSSam Leffler 		sc->sc_txqsetup |= 1<<qnum;
4089c42a7b7eSSam Leffler 	}
4090c42a7b7eSSam Leffler 	return &sc->sc_txq[qnum];
4091c42a7b7eSSam Leffler #undef N
4092c42a7b7eSSam Leffler }
4093c42a7b7eSSam Leffler 
4094c42a7b7eSSam Leffler /*
4095c42a7b7eSSam Leffler  * Setup a hardware data transmit queue for the specified
4096c42a7b7eSSam Leffler  * access control.  The hal may not support all requested
4097c42a7b7eSSam Leffler  * queues in which case it will return a reference to a
4098c42a7b7eSSam Leffler  * previously setup queue.  We record the mapping from ac's
4099c42a7b7eSSam Leffler  * to h/w queues for use by ath_tx_start and also track
4100c42a7b7eSSam Leffler  * the set of h/w queues being used to optimize work in the
4101c42a7b7eSSam Leffler  * transmit interrupt handler and related routines.
4102c42a7b7eSSam Leffler  */
4103c42a7b7eSSam Leffler static int
4104c42a7b7eSSam Leffler ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
4105c42a7b7eSSam Leffler {
4106c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
4107c42a7b7eSSam Leffler 	struct ath_txq *txq;
4108c42a7b7eSSam Leffler 
4109c42a7b7eSSam Leffler 	if (ac >= N(sc->sc_ac2q)) {
41106891c875SPeter Wemm 		device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
4111c42a7b7eSSam Leffler 			ac, N(sc->sc_ac2q));
4112c42a7b7eSSam Leffler 		return 0;
4113c42a7b7eSSam Leffler 	}
4114c42a7b7eSSam Leffler 	txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
4115c42a7b7eSSam Leffler 	if (txq != NULL) {
4116339ccfb3SSam Leffler 		txq->axq_ac = ac;
4117c42a7b7eSSam Leffler 		sc->sc_ac2q[ac] = txq;
4118c42a7b7eSSam Leffler 		return 1;
4119c42a7b7eSSam Leffler 	} else
4120c42a7b7eSSam Leffler 		return 0;
4121c42a7b7eSSam Leffler #undef N
4122c42a7b7eSSam Leffler }
4123c42a7b7eSSam Leffler 
4124c42a7b7eSSam Leffler /*
4125c42a7b7eSSam Leffler  * Update WME parameters for a transmit queue.
4126c42a7b7eSSam Leffler  */
4127c42a7b7eSSam Leffler static int
4128c42a7b7eSSam Leffler ath_txq_update(struct ath_softc *sc, int ac)
4129c42a7b7eSSam Leffler {
4130c42a7b7eSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<v)-1)
4131c42a7b7eSSam Leffler #define	ATH_TXOP_TO_US(v)		(v<<5)
4132b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
4133b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
4134c42a7b7eSSam Leffler 	struct ath_txq *txq = sc->sc_ac2q[ac];
4135c42a7b7eSSam Leffler 	struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
4136c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4137c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
4138c42a7b7eSSam Leffler 
4139c42a7b7eSSam Leffler 	ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
4140584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
414110ad9a77SSam Leffler 	if (sc->sc_tdma) {
414210ad9a77SSam Leffler 		/*
414310ad9a77SSam Leffler 		 * AIFS is zero so there's no pre-transmit wait.  The
414410ad9a77SSam Leffler 		 * burst time defines the slot duration and is configured
414509be6601SSam Leffler 		 * through net80211.  The QCU is setup to not do post-xmit
414610ad9a77SSam Leffler 		 * back off, lockout all lower-priority QCU's, and fire
414710ad9a77SSam Leffler 		 * off the DMA beacon alert timer which is setup based
414810ad9a77SSam Leffler 		 * on the slot configuration.
414910ad9a77SSam Leffler 		 */
415010ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
415110ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
415210ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
415310ad9a77SSam Leffler 			      | HAL_TXQ_TXEOLINT_ENABLE
415410ad9a77SSam Leffler 			      | HAL_TXQ_DBA_GATED
415510ad9a77SSam Leffler 			      | HAL_TXQ_BACKOFF_DISABLE
415610ad9a77SSam Leffler 			      | HAL_TXQ_ARB_LOCKOUT_GLOBAL
415710ad9a77SSam Leffler 			      ;
415810ad9a77SSam Leffler 		qi.tqi_aifs = 0;
415910ad9a77SSam Leffler 		/* XXX +dbaprep? */
416010ad9a77SSam Leffler 		qi.tqi_readyTime = sc->sc_tdmaslotlen;
416110ad9a77SSam Leffler 		qi.tqi_burstTime = qi.tqi_readyTime;
416210ad9a77SSam Leffler 	} else {
416310ad9a77SSam Leffler #endif
416416d4de92SAdrian Chadd 		/*
416516d4de92SAdrian Chadd 		 * XXX shouldn't this just use the default flags
416616d4de92SAdrian Chadd 		 * used in the previous queue setup?
416716d4de92SAdrian Chadd 		 */
416810ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
416910ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
417010ad9a77SSam Leffler 			      | HAL_TXQ_TXDESCINT_ENABLE
417110ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
41721f25c0f7SAdrian Chadd 			      | HAL_TXQ_TXEOLINT_ENABLE
417310ad9a77SSam Leffler 			      ;
4174c42a7b7eSSam Leffler 		qi.tqi_aifs = wmep->wmep_aifsn;
4175c42a7b7eSSam Leffler 		qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
4176c42a7b7eSSam Leffler 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
417710ad9a77SSam Leffler 		qi.tqi_readyTime = 0;
4178c42a7b7eSSam Leffler 		qi.tqi_burstTime = ATH_TXOP_TO_US(wmep->wmep_txopLimit);
4179584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
418010ad9a77SSam Leffler 	}
418110ad9a77SSam Leffler #endif
418210ad9a77SSam Leffler 
418310ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
418410ad9a77SSam Leffler 	    "%s: Q%u qflags 0x%x aifs %u cwmin %u cwmax %u burstTime %u\n",
418510ad9a77SSam Leffler 	    __func__, txq->axq_qnum, qi.tqi_qflags,
418610ad9a77SSam Leffler 	    qi.tqi_aifs, qi.tqi_cwmin, qi.tqi_cwmax, qi.tqi_burstTime);
4187c42a7b7eSSam Leffler 
4188c42a7b7eSSam Leffler 	if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
4189b032f27cSSam Leffler 		if_printf(ifp, "unable to update hardware queue "
4190c42a7b7eSSam Leffler 			"parameters for %s traffic!\n",
4191c42a7b7eSSam Leffler 			ieee80211_wme_acnames[ac]);
4192c42a7b7eSSam Leffler 		return 0;
4193c42a7b7eSSam Leffler 	} else {
4194c42a7b7eSSam Leffler 		ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
4195c42a7b7eSSam Leffler 		return 1;
4196c42a7b7eSSam Leffler 	}
4197c42a7b7eSSam Leffler #undef ATH_TXOP_TO_US
4198c42a7b7eSSam Leffler #undef ATH_EXPONENT_TO_VALUE
4199c42a7b7eSSam Leffler }
4200c42a7b7eSSam Leffler 
4201c42a7b7eSSam Leffler /*
4202c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update WME parameters.
4203c42a7b7eSSam Leffler  */
4204a35dae8dSAdrian Chadd int
4205c42a7b7eSSam Leffler ath_wme_update(struct ieee80211com *ic)
4206c42a7b7eSSam Leffler {
4207c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4208c42a7b7eSSam Leffler 
4209c42a7b7eSSam Leffler 	return !ath_txq_update(sc, WME_AC_BE) ||
4210c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_BK) ||
4211c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VI) ||
4212c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
4213c42a7b7eSSam Leffler }
4214c42a7b7eSSam Leffler 
4215c42a7b7eSSam Leffler /*
4216c42a7b7eSSam Leffler  * Reclaim resources for a setup queue.
4217c42a7b7eSSam Leffler  */
4218c42a7b7eSSam Leffler static void
4219c42a7b7eSSam Leffler ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
4220c42a7b7eSSam Leffler {
4221c42a7b7eSSam Leffler 
4222c42a7b7eSSam Leffler 	ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
4223c42a7b7eSSam Leffler 	sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
4224b837332dSAdrian Chadd 	ATH_TXQ_LOCK_DESTROY(txq);
4225c42a7b7eSSam Leffler }
4226c42a7b7eSSam Leffler 
4227c42a7b7eSSam Leffler /*
4228c42a7b7eSSam Leffler  * Reclaim all tx queue resources.
4229c42a7b7eSSam Leffler  */
4230c42a7b7eSSam Leffler static void
4231c42a7b7eSSam Leffler ath_tx_cleanup(struct ath_softc *sc)
4232c42a7b7eSSam Leffler {
4233c42a7b7eSSam Leffler 	int i;
4234c42a7b7eSSam Leffler 
4235c42a7b7eSSam Leffler 	ATH_TXBUF_LOCK_DESTROY(sc);
4236c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4237c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
4238c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, &sc->sc_txq[i]);
4239c42a7b7eSSam Leffler }
42405591b213SSam Leffler 
424199d258fdSSam Leffler /*
4242ab06fdf2SSam Leffler  * Return h/w rate index for an IEEE rate (w/o basic rate bit)
4243ab06fdf2SSam Leffler  * using the current rates in sc_rixmap.
42448b5341deSSam Leffler  */
4245b8e788a5SAdrian Chadd int
4246ab06fdf2SSam Leffler ath_tx_findrix(const struct ath_softc *sc, uint8_t rate)
42478b5341deSSam Leffler {
4248ab06fdf2SSam Leffler 	int rix = sc->sc_rixmap[rate];
4249ab06fdf2SSam Leffler 	/* NB: return lowest rix for invalid rate */
4250ab06fdf2SSam Leffler 	return (rix == 0xff ? 0 : rix);
42518b5341deSSam Leffler }
42528b5341deSSam Leffler 
42539352fb7aSAdrian Chadd static void
42549352fb7aSAdrian Chadd ath_tx_update_stats(struct ath_softc *sc, struct ath_tx_status *ts,
42559352fb7aSAdrian Chadd     struct ath_buf *bf)
42569352fb7aSAdrian Chadd {
42579352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
42589352fb7aSAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
42599352fb7aSAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
42609352fb7aSAdrian Chadd 	int sr, lr, pri;
42619352fb7aSAdrian Chadd 
42629352fb7aSAdrian Chadd 	if (ts->ts_status == 0) {
42639352fb7aSAdrian Chadd 		u_int8_t txant = ts->ts_antenna;
42649352fb7aSAdrian Chadd 		sc->sc_stats.ast_ant_tx[txant]++;
42659352fb7aSAdrian Chadd 		sc->sc_ant_tx[txant]++;
42669352fb7aSAdrian Chadd 		if (ts->ts_finaltsi != 0)
42679352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_altrate++;
42689352fb7aSAdrian Chadd 		pri = M_WME_GETAC(bf->bf_m);
42699352fb7aSAdrian Chadd 		if (pri >= WME_AC_VO)
42709352fb7aSAdrian Chadd 			ic->ic_wme.wme_hipri_traffic++;
4271875a9451SAdrian Chadd 		if ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)
42729352fb7aSAdrian Chadd 			ni->ni_inact = ni->ni_inact_reload;
42739352fb7aSAdrian Chadd 	} else {
42749352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XRETRY)
42759352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xretries++;
42769352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FIFO)
42779352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_fifoerr++;
42789352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FILT)
42799352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_filtered++;
42809352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XTXOP)
42819352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xtxop++;
42829352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_TIMER_EXPIRED)
42839352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_timerexpired++;
42849352fb7aSAdrian Chadd 
42859352fb7aSAdrian Chadd 		if (bf->bf_m->m_flags & M_FF)
42869352fb7aSAdrian Chadd 			sc->sc_stats.ast_ff_txerr++;
42879352fb7aSAdrian Chadd 	}
42889352fb7aSAdrian Chadd 	/* XXX when is this valid? */
4289158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DESC_CFG_ERR)
42909352fb7aSAdrian Chadd 		sc->sc_stats.ast_tx_desccfgerr++;
4291158cb431SAdrian Chadd 	/*
4292158cb431SAdrian Chadd 	 * This can be valid for successful frame transmission!
4293158cb431SAdrian Chadd 	 * If there's a TX FIFO underrun during aggregate transmission,
4294158cb431SAdrian Chadd 	 * the MAC will pad the rest of the aggregate with delimiters.
4295158cb431SAdrian Chadd 	 * If a BA is returned, the frame is marked as "OK" and it's up
4296158cb431SAdrian Chadd 	 * to the TX completion code to notice which frames weren't
4297158cb431SAdrian Chadd 	 * successfully transmitted.
4298158cb431SAdrian Chadd 	 */
4299158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DATA_UNDERRUN)
4300158cb431SAdrian Chadd 		sc->sc_stats.ast_tx_data_underrun++;
4301158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DELIM_UNDERRUN)
4302158cb431SAdrian Chadd 		sc->sc_stats.ast_tx_delim_underrun++;
43039352fb7aSAdrian Chadd 
43049352fb7aSAdrian Chadd 	sr = ts->ts_shortretry;
43059352fb7aSAdrian Chadd 	lr = ts->ts_longretry;
43069352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_shortretry += sr;
43079352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_longretry += lr;
43089352fb7aSAdrian Chadd 
43099352fb7aSAdrian Chadd }
43109352fb7aSAdrian Chadd 
43119352fb7aSAdrian Chadd /*
43129352fb7aSAdrian Chadd  * The default completion. If fail is 1, this means
43139352fb7aSAdrian Chadd  * "please don't retry the frame, and just return -1 status
43149352fb7aSAdrian Chadd  * to the net80211 stack.
43159352fb7aSAdrian Chadd  */
43169352fb7aSAdrian Chadd void
43179352fb7aSAdrian Chadd ath_tx_default_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
43189352fb7aSAdrian Chadd {
43199352fb7aSAdrian Chadd 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
43209352fb7aSAdrian Chadd 	int st;
43219352fb7aSAdrian Chadd 
43229352fb7aSAdrian Chadd 	if (fail == 1)
43239352fb7aSAdrian Chadd 		st = -1;
43249352fb7aSAdrian Chadd 	else
4325875a9451SAdrian Chadd 		st = ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) ?
43269352fb7aSAdrian Chadd 		    ts->ts_status : HAL_TXERR_XRETRY;
43279352fb7aSAdrian Chadd 
4328ce597531SAdrian Chadd #if 0
43299352fb7aSAdrian Chadd 	if (bf->bf_state.bfs_dobaw)
43309352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
4331a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: dobaw should've been cleared!\n",
4332a66d5089SAdrian Chadd 		    __func__,
4333a66d5089SAdrian Chadd 		    bf,
4334a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
4335ce597531SAdrian Chadd #endif
43369352fb7aSAdrian Chadd 	if (bf->bf_next != NULL)
43379352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
4338a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: bf_next not NULL!\n",
4339a66d5089SAdrian Chadd 		    __func__,
4340a66d5089SAdrian Chadd 		    bf,
4341a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
43429352fb7aSAdrian Chadd 
43439352fb7aSAdrian Chadd 	/*
4344548a605dSAdrian Chadd 	 * Check if the node software queue is empty; if so
4345548a605dSAdrian Chadd 	 * then clear the TIM.
4346548a605dSAdrian Chadd 	 *
4347548a605dSAdrian Chadd 	 * This needs to be done before the buffer is freed as
4348548a605dSAdrian Chadd 	 * otherwise the node reference will have been released
4349548a605dSAdrian Chadd 	 * and the node may not actually exist any longer.
4350548a605dSAdrian Chadd 	 *
4351548a605dSAdrian Chadd 	 * XXX I don't like this belonging here, but it's cleaner
4352548a605dSAdrian Chadd 	 * to do it here right now then all the other places
4353548a605dSAdrian Chadd 	 * where ath_tx_default_comp() is called.
4354548a605dSAdrian Chadd 	 *
4355548a605dSAdrian Chadd 	 * XXX TODO: during drain, ensure that the callback is
4356548a605dSAdrian Chadd 	 * being called so we get a chance to update the TIM.
4357548a605dSAdrian Chadd 	 */
43584bed2b67SAdrian Chadd 	if (bf->bf_node) {
43594bed2b67SAdrian Chadd 		ATH_TX_LOCK(sc);
4360548a605dSAdrian Chadd 		ath_tx_update_tim(sc, bf->bf_node, 0);
43614bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
43624bed2b67SAdrian Chadd 	}
4363548a605dSAdrian Chadd 
4364548a605dSAdrian Chadd 	/*
43659352fb7aSAdrian Chadd 	 * Do any tx complete callback.  Note this must
43669352fb7aSAdrian Chadd 	 * be done before releasing the node reference.
43679352fb7aSAdrian Chadd 	 * This will free the mbuf, release the net80211
43689352fb7aSAdrian Chadd 	 * node and recycle the ath_buf.
43699352fb7aSAdrian Chadd 	 */
43709352fb7aSAdrian Chadd 	ath_tx_freebuf(sc, bf, st);
43719352fb7aSAdrian Chadd }
43729352fb7aSAdrian Chadd 
43739352fb7aSAdrian Chadd /*
4374eb6f0de0SAdrian Chadd  * Update rate control with the given completion status.
4375eb6f0de0SAdrian Chadd  */
4376eb6f0de0SAdrian Chadd void
4377eb6f0de0SAdrian Chadd ath_tx_update_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
4378eb6f0de0SAdrian Chadd     struct ath_rc_series *rc, struct ath_tx_status *ts, int frmlen,
4379eb6f0de0SAdrian Chadd     int nframes, int nbad)
4380eb6f0de0SAdrian Chadd {
4381eb6f0de0SAdrian Chadd 	struct ath_node *an;
4382eb6f0de0SAdrian Chadd 
4383eb6f0de0SAdrian Chadd 	/* Only for unicast frames */
4384eb6f0de0SAdrian Chadd 	if (ni == NULL)
4385eb6f0de0SAdrian Chadd 		return;
4386eb6f0de0SAdrian Chadd 
4387eb6f0de0SAdrian Chadd 	an = ATH_NODE(ni);
4388548a605dSAdrian Chadd 	ATH_NODE_UNLOCK_ASSERT(an);
4389eb6f0de0SAdrian Chadd 
4390eb6f0de0SAdrian Chadd 	if ((ts->ts_status & HAL_TXERR_FILT) == 0) {
4391eb6f0de0SAdrian Chadd 		ATH_NODE_LOCK(an);
4392eb6f0de0SAdrian Chadd 		ath_rate_tx_complete(sc, an, rc, ts, frmlen, nframes, nbad);
4393eb6f0de0SAdrian Chadd 		ATH_NODE_UNLOCK(an);
4394eb6f0de0SAdrian Chadd 	}
4395eb6f0de0SAdrian Chadd }
4396eb6f0de0SAdrian Chadd 
4397eb6f0de0SAdrian Chadd /*
4398bad98824SAdrian Chadd  * Process the completion of the given buffer.
4399bad98824SAdrian Chadd  *
4400bad98824SAdrian Chadd  * This calls the rate control update and then the buffer completion.
4401bad98824SAdrian Chadd  * This will either free the buffer or requeue it.  In any case, the
4402bad98824SAdrian Chadd  * bf pointer should be treated as invalid after this function is called.
4403bad98824SAdrian Chadd  */
4404bad98824SAdrian Chadd void
4405bad98824SAdrian Chadd ath_tx_process_buf_completion(struct ath_softc *sc, struct ath_txq *txq,
4406bad98824SAdrian Chadd     struct ath_tx_status *ts, struct ath_buf *bf)
4407bad98824SAdrian Chadd {
4408bad98824SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
4409bad98824SAdrian Chadd 	struct ath_node *an = NULL;
4410bad98824SAdrian Chadd 
4411375307d4SAdrian Chadd 	ATH_TX_UNLOCK_ASSERT(sc);
44125e018508SAdrian Chadd 	ATH_TXQ_UNLOCK_ASSERT(txq);
4413bad98824SAdrian Chadd 
4414bad98824SAdrian Chadd 	/* If unicast frame, update general statistics */
4415bad98824SAdrian Chadd 	if (ni != NULL) {
4416bad98824SAdrian Chadd 		an = ATH_NODE(ni);
4417bad98824SAdrian Chadd 		/* update statistics */
4418bad98824SAdrian Chadd 		ath_tx_update_stats(sc, ts, bf);
4419bad98824SAdrian Chadd 	}
4420bad98824SAdrian Chadd 
4421bad98824SAdrian Chadd 	/*
4422bad98824SAdrian Chadd 	 * Call the completion handler.
4423bad98824SAdrian Chadd 	 * The completion handler is responsible for
4424bad98824SAdrian Chadd 	 * calling the rate control code.
4425bad98824SAdrian Chadd 	 *
4426bad98824SAdrian Chadd 	 * Frames with no completion handler get the
4427bad98824SAdrian Chadd 	 * rate control code called here.
4428bad98824SAdrian Chadd 	 */
4429bad98824SAdrian Chadd 	if (bf->bf_comp == NULL) {
4430bad98824SAdrian Chadd 		if ((ts->ts_status & HAL_TXERR_FILT) == 0 &&
4431bad98824SAdrian Chadd 		    (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) {
4432bad98824SAdrian Chadd 			/*
4433bad98824SAdrian Chadd 			 * XXX assume this isn't an aggregate
4434bad98824SAdrian Chadd 			 * frame.
4435bad98824SAdrian Chadd 			 */
4436bad98824SAdrian Chadd 			ath_tx_update_ratectrl(sc, ni,
4437bad98824SAdrian Chadd 			     bf->bf_state.bfs_rc, ts,
4438bad98824SAdrian Chadd 			    bf->bf_state.bfs_pktlen, 1,
4439bad98824SAdrian Chadd 			    (ts->ts_status == 0 ? 0 : 1));
4440bad98824SAdrian Chadd 		}
4441bad98824SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4442bad98824SAdrian Chadd 	} else
4443bad98824SAdrian Chadd 		bf->bf_comp(sc, bf, 0);
4444bad98824SAdrian Chadd }
4445bad98824SAdrian Chadd 
4446bad98824SAdrian Chadd 
4447bad98824SAdrian Chadd 
4448bad98824SAdrian Chadd /*
4449c42a7b7eSSam Leffler  * Process completed xmit descriptors from the specified queue.
4450eb6f0de0SAdrian Chadd  * Kick the packet scheduler if needed. This can occur from this
4451eb6f0de0SAdrian Chadd  * particular task.
4452c42a7b7eSSam Leffler  */
4453788e6aa9SAdrian Chadd static int
4454788e6aa9SAdrian Chadd ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq, int dosched)
44555591b213SSam Leffler {
44565591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
44579352fb7aSAdrian Chadd 	struct ath_buf *bf;
44586edf1dc7SAdrian Chadd 	struct ath_desc *ds;
445965f9edeeSSam Leffler 	struct ath_tx_status *ts;
44605591b213SSam Leffler 	struct ieee80211_node *ni;
446153e98d5aSAdrian Chadd #ifdef	IEEE80211_SUPPORT_SUPERG
446243faa6b2SAdrian Chadd 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
446353e98d5aSAdrian Chadd #endif	/* IEEE80211_SUPPORT_SUPERG */
44649352fb7aSAdrian Chadd 	int nacked;
44655591b213SSam Leffler 	HAL_STATUS status;
44665591b213SSam Leffler 
4467c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
4468c42a7b7eSSam Leffler 		__func__, txq->axq_qnum,
4469c42a7b7eSSam Leffler 		(caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
4470c42a7b7eSSam Leffler 		txq->axq_link);
447103682514SAdrian Chadd 
447203682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 4,
447303682514SAdrian Chadd 	    "ath_tx_processq: txq=%u head %p link %p depth %p",
447403682514SAdrian Chadd 	    txq->axq_qnum,
447503682514SAdrian Chadd 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
447603682514SAdrian Chadd 	    txq->axq_link,
447703682514SAdrian Chadd 	    txq->axq_depth);
447803682514SAdrian Chadd 
4479d7736e13SSam Leffler 	nacked = 0;
44805591b213SSam Leffler 	for (;;) {
4481b837332dSAdrian Chadd 		ATH_TXQ_LOCK(txq);
4482c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;	/* reset periodic desc intr count */
44836b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&txq->axq_q);
44845591b213SSam Leffler 		if (bf == NULL) {
4485b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
44865591b213SSam Leffler 			break;
44875591b213SSam Leffler 		}
44886edf1dc7SAdrian Chadd 		ds = bf->bf_lastds;	/* XXX must be setup correctly! */
448965f9edeeSSam Leffler 		ts = &bf->bf_status.ds_txstat;
449003682514SAdrian Chadd 
449165f9edeeSSam Leffler 		status = ath_hal_txprocdesc(ah, ds, ts);
4492a585a9a1SSam Leffler #ifdef ATH_DEBUG
4493c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
44946902009eSSam Leffler 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
44956902009eSSam Leffler 			    status == HAL_OK);
449603682514SAdrian Chadd 		else if ((sc->sc_debug & ATH_DEBUG_RESET) && (dosched == 0))
4497d6b20023SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
4498d6b20023SAdrian Chadd 			    status == HAL_OK);
44995591b213SSam Leffler #endif
4500bb327d28SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
4501bb327d28SAdrian Chadd 		if (if_ath_alq_checkdebug(&sc->sc_alq,
4502bb327d28SAdrian Chadd 		    ATH_ALQ_EDMA_TXSTATUS)) {
4503bb327d28SAdrian Chadd 			if_ath_alq_post(&sc->sc_alq, ATH_ALQ_EDMA_TXSTATUS,
4504bb327d28SAdrian Chadd 			sc->sc_tx_statuslen,
4505bb327d28SAdrian Chadd 			(char *) ds);
4506bb327d28SAdrian Chadd 		}
4507bb327d28SAdrian Chadd #endif
450803682514SAdrian Chadd 
45095591b213SSam Leffler 		if (status == HAL_EINPROGRESS) {
451003682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_TXCOMP, 3,
451103682514SAdrian Chadd 			    "ath_tx_processq: txq=%u, bf=%p ds=%p, HAL_EINPROGRESS",
451203682514SAdrian Chadd 			    txq->axq_qnum, bf, ds);
4513b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
45145591b213SSam Leffler 			break;
45155591b213SSam Leffler 		}
45166b349e5aSAdrian Chadd 		ATH_TXQ_REMOVE(txq, bf, bf_list);
45175e018508SAdrian Chadd 
45185e018508SAdrian Chadd 		/*
45195e018508SAdrian Chadd 		 * Sanity check.
45205e018508SAdrian Chadd 		 */
45215e018508SAdrian Chadd 		if (txq->axq_qnum != bf->bf_state.bfs_tx_queue) {
45225e018508SAdrian Chadd 			device_printf(sc->sc_dev,
45235e018508SAdrian Chadd 			    "%s: TXQ=%d: bf=%p, bfs_tx_queue=%d\n",
45245e018508SAdrian Chadd 			    __func__,
45255e018508SAdrian Chadd 			    txq->axq_qnum,
45265e018508SAdrian Chadd 			    bf,
45275e018508SAdrian Chadd 			    bf->bf_state.bfs_tx_queue);
45285e018508SAdrian Chadd 		}
45295e018508SAdrian Chadd 		if (txq->axq_qnum != bf->bf_last->bf_state.bfs_tx_queue) {
45305e018508SAdrian Chadd 			device_printf(sc->sc_dev,
45315e018508SAdrian Chadd 			    "%s: TXQ=%d: bf_last=%p, bfs_tx_queue=%d\n",
45325e018508SAdrian Chadd 			    __func__,
45335e018508SAdrian Chadd 			    txq->axq_qnum,
45345e018508SAdrian Chadd 			    bf->bf_last,
45355e018508SAdrian Chadd 			    bf->bf_last->bf_state.bfs_tx_queue);
45365e018508SAdrian Chadd 		}
45375e018508SAdrian Chadd 
45385e018508SAdrian Chadd #if 0
4539d3731e4bSAdrian Chadd 		if (txq->axq_depth > 0) {
454010ad9a77SSam Leffler 			/*
4541d3731e4bSAdrian Chadd 			 * More frames follow.  Mark the buffer busy
4542d3731e4bSAdrian Chadd 			 * so it's not re-used while the hardware may
4543d3731e4bSAdrian Chadd 			 * still re-read the link field in the descriptor.
45446edf1dc7SAdrian Chadd 			 *
4545d3731e4bSAdrian Chadd 			 * Use the last buffer in an aggregate as that
4546d3731e4bSAdrian Chadd 			 * is where the hardware may be - intermediate
4547d3731e4bSAdrian Chadd 			 * descriptors won't be "busy".
454810ad9a77SSam Leffler 			 */
45496edf1dc7SAdrian Chadd 			bf->bf_last->bf_flags |= ATH_BUF_BUSY;
4550d3731e4bSAdrian Chadd 		} else
4551d3731e4bSAdrian Chadd 			txq->axq_link = NULL;
45525e018508SAdrian Chadd #else
45535e018508SAdrian Chadd 		bf->bf_last->bf_flags |= ATH_BUF_BUSY;
45545e018508SAdrian Chadd #endif
45556edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
45566edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
45575591b213SSam Leffler 
45585591b213SSam Leffler 		ni = bf->bf_node;
455903682514SAdrian Chadd 
456003682514SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TXCOMP, 5,
456103682514SAdrian Chadd 		    "ath_tx_processq: txq=%u, bf=%p, ds=%p, ni=%p, ts_status=0x%08x",
456203682514SAdrian Chadd 		    txq->axq_qnum, bf, ds, ni, ts->ts_status);
4563c42a7b7eSSam Leffler 		/*
45649352fb7aSAdrian Chadd 		 * If unicast frame was ack'd update RSSI,
456584784be1SSam Leffler 		 * including the last rx time used to
456684784be1SSam Leffler 		 * workaround phantom bmiss interrupts.
4567d7736e13SSam Leffler 		 */
45689352fb7aSAdrian Chadd 		if (ni != NULL && ts->ts_status == 0 &&
4569875a9451SAdrian Chadd 		    ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)) {
4570d7736e13SSam Leffler 			nacked++;
457184784be1SSam Leffler 			sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
457284784be1SSam Leffler 			ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi,
457384784be1SSam Leffler 				ts->ts_rssi);
457484784be1SSam Leffler 		}
4575b837332dSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
45769352fb7aSAdrian Chadd 
4577bad98824SAdrian Chadd 		/*
4578bad98824SAdrian Chadd 		 * Update statistics and call completion
4579bad98824SAdrian Chadd 		 */
4580bad98824SAdrian Chadd 		ath_tx_process_buf_completion(sc, txq, ts, bf);
4581548a605dSAdrian Chadd 
4582548a605dSAdrian Chadd 		/* XXX at this point, bf and ni may be totally invalid */
45835591b213SSam Leffler 	}
4584339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
458568e8e04eSSam Leffler 	/*
458668e8e04eSSam Leffler 	 * Flush fast-frame staging queue when traffic slows.
458768e8e04eSSam Leffler 	 */
458868e8e04eSSam Leffler 	if (txq->axq_depth <= 1)
458904f19fd6SSam Leffler 		ieee80211_ff_flush(ic, txq->axq_ac);
4590339ccfb3SSam Leffler #endif
4591eb6f0de0SAdrian Chadd 
459221bca442SAdrian Chadd 	/* Kick the software TXQ scheduler */
4593eb6f0de0SAdrian Chadd 	if (dosched) {
4594a40880adSAdrian Chadd 		ATH_TX_LOCK(sc);
4595a40880adSAdrian Chadd 		ath_txq_sched(sc, txq);
4596a40880adSAdrian Chadd 		ATH_TX_UNLOCK(sc);
4597eb6f0de0SAdrian Chadd 	}
4598eb6f0de0SAdrian Chadd 
459903682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
460003682514SAdrian Chadd 	    "ath_tx_processq: txq=%u: done",
460103682514SAdrian Chadd 	    txq->axq_qnum);
460203682514SAdrian Chadd 
4603d7736e13SSam Leffler 	return nacked;
4604d7736e13SSam Leffler }
4605d7736e13SSam Leffler 
46068f939e79SAdrian Chadd #define	TXQACTIVE(t, q)		( (t) & (1 << (q)))
4607c42a7b7eSSam Leffler 
4608c42a7b7eSSam Leffler /*
4609c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4610c42a7b7eSSam Leffler  * for a single hardware transmit queue (e.g. 5210 and 5211).
4611c42a7b7eSSam Leffler  */
4612c42a7b7eSSam Leffler static void
4613c42a7b7eSSam Leffler ath_tx_proc_q0(void *arg, int npending)
4614c42a7b7eSSam Leffler {
4615c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4616fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
46178f939e79SAdrian Chadd 	uint32_t txqs;
4618c42a7b7eSSam Leffler 
4619ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4620ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
46218f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
46228f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4623ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
46248f939e79SAdrian Chadd 
4625f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4626f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4627f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4628f5c30c4eSAdrian Chadd 
462903682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
463003682514SAdrian Chadd 	    "ath_tx_proc_q0: txqs=0x%08x", txqs);
463103682514SAdrian Chadd 
463296ff485dSAdrian Chadd 	if (TXQACTIVE(txqs, 0) && ath_tx_processq(sc, &sc->sc_txq[0], 1))
46338f939e79SAdrian Chadd 		/* XXX why is lastrx updated in tx code? */
4634d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
46358f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
463696ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
4637e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
463813f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4639e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
46402e986da5SSam Leffler 	sc->sc_wd_timer = 0;
46415591b213SSam Leffler 
46423e50ec2cSSam Leffler 	if (sc->sc_softled)
464346d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
46443e50ec2cSSam Leffler 
4645ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4646ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4647ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
46481a85141aSAdrian Chadd 
4649f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4650f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4651f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4652f5c30c4eSAdrian Chadd 
46531a85141aSAdrian Chadd 	ath_tx_kick(sc);
46545591b213SSam Leffler }
46555591b213SSam Leffler 
46565591b213SSam Leffler /*
4657c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4658c42a7b7eSSam Leffler  * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
46595591b213SSam Leffler  */
46605591b213SSam Leffler static void
4661c42a7b7eSSam Leffler ath_tx_proc_q0123(void *arg, int npending)
4662c42a7b7eSSam Leffler {
4663c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4664fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4665d7736e13SSam Leffler 	int nacked;
46668f939e79SAdrian Chadd 	uint32_t txqs;
46678f939e79SAdrian Chadd 
4668ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4669ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
46708f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
46718f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4672ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4673c42a7b7eSSam Leffler 
4674f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4675f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4676f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4677f5c30c4eSAdrian Chadd 
467803682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
467903682514SAdrian Chadd 	    "ath_tx_proc_q0123: txqs=0x%08x", txqs);
468003682514SAdrian Chadd 
4681c42a7b7eSSam Leffler 	/*
4682c42a7b7eSSam Leffler 	 * Process each active queue.
4683c42a7b7eSSam Leffler 	 */
4684d7736e13SSam Leffler 	nacked = 0;
46858f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 0))
468696ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[0], 1);
46878f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 1))
468896ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[1], 1);
46898f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 2))
469096ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[2], 1);
46918f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 3))
469296ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[3], 1);
46938f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
469496ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
4695d7736e13SSam Leffler 	if (nacked)
4696d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4697c42a7b7eSSam Leffler 
4698e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
469913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4700e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
47012e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4702c42a7b7eSSam Leffler 
47033e50ec2cSSam Leffler 	if (sc->sc_softled)
470446d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
47053e50ec2cSSam Leffler 
4706ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4707ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4708ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
47091a85141aSAdrian Chadd 
4710f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4711f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4712f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4713f5c30c4eSAdrian Chadd 
47141a85141aSAdrian Chadd 	ath_tx_kick(sc);
4715c42a7b7eSSam Leffler }
4716c42a7b7eSSam Leffler 
4717c42a7b7eSSam Leffler /*
4718c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt.
4719c42a7b7eSSam Leffler  */
4720c42a7b7eSSam Leffler static void
4721c42a7b7eSSam Leffler ath_tx_proc(void *arg, int npending)
4722c42a7b7eSSam Leffler {
4723c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4724fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4725d7736e13SSam Leffler 	int i, nacked;
47268f939e79SAdrian Chadd 	uint32_t txqs;
47278f939e79SAdrian Chadd 
4728ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4729ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
47308f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
47318f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4732ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4733c42a7b7eSSam Leffler 
4734f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4735f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4736f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4737f5c30c4eSAdrian Chadd 
473803682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1, "ath_tx_proc: txqs=0x%08x", txqs);
473903682514SAdrian Chadd 
4740c42a7b7eSSam Leffler 	/*
4741c42a7b7eSSam Leffler 	 * Process each active queue.
4742c42a7b7eSSam Leffler 	 */
4743d7736e13SSam Leffler 	nacked = 0;
4744c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
47458f939e79SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i) && TXQACTIVE(txqs, i))
474696ff485dSAdrian Chadd 			nacked += ath_tx_processq(sc, &sc->sc_txq[i], 1);
4747d7736e13SSam Leffler 	if (nacked)
4748d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4749c42a7b7eSSam Leffler 
4750ef27340cSAdrian Chadd 	/* XXX check this inside of IF_LOCK? */
4751e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
475213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4753e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
47542e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4755c42a7b7eSSam Leffler 
47563e50ec2cSSam Leffler 	if (sc->sc_softled)
475746d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
47583e50ec2cSSam Leffler 
4759ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4760ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4761ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
47621a85141aSAdrian Chadd 
4763f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4764f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4765f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4766f5c30c4eSAdrian Chadd 
47671a85141aSAdrian Chadd 	ath_tx_kick(sc);
4768c42a7b7eSSam Leffler }
476916d4de92SAdrian Chadd #undef	TXQACTIVE
4770c42a7b7eSSam Leffler 
47719352fb7aSAdrian Chadd /*
477203e9308fSAdrian Chadd  * Deferred processing of TXQ rescheduling.
477303e9308fSAdrian Chadd  */
477403e9308fSAdrian Chadd static void
477503e9308fSAdrian Chadd ath_txq_sched_tasklet(void *arg, int npending)
477603e9308fSAdrian Chadd {
477703e9308fSAdrian Chadd 	struct ath_softc *sc = arg;
477803e9308fSAdrian Chadd 	int i;
477903e9308fSAdrian Chadd 
478003e9308fSAdrian Chadd 	/* XXX is skipping ok? */
478103e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
478203e9308fSAdrian Chadd #if 0
478303e9308fSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
478403e9308fSAdrian Chadd 		device_printf(sc->sc_dev,
478503e9308fSAdrian Chadd 		    "%s: sc_inreset_cnt > 0; skipping\n", __func__);
478603e9308fSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
478703e9308fSAdrian Chadd 		return;
478803e9308fSAdrian Chadd 	}
478903e9308fSAdrian Chadd #endif
479003e9308fSAdrian Chadd 	sc->sc_txproc_cnt++;
479103e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
479203e9308fSAdrian Chadd 
4793f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4794f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4795f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4796f5c30c4eSAdrian Chadd 
4797375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
479803e9308fSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
4799b5a9dfd5SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
480003e9308fSAdrian Chadd 			ath_txq_sched(sc, &sc->sc_txq[i]);
4801b5a9dfd5SAdrian Chadd 		}
480203e9308fSAdrian Chadd 	}
4803375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
480403e9308fSAdrian Chadd 
4805f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4806f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4807f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4808f5c30c4eSAdrian Chadd 
480903e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
481003e9308fSAdrian Chadd 	sc->sc_txproc_cnt--;
481103e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
481203e9308fSAdrian Chadd }
481303e9308fSAdrian Chadd 
4814e1a50456SAdrian Chadd void
4815e1a50456SAdrian Chadd ath_returnbuf_tail(struct ath_softc *sc, struct ath_buf *bf)
4816e1a50456SAdrian Chadd {
4817e1a50456SAdrian Chadd 
4818e1a50456SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
4819e1a50456SAdrian Chadd 
4820af33d486SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_MGMT)
4821af33d486SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf_mgmt, bf, bf_list);
482223ced6c1SAdrian Chadd 	else {
4823e1a50456SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
482423ced6c1SAdrian Chadd 		sc->sc_txbuf_cnt++;
482523ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt > ath_txbuf) {
482623ced6c1SAdrian Chadd 			device_printf(sc->sc_dev,
482723ced6c1SAdrian Chadd 			    "%s: sc_txbuf_cnt > %d?\n",
482823ced6c1SAdrian Chadd 			    __func__,
482923ced6c1SAdrian Chadd 			    ath_txbuf);
483023ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt = ath_txbuf;
483123ced6c1SAdrian Chadd 		}
483223ced6c1SAdrian Chadd 	}
4833e1a50456SAdrian Chadd }
4834e1a50456SAdrian Chadd 
4835e1a50456SAdrian Chadd void
4836e1a50456SAdrian Chadd ath_returnbuf_head(struct ath_softc *sc, struct ath_buf *bf)
4837e1a50456SAdrian Chadd {
4838e1a50456SAdrian Chadd 
4839e1a50456SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
4840e1a50456SAdrian Chadd 
4841af33d486SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_MGMT)
4842af33d486SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf_mgmt, bf, bf_list);
484323ced6c1SAdrian Chadd 	else {
4844e1a50456SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
484523ced6c1SAdrian Chadd 		sc->sc_txbuf_cnt++;
484623ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt > ATH_TXBUF) {
484723ced6c1SAdrian Chadd 			device_printf(sc->sc_dev,
484823ced6c1SAdrian Chadd 			    "%s: sc_txbuf_cnt > %d?\n",
484923ced6c1SAdrian Chadd 			    __func__,
485023ced6c1SAdrian Chadd 			    ATH_TXBUF);
485123ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt = ATH_TXBUF;
485223ced6c1SAdrian Chadd 		}
485323ced6c1SAdrian Chadd 	}
4854e1a50456SAdrian Chadd }
4855e1a50456SAdrian Chadd 
485603e9308fSAdrian Chadd /*
4857629ce218SAdrian Chadd  * Free the holding buffer if it exists
4858629ce218SAdrian Chadd  */
48593feffbd7SAdrian Chadd void
4860629ce218SAdrian Chadd ath_txq_freeholdingbuf(struct ath_softc *sc, struct ath_txq *txq)
4861629ce218SAdrian Chadd {
48625e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK_ASSERT(sc);
48635e018508SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
4864629ce218SAdrian Chadd 
4865629ce218SAdrian Chadd 	if (txq->axq_holdingbf == NULL)
4866629ce218SAdrian Chadd 		return;
4867629ce218SAdrian Chadd 
4868629ce218SAdrian Chadd 	txq->axq_holdingbf->bf_flags &= ~ATH_BUF_BUSY;
48695e018508SAdrian Chadd 
48705e018508SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
4871629ce218SAdrian Chadd 	ath_returnbuf_tail(sc, txq->axq_holdingbf);
48725e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
48735e018508SAdrian Chadd 
4874629ce218SAdrian Chadd 	txq->axq_holdingbf = NULL;
4875629ce218SAdrian Chadd }
4876629ce218SAdrian Chadd 
4877629ce218SAdrian Chadd /*
4878629ce218SAdrian Chadd  * Add this buffer to the holding queue, freeing the previous
4879629ce218SAdrian Chadd  * one if it exists.
4880629ce218SAdrian Chadd  */
4881629ce218SAdrian Chadd static void
4882629ce218SAdrian Chadd ath_txq_addholdingbuf(struct ath_softc *sc, struct ath_buf *bf)
4883629ce218SAdrian Chadd {
4884629ce218SAdrian Chadd 	struct ath_txq *txq;
4885629ce218SAdrian Chadd 
48865e018508SAdrian Chadd 	txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue];
48875e018508SAdrian Chadd 
48885e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK_ASSERT(sc);
48895e018508SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
48905f2f0e61SAdrian Chadd 
4891629ce218SAdrian Chadd 	/* XXX assert ATH_BUF_BUSY is set */
4892629ce218SAdrian Chadd 
4893629ce218SAdrian Chadd 	/* XXX assert the tx queue is under the max number */
4894629ce218SAdrian Chadd 	if (bf->bf_state.bfs_tx_queue > HAL_NUM_TX_QUEUES) {
4895629ce218SAdrian Chadd 		device_printf(sc->sc_dev, "%s: bf=%p: invalid tx queue (%d)\n",
4896629ce218SAdrian Chadd 		    __func__,
4897629ce218SAdrian Chadd 		    bf,
4898629ce218SAdrian Chadd 		    bf->bf_state.bfs_tx_queue);
4899629ce218SAdrian Chadd 		bf->bf_flags &= ~ATH_BUF_BUSY;
4900629ce218SAdrian Chadd 		ath_returnbuf_tail(sc, bf);
4901629ce218SAdrian Chadd 		return;
4902629ce218SAdrian Chadd 	}
4903629ce218SAdrian Chadd 	ath_txq_freeholdingbuf(sc, txq);
4904629ce218SAdrian Chadd 	txq->axq_holdingbf = bf;
4905629ce218SAdrian Chadd }
4906629ce218SAdrian Chadd 
4907629ce218SAdrian Chadd /*
49089352fb7aSAdrian Chadd  * Return a buffer to the pool and update the 'busy' flag on the
49099352fb7aSAdrian Chadd  * previous 'tail' entry.
49109352fb7aSAdrian Chadd  *
49119352fb7aSAdrian Chadd  * This _must_ only be called when the buffer is involved in a completed
49129352fb7aSAdrian Chadd  * TX. The logic is that if it was part of an active TX, the previous
49139352fb7aSAdrian Chadd  * buffer on the list is now not involved in a halted TX DMA queue, waiting
49149352fb7aSAdrian Chadd  * for restart (eg for TDMA.)
49159352fb7aSAdrian Chadd  *
49169352fb7aSAdrian Chadd  * The caller must free the mbuf and recycle the node reference.
49175e018508SAdrian Chadd  *
49185e018508SAdrian Chadd  * XXX This method of handling busy / holding buffers is insanely stupid.
49195e018508SAdrian Chadd  * It requires bf_state.bfs_tx_queue to be correctly assigned.  It would
49205e018508SAdrian Chadd  * be much nicer if buffers in the processq() methods would instead be
49215e018508SAdrian Chadd  * always completed there (pushed onto a txq or ath_bufhead) so we knew
49225e018508SAdrian Chadd  * exactly what hardware queue they came from in the first place.
49239352fb7aSAdrian Chadd  */
49249352fb7aSAdrian Chadd void
49259352fb7aSAdrian Chadd ath_freebuf(struct ath_softc *sc, struct ath_buf *bf)
49269352fb7aSAdrian Chadd {
49275e018508SAdrian Chadd 	struct ath_txq *txq;
49285e018508SAdrian Chadd 
49295e018508SAdrian Chadd 	txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue];
49305e018508SAdrian Chadd 
49319352fb7aSAdrian Chadd 	KASSERT((bf->bf_node == NULL), ("%s: bf->bf_node != NULL\n", __func__));
49329352fb7aSAdrian Chadd 	KASSERT((bf->bf_m == NULL), ("%s: bf->bf_m != NULL\n", __func__));
49339352fb7aSAdrian Chadd 
4934629ce218SAdrian Chadd 	/*
49355e018508SAdrian Chadd 	 * If this buffer is busy, push it onto the holding queue.
4936629ce218SAdrian Chadd 	 */
4937629ce218SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_BUSY) {
49385e018508SAdrian Chadd 		ATH_TXQ_LOCK(txq);
4939629ce218SAdrian Chadd 		ath_txq_addholdingbuf(sc, bf);
49405e018508SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
4941629ce218SAdrian Chadd 		return;
4942629ce218SAdrian Chadd 	}
4943629ce218SAdrian Chadd 
4944629ce218SAdrian Chadd 	/*
4945629ce218SAdrian Chadd 	 * Not a busy buffer, so free normally
4946629ce218SAdrian Chadd 	 */
49479352fb7aSAdrian Chadd 	ATH_TXBUF_LOCK(sc);
4948e1a50456SAdrian Chadd 	ath_returnbuf_tail(sc, bf);
49499352fb7aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
49509352fb7aSAdrian Chadd }
49519352fb7aSAdrian Chadd 
49529352fb7aSAdrian Chadd /*
49539352fb7aSAdrian Chadd  * This is currently used by ath_tx_draintxq() and
49549352fb7aSAdrian Chadd  * ath_tx_tid_free_pkts().
49559352fb7aSAdrian Chadd  *
49569352fb7aSAdrian Chadd  * It recycles a single ath_buf.
49579352fb7aSAdrian Chadd  */
49589352fb7aSAdrian Chadd void
49599352fb7aSAdrian Chadd ath_tx_freebuf(struct ath_softc *sc, struct ath_buf *bf, int status)
49609352fb7aSAdrian Chadd {
49619352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
49629352fb7aSAdrian Chadd 	struct mbuf *m0 = bf->bf_m;
49639352fb7aSAdrian Chadd 
49643f3a5dbdSAdrian Chadd 	/*
49653f3a5dbdSAdrian Chadd 	 * Make sure that we only sync/unload if there's an mbuf.
49663f3a5dbdSAdrian Chadd 	 * If not (eg we cloned a buffer), the unload will have already
49673f3a5dbdSAdrian Chadd 	 * occured.
49683f3a5dbdSAdrian Chadd 	 */
49693f3a5dbdSAdrian Chadd 	if (bf->bf_m != NULL) {
49703f3a5dbdSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
49713f3a5dbdSAdrian Chadd 		    BUS_DMASYNC_POSTWRITE);
49723f3a5dbdSAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
49733f3a5dbdSAdrian Chadd 	}
49743f3a5dbdSAdrian Chadd 
49759352fb7aSAdrian Chadd 	bf->bf_node = NULL;
49769352fb7aSAdrian Chadd 	bf->bf_m = NULL;
49779352fb7aSAdrian Chadd 
49789352fb7aSAdrian Chadd 	/* Free the buffer, it's not needed any longer */
49799352fb7aSAdrian Chadd 	ath_freebuf(sc, bf);
49809352fb7aSAdrian Chadd 
4981e95f3424SAdrian Chadd 	/* Pass the buffer back to net80211 - completing it */
4982e95f3424SAdrian Chadd 	ieee80211_tx_complete(ni, m0, status);
49839352fb7aSAdrian Chadd }
49849352fb7aSAdrian Chadd 
49853feffbd7SAdrian Chadd static struct ath_buf *
49863feffbd7SAdrian Chadd ath_tx_draintxq_get_one(struct ath_softc *sc, struct ath_txq *txq)
49873feffbd7SAdrian Chadd {
49883feffbd7SAdrian Chadd 	struct ath_buf *bf;
49893feffbd7SAdrian Chadd 
49903feffbd7SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
49913feffbd7SAdrian Chadd 
49923feffbd7SAdrian Chadd 	/*
49933feffbd7SAdrian Chadd 	 * Drain the FIFO queue first, then if it's
49943feffbd7SAdrian Chadd 	 * empty, move to the normal frame queue.
49953feffbd7SAdrian Chadd 	 */
49963feffbd7SAdrian Chadd 	bf = TAILQ_FIRST(&txq->fifo.axq_q);
49973feffbd7SAdrian Chadd 	if (bf != NULL) {
49983feffbd7SAdrian Chadd 		/*
49993feffbd7SAdrian Chadd 		 * Is it the last buffer in this set?
50003feffbd7SAdrian Chadd 		 * Decrement the FIFO counter.
50013feffbd7SAdrian Chadd 		 */
50023feffbd7SAdrian Chadd 		if (bf->bf_flags & ATH_BUF_FIFOEND) {
50033feffbd7SAdrian Chadd 			if (txq->axq_fifo_depth == 0) {
50043feffbd7SAdrian Chadd 				device_printf(sc->sc_dev,
50053feffbd7SAdrian Chadd 				    "%s: Q%d: fifo_depth=0, fifo.axq_depth=%d?\n",
50063feffbd7SAdrian Chadd 				    __func__,
50073feffbd7SAdrian Chadd 				    txq->axq_qnum,
50083feffbd7SAdrian Chadd 				    txq->fifo.axq_depth);
50093feffbd7SAdrian Chadd 			} else
50103feffbd7SAdrian Chadd 				txq->axq_fifo_depth--;
50113feffbd7SAdrian Chadd 		}
50123feffbd7SAdrian Chadd 		ATH_TXQ_REMOVE(&txq->fifo, bf, bf_list);
50133feffbd7SAdrian Chadd 		return (bf);
50143feffbd7SAdrian Chadd 	}
50153feffbd7SAdrian Chadd 
50163feffbd7SAdrian Chadd 	/*
50173feffbd7SAdrian Chadd 	 * Debugging!
50183feffbd7SAdrian Chadd 	 */
50193feffbd7SAdrian Chadd 	if (txq->axq_fifo_depth != 0 || txq->fifo.axq_depth != 0) {
50203feffbd7SAdrian Chadd 		device_printf(sc->sc_dev,
50213feffbd7SAdrian Chadd 		    "%s: Q%d: fifo_depth=%d, fifo.axq_depth=%d\n",
50223feffbd7SAdrian Chadd 		    __func__,
50233feffbd7SAdrian Chadd 		    txq->axq_qnum,
50243feffbd7SAdrian Chadd 		    txq->axq_fifo_depth,
50253feffbd7SAdrian Chadd 		    txq->fifo.axq_depth);
50263feffbd7SAdrian Chadd 	}
50273feffbd7SAdrian Chadd 
50283feffbd7SAdrian Chadd 	/*
50293feffbd7SAdrian Chadd 	 * Now drain the pending queue.
50303feffbd7SAdrian Chadd 	 */
50313feffbd7SAdrian Chadd 	bf = TAILQ_FIRST(&txq->axq_q);
50323feffbd7SAdrian Chadd 	if (bf == NULL) {
50333feffbd7SAdrian Chadd 		txq->axq_link = NULL;
50343feffbd7SAdrian Chadd 		return (NULL);
50353feffbd7SAdrian Chadd 	}
50363feffbd7SAdrian Chadd 	ATH_TXQ_REMOVE(txq, bf, bf_list);
50373feffbd7SAdrian Chadd 	return (bf);
50383feffbd7SAdrian Chadd }
50393feffbd7SAdrian Chadd 
50409352fb7aSAdrian Chadd void
50411762ec94SAdrian Chadd ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
50425591b213SSam Leffler {
5043a585a9a1SSam Leffler #ifdef ATH_DEBUG
50445591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5045d2f6ed15SSam Leffler #endif
50465591b213SSam Leffler 	struct ath_buf *bf;
50477a4c5ed9SSam Leffler 	u_int ix;
50485591b213SSam Leffler 
5049c42a7b7eSSam Leffler 	/*
5050c42a7b7eSSam Leffler 	 * NB: this assumes output has been stopped and
50515d61b5e8SSam Leffler 	 *     we do not need to block ath_tx_proc
5052c42a7b7eSSam Leffler 	 */
50537a4c5ed9SSam Leffler 	for (ix = 0;; ix++) {
5054b837332dSAdrian Chadd 		ATH_TXQ_LOCK(txq);
50553feffbd7SAdrian Chadd 		bf = ath_tx_draintxq_get_one(sc, txq);
50565591b213SSam Leffler 		if (bf == NULL) {
5057b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
50585591b213SSam Leffler 			break;
50595591b213SSam Leffler 		}
50606edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
50616edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
5062a585a9a1SSam Leffler #ifdef ATH_DEBUG
50634a3ac3fcSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RESET) {
5064b032f27cSSam Leffler 			struct ieee80211com *ic = sc->sc_ifp->if_l2com;
50651762ec94SAdrian Chadd 			int status = 0;
5066b032f27cSSam Leffler 
50671762ec94SAdrian Chadd 			/*
50681762ec94SAdrian Chadd 			 * EDMA operation has a TX completion FIFO
50691762ec94SAdrian Chadd 			 * separate from the TX descriptor, so this
50701762ec94SAdrian Chadd 			 * method of checking the "completion" status
50711762ec94SAdrian Chadd 			 * is wrong.
50721762ec94SAdrian Chadd 			 */
50731762ec94SAdrian Chadd 			if (! sc->sc_isedma) {
50741762ec94SAdrian Chadd 				status = (ath_hal_txprocdesc(ah,
50751762ec94SAdrian Chadd 				    bf->bf_lastds,
507665f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
50771762ec94SAdrian Chadd 			}
50781762ec94SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, ix, status);
5079e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ic, mtod(bf->bf_m, const uint8_t *),
50804a3ac3fcSSam Leffler 			    bf->bf_m->m_len, 0, -1);
50814a3ac3fcSSam Leffler 		}
5082a585a9a1SSam Leffler #endif /* ATH_DEBUG */
508323428eafSSam Leffler 		/*
50849352fb7aSAdrian Chadd 		 * Since we're now doing magic in the completion
50859352fb7aSAdrian Chadd 		 * functions, we -must- call it for aggregation
50869352fb7aSAdrian Chadd 		 * destinations or BAW tracking will get upset.
508723428eafSSam Leffler 		 */
50889352fb7aSAdrian Chadd 		/*
50899352fb7aSAdrian Chadd 		 * Clear ATH_BUF_BUSY; the completion handler
50909352fb7aSAdrian Chadd 		 * will free the buffer.
50919352fb7aSAdrian Chadd 		 */
5092b837332dSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
509310ad9a77SSam Leffler 		bf->bf_flags &= ~ATH_BUF_BUSY;
50949352fb7aSAdrian Chadd 		if (bf->bf_comp)
50959352fb7aSAdrian Chadd 			bf->bf_comp(sc, bf, 1);
50969352fb7aSAdrian Chadd 		else
50979352fb7aSAdrian Chadd 			ath_tx_default_comp(sc, bf, 1);
50985591b213SSam Leffler 	}
50999352fb7aSAdrian Chadd 
5100eb6f0de0SAdrian Chadd 	/*
5101629ce218SAdrian Chadd 	 * Free the holding buffer if it exists
5102629ce218SAdrian Chadd 	 */
51035e018508SAdrian Chadd 	ATH_TXQ_LOCK(txq);
5104629ce218SAdrian Chadd 	ath_txq_freeholdingbuf(sc, txq);
51055e018508SAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
5106629ce218SAdrian Chadd 
5107629ce218SAdrian Chadd 	/*
5108eb6f0de0SAdrian Chadd 	 * Drain software queued frames which are on
5109eb6f0de0SAdrian Chadd 	 * active TIDs.
5110eb6f0de0SAdrian Chadd 	 */
5111eb6f0de0SAdrian Chadd 	ath_tx_txq_drain(sc, txq);
5112c42a7b7eSSam Leffler }
5113c42a7b7eSSam Leffler 
5114c42a7b7eSSam Leffler static void
5115c42a7b7eSSam Leffler ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
5116c42a7b7eSSam Leffler {
5117c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5118c42a7b7eSSam Leffler 
51199be82a42SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
51209be82a42SAdrian Chadd 
51219d2a962bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
5122dfaf8de9SAdrian Chadd 	    "%s: tx queue [%u] %p, active=%d, hwpending=%d, flags 0x%08x, "
5123dfaf8de9SAdrian Chadd 	    "link %p, holdingbf=%p\n",
51249d2a962bSAdrian Chadd 	    __func__,
51259d2a962bSAdrian Chadd 	    txq->axq_qnum,
51266891c875SPeter Wemm 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
51278d060542SAdrian Chadd 	    (int) (!! ath_hal_txqenabled(ah, txq->axq_qnum)),
51288d060542SAdrian Chadd 	    (int) ath_hal_numtxpending(ah, txq->axq_qnum),
51299d2a962bSAdrian Chadd 	    txq->axq_flags,
5130dfaf8de9SAdrian Chadd 	    txq->axq_link,
5131dfaf8de9SAdrian Chadd 	    txq->axq_holdingbf);
5132dfaf8de9SAdrian Chadd 
51334a3ac3fcSSam Leffler 	(void) ath_hal_stoptxdma(ah, txq->axq_qnum);
51349be82a42SAdrian Chadd 	/* We've stopped TX DMA, so mark this as stopped. */
51359be82a42SAdrian Chadd 	txq->axq_flags &= ~ATH_TXQ_PUTRUNNING;
5136dfaf8de9SAdrian Chadd 
5137dfaf8de9SAdrian Chadd #ifdef	ATH_DEBUG
5138dfaf8de9SAdrian Chadd 	if ((sc->sc_debug & ATH_DEBUG_RESET)
5139dfaf8de9SAdrian Chadd 	    && (txq->axq_holdingbf != NULL)) {
5140dfaf8de9SAdrian Chadd 		ath_printtxbuf(sc, txq->axq_holdingbf, txq->axq_qnum, 0, 0);
5141dfaf8de9SAdrian Chadd 	}
5142dfaf8de9SAdrian Chadd #endif
5143c42a7b7eSSam Leffler }
5144c42a7b7eSSam Leffler 
5145bad98824SAdrian Chadd int
51462d433424SAdrian Chadd ath_stoptxdma(struct ath_softc *sc)
5147c42a7b7eSSam Leffler {
5148c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5149c42a7b7eSSam Leffler 	int i;
5150c42a7b7eSSam Leffler 
5151c42a7b7eSSam Leffler 	/* XXX return value */
51522d433424SAdrian Chadd 	if (sc->sc_invalid)
51532d433424SAdrian Chadd 		return 0;
51542d433424SAdrian Chadd 
5155c42a7b7eSSam Leffler 	if (!sc->sc_invalid) {
5156c42a7b7eSSam Leffler 		/* don't touch the hardware if marked invalid */
51574a3ac3fcSSam Leffler 		DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
51584a3ac3fcSSam Leffler 		    __func__, sc->sc_bhalq,
51594a3ac3fcSSam Leffler 		    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq),
51604a3ac3fcSSam Leffler 		    NULL);
51619be82a42SAdrian Chadd 
51629be82a42SAdrian Chadd 		/* stop the beacon queue */
5163c42a7b7eSSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
51649be82a42SAdrian Chadd 
51659be82a42SAdrian Chadd 		/* Stop the data queues */
51669be82a42SAdrian Chadd 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
51679be82a42SAdrian Chadd 			if (ATH_TXQ_SETUP(sc, i)) {
51689be82a42SAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
5169c42a7b7eSSam Leffler 				ath_tx_stopdma(sc, &sc->sc_txq[i]);
51709be82a42SAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
51719be82a42SAdrian Chadd 			}
51729be82a42SAdrian Chadd 		}
5173c42a7b7eSSam Leffler 	}
51742d433424SAdrian Chadd 
51752d433424SAdrian Chadd 	return 1;
51762d433424SAdrian Chadd }
51772d433424SAdrian Chadd 
517807187d11SAdrian Chadd #ifdef	ATH_DEBUG
51799be82a42SAdrian Chadd void
5180ed261a61SAdrian Chadd ath_tx_dump(struct ath_softc *sc, struct ath_txq *txq)
5181ed261a61SAdrian Chadd {
5182ed261a61SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
5183ed261a61SAdrian Chadd 	struct ath_buf *bf;
5184ed261a61SAdrian Chadd 	int i = 0;
5185ed261a61SAdrian Chadd 
5186ed261a61SAdrian Chadd 	if (! (sc->sc_debug & ATH_DEBUG_RESET))
5187ed261a61SAdrian Chadd 		return;
5188ed261a61SAdrian Chadd 
5189ed261a61SAdrian Chadd 	device_printf(sc->sc_dev, "%s: Q%d: begin\n",
5190ed261a61SAdrian Chadd 	    __func__, txq->axq_qnum);
5191ed261a61SAdrian Chadd 	TAILQ_FOREACH(bf, &txq->axq_q, bf_list) {
5192ed261a61SAdrian Chadd 		ath_printtxbuf(sc, bf, txq->axq_qnum, i,
5193ed261a61SAdrian Chadd 			ath_hal_txprocdesc(ah, bf->bf_lastds,
5194ed261a61SAdrian Chadd 			    &bf->bf_status.ds_txstat) == HAL_OK);
5195ed261a61SAdrian Chadd 		i++;
5196ed261a61SAdrian Chadd 	}
5197ed261a61SAdrian Chadd 	device_printf(sc->sc_dev, "%s: Q%d: end\n",
5198ed261a61SAdrian Chadd 	    __func__, txq->axq_qnum);
5199ed261a61SAdrian Chadd }
520007187d11SAdrian Chadd #endif /* ATH_DEBUG */
5201ed261a61SAdrian Chadd 
52022d433424SAdrian Chadd /*
52032d433424SAdrian Chadd  * Drain the transmit queues and reclaim resources.
52042d433424SAdrian Chadd  */
5205788e6aa9SAdrian Chadd void
5206788e6aa9SAdrian Chadd ath_legacy_tx_drain(struct ath_softc *sc, ATH_RESET_TYPE reset_type)
52072d433424SAdrian Chadd {
52082d433424SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
52092d433424SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
52102d433424SAdrian Chadd 	int i;
52119be82a42SAdrian Chadd 	struct ath_buf *bf_last;
52122d433424SAdrian Chadd 
52132d433424SAdrian Chadd 	(void) ath_stoptxdma(sc);
52142d433424SAdrian Chadd 
5215ed261a61SAdrian Chadd 	/*
5216ed261a61SAdrian Chadd 	 * Dump the queue contents
5217ed261a61SAdrian Chadd 	 */
5218ef27340cSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
5219ef27340cSAdrian Chadd 		/*
5220ef27340cSAdrian Chadd 		 * XXX TODO: should we just handle the completed TX frames
5221ef27340cSAdrian Chadd 		 * here, whether or not the reset is a full one or not?
5222ef27340cSAdrian Chadd 		 */
5223ef27340cSAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
522407187d11SAdrian Chadd #ifdef	ATH_DEBUG
5225ed261a61SAdrian Chadd 			if (sc->sc_debug & ATH_DEBUG_RESET)
5226ed261a61SAdrian Chadd 				ath_tx_dump(sc, &sc->sc_txq[i]);
522707187d11SAdrian Chadd #endif	/* ATH_DEBUG */
52288328d6e4SAdrian Chadd 			if (reset_type == ATH_RESET_NOLOSS) {
5229ef27340cSAdrian Chadd 				ath_tx_processq(sc, &sc->sc_txq[i], 0);
52308328d6e4SAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
52318328d6e4SAdrian Chadd 				/*
52328328d6e4SAdrian Chadd 				 * Free the holding buffer; DMA is now
52338328d6e4SAdrian Chadd 				 * stopped.
52348328d6e4SAdrian Chadd 				 */
52358328d6e4SAdrian Chadd 				ath_txq_freeholdingbuf(sc, &sc->sc_txq[i]);
52368328d6e4SAdrian Chadd 				/*
52379be82a42SAdrian Chadd 				 * Setup the link pointer to be the
52389be82a42SAdrian Chadd 				 * _last_ buffer/descriptor in the list.
52399be82a42SAdrian Chadd 				 * If there's nothing in the list, set it
52409be82a42SAdrian Chadd 				 * to NULL.
52418328d6e4SAdrian Chadd 				 */
52429be82a42SAdrian Chadd 				bf_last = ATH_TXQ_LAST(&sc->sc_txq[i],
52439be82a42SAdrian Chadd 				    axq_q_s);
52449be82a42SAdrian Chadd 				if (bf_last != NULL) {
52459be82a42SAdrian Chadd 					ath_hal_gettxdesclinkptr(ah,
52469be82a42SAdrian Chadd 					    bf_last->bf_lastds,
52479be82a42SAdrian Chadd 					    &sc->sc_txq[i].axq_link);
52489be82a42SAdrian Chadd 				} else {
52498328d6e4SAdrian Chadd 					sc->sc_txq[i].axq_link = NULL;
52509be82a42SAdrian Chadd 				}
52518328d6e4SAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
52528328d6e4SAdrian Chadd 			} else
5253c42a7b7eSSam Leffler 				ath_tx_draintxq(sc, &sc->sc_txq[i]);
5254ef27340cSAdrian Chadd 		}
5255ef27340cSAdrian Chadd 	}
52564a3ac3fcSSam Leffler #ifdef ATH_DEBUG
52574a3ac3fcSSam Leffler 	if (sc->sc_debug & ATH_DEBUG_RESET) {
52586b349e5aSAdrian Chadd 		struct ath_buf *bf = TAILQ_FIRST(&sc->sc_bbuf);
52594a3ac3fcSSam Leffler 		if (bf != NULL && bf->bf_m != NULL) {
52606902009eSSam Leffler 			ath_printtxbuf(sc, bf, sc->sc_bhalq, 0,
52616edf1dc7SAdrian Chadd 				ath_hal_txprocdesc(ah, bf->bf_lastds,
526265f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
5263e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ifp->if_l2com,
5264e40b6ab1SSam Leffler 			    mtod(bf->bf_m, const uint8_t *), bf->bf_m->m_len,
5265e40b6ab1SSam Leffler 			    0, -1);
52664a3ac3fcSSam Leffler 		}
52674a3ac3fcSSam Leffler 	}
52684a3ac3fcSSam Leffler #endif /* ATH_DEBUG */
5269e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
527013f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5271e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
52722e986da5SSam Leffler 	sc->sc_wd_timer = 0;
52735591b213SSam Leffler }
52745591b213SSam Leffler 
52755591b213SSam Leffler /*
5276c42a7b7eSSam Leffler  * Update internal state after a channel change.
5277c42a7b7eSSam Leffler  */
5278c42a7b7eSSam Leffler static void
5279c42a7b7eSSam Leffler ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
5280c42a7b7eSSam Leffler {
5281c42a7b7eSSam Leffler 	enum ieee80211_phymode mode;
5282c42a7b7eSSam Leffler 
5283c42a7b7eSSam Leffler 	/*
5284c42a7b7eSSam Leffler 	 * Change channels and update the h/w rate map
5285c42a7b7eSSam Leffler 	 * if we're switching; e.g. 11a to 11b/g.
5286c42a7b7eSSam Leffler 	 */
528768e8e04eSSam Leffler 	mode = ieee80211_chan2mode(chan);
5288c42a7b7eSSam Leffler 	if (mode != sc->sc_curmode)
5289c42a7b7eSSam Leffler 		ath_setcurmode(sc, mode);
529059efa8b5SSam Leffler 	sc->sc_curchan = chan;
5291c42a7b7eSSam Leffler }
5292c42a7b7eSSam Leffler 
5293c42a7b7eSSam Leffler /*
52945591b213SSam Leffler  * Set/change channels.  If the channel is really being changed,
52954fa8d4efSDaniel Eischen  * it's done by resetting the chip.  To accomplish this we must
52965591b213SSam Leffler  * first cleanup any pending DMA, then restart stuff after a la
52975591b213SSam Leffler  * ath_init.
52985591b213SSam Leffler  */
52995591b213SSam Leffler static int
53005591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
53015591b213SSam Leffler {
5302b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
5303b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
53045591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5305ef27340cSAdrian Chadd 	int ret = 0;
5306ef27340cSAdrian Chadd 
5307ef27340cSAdrian Chadd 	/* Treat this as an interface reset */
5308d52f7132SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
5309d52f7132SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
5310d52f7132SAdrian Chadd 
5311d52f7132SAdrian Chadd 	/* (Try to) stop TX/RX from occuring */
5312d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
5313d52f7132SAdrian Chadd 
5314ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5315904e385eSAdrian Chadd 
5316904e385eSAdrian Chadd 	/* Stop new RX/TX/interrupt completion */
5317ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
5318ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
5319ef27340cSAdrian Chadd 		    __func__);
5320ee321975SAdrian Chadd 	}
5321904e385eSAdrian Chadd 
5322904e385eSAdrian Chadd 	ath_hal_intrset(ah, 0);
5323904e385eSAdrian Chadd 
5324904e385eSAdrian Chadd 	/* Stop pending RX/TX completion */
5325904e385eSAdrian Chadd 	ath_txrx_stop_locked(sc);
5326904e385eSAdrian Chadd 
5327ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5328c42a7b7eSSam Leffler 
532959efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz, flags 0x%x)\n",
533059efa8b5SSam Leffler 	    __func__, ieee80211_chan2ieee(ic, chan),
533159efa8b5SSam Leffler 	    chan->ic_freq, chan->ic_flags);
533259efa8b5SSam Leffler 	if (chan != sc->sc_curchan) {
5333c42a7b7eSSam Leffler 		HAL_STATUS status;
53345591b213SSam Leffler 		/*
53355591b213SSam Leffler 		 * To switch channels clear any pending DMA operations;
53365591b213SSam Leffler 		 * wait long enough for the RX fifo to drain, reset the
53375591b213SSam Leffler 		 * hardware at the new frequency, and then re-enable
53385591b213SSam Leffler 		 * the relevant bits of the h/w.
53395591b213SSam Leffler 		 */
5340ef27340cSAdrian Chadd #if 0
53415591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
5342ef27340cSAdrian Chadd #endif
53439a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* turn off frame recv */
53449a842e8bSAdrian Chadd 		/*
53459a842e8bSAdrian Chadd 		 * First, handle completed TX/RX frames.
53469a842e8bSAdrian Chadd 		 */
5347f8cc9b09SAdrian Chadd 		ath_rx_flush(sc);
53489a842e8bSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_NOLOSS);
53499a842e8bSAdrian Chadd 		/*
53509a842e8bSAdrian Chadd 		 * Next, flush the non-scheduled frames.
53519a842e8bSAdrian Chadd 		 */
5352517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_FULL);	/* clear pending tx frames */
53539a842e8bSAdrian Chadd 
53546322256bSAdrian Chadd 		ath_update_chainmasks(sc, chan);
53556322256bSAdrian Chadd 		ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
53566322256bSAdrian Chadd 		    sc->sc_cur_rxchainmask);
535759efa8b5SSam Leffler 		if (!ath_hal_reset(ah, sc->sc_opmode, chan, AH_TRUE, &status)) {
5358b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to reset "
535979649302SGavin Atkinson 			    "channel %u (%u MHz, flags 0x%x), hal status %u\n",
536059efa8b5SSam Leffler 			    __func__, ieee80211_chan2ieee(ic, chan),
536159efa8b5SSam Leffler 			    chan->ic_freq, chan->ic_flags, status);
5362ef27340cSAdrian Chadd 			ret = EIO;
5363ef27340cSAdrian Chadd 			goto finish;
53645591b213SSam Leffler 		}
5365c59005e9SSam Leffler 		sc->sc_diversity = ath_hal_getdiversity(ah);
5366c42a7b7eSSam Leffler 
536748237774SAdrian Chadd 		/* Let DFS at it in case it's a DFS channel */
5368398bca2eSAdrian Chadd 		ath_dfs_radar_enable(sc, chan);
536948237774SAdrian Chadd 
53709af351f9SAdrian Chadd 		/* Let spectral at in case spectral is enabled */
53719af351f9SAdrian Chadd 		ath_spectral_enable(sc, chan);
53729af351f9SAdrian Chadd 
53735591b213SSam Leffler 		/*
5374b70f530bSAdrian Chadd 		 * Let bluetooth coexistence at in case it's needed for this
5375b70f530bSAdrian Chadd 		 * channel
5376b70f530bSAdrian Chadd 		 */
5377b70f530bSAdrian Chadd 		ath_btcoex_enable(sc, ic->ic_curchan);
5378b70f530bSAdrian Chadd 
5379b70f530bSAdrian Chadd 		/*
5380dd6a574eSAdrian Chadd 		 * If we're doing TDMA, enforce the TXOP limitation for chips
5381dd6a574eSAdrian Chadd 		 * that support it.
5382dd6a574eSAdrian Chadd 		 */
5383dd6a574eSAdrian Chadd 		if (sc->sc_hasenforcetxop && sc->sc_tdma)
5384dd6a574eSAdrian Chadd 			ath_hal_setenforcetxop(sc->sc_ah, 1);
5385dd6a574eSAdrian Chadd 		else
5386dd6a574eSAdrian Chadd 			ath_hal_setenforcetxop(sc->sc_ah, 0);
5387dd6a574eSAdrian Chadd 
5388dd6a574eSAdrian Chadd 		/*
53895591b213SSam Leffler 		 * Re-enable rx framework.
53905591b213SSam Leffler 		 */
53915591b213SSam Leffler 		if (ath_startrecv(sc) != 0) {
5392b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to restart recv logic\n",
5393b032f27cSSam Leffler 			    __func__);
5394ef27340cSAdrian Chadd 			ret = EIO;
5395ef27340cSAdrian Chadd 			goto finish;
53965591b213SSam Leffler 		}
53975591b213SSam Leffler 
53985591b213SSam Leffler 		/*
53995591b213SSam Leffler 		 * Change channels and update the h/w rate map
54005591b213SSam Leffler 		 * if we're switching; e.g. 11a to 11b/g.
54015591b213SSam Leffler 		 */
5402c42a7b7eSSam Leffler 		ath_chan_change(sc, chan);
54030a915fadSSam Leffler 
54040a915fadSSam Leffler 		/*
54052fd9aabbSAdrian Chadd 		 * Reset clears the beacon timers; reset them
54062fd9aabbSAdrian Chadd 		 * here if needed.
54072fd9aabbSAdrian Chadd 		 */
54082fd9aabbSAdrian Chadd 		if (sc->sc_beacons) {		/* restart beacons */
54092fd9aabbSAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
54102fd9aabbSAdrian Chadd 			if (sc->sc_tdma)
54112fd9aabbSAdrian Chadd 				ath_tdma_config(sc, NULL);
54122fd9aabbSAdrian Chadd 			else
54132fd9aabbSAdrian Chadd #endif
54142fd9aabbSAdrian Chadd 			ath_beacon_config(sc, NULL);
54152fd9aabbSAdrian Chadd 		}
54162fd9aabbSAdrian Chadd 
54172fd9aabbSAdrian Chadd 		/*
54180a915fadSSam Leffler 		 * Re-enable interrupts.
54190a915fadSSam Leffler 		 */
5420e78719adSAdrian Chadd #if 0
54210a915fadSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
5422ef27340cSAdrian Chadd #endif
54235591b213SSam Leffler 	}
5424ef27340cSAdrian Chadd 
5425ef27340cSAdrian Chadd finish:
5426ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5427ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
5428ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
5429ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
5430ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5431ef27340cSAdrian Chadd 
5432e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
5433ef27340cSAdrian Chadd 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5434e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
5435ef27340cSAdrian Chadd 	ath_txrx_start(sc);
5436ef27340cSAdrian Chadd 	/* XXX ath_start? */
5437ef27340cSAdrian Chadd 
5438ef27340cSAdrian Chadd 	return ret;
54395591b213SSam Leffler }
54405591b213SSam Leffler 
54415591b213SSam Leffler /*
54425591b213SSam Leffler  * Periodically recalibrate the PHY to account
54435591b213SSam Leffler  * for temperature/environment changes.
54445591b213SSam Leffler  */
54455591b213SSam Leffler static void
54465591b213SSam Leffler ath_calibrate(void *arg)
54475591b213SSam Leffler {
54485591b213SSam Leffler 	struct ath_softc *sc = arg;
54495591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
54502dc7fcc4SSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
54518d91de92SSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
5452943e37a1SAdrian Chadd 	HAL_BOOL longCal, isCalDone = AH_TRUE;
5453a108ab63SAdrian Chadd 	HAL_BOOL aniCal, shortCal = AH_FALSE;
54542dc7fcc4SSam Leffler 	int nextcal;
54555591b213SSam Leffler 
5456f5c30c4eSAdrian Chadd 	/*
5457f5c30c4eSAdrian Chadd 	 * Force the hardware awake for ANI work.
5458f5c30c4eSAdrian Chadd 	 */
5459f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
5460f5c30c4eSAdrian Chadd 
5461f5c30c4eSAdrian Chadd 	/* Skip trying to do this if we're in reset */
5462f5c30c4eSAdrian Chadd 	if (sc->sc_inreset_cnt)
5463f5c30c4eSAdrian Chadd 		goto restart;
5464f5c30c4eSAdrian Chadd 
54658d91de92SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SCAN)	/* defer, off channel */
54668d91de92SSam Leffler 		goto restart;
54672dc7fcc4SSam Leffler 	longCal = (ticks - sc->sc_lastlongcal >= ath_longcalinterval*hz);
5468a108ab63SAdrian Chadd 	aniCal = (ticks - sc->sc_lastani >= ath_anicalinterval*hz/1000);
5469a108ab63SAdrian Chadd 	if (sc->sc_doresetcal)
5470a108ab63SAdrian Chadd 		shortCal = (ticks - sc->sc_lastshortcal >= ath_shortcalinterval*hz/1000);
5471a108ab63SAdrian Chadd 
5472a108ab63SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: shortCal=%d; longCal=%d; aniCal=%d\n", __func__, shortCal, longCal, aniCal);
5473a108ab63SAdrian Chadd 	if (aniCal) {
5474a108ab63SAdrian Chadd 		sc->sc_stats.ast_ani_cal++;
5475a108ab63SAdrian Chadd 		sc->sc_lastani = ticks;
5476a108ab63SAdrian Chadd 		ath_hal_ani_poll(ah, sc->sc_curchan);
5477a108ab63SAdrian Chadd 	}
5478a108ab63SAdrian Chadd 
54792dc7fcc4SSam Leffler 	if (longCal) {
54805591b213SSam Leffler 		sc->sc_stats.ast_per_cal++;
54818197f57eSAdrian Chadd 		sc->sc_lastlongcal = ticks;
54825591b213SSam Leffler 		if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
54835591b213SSam Leffler 			/*
54845591b213SSam Leffler 			 * Rfgain is out of bounds, reset the chip
54855591b213SSam Leffler 			 * to load new gain values.
54865591b213SSam Leffler 			 */
5487370572d9SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
5488370572d9SSam Leffler 				"%s: rfgain change\n", __func__);
54895591b213SSam Leffler 			sc->sc_stats.ast_per_rfgain++;
5490ef27340cSAdrian Chadd 			sc->sc_resetcal = 0;
5491ef27340cSAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
5492d52f7132SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
5493d52f7132SAdrian Chadd 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
5494f5c30c4eSAdrian Chadd 			ath_power_restore_power_state(sc);
5495ef27340cSAdrian Chadd 			return;
54965591b213SSam Leffler 		}
54972dc7fcc4SSam Leffler 		/*
54982dc7fcc4SSam Leffler 		 * If this long cal is after an idle period, then
54992dc7fcc4SSam Leffler 		 * reset the data collection state so we start fresh.
55002dc7fcc4SSam Leffler 		 */
55012dc7fcc4SSam Leffler 		if (sc->sc_resetcal) {
550259efa8b5SSam Leffler 			(void) ath_hal_calreset(ah, sc->sc_curchan);
55032dc7fcc4SSam Leffler 			sc->sc_lastcalreset = ticks;
5504a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
55052dc7fcc4SSam Leffler 			sc->sc_resetcal = 0;
5506a108ab63SAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
55072dc7fcc4SSam Leffler 		}
55082dc7fcc4SSam Leffler 	}
5509a108ab63SAdrian Chadd 
5510a108ab63SAdrian Chadd 	/* Only call if we're doing a short/long cal, not for ANI calibration */
5511a108ab63SAdrian Chadd 	if (shortCal || longCal) {
5512943e37a1SAdrian Chadd 		isCalDone = AH_FALSE;
551359efa8b5SSam Leffler 		if (ath_hal_calibrateN(ah, sc->sc_curchan, longCal, &isCalDone)) {
55142dc7fcc4SSam Leffler 			if (longCal) {
55152dc7fcc4SSam Leffler 				/*
55162dc7fcc4SSam Leffler 				 * Calibrate noise floor data again in case of change.
55172dc7fcc4SSam Leffler 				 */
55182dc7fcc4SSam Leffler 				ath_hal_process_noisefloor(ah);
55192dc7fcc4SSam Leffler 			}
55202dc7fcc4SSam Leffler 		} else {
5521c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
5522c42a7b7eSSam Leffler 				"%s: calibration of channel %u failed\n",
552359efa8b5SSam Leffler 				__func__, sc->sc_curchan->ic_freq);
55245591b213SSam Leffler 			sc->sc_stats.ast_per_calfail++;
55255591b213SSam Leffler 		}
5526a108ab63SAdrian Chadd 		if (shortCal)
5527a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
5528a108ab63SAdrian Chadd 	}
55292dc7fcc4SSam Leffler 	if (!isCalDone) {
55308d91de92SSam Leffler restart:
55317b0c77ecSSam Leffler 		/*
55322dc7fcc4SSam Leffler 		 * Use a shorter interval to potentially collect multiple
55332dc7fcc4SSam Leffler 		 * data samples required to complete calibration.  Once
55342dc7fcc4SSam Leffler 		 * we're told the work is done we drop back to a longer
55352dc7fcc4SSam Leffler 		 * interval between requests.  We're more aggressive doing
55362dc7fcc4SSam Leffler 		 * work when operating as an AP to improve operation right
55372dc7fcc4SSam Leffler 		 * after startup.
55387b0c77ecSSam Leffler 		 */
5539a108ab63SAdrian Chadd 		sc->sc_lastshortcal = ticks;
5540a108ab63SAdrian Chadd 		nextcal = ath_shortcalinterval*hz/1000;
55412dc7fcc4SSam Leffler 		if (sc->sc_opmode != HAL_M_HOSTAP)
55422dc7fcc4SSam Leffler 			nextcal *= 10;
5543a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_TRUE;
55442dc7fcc4SSam Leffler 	} else {
5545a108ab63SAdrian Chadd 		/* nextcal should be the shortest time for next event */
55462dc7fcc4SSam Leffler 		nextcal = ath_longcalinterval*hz;
55472dc7fcc4SSam Leffler 		if (sc->sc_lastcalreset == 0)
55482dc7fcc4SSam Leffler 			sc->sc_lastcalreset = sc->sc_lastlongcal;
55492dc7fcc4SSam Leffler 		else if (ticks - sc->sc_lastcalreset >= ath_resetcalinterval*hz)
55502dc7fcc4SSam Leffler 			sc->sc_resetcal = 1;	/* setup reset next trip */
5551a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_FALSE;
5552bd5a9920SSam Leffler 	}
5553a108ab63SAdrian Chadd 	/* ANI calibration may occur more often than short/long/resetcal */
5554a108ab63SAdrian Chadd 	if (ath_anicalinterval > 0)
5555a108ab63SAdrian Chadd 		nextcal = MIN(nextcal, ath_anicalinterval*hz/1000);
5556bd5a9920SSam Leffler 
55572dc7fcc4SSam Leffler 	if (nextcal != 0) {
55582dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: next +%u (%sisCalDone)\n",
55592dc7fcc4SSam Leffler 		    __func__, nextcal, isCalDone ? "" : "!");
55602dc7fcc4SSam Leffler 		callout_reset(&sc->sc_cal_ch, nextcal, ath_calibrate, sc);
55612dc7fcc4SSam Leffler 	} else {
55622dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: calibration disabled\n",
55632dc7fcc4SSam Leffler 		    __func__);
55642dc7fcc4SSam Leffler 		/* NB: don't rearm timer */
55652dc7fcc4SSam Leffler 	}
5566f5c30c4eSAdrian Chadd 	/*
5567f5c30c4eSAdrian Chadd 	 * Restore power state now that we're done.
5568f5c30c4eSAdrian Chadd 	 */
5569f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
55705591b213SSam Leffler }
55715591b213SSam Leffler 
557268e8e04eSSam Leffler static void
557368e8e04eSSam Leffler ath_scan_start(struct ieee80211com *ic)
557468e8e04eSSam Leffler {
557568e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
557668e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
557768e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
557868e8e04eSSam Leffler 	u_int32_t rfilt;
557968e8e04eSSam Leffler 
558068e8e04eSSam Leffler 	/* XXX calibration timer? */
558168e8e04eSSam Leffler 
5582c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
558368e8e04eSSam Leffler 	sc->sc_scanning = 1;
558468e8e04eSSam Leffler 	sc->sc_syncbeacon = 0;
558568e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5586c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
5587c98cefc5SAdrian Chadd 
5588c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
558968e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
559068e8e04eSSam Leffler 	ath_hal_setassocid(ah, ifp->if_broadcastaddr, 0);
5591c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
559268e8e04eSSam Leffler 
559368e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0\n",
559468e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(ifp->if_broadcastaddr));
559568e8e04eSSam Leffler }
559668e8e04eSSam Leffler 
559768e8e04eSSam Leffler static void
559868e8e04eSSam Leffler ath_scan_end(struct ieee80211com *ic)
559968e8e04eSSam Leffler {
560068e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
560168e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
560268e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
560368e8e04eSSam Leffler 	u_int32_t rfilt;
560468e8e04eSSam Leffler 
5605c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
560668e8e04eSSam Leffler 	sc->sc_scanning = 0;
560768e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5608c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
5609c98cefc5SAdrian Chadd 
5610c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
561168e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
561268e8e04eSSam Leffler 	ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
561368e8e04eSSam Leffler 
561468e8e04eSSam Leffler 	ath_hal_process_noisefloor(ah);
5615c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
561668e8e04eSSam Leffler 
561768e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
561868e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(sc->sc_curbssid),
561968e8e04eSSam Leffler 		 sc->sc_curaid);
562068e8e04eSSam Leffler }
562168e8e04eSSam Leffler 
5622fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
5623e7200579SAdrian Chadd /*
5624e7200579SAdrian Chadd  * For now, just do a channel change.
5625e7200579SAdrian Chadd  *
5626e7200579SAdrian Chadd  * Later, we'll go through the hard slog of suspending tx/rx, changing rate
5627e7200579SAdrian Chadd  * control state and resetting the hardware without dropping frames out
5628e7200579SAdrian Chadd  * of the queue.
5629e7200579SAdrian Chadd  *
5630e7200579SAdrian Chadd  * The unfortunate trouble here is making absolutely sure that the
5631e7200579SAdrian Chadd  * channel width change has propagated enough so the hardware
5632e7200579SAdrian Chadd  * absolutely isn't handed bogus frames for it's current operating
5633e7200579SAdrian Chadd  * mode. (Eg, 40MHz frames in 20MHz mode.) Since TX and RX can and
5634e7200579SAdrian Chadd  * does occur in parallel, we need to make certain we've blocked
5635e7200579SAdrian Chadd  * any further ongoing TX (and RX, that can cause raw TX)
5636e7200579SAdrian Chadd  * before we do this.
5637e7200579SAdrian Chadd  */
5638e7200579SAdrian Chadd static void
5639e7200579SAdrian Chadd ath_update_chw(struct ieee80211com *ic)
5640e7200579SAdrian Chadd {
5641e7200579SAdrian Chadd 	struct ifnet *ifp = ic->ic_ifp;
5642e7200579SAdrian Chadd 	struct ath_softc *sc = ifp->if_softc;
5643e7200579SAdrian Chadd 
5644e7200579SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: called\n", __func__);
5645e7200579SAdrian Chadd 	ath_set_channel(ic);
5646e7200579SAdrian Chadd }
5647fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
5648e7200579SAdrian Chadd 
564968e8e04eSSam Leffler static void
565068e8e04eSSam Leffler ath_set_channel(struct ieee80211com *ic)
565168e8e04eSSam Leffler {
565268e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
565368e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
565468e8e04eSSam Leffler 
5655f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
5656f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
5657f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
5658f5c30c4eSAdrian Chadd 
565968e8e04eSSam Leffler 	(void) ath_chan_set(sc, ic->ic_curchan);
566068e8e04eSSam Leffler 	/*
566168e8e04eSSam Leffler 	 * If we are returning to our bss channel then mark state
566268e8e04eSSam Leffler 	 * so the next recv'd beacon's tsf will be used to sync the
566368e8e04eSSam Leffler 	 * beacon timers.  Note that since we only hear beacons in
566468e8e04eSSam Leffler 	 * sta/ibss mode this has no effect in other operating modes.
566568e8e04eSSam Leffler 	 */
5666a887b1e3SAdrian Chadd 	ATH_LOCK(sc);
566768e8e04eSSam Leffler 	if (!sc->sc_scanning && ic->ic_curchan == ic->ic_bsschan)
566868e8e04eSSam Leffler 		sc->sc_syncbeacon = 1;
5669f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
5670a887b1e3SAdrian Chadd 	ATH_UNLOCK(sc);
567168e8e04eSSam Leffler }
567268e8e04eSSam Leffler 
5673b032f27cSSam Leffler /*
5674b032f27cSSam Leffler  * Walk the vap list and check if there any vap's in RUN state.
5675b032f27cSSam Leffler  */
56765591b213SSam Leffler static int
5677b032f27cSSam Leffler ath_isanyrunningvaps(struct ieee80211vap *this)
56785591b213SSam Leffler {
5679b032f27cSSam Leffler 	struct ieee80211com *ic = this->iv_ic;
5680b032f27cSSam Leffler 	struct ieee80211vap *vap;
5681b032f27cSSam Leffler 
5682b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
5683b032f27cSSam Leffler 
5684b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
5685309a3e45SSam Leffler 		if (vap != this && vap->iv_state >= IEEE80211_S_RUN)
5686b032f27cSSam Leffler 			return 1;
5687b032f27cSSam Leffler 	}
5688b032f27cSSam Leffler 	return 0;
5689b032f27cSSam Leffler }
5690b032f27cSSam Leffler 
5691b032f27cSSam Leffler static int
5692b032f27cSSam Leffler ath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
5693b032f27cSSam Leffler {
5694b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
5695b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5696b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
569745bbf62fSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5698b032f27cSSam Leffler 	struct ieee80211_node *ni = NULL;
569968e8e04eSSam Leffler 	int i, error, stamode;
57005591b213SSam Leffler 	u_int32_t rfilt;
5701f52efb6dSAdrian Chadd 	int csa_run_transition = 0;
5702f5c30c4eSAdrian Chadd 	enum ieee80211_state ostate = vap->iv_state;
5703a74ebfe5SAdrian Chadd 
57045591b213SSam Leffler 	static const HAL_LED_STATE leds[] = {
57055591b213SSam Leffler 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
57065591b213SSam Leffler 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
57075591b213SSam Leffler 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
57085591b213SSam Leffler 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
570977d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CAC */
57105591b213SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
571177d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CSA */
571277d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_SLEEP */
57135591b213SSam Leffler 	};
57145591b213SSam Leffler 
5715c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
5716f5c30c4eSAdrian Chadd 		ieee80211_state_name[ostate],
5717c42a7b7eSSam Leffler 		ieee80211_state_name[nstate]);
57185591b213SSam Leffler 
5719107fdf96SAdrian Chadd 	/*
5720107fdf96SAdrian Chadd 	 * net80211 _should_ have the comlock asserted at this point.
5721107fdf96SAdrian Chadd 	 * There are some comments around the calls to vap->iv_newstate
5722107fdf96SAdrian Chadd 	 * which indicate that it (newstate) may end up dropping the
5723107fdf96SAdrian Chadd 	 * lock.  This and the subsequent lock assert check after newstate
5724107fdf96SAdrian Chadd 	 * are an attempt to catch these and figure out how/why.
5725107fdf96SAdrian Chadd 	 */
5726107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
5727107fdf96SAdrian Chadd 
5728f5c30c4eSAdrian Chadd 	/* Before we touch the hardware - wake it up */
5729f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
5730*7d567ed6SAdrian Chadd 	/*
5731*7d567ed6SAdrian Chadd 	 * If the NIC is in anything other than SLEEP state,
5732*7d567ed6SAdrian Chadd 	 * we need to ensure that self-generated frames are
5733*7d567ed6SAdrian Chadd 	 * set for PWRMGT=0.  Otherwise we may end up with
5734*7d567ed6SAdrian Chadd 	 * strange situations.
5735*7d567ed6SAdrian Chadd 	 *
5736*7d567ed6SAdrian Chadd 	 * XXX TODO: is this actually the case? :-)
5737*7d567ed6SAdrian Chadd 	 */
5738*7d567ed6SAdrian Chadd 	if (nstate != IEEE80211_S_SLEEP)
5739*7d567ed6SAdrian Chadd 		ath_power_setselfgen(sc, HAL_PM_AWAKE);
5740*7d567ed6SAdrian Chadd 
5741*7d567ed6SAdrian Chadd 	/*
5742*7d567ed6SAdrian Chadd 	 * Now, wake the thing up.
5743*7d567ed6SAdrian Chadd 	 */
5744f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
5745f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
5746f5c30c4eSAdrian Chadd 
5747f5c30c4eSAdrian Chadd 	if (ostate == IEEE80211_S_CSA && nstate == IEEE80211_S_RUN)
5748f52efb6dSAdrian Chadd 		csa_run_transition = 1;
5749f52efb6dSAdrian Chadd 
57502e986da5SSam Leffler 	callout_drain(&sc->sc_cal_ch);
57515591b213SSam Leffler 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
57525591b213SSam Leffler 
5753b032f27cSSam Leffler 	if (nstate == IEEE80211_S_SCAN) {
575458769f58SSam Leffler 		/*
5755b032f27cSSam Leffler 		 * Scanning: turn off beacon miss and don't beacon.
5756b032f27cSSam Leffler 		 * Mark beacon state so when we reach RUN state we'll
5757b032f27cSSam Leffler 		 * [re]setup beacons.  Unblock the task q thread so
5758b032f27cSSam Leffler 		 * deferred interrupt processing is done.
575958769f58SSam Leffler 		 */
5760f5c30c4eSAdrian Chadd 
5761f5c30c4eSAdrian Chadd 		/* Ensure we stay awake during scan */
5762f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
5763*7d567ed6SAdrian Chadd 		ath_power_setselfgen(sc, HAL_PM_AWAKE);
5764f5c30c4eSAdrian Chadd 		ath_power_setpower(sc, HAL_PM_AWAKE);
5765f5c30c4eSAdrian Chadd 		ATH_UNLOCK(sc);
5766f5c30c4eSAdrian Chadd 
5767b032f27cSSam Leffler 		ath_hal_intrset(ah,
5768b032f27cSSam Leffler 		    sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
57695591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5770b032f27cSSam Leffler 		sc->sc_beacons = 0;
5771b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
57725591b213SSam Leffler 	}
57735591b213SSam Leffler 
577480767531SAdrian Chadd 	ni = ieee80211_ref_node(vap->iv_bss);
577568e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5776b032f27cSSam Leffler 	stamode = (vap->iv_opmode == IEEE80211_M_STA ||
57777b916f89SSam Leffler 		   vap->iv_opmode == IEEE80211_M_AHDEMO ||
5778b032f27cSSam Leffler 		   vap->iv_opmode == IEEE80211_M_IBSS);
5779f5c30c4eSAdrian Chadd 
5780f5c30c4eSAdrian Chadd 	/*
5781f5c30c4eSAdrian Chadd 	 * XXX Dont need to do this (and others) if we've transitioned
5782f5c30c4eSAdrian Chadd 	 * from SLEEP->RUN.
5783f5c30c4eSAdrian Chadd 	 */
578468e8e04eSSam Leffler 	if (stamode && nstate == IEEE80211_S_RUN) {
578568e8e04eSSam Leffler 		sc->sc_curaid = ni->ni_associd;
578668e8e04eSSam Leffler 		IEEE80211_ADDR_COPY(sc->sc_curbssid, ni->ni_bssid);
5787b032f27cSSam Leffler 		ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
5788b032f27cSSam Leffler 	}
578968e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
5790b032f27cSSam Leffler 	   __func__, rfilt, ether_sprintf(sc->sc_curbssid), sc->sc_curaid);
579168e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
579268e8e04eSSam Leffler 
5793b032f27cSSam Leffler 	/* XXX is this to restore keycache on resume? */
5794b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_STA &&
5795b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY)) {
57965591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
57975591b213SSam Leffler 			if (ath_hal_keyisvalid(ah, i))
579868e8e04eSSam Leffler 				ath_hal_keysetmac(ah, i, ni->ni_bssid);
57995591b213SSam Leffler 	}
5800b032f27cSSam Leffler 
5801b032f27cSSam Leffler 	/*
5802b032f27cSSam Leffler 	 * Invoke the parent method to do net80211 work.
5803b032f27cSSam Leffler 	 */
5804b032f27cSSam Leffler 	error = avp->av_newstate(vap, nstate, arg);
5805b032f27cSSam Leffler 	if (error != 0)
5806b032f27cSSam Leffler 		goto bad;
5807c42a7b7eSSam Leffler 
5808107fdf96SAdrian Chadd 	/*
5809107fdf96SAdrian Chadd 	 * See above: ensure av_newstate() doesn't drop the lock
5810107fdf96SAdrian Chadd 	 * on us.
5811107fdf96SAdrian Chadd 	 */
5812107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
5813107fdf96SAdrian Chadd 
581468e8e04eSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
5815b032f27cSSam Leffler 		/* NB: collect bss node again, it may have changed */
581680767531SAdrian Chadd 		ieee80211_free_node(ni);
581780767531SAdrian Chadd 		ni = ieee80211_ref_node(vap->iv_bss);
58185591b213SSam Leffler 
5819b032f27cSSam Leffler 		DPRINTF(sc, ATH_DEBUG_STATE,
5820b032f27cSSam Leffler 		    "%s(RUN): iv_flags 0x%08x bintvl %d bssid %s "
5821b032f27cSSam Leffler 		    "capinfo 0x%04x chan %d\n", __func__,
5822b032f27cSSam Leffler 		    vap->iv_flags, ni->ni_intval, ether_sprintf(ni->ni_bssid),
5823b032f27cSSam Leffler 		    ni->ni_capinfo, ieee80211_chan2ieee(ic, ic->ic_curchan));
5824b032f27cSSam Leffler 
5825b032f27cSSam Leffler 		switch (vap->iv_opmode) {
5826584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
582710ad9a77SSam Leffler 		case IEEE80211_M_AHDEMO:
582810ad9a77SSam Leffler 			if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
582910ad9a77SSam Leffler 				break;
583010ad9a77SSam Leffler 			/* fall thru... */
583110ad9a77SSam Leffler #endif
5832e8fd88a3SSam Leffler 		case IEEE80211_M_HOSTAP:
5833e8fd88a3SSam Leffler 		case IEEE80211_M_IBSS:
583459aa14a9SRui Paulo 		case IEEE80211_M_MBSS:
58355591b213SSam Leffler 			/*
5836e8fd88a3SSam Leffler 			 * Allocate and setup the beacon frame.
5837e8fd88a3SSam Leffler 			 *
5838f818612bSSam Leffler 			 * Stop any previous beacon DMA.  This may be
5839f818612bSSam Leffler 			 * necessary, for example, when an ibss merge
5840f818612bSSam Leffler 			 * causes reconfiguration; there will be a state
5841f818612bSSam Leffler 			 * transition from RUN->RUN that means we may
5842f818612bSSam Leffler 			 * be called with beacon transmission active.
5843f818612bSSam Leffler 			 */
5844f818612bSSam Leffler 			ath_hal_stoptxdma(ah, sc->sc_bhalq);
5845b032f27cSSam Leffler 
58465591b213SSam Leffler 			error = ath_beacon_alloc(sc, ni);
58475591b213SSam Leffler 			if (error != 0)
58485591b213SSam Leffler 				goto bad;
58497a04dc27SSam Leffler 			/*
585080d939bfSSam Leffler 			 * If joining an adhoc network defer beacon timer
585180d939bfSSam Leffler 			 * configuration to the next beacon frame so we
585280d939bfSSam Leffler 			 * have a current TSF to use.  Otherwise we're
5853b032f27cSSam Leffler 			 * starting an ibss/bss so there's no need to delay;
5854b032f27cSSam Leffler 			 * if this is the first vap moving to RUN state, then
5855b032f27cSSam Leffler 			 * beacon state needs to be [re]configured.
58567a04dc27SSam Leffler 			 */
5857b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_IBSS &&
5858b032f27cSSam Leffler 			    ni->ni_tstamp.tsf != 0) {
585980d939bfSSam Leffler 				sc->sc_syncbeacon = 1;
5860b032f27cSSam Leffler 			} else if (!sc->sc_beacons) {
5861584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
586210ad9a77SSam Leffler 				if (vap->iv_caps & IEEE80211_C_TDMA)
586310ad9a77SSam Leffler 					ath_tdma_config(sc, vap);
586410ad9a77SSam Leffler 				else
586510ad9a77SSam Leffler #endif
5866b032f27cSSam Leffler 					ath_beacon_config(sc, vap);
5867b032f27cSSam Leffler 				sc->sc_beacons = 1;
5868b032f27cSSam Leffler 			}
5869e8fd88a3SSam Leffler 			break;
5870e8fd88a3SSam Leffler 		case IEEE80211_M_STA:
5871e8fd88a3SSam Leffler 			/*
587280d939bfSSam Leffler 			 * Defer beacon timer configuration to the next
587380d939bfSSam Leffler 			 * beacon frame so we have a current TSF to use
587480d939bfSSam Leffler 			 * (any TSF collected when scanning is likely old).
5875f52efb6dSAdrian Chadd 			 * However if it's due to a CSA -> RUN transition,
5876f52efb6dSAdrian Chadd 			 * force a beacon update so we pick up a lack of
5877f52efb6dSAdrian Chadd 			 * beacons from an AP in CAC and thus force a
5878f52efb6dSAdrian Chadd 			 * scan.
5879a74ebfe5SAdrian Chadd 			 *
5880a74ebfe5SAdrian Chadd 			 * And, there's also corner cases here where
5881a74ebfe5SAdrian Chadd 			 * after a scan, the AP may have disappeared.
5882a74ebfe5SAdrian Chadd 			 * In that case, we may not receive an actual
5883a74ebfe5SAdrian Chadd 			 * beacon to update the beacon timer and thus we
5884a74ebfe5SAdrian Chadd 			 * won't get notified of the missing beacons.
58857a04dc27SSam Leffler 			 */
5886f5c30c4eSAdrian Chadd 			if (ostate != IEEE80211_S_RUN &&
5887f5c30c4eSAdrian Chadd 			    ostate != IEEE80211_S_SLEEP) {
5888f5c30c4eSAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_BEACON,
5889f5c30c4eSAdrian Chadd 				    "%s: STA; syncbeacon=1\n", __func__);
589080d939bfSSam Leffler 				sc->sc_syncbeacon = 1;
5891f5c30c4eSAdrian Chadd 
5892f52efb6dSAdrian Chadd 				if (csa_run_transition)
5893f52efb6dSAdrian Chadd 					ath_beacon_config(sc, vap);
5894a74ebfe5SAdrian Chadd 
5895a74ebfe5SAdrian Chadd 			/*
5896a74ebfe5SAdrian Chadd 			 * PR: kern/175227
5897a74ebfe5SAdrian Chadd 			 *
5898a74ebfe5SAdrian Chadd 			 * Reconfigure beacons during reset; as otherwise
5899a74ebfe5SAdrian Chadd 			 * we won't get the beacon timers reprogrammed
5900a74ebfe5SAdrian Chadd 			 * after a reset and thus we won't pick up a
5901a74ebfe5SAdrian Chadd 			 * beacon miss interrupt.
5902a74ebfe5SAdrian Chadd 			 *
5903a74ebfe5SAdrian Chadd 			 * Hopefully we'll see a beacon before the BMISS
5904a74ebfe5SAdrian Chadd 			 * timer fires (too often), leading to a STA
5905a74ebfe5SAdrian Chadd 			 * disassociation.
5906a74ebfe5SAdrian Chadd 			 */
5907a74ebfe5SAdrian Chadd 				sc->sc_beacons = 1;
5908f5c30c4eSAdrian Chadd 			}
5909e8fd88a3SSam Leffler 			break;
5910b032f27cSSam Leffler 		case IEEE80211_M_MONITOR:
5911b032f27cSSam Leffler 			/*
5912b032f27cSSam Leffler 			 * Monitor mode vaps have only INIT->RUN and RUN->RUN
5913b032f27cSSam Leffler 			 * transitions so we must re-enable interrupts here to
5914b032f27cSSam Leffler 			 * handle the case of a single monitor mode vap.
5915b032f27cSSam Leffler 			 */
5916b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask);
5917b032f27cSSam Leffler 			break;
5918b032f27cSSam Leffler 		case IEEE80211_M_WDS:
5919b032f27cSSam Leffler 			break;
5920e8fd88a3SSam Leffler 		default:
5921e8fd88a3SSam Leffler 			break;
59225591b213SSam Leffler 		}
59235591b213SSam Leffler 		/*
59247b0c77ecSSam Leffler 		 * Let the hal process statistics collected during a
59257b0c77ecSSam Leffler 		 * scan so it can provide calibrated noise floor data.
59267b0c77ecSSam Leffler 		 */
59277b0c77ecSSam Leffler 		ath_hal_process_noisefloor(ah);
59287b0c77ecSSam Leffler 		/*
5929ffa2cab6SSam Leffler 		 * Reset rssi stats; maybe not the best place...
5930ffa2cab6SSam Leffler 		 */
5931ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
5932ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
5933ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
5934f5c30c4eSAdrian Chadd 
5935f5c30c4eSAdrian Chadd 		/*
5936f5c30c4eSAdrian Chadd 		 * Force awake for RUN mode.
5937f5c30c4eSAdrian Chadd 		 */
5938f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
5939*7d567ed6SAdrian Chadd 		ath_power_setselfgen(sc, HAL_PM_AWAKE);
5940f5c30c4eSAdrian Chadd 		ath_power_setpower(sc, HAL_PM_AWAKE);
5941f5c30c4eSAdrian Chadd 		ATH_UNLOCK(sc);
5942f5c30c4eSAdrian Chadd 
594345bbf62fSSam Leffler 		/*
5944b032f27cSSam Leffler 		 * Finally, start any timers and the task q thread
5945b032f27cSSam Leffler 		 * (in case we didn't go through SCAN state).
594645bbf62fSSam Leffler 		 */
59472dc7fcc4SSam Leffler 		if (ath_longcalinterval != 0) {
5948c42a7b7eSSam Leffler 			/* start periodic recalibration timer */
59492dc7fcc4SSam Leffler 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
59502dc7fcc4SSam Leffler 		} else {
59512dc7fcc4SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
59522dc7fcc4SSam Leffler 			    "%s: calibration disabled\n", __func__);
5953c42a7b7eSSam Leffler 		}
5954f5c30c4eSAdrian Chadd 
5955b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
5956b032f27cSSam Leffler 	} else if (nstate == IEEE80211_S_INIT) {
5957b032f27cSSam Leffler 		/*
5958b032f27cSSam Leffler 		 * If there are no vaps left in RUN state then
5959b032f27cSSam Leffler 		 * shutdown host/driver operation:
5960b032f27cSSam Leffler 		 * o disable interrupts
5961b032f27cSSam Leffler 		 * o disable the task queue thread
5962b032f27cSSam Leffler 		 * o mark beacon processing as stopped
5963b032f27cSSam Leffler 		 */
5964b032f27cSSam Leffler 		if (!ath_isanyrunningvaps(vap)) {
5965b032f27cSSam Leffler 			sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5966b032f27cSSam Leffler 			/* disable interrupts  */
5967b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL);
5968b032f27cSSam Leffler 			taskqueue_block(sc->sc_tq);
5969b032f27cSSam Leffler 			sc->sc_beacons = 0;
5970b032f27cSSam Leffler 		}
5971584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
597210ad9a77SSam Leffler 		ath_hal_setcca(ah, AH_TRUE);
597310ad9a77SSam Leffler #endif
5974f5c30c4eSAdrian Chadd 	} else if (nstate == IEEE80211_S_SLEEP) {
5975f5c30c4eSAdrian Chadd 		/* We're going to sleep, so transition appropriately */
5976f5c30c4eSAdrian Chadd 		/* For now, only do this if we're a single STA vap */
5977f5c30c4eSAdrian Chadd 		if (sc->sc_nvaps == 1 &&
5978f5c30c4eSAdrian Chadd 		    vap->iv_opmode == IEEE80211_M_STA) {
5979f5c30c4eSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_BEACON, "%s: syncbeacon=%d\n", __func__, sc->sc_syncbeacon);
5980f5c30c4eSAdrian Chadd 			ATH_LOCK(sc);
5981*7d567ed6SAdrian Chadd 			/*
5982*7d567ed6SAdrian Chadd 			 * Always at least set the self-generated
5983*7d567ed6SAdrian Chadd 			 * frame config to set PWRMGT=1.
5984*7d567ed6SAdrian Chadd 			 */
5985*7d567ed6SAdrian Chadd 			ath_power_setselfgen(sc, HAL_PM_NETWORK_SLEEP);
5986*7d567ed6SAdrian Chadd 
5987*7d567ed6SAdrian Chadd 			/*
5988*7d567ed6SAdrian Chadd 			 * If we're not syncing beacons, transition
5989*7d567ed6SAdrian Chadd 			 * to NETWORK_SLEEP.
5990*7d567ed6SAdrian Chadd 			 *
5991*7d567ed6SAdrian Chadd 			 * We stay awake if syncbeacon > 0 in case
5992*7d567ed6SAdrian Chadd 			 * we need to listen for some beacons otherwise
5993*7d567ed6SAdrian Chadd 			 * our beacon timer config may be wrong.
5994*7d567ed6SAdrian Chadd 			 */
5995f5c30c4eSAdrian Chadd 			if (sc->sc_syncbeacon == 0) {
5996f5c30c4eSAdrian Chadd 				ath_power_setpower(sc, HAL_PM_NETWORK_SLEEP);
5997f5c30c4eSAdrian Chadd 			}
5998f5c30c4eSAdrian Chadd 			ATH_UNLOCK(sc);
5999f5c30c4eSAdrian Chadd 		}
6000b032f27cSSam Leffler 	}
60015591b213SSam Leffler bad:
600280767531SAdrian Chadd 	ieee80211_free_node(ni);
6003f5c30c4eSAdrian Chadd 
6004f5c30c4eSAdrian Chadd 	/*
6005f5c30c4eSAdrian Chadd 	 * Restore the power state - either to what it was, or
6006f5c30c4eSAdrian Chadd 	 * to network_sleep if it's alright.
6007f5c30c4eSAdrian Chadd 	 */
6008f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
6009f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
6010f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
60115591b213SSam Leffler 	return error;
60125591b213SSam Leffler }
60135591b213SSam Leffler 
60145591b213SSam Leffler /*
6015e8fd88a3SSam Leffler  * Allocate a key cache slot to the station so we can
6016e8fd88a3SSam Leffler  * setup a mapping from key index to node. The key cache
6017e8fd88a3SSam Leffler  * slot is needed for managing antenna state and for
6018e8fd88a3SSam Leffler  * compression when stations do not use crypto.  We do
6019e8fd88a3SSam Leffler  * it uniliaterally here; if crypto is employed this slot
6020e8fd88a3SSam Leffler  * will be reassigned.
6021e8fd88a3SSam Leffler  */
6022e8fd88a3SSam Leffler static void
6023e8fd88a3SSam Leffler ath_setup_stationkey(struct ieee80211_node *ni)
6024e8fd88a3SSam Leffler {
6025b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
6026b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
6027c1225b52SSam Leffler 	ieee80211_keyix keyix, rxkeyix;
6028e8fd88a3SSam Leffler 
602980767531SAdrian Chadd 	/* XXX should take a locked ref to vap->iv_bss */
6030b032f27cSSam Leffler 	if (!ath_key_alloc(vap, &ni->ni_ucastkey, &keyix, &rxkeyix)) {
6031e8fd88a3SSam Leffler 		/*
6032e8fd88a3SSam Leffler 		 * Key cache is full; we'll fall back to doing
6033e8fd88a3SSam Leffler 		 * the more expensive lookup in software.  Note
6034e8fd88a3SSam Leffler 		 * this also means no h/w compression.
6035e8fd88a3SSam Leffler 		 */
6036e8fd88a3SSam Leffler 		/* XXX msg+statistic */
6037e8fd88a3SSam Leffler 	} else {
6038c1225b52SSam Leffler 		/* XXX locking? */
6039e8fd88a3SSam Leffler 		ni->ni_ucastkey.wk_keyix = keyix;
6040c1225b52SSam Leffler 		ni->ni_ucastkey.wk_rxkeyix = rxkeyix;
604133052833SSam Leffler 		/* NB: must mark device key to get called back on delete */
604233052833SSam Leffler 		ni->ni_ucastkey.wk_flags |= IEEE80211_KEY_DEVKEY;
6043d3ac945bSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_ucastkey.wk_macaddr, ni->ni_macaddr);
6044e8fd88a3SSam Leffler 		/* NB: this will create a pass-thru key entry */
604555c7b877SAdrian Chadd 		ath_keyset(sc, vap, &ni->ni_ucastkey, vap->iv_bss);
6046e8fd88a3SSam Leffler 	}
6047e8fd88a3SSam Leffler }
6048e8fd88a3SSam Leffler 
6049e8fd88a3SSam Leffler /*
60505591b213SSam Leffler  * Setup driver-specific state for a newly associated node.
60515591b213SSam Leffler  * Note that we're called also on a re-associate, the isnew
60525591b213SSam Leffler  * param tells us if this is the first time or not.
60535591b213SSam Leffler  */
60545591b213SSam Leffler static void
6055e9962332SSam Leffler ath_newassoc(struct ieee80211_node *ni, int isnew)
60565591b213SSam Leffler {
6057b032f27cSSam Leffler 	struct ath_node *an = ATH_NODE(ni);
6058b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
6059b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
6060c62362cbSSam Leffler 	const struct ieee80211_txparam *tp = ni->ni_txparms;
60615591b213SSam Leffler 
6062ab06fdf2SSam Leffler 	an->an_mcastrix = ath_tx_findrix(sc, tp->mcastrate);
6063ab06fdf2SSam Leffler 	an->an_mgmtrix = ath_tx_findrix(sc, tp->mgmtrate);
6064b032f27cSSam Leffler 
6065f5c30c4eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: reassoc; isnew=%d, is_powersave=%d\n",
6066f5c30c4eSAdrian Chadd 	    __func__,
6067f5c30c4eSAdrian Chadd 	    ni->ni_macaddr,
6068f5c30c4eSAdrian Chadd 	    ":",
6069f5c30c4eSAdrian Chadd 	    isnew,
6070f5c30c4eSAdrian Chadd 	    an->an_is_powersave);
6071f5c30c4eSAdrian Chadd 
6072656380e7SAdrian Chadd 	ATH_NODE_LOCK(an);
6073b032f27cSSam Leffler 	ath_rate_newassoc(sc, an, isnew);
6074656380e7SAdrian Chadd 	ATH_NODE_UNLOCK(an);
607532da86a0SAdrian Chadd 
6076e8fd88a3SSam Leffler 	if (isnew &&
6077b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey &&
6078b032f27cSSam Leffler 	    ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE)
6079e8fd88a3SSam Leffler 		ath_setup_stationkey(ni);
60804bed2b67SAdrian Chadd 
60814bed2b67SAdrian Chadd 	/*
60824bed2b67SAdrian Chadd 	 * If we're reassociating, make sure that any paused queues
60834bed2b67SAdrian Chadd 	 * get unpaused.
60844bed2b67SAdrian Chadd 	 *
60854bed2b67SAdrian Chadd 	 * Now, we may hvae frames in the hardware queue for this node.
60864bed2b67SAdrian Chadd 	 * So if we are reassociating and there are frames in the queue,
60874bed2b67SAdrian Chadd 	 * we need to go through the cleanup path to ensure that they're
60884bed2b67SAdrian Chadd 	 * marked as non-aggregate.
60894bed2b67SAdrian Chadd 	 */
60904bed2b67SAdrian Chadd 	if (! isnew) {
609132da86a0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE,
60924bed2b67SAdrian Chadd 		    "%s: %6D: reassoc; is_powersave=%d\n",
60934bed2b67SAdrian Chadd 		    __func__,
60944bed2b67SAdrian Chadd 		    ni->ni_macaddr,
60954bed2b67SAdrian Chadd 		    ":",
60964bed2b67SAdrian Chadd 		    an->an_is_powersave);
60974bed2b67SAdrian Chadd 
60984bed2b67SAdrian Chadd 		/* XXX for now, we can't hold the lock across assoc */
60994bed2b67SAdrian Chadd 		ath_tx_node_reassoc(sc, an);
61004bed2b67SAdrian Chadd 
61014bed2b67SAdrian Chadd 		/* XXX for now, we can't hold the lock across wakeup */
61024bed2b67SAdrian Chadd 		if (an->an_is_powersave)
61034bed2b67SAdrian Chadd 			ath_tx_node_wakeup(sc, an);
61044bed2b67SAdrian Chadd 	}
6105e8fd88a3SSam Leffler }
61065591b213SSam Leffler 
61075591b213SSam Leffler static int
610859efa8b5SSam Leffler ath_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *reg,
6109b032f27cSSam Leffler 	int nchans, struct ieee80211_channel chans[])
6110b032f27cSSam Leffler {
6111b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
6112b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
611359efa8b5SSam Leffler 	HAL_STATUS status;
6114b032f27cSSam Leffler 
6115033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
611659efa8b5SSam Leffler 	    "%s: rd %u cc %u location %c%s\n",
611759efa8b5SSam Leffler 	    __func__, reg->regdomain, reg->country, reg->location,
611859efa8b5SSam Leffler 	    reg->ecm ? " ecm" : "");
6119033022a9SSam Leffler 
612059efa8b5SSam Leffler 	status = ath_hal_set_channels(ah, chans, nchans,
612159efa8b5SSam Leffler 	    reg->country, reg->regdomain);
612259efa8b5SSam Leffler 	if (status != HAL_OK) {
612359efa8b5SSam Leffler 		DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: failed, status %u\n",
612459efa8b5SSam Leffler 		    __func__, status);
612559efa8b5SSam Leffler 		return EINVAL;		/* XXX */
6126b032f27cSSam Leffler 	}
61278db87e40SAdrian Chadd 
6128b032f27cSSam Leffler 	return 0;
6129b032f27cSSam Leffler }
6130b032f27cSSam Leffler 
6131b032f27cSSam Leffler static void
6132b032f27cSSam Leffler ath_getradiocaps(struct ieee80211com *ic,
61335fe9f044SSam Leffler 	int maxchans, int *nchans, struct ieee80211_channel chans[])
6134b032f27cSSam Leffler {
6135b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
6136b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6137b032f27cSSam Leffler 
613859efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: use rd %u cc %d\n",
613959efa8b5SSam Leffler 	    __func__, SKU_DEBUG, CTRY_DEFAULT);
6140033022a9SSam Leffler 
614159efa8b5SSam Leffler 	/* XXX check return */
614259efa8b5SSam Leffler 	(void) ath_hal_getchannels(ah, chans, maxchans, nchans,
614359efa8b5SSam Leffler 	    HAL_MODE_ALL, CTRY_DEFAULT, SKU_DEBUG, AH_TRUE);
6144033022a9SSam Leffler 
6145b032f27cSSam Leffler }
6146b032f27cSSam Leffler 
6147b032f27cSSam Leffler static int
6148b032f27cSSam Leffler ath_getchannels(struct ath_softc *sc)
6149b032f27cSSam Leffler {
6150b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
6151b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
6152b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
615359efa8b5SSam Leffler 	HAL_STATUS status;
6154b032f27cSSam Leffler 
6155b032f27cSSam Leffler 	/*
615659efa8b5SSam Leffler 	 * Collect channel set based on EEPROM contents.
6157b032f27cSSam Leffler 	 */
615859efa8b5SSam Leffler 	status = ath_hal_init_channels(ah, ic->ic_channels, IEEE80211_CHAN_MAX,
615959efa8b5SSam Leffler 	    &ic->ic_nchans, HAL_MODE_ALL, CTRY_DEFAULT, SKU_NONE, AH_TRUE);
616059efa8b5SSam Leffler 	if (status != HAL_OK) {
616159efa8b5SSam Leffler 		if_printf(ifp, "%s: unable to collect channel list from hal, "
616259efa8b5SSam Leffler 		    "status %d\n", __func__, status);
616359efa8b5SSam Leffler 		return EINVAL;
616459efa8b5SSam Leffler 	}
6165ca876918SSam Leffler 	(void) ath_hal_getregdomain(ah, &sc->sc_eerd);
6166ca876918SSam Leffler 	ath_hal_getcountrycode(ah, &sc->sc_eecc);	/* NB: cannot fail */
616759efa8b5SSam Leffler 	/* XXX map Atheros sku's to net80211 SKU's */
616859efa8b5SSam Leffler 	/* XXX net80211 types too small */
616959efa8b5SSam Leffler 	ic->ic_regdomain.regdomain = (uint16_t) sc->sc_eerd;
617059efa8b5SSam Leffler 	ic->ic_regdomain.country = (uint16_t) sc->sc_eecc;
617159efa8b5SSam Leffler 	ic->ic_regdomain.isocc[0] = ' ';	/* XXX don't know */
617259efa8b5SSam Leffler 	ic->ic_regdomain.isocc[1] = ' ';
617359efa8b5SSam Leffler 
6174b032f27cSSam Leffler 	ic->ic_regdomain.ecm = 1;
6175b032f27cSSam Leffler 	ic->ic_regdomain.location = 'I';
6176033022a9SSam Leffler 
6177033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
617859efa8b5SSam Leffler 	    "%s: eeprom rd %u cc %u (mapped rd %u cc %u) location %c%s\n",
6179033022a9SSam Leffler 	    __func__, sc->sc_eerd, sc->sc_eecc,
6180033022a9SSam Leffler 	    ic->ic_regdomain.regdomain, ic->ic_regdomain.country,
618159efa8b5SSam Leffler 	    ic->ic_regdomain.location, ic->ic_regdomain.ecm ? " ecm" : "");
61825591b213SSam Leffler 	return 0;
61835591b213SSam Leffler }
61845591b213SSam Leffler 
61856c4612b9SSam Leffler static int
61866c4612b9SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode)
61876c4612b9SSam Leffler {
61886c4612b9SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
61896c4612b9SSam Leffler 	const HAL_RATE_TABLE *rt;
61906c4612b9SSam Leffler 
61916c4612b9SSam Leffler 	switch (mode) {
61926c4612b9SSam Leffler 	case IEEE80211_MODE_11A:
61936c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A);
61946c4612b9SSam Leffler 		break;
6195724c193aSSam Leffler 	case IEEE80211_MODE_HALF:
6196aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_HALF_RATE);
6197aaa70f2fSSam Leffler 		break;
6198724c193aSSam Leffler 	case IEEE80211_MODE_QUARTER:
6199aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_QUARTER_RATE);
6200aaa70f2fSSam Leffler 		break;
62016c4612b9SSam Leffler 	case IEEE80211_MODE_11B:
62026c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11B);
62036c4612b9SSam Leffler 		break;
62046c4612b9SSam Leffler 	case IEEE80211_MODE_11G:
62056c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11G);
62066c4612b9SSam Leffler 		break;
62076c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_A:
620868e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108A);
62096c4612b9SSam Leffler 		break;
62106c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_G:
62116c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108G);
62126c4612b9SSam Leffler 		break;
621368e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
621468e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_TURBO);
621568e8e04eSSam Leffler 		break;
621668e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
621768e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NA_HT20);
621868e8e04eSSam Leffler 		break;
621968e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
622068e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NG_HT20);
622168e8e04eSSam Leffler 		break;
62226c4612b9SSam Leffler 	default:
62236c4612b9SSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
62246c4612b9SSam Leffler 			__func__, mode);
62256c4612b9SSam Leffler 		return 0;
62266c4612b9SSam Leffler 	}
62276c4612b9SSam Leffler 	sc->sc_rates[mode] = rt;
6228aaa70f2fSSam Leffler 	return (rt != NULL);
62295591b213SSam Leffler }
62305591b213SSam Leffler 
62315591b213SSam Leffler static void
62325591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
62335591b213SSam Leffler {
62343e50ec2cSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
62353e50ec2cSSam Leffler 	/* NB: on/off times from the Atheros NDIS driver, w/ permission */
62363e50ec2cSSam Leffler 	static const struct {
62373e50ec2cSSam Leffler 		u_int		rate;		/* tx/rx 802.11 rate */
62383e50ec2cSSam Leffler 		u_int16_t	timeOn;		/* LED on time (ms) */
62393e50ec2cSSam Leffler 		u_int16_t	timeOff;	/* LED off time (ms) */
62403e50ec2cSSam Leffler 	} blinkrates[] = {
62413e50ec2cSSam Leffler 		{ 108,  40,  10 },
62423e50ec2cSSam Leffler 		{  96,  44,  11 },
62433e50ec2cSSam Leffler 		{  72,  50,  13 },
62443e50ec2cSSam Leffler 		{  48,  57,  14 },
62453e50ec2cSSam Leffler 		{  36,  67,  16 },
62463e50ec2cSSam Leffler 		{  24,  80,  20 },
62473e50ec2cSSam Leffler 		{  22, 100,  25 },
62483e50ec2cSSam Leffler 		{  18, 133,  34 },
62493e50ec2cSSam Leffler 		{  12, 160,  40 },
62503e50ec2cSSam Leffler 		{  10, 200,  50 },
62513e50ec2cSSam Leffler 		{   6, 240,  58 },
62523e50ec2cSSam Leffler 		{   4, 267,  66 },
62533e50ec2cSSam Leffler 		{   2, 400, 100 },
62543e50ec2cSSam Leffler 		{   0, 500, 130 },
6255724c193aSSam Leffler 		/* XXX half/quarter rates */
62563e50ec2cSSam Leffler 	};
62575591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
62583e50ec2cSSam Leffler 	int i, j;
62595591b213SSam Leffler 
62605591b213SSam Leffler 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
62615591b213SSam Leffler 	rt = sc->sc_rates[mode];
62625591b213SSam Leffler 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
6263180f268dSSam Leffler 	for (i = 0; i < rt->rateCount; i++) {
6264180f268dSSam Leffler 		uint8_t ieeerate = rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
6265180f268dSSam Leffler 		if (rt->info[i].phy != IEEE80211_T_HT)
6266180f268dSSam Leffler 			sc->sc_rixmap[ieeerate] = i;
6267180f268dSSam Leffler 		else
6268180f268dSSam Leffler 			sc->sc_rixmap[ieeerate | IEEE80211_RATE_MCS] = i;
6269180f268dSSam Leffler 	}
62701b1a8e41SSam Leffler 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
627146d4d74cSSam Leffler 	for (i = 0; i < N(sc->sc_hwmap); i++) {
627246d4d74cSSam Leffler 		if (i >= rt->rateCount) {
62733e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledon = (500 * hz) / 1000;
62743e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledoff = (130 * hz) / 1000;
627516b4851aSSam Leffler 			continue;
62763e50ec2cSSam Leffler 		}
62773e50ec2cSSam Leffler 		sc->sc_hwmap[i].ieeerate =
627846d4d74cSSam Leffler 			rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
627946d4d74cSSam Leffler 		if (rt->info[i].phy == IEEE80211_T_HT)
628026041a14SSam Leffler 			sc->sc_hwmap[i].ieeerate |= IEEE80211_RATE_MCS;
6281d3be6f5bSSam Leffler 		sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
628246d4d74cSSam Leffler 		if (rt->info[i].shortPreamble ||
628346d4d74cSSam Leffler 		    rt->info[i].phy == IEEE80211_T_OFDM)
6284d3be6f5bSSam Leffler 			sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE;
62855463c4a4SSam Leffler 		sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags;
62863e50ec2cSSam Leffler 		for (j = 0; j < N(blinkrates)-1; j++)
62873e50ec2cSSam Leffler 			if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate)
62883e50ec2cSSam Leffler 				break;
62893e50ec2cSSam Leffler 		/* NB: this uses the last entry if the rate isn't found */
62903e50ec2cSSam Leffler 		/* XXX beware of overlow */
62913e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000;
62923e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000;
6293c42a7b7eSSam Leffler 	}
62945591b213SSam Leffler 	sc->sc_currates = rt;
62955591b213SSam Leffler 	sc->sc_curmode = mode;
62965591b213SSam Leffler 	/*
6297c42a7b7eSSam Leffler 	 * All protection frames are transmited at 2Mb/s for
6298c42a7b7eSSam Leffler 	 * 11g, otherwise at 1Mb/s.
62995591b213SSam Leffler 	 */
6300913a1ba1SSam Leffler 	if (mode == IEEE80211_MODE_11G)
6301ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*2);
6302913a1ba1SSam Leffler 	else
6303ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*1);
63044fa8d4efSDaniel Eischen 	/* NB: caller is responsible for resetting rate control state */
63053e50ec2cSSam Leffler #undef N
63065591b213SSam Leffler }
63075591b213SSam Leffler 
6308c42a7b7eSSam Leffler static void
63092e986da5SSam Leffler ath_watchdog(void *arg)
6310c42a7b7eSSam Leffler {
63112e986da5SSam Leffler 	struct ath_softc *sc = arg;
6312ef27340cSAdrian Chadd 	int do_reset = 0;
6313c42a7b7eSSam Leffler 
63142e986da5SSam Leffler 	if (sc->sc_wd_timer != 0 && --sc->sc_wd_timer == 0) {
63152e986da5SSam Leffler 		struct ifnet *ifp = sc->sc_ifp;
6316459bc4f0SSam Leffler 		uint32_t hangs;
6317459bc4f0SSam Leffler 
6318f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
6319f5c30c4eSAdrian Chadd 		ath_power_set_power_state(sc, HAL_PM_AWAKE);
6320f5c30c4eSAdrian Chadd 		ATH_UNLOCK(sc);
6321f5c30c4eSAdrian Chadd 
6322459bc4f0SSam Leffler 		if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) &&
6323459bc4f0SSam Leffler 		    hangs != 0) {
6324459bc4f0SSam Leffler 			if_printf(ifp, "%s hang detected (0x%x)\n",
6325459bc4f0SSam Leffler 			    hangs & 0xff ? "bb" : "mac", hangs);
6326459bc4f0SSam Leffler 		} else
6327c42a7b7eSSam Leffler 			if_printf(ifp, "device timeout\n");
6328ef27340cSAdrian Chadd 		do_reset = 1;
6329c42a7b7eSSam Leffler 		ifp->if_oerrors++;
6330c42a7b7eSSam Leffler 		sc->sc_stats.ast_watchdog++;
6331f5c30c4eSAdrian Chadd 
6332f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
6333f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
6334f5c30c4eSAdrian Chadd 		ATH_UNLOCK(sc);
6335c42a7b7eSSam Leffler 	}
6336ef27340cSAdrian Chadd 
6337ef27340cSAdrian Chadd 	/*
6338ef27340cSAdrian Chadd 	 * We can't hold the lock across the ath_reset() call.
6339d52f7132SAdrian Chadd 	 *
6340d52f7132SAdrian Chadd 	 * And since this routine can't hold a lock and sleep,
6341d52f7132SAdrian Chadd 	 * do the reset deferred.
6342ef27340cSAdrian Chadd 	 */
6343ef27340cSAdrian Chadd 	if (do_reset) {
6344d52f7132SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
6345ef27340cSAdrian Chadd 	}
6346ef27340cSAdrian Chadd 
63472e986da5SSam Leffler 	callout_schedule(&sc->sc_wd_ch, hz);
6348c42a7b7eSSam Leffler }
6349c42a7b7eSSam Leffler 
6350b8f2a853SAdrian Chadd /*
6351b8f2a853SAdrian Chadd  * Fetch the rate control statistics for the given node.
6352b8f2a853SAdrian Chadd  */
6353b8f2a853SAdrian Chadd static int
6354b8f2a853SAdrian Chadd ath_ioctl_ratestats(struct ath_softc *sc, struct ath_rateioctl *rs)
6355b8f2a853SAdrian Chadd {
6356b8f2a853SAdrian Chadd 	struct ath_node *an;
6357b8f2a853SAdrian Chadd 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
6358b8f2a853SAdrian Chadd 	struct ieee80211_node *ni;
6359b8f2a853SAdrian Chadd 	int error = 0;
6360b8f2a853SAdrian Chadd 
6361b8f2a853SAdrian Chadd 	/* Perform a lookup on the given node */
6362b8f2a853SAdrian Chadd 	ni = ieee80211_find_node(&ic->ic_sta, rs->is_u.macaddr);
6363b8f2a853SAdrian Chadd 	if (ni == NULL) {
6364b8f2a853SAdrian Chadd 		error = EINVAL;
6365b8f2a853SAdrian Chadd 		goto bad;
6366b8f2a853SAdrian Chadd 	}
6367b8f2a853SAdrian Chadd 
6368b8f2a853SAdrian Chadd 	/* Lock the ath_node */
6369b8f2a853SAdrian Chadd 	an = ATH_NODE(ni);
6370b8f2a853SAdrian Chadd 	ATH_NODE_LOCK(an);
6371b8f2a853SAdrian Chadd 
6372b8f2a853SAdrian Chadd 	/* Fetch the rate control stats for this node */
6373b8f2a853SAdrian Chadd 	error = ath_rate_fetch_node_stats(sc, an, rs);
6374b8f2a853SAdrian Chadd 
6375b8f2a853SAdrian Chadd 	/* No matter what happens here, just drop through */
6376b8f2a853SAdrian Chadd 
6377b8f2a853SAdrian Chadd 	/* Unlock the ath_node */
6378b8f2a853SAdrian Chadd 	ATH_NODE_UNLOCK(an);
6379b8f2a853SAdrian Chadd 
6380b8f2a853SAdrian Chadd 	/* Unref the node */
6381b8f2a853SAdrian Chadd 	ieee80211_node_decref(ni);
6382b8f2a853SAdrian Chadd 
6383b8f2a853SAdrian Chadd bad:
6384b8f2a853SAdrian Chadd 	return (error);
6385b8f2a853SAdrian Chadd }
6386b8f2a853SAdrian Chadd 
6387a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
6388c42a7b7eSSam Leffler /*
6389c42a7b7eSSam Leffler  * Diagnostic interface to the HAL.  This is used by various
6390c42a7b7eSSam Leffler  * tools to do things like retrieve register contents for
6391c42a7b7eSSam Leffler  * debugging.  The mechanism is intentionally opaque so that
6392c42a7b7eSSam Leffler  * it can change frequently w/o concern for compatiblity.
6393c42a7b7eSSam Leffler  */
6394c42a7b7eSSam Leffler static int
6395c42a7b7eSSam Leffler ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
6396c42a7b7eSSam Leffler {
6397c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6398c42a7b7eSSam Leffler 	u_int id = ad->ad_id & ATH_DIAG_ID;
6399c42a7b7eSSam Leffler 	void *indata = NULL;
6400c42a7b7eSSam Leffler 	void *outdata = NULL;
6401c42a7b7eSSam Leffler 	u_int32_t insize = ad->ad_in_size;
6402c42a7b7eSSam Leffler 	u_int32_t outsize = ad->ad_out_size;
6403c42a7b7eSSam Leffler 	int error = 0;
6404c42a7b7eSSam Leffler 
6405c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_IN) {
6406c42a7b7eSSam Leffler 		/*
6407c42a7b7eSSam Leffler 		 * Copy in data.
6408c42a7b7eSSam Leffler 		 */
6409c42a7b7eSSam Leffler 		indata = malloc(insize, M_TEMP, M_NOWAIT);
6410c42a7b7eSSam Leffler 		if (indata == NULL) {
6411c42a7b7eSSam Leffler 			error = ENOMEM;
6412c42a7b7eSSam Leffler 			goto bad;
6413c42a7b7eSSam Leffler 		}
6414c42a7b7eSSam Leffler 		error = copyin(ad->ad_in_data, indata, insize);
6415c42a7b7eSSam Leffler 		if (error)
6416c42a7b7eSSam Leffler 			goto bad;
6417c42a7b7eSSam Leffler 	}
6418c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_DYN) {
6419c42a7b7eSSam Leffler 		/*
6420c42a7b7eSSam Leffler 		 * Allocate a buffer for the results (otherwise the HAL
6421c42a7b7eSSam Leffler 		 * returns a pointer to a buffer where we can read the
6422c42a7b7eSSam Leffler 		 * results).  Note that we depend on the HAL leaving this
6423c42a7b7eSSam Leffler 		 * pointer for us to use below in reclaiming the buffer;
6424c42a7b7eSSam Leffler 		 * may want to be more defensive.
6425c42a7b7eSSam Leffler 		 */
6426c42a7b7eSSam Leffler 		outdata = malloc(outsize, M_TEMP, M_NOWAIT);
6427c42a7b7eSSam Leffler 		if (outdata == NULL) {
6428c42a7b7eSSam Leffler 			error = ENOMEM;
6429c42a7b7eSSam Leffler 			goto bad;
6430c42a7b7eSSam Leffler 		}
6431c42a7b7eSSam Leffler 	}
6432f5c30c4eSAdrian Chadd 
6433f5c30c4eSAdrian Chadd 
6434f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
6435f5c30c4eSAdrian Chadd 	if (id != HAL_DIAG_REGS)
6436f5c30c4eSAdrian Chadd 		ath_power_set_power_state(sc, HAL_PM_AWAKE);
6437f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
6438f5c30c4eSAdrian Chadd 
6439c42a7b7eSSam Leffler 	if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) {
6440c42a7b7eSSam Leffler 		if (outsize < ad->ad_out_size)
6441c42a7b7eSSam Leffler 			ad->ad_out_size = outsize;
6442c42a7b7eSSam Leffler 		if (outdata != NULL)
6443c42a7b7eSSam Leffler 			error = copyout(outdata, ad->ad_out_data,
6444c42a7b7eSSam Leffler 					ad->ad_out_size);
6445c42a7b7eSSam Leffler 	} else {
6446c42a7b7eSSam Leffler 		error = EINVAL;
6447c42a7b7eSSam Leffler 	}
6448f5c30c4eSAdrian Chadd 
6449f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
6450f5c30c4eSAdrian Chadd 	if (id != HAL_DIAG_REGS)
6451f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
6452f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
6453f5c30c4eSAdrian Chadd 
6454c42a7b7eSSam Leffler bad:
6455c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
6456c42a7b7eSSam Leffler 		free(indata, M_TEMP);
6457c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
6458c42a7b7eSSam Leffler 		free(outdata, M_TEMP);
6459c42a7b7eSSam Leffler 	return error;
6460c42a7b7eSSam Leffler }
6461a585a9a1SSam Leffler #endif /* ATH_DIAGAPI */
6462c42a7b7eSSam Leffler 
6463c42a7b7eSSam Leffler static int
6464c42a7b7eSSam Leffler ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
6465c42a7b7eSSam Leffler {
6466c42a7b7eSSam Leffler #define	IS_RUNNING(ifp) \
646713f4c340SRobert Watson 	((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
6468c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
6469b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
6470c42a7b7eSSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
647184784be1SSam Leffler 	const HAL_RATE_TABLE *rt;
6472c42a7b7eSSam Leffler 	int error = 0;
6473c42a7b7eSSam Leffler 
6474c42a7b7eSSam Leffler 	switch (cmd) {
6475c42a7b7eSSam Leffler 	case SIOCSIFFLAGS:
6476c42a7b7eSSam Leffler 		if (IS_RUNNING(ifp)) {
6477c42a7b7eSSam Leffler 			/*
6478c42a7b7eSSam Leffler 			 * To avoid rescanning another access point,
6479c42a7b7eSSam Leffler 			 * do not call ath_init() here.  Instead,
6480c42a7b7eSSam Leffler 			 * only reflect promisc mode settings.
6481c42a7b7eSSam Leffler 			 */
6482410302ebSAdrian Chadd 			ATH_LOCK(sc);
6483c42a7b7eSSam Leffler 			ath_mode_init(sc);
6484410302ebSAdrian Chadd 			ATH_UNLOCK(sc);
6485c42a7b7eSSam Leffler 		} else if (ifp->if_flags & IFF_UP) {
6486c42a7b7eSSam Leffler 			/*
6487c42a7b7eSSam Leffler 			 * Beware of being called during attach/detach
6488c42a7b7eSSam Leffler 			 * to reset promiscuous mode.  In that case we
6489c42a7b7eSSam Leffler 			 * will still be marked UP but not RUNNING.
6490c42a7b7eSSam Leffler 			 * However trying to re-init the interface
6491c42a7b7eSSam Leffler 			 * is the wrong thing to do as we've already
6492c42a7b7eSSam Leffler 			 * torn down much of our state.  There's
6493c42a7b7eSSam Leffler 			 * probably a better way to deal with this.
6494c42a7b7eSSam Leffler 			 */
6495b032f27cSSam Leffler 			if (!sc->sc_invalid)
6496fc74a9f9SBrooks Davis 				ath_init(sc);	/* XXX lose error */
6497d3ac945bSSam Leffler 		} else {
6498410302ebSAdrian Chadd 			ATH_LOCK(sc);
6499c42a7b7eSSam Leffler 			ath_stop_locked(ifp);
6500d3ac945bSSam Leffler 			if (!sc->sc_invalid)
6501f5c30c4eSAdrian Chadd 				ath_power_setpower(sc, HAL_PM_FULL_SLEEP);
650231a8c1edSAndrew Thompson 			ATH_UNLOCK(sc);
6503410302ebSAdrian Chadd 		}
6504c42a7b7eSSam Leffler 		break;
6505b032f27cSSam Leffler 	case SIOCGIFMEDIA:
6506b032f27cSSam Leffler 	case SIOCSIFMEDIA:
6507b032f27cSSam Leffler 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
6508b032f27cSSam Leffler 		break;
6509c42a7b7eSSam Leffler 	case SIOCGATHSTATS:
6510c42a7b7eSSam Leffler 		/* NB: embed these numbers to get a consistent view */
6511c42a7b7eSSam Leffler 		sc->sc_stats.ast_tx_packets = ifp->if_opackets;
6512c42a7b7eSSam Leffler 		sc->sc_stats.ast_rx_packets = ifp->if_ipackets;
651384784be1SSam Leffler 		sc->sc_stats.ast_tx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgtxrssi);
651484784be1SSam Leffler 		sc->sc_stats.ast_rx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgrssi);
6515584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
651610ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsfadjp = TDMA_AVG(sc->sc_avgtsfdeltap);
651710ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsfadjm = TDMA_AVG(sc->sc_avgtsfdeltam);
651810ad9a77SSam Leffler #endif
651984784be1SSam Leffler 		rt = sc->sc_currates;
652046d4d74cSSam Leffler 		sc->sc_stats.ast_tx_rate =
652146d4d74cSSam Leffler 		    rt->info[sc->sc_txrix].dot11Rate &~ IEEE80211_RATE_BASIC;
65226aa113fdSAdrian Chadd 		if (rt->info[sc->sc_txrix].phy & IEEE80211_T_HT)
65236aa113fdSAdrian Chadd 			sc->sc_stats.ast_tx_rate |= IEEE80211_RATE_MCS;
6524c42a7b7eSSam Leffler 		return copyout(&sc->sc_stats,
6525c42a7b7eSSam Leffler 		    ifr->ifr_data, sizeof (sc->sc_stats));
652694fe37d2SAdrian Chadd 	case SIOCGATHAGSTATS:
652794fe37d2SAdrian Chadd 		return copyout(&sc->sc_aggr_stats,
652894fe37d2SAdrian Chadd 		    ifr->ifr_data, sizeof (sc->sc_aggr_stats));
65293fc21fedSSam Leffler 	case SIOCZATHSTATS:
65303fc21fedSSam Leffler 		error = priv_check(curthread, PRIV_DRIVER);
65319467e3f3SAdrian Chadd 		if (error == 0) {
65323fc21fedSSam Leffler 			memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
653341b6b507SAdrian Chadd 			memset(&sc->sc_aggr_stats, 0,
653441b6b507SAdrian Chadd 			    sizeof(sc->sc_aggr_stats));
65359467e3f3SAdrian Chadd 			memset(&sc->sc_intr_stats, 0,
65369467e3f3SAdrian Chadd 			    sizeof(sc->sc_intr_stats));
65379467e3f3SAdrian Chadd 		}
65383fc21fedSSam Leffler 		break;
6539a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
6540c42a7b7eSSam Leffler 	case SIOCGATHDIAG:
6541c42a7b7eSSam Leffler 		error = ath_ioctl_diag(sc, (struct ath_diag *) ifr);
6542c42a7b7eSSam Leffler 		break;
6543f51c84eaSAdrian Chadd 	case SIOCGATHPHYERR:
6544f51c84eaSAdrian Chadd 		error = ath_ioctl_phyerr(sc,(struct ath_diag*) ifr);
6545f51c84eaSAdrian Chadd 		break;
6546a585a9a1SSam Leffler #endif
65479af351f9SAdrian Chadd 	case SIOCGATHSPECTRAL:
65489af351f9SAdrian Chadd 		error = ath_ioctl_spectral(sc,(struct ath_diag*) ifr);
65499af351f9SAdrian Chadd 		break;
6550b8f2a853SAdrian Chadd 	case SIOCGATHNODERATESTATS:
6551b8f2a853SAdrian Chadd 		error = ath_ioctl_ratestats(sc, (struct ath_rateioctl *) ifr);
6552b8f2a853SAdrian Chadd 		break;
655331a8c1edSAndrew Thompson 	case SIOCGIFADDR:
6554b032f27cSSam Leffler 		error = ether_ioctl(ifp, cmd, data);
6555c42a7b7eSSam Leffler 		break;
655631a8c1edSAndrew Thompson 	default:
655731a8c1edSAndrew Thompson 		error = EINVAL;
655831a8c1edSAndrew Thompson 		break;
6559c42a7b7eSSam Leffler 	}
6560c42a7b7eSSam Leffler 	return error;
6561a614e076SSam Leffler #undef IS_RUNNING
6562c42a7b7eSSam Leffler }
6563c42a7b7eSSam Leffler 
6564c42a7b7eSSam Leffler /*
6565c42a7b7eSSam Leffler  * Announce various information on device/driver attach.
6566c42a7b7eSSam Leffler  */
6567c42a7b7eSSam Leffler static void
6568c42a7b7eSSam Leffler ath_announce(struct ath_softc *sc)
6569c42a7b7eSSam Leffler {
6570fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
6571c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6572c42a7b7eSSam Leffler 
6573498657cfSSam Leffler 	if_printf(ifp, "AR%s mac %d.%d RF%s phy %d.%d\n",
6574498657cfSSam Leffler 		ath_hal_mac_name(ah), ah->ah_macVersion, ah->ah_macRev,
6575498657cfSSam Leffler 		ath_hal_rf_name(ah), ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
657646a924c4SAdrian Chadd 	if_printf(ifp, "2GHz radio: 0x%.4x; 5GHz radio: 0x%.4x\n",
657746a924c4SAdrian Chadd 		ah->ah_analog2GhzRev, ah->ah_analog5GhzRev);
6578c42a7b7eSSam Leffler 	if (bootverbose) {
6579c42a7b7eSSam Leffler 		int i;
6580c42a7b7eSSam Leffler 		for (i = 0; i <= WME_AC_VO; i++) {
6581c42a7b7eSSam Leffler 			struct ath_txq *txq = sc->sc_ac2q[i];
6582c42a7b7eSSam Leffler 			if_printf(ifp, "Use hw queue %u for %s traffic\n",
6583c42a7b7eSSam Leffler 				txq->axq_qnum, ieee80211_wme_acnames[i]);
6584c42a7b7eSSam Leffler 		}
6585c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for CAB traffic\n",
6586c42a7b7eSSam Leffler 			sc->sc_cabq->axq_qnum);
6587c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for beacons\n", sc->sc_bhalq);
6588c42a7b7eSSam Leffler 	}
6589e2d787faSSam Leffler 	if (ath_rxbuf != ATH_RXBUF)
6590e2d787faSSam Leffler 		if_printf(ifp, "using %u rx buffers\n", ath_rxbuf);
6591e2d787faSSam Leffler 	if (ath_txbuf != ATH_TXBUF)
6592e2d787faSSam Leffler 		if_printf(ifp, "using %u tx buffers\n", ath_txbuf);
65939ac01d39SRui Paulo 	if (sc->sc_mcastkey && bootverbose)
65949ac01d39SRui Paulo 		if_printf(ifp, "using multicast key search\n");
6595c42a7b7eSSam Leffler }
659610ad9a77SSam Leffler 
659748237774SAdrian Chadd static void
659848237774SAdrian Chadd ath_dfs_tasklet(void *p, int npending)
659948237774SAdrian Chadd {
660048237774SAdrian Chadd 	struct ath_softc *sc = (struct ath_softc *) p;
660148237774SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
660248237774SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
660348237774SAdrian Chadd 
660448237774SAdrian Chadd 	/*
660548237774SAdrian Chadd 	 * If previous processing has found a radar event,
660648237774SAdrian Chadd 	 * signal this to the net80211 layer to begin DFS
660748237774SAdrian Chadd 	 * processing.
660848237774SAdrian Chadd 	 */
660948237774SAdrian Chadd 	if (ath_dfs_process_radar_event(sc, sc->sc_curchan)) {
661048237774SAdrian Chadd 		/* DFS event found, initiate channel change */
661106fc4a10SAdrian Chadd 		/*
661206fc4a10SAdrian Chadd 		 * XXX doesn't currently tell us whether the event
661306fc4a10SAdrian Chadd 		 * XXX was found in the primary or extension
661406fc4a10SAdrian Chadd 		 * XXX channel!
661506fc4a10SAdrian Chadd 		 */
661606fc4a10SAdrian Chadd 		IEEE80211_LOCK(ic);
661748237774SAdrian Chadd 		ieee80211_dfs_notify_radar(ic, sc->sc_curchan);
661806fc4a10SAdrian Chadd 		IEEE80211_UNLOCK(ic);
661948237774SAdrian Chadd 	}
662048237774SAdrian Chadd }
662148237774SAdrian Chadd 
66220eb81626SAdrian Chadd /*
66230eb81626SAdrian Chadd  * Enable/disable power save.  This must be called with
66240eb81626SAdrian Chadd  * no TX driver locks currently held, so it should only
66250eb81626SAdrian Chadd  * be called from the RX path (which doesn't hold any
66260eb81626SAdrian Chadd  * TX driver locks.)
66270eb81626SAdrian Chadd  */
66280eb81626SAdrian Chadd static void
66290eb81626SAdrian Chadd ath_node_powersave(struct ieee80211_node *ni, int enable)
66300eb81626SAdrian Chadd {
6631bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
66320eb81626SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
66330eb81626SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
66340eb81626SAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
66350eb81626SAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
66360eb81626SAdrian Chadd 
66370eb81626SAdrian Chadd 	/* XXX and no TXQ locks should be held here */
66380eb81626SAdrian Chadd 
66399b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, "%s: %6D: enable=%d\n",
66409b48fb4bSAdrian Chadd 	    __func__,
66419b48fb4bSAdrian Chadd 	    ni->ni_macaddr,
66429b48fb4bSAdrian Chadd 	    ":",
66439b48fb4bSAdrian Chadd 	    !! enable);
66440eb81626SAdrian Chadd 
66450eb81626SAdrian Chadd 	/* Suspend or resume software queue handling */
66460eb81626SAdrian Chadd 	if (enable)
66470eb81626SAdrian Chadd 		ath_tx_node_sleep(sc, an);
66480eb81626SAdrian Chadd 	else
66490eb81626SAdrian Chadd 		ath_tx_node_wakeup(sc, an);
66500eb81626SAdrian Chadd 
66510eb81626SAdrian Chadd 	/* Update net80211 state */
66520eb81626SAdrian Chadd 	avp->av_node_ps(ni, enable);
6653bdbb6e5bSAdrian Chadd #else
6654bdbb6e5bSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6655bdbb6e5bSAdrian Chadd 
6656bdbb6e5bSAdrian Chadd 	/* Update net80211 state */
6657bdbb6e5bSAdrian Chadd 	avp->av_node_ps(ni, enable);
6658bdbb6e5bSAdrian Chadd #endif/* ATH_SW_PSQ */
66590eb81626SAdrian Chadd }
66600eb81626SAdrian Chadd 
6661548a605dSAdrian Chadd /*
6662548a605dSAdrian Chadd  * Notification from net80211 that the powersave queue state has
6663548a605dSAdrian Chadd  * changed.
6664548a605dSAdrian Chadd  *
6665548a605dSAdrian Chadd  * Since the software queue also may have some frames:
6666548a605dSAdrian Chadd  *
6667548a605dSAdrian Chadd  * + if the node software queue has frames and the TID state
6668548a605dSAdrian Chadd  *   is 0, we set the TIM;
6669548a605dSAdrian Chadd  * + if the node and the stack are both empty, we clear the TIM bit.
6670548a605dSAdrian Chadd  * + If the stack tries to set the bit, always set it.
6671548a605dSAdrian Chadd  * + If the stack tries to clear the bit, only clear it if the
6672548a605dSAdrian Chadd  *   software queue in question is also cleared.
6673548a605dSAdrian Chadd  *
6674548a605dSAdrian Chadd  * TODO: this is called during node teardown; so let's ensure this
6675548a605dSAdrian Chadd  * is all correctly handled and that the TIM bit is cleared.
6676548a605dSAdrian Chadd  * It may be that the node flush is called _AFTER_ the net80211
6677548a605dSAdrian Chadd  * stack clears the TIM.
6678548a605dSAdrian Chadd  *
6679548a605dSAdrian Chadd  * Here is the racy part.  Since it's possible >1 concurrent,
6680548a605dSAdrian Chadd  * overlapping TXes will appear complete with a TX completion in
6681548a605dSAdrian Chadd  * another thread, it's possible that the concurrent TIM calls will
6682548a605dSAdrian Chadd  * clash.  We can't hold the node lock here because setting the
6683548a605dSAdrian Chadd  * TIM grabs the net80211 comlock and this may cause a LOR.
6684548a605dSAdrian Chadd  * The solution is either to totally serialise _everything_ at
6685548a605dSAdrian Chadd  * this point (ie, all TX, completion and any reset/flush go into
6686548a605dSAdrian Chadd  * one taskqueue) or a new "ath TIM lock" needs to be created that
6687548a605dSAdrian Chadd  * just wraps the driver state change and this call to avp->av_set_tim().
6688548a605dSAdrian Chadd  *
6689548a605dSAdrian Chadd  * The same race exists in the net80211 power save queue handling
6690548a605dSAdrian Chadd  * as well.  Since multiple transmitting threads may queue frames
6691548a605dSAdrian Chadd  * into the driver, as well as ps-poll and the driver transmitting
6692548a605dSAdrian Chadd  * frames (and thus clearing the psq), it's quite possible that
6693548a605dSAdrian Chadd  * a packet entering the PSQ and a ps-poll being handled will
6694548a605dSAdrian Chadd  * race, causing the TIM to be cleared and not re-set.
6695548a605dSAdrian Chadd  */
6696548a605dSAdrian Chadd static int
6697548a605dSAdrian Chadd ath_node_set_tim(struct ieee80211_node *ni, int enable)
6698548a605dSAdrian Chadd {
6699bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
6700548a605dSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
6701548a605dSAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
6702548a605dSAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
6703548a605dSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6704548a605dSAdrian Chadd 	int changed = 0;
6705548a605dSAdrian Chadd 
67064bed2b67SAdrian Chadd 	ATH_TX_LOCK(sc);
6707548a605dSAdrian Chadd 	an->an_stack_psq = enable;
6708548a605dSAdrian Chadd 
6709548a605dSAdrian Chadd 	/*
6710548a605dSAdrian Chadd 	 * This will get called for all operating modes,
6711548a605dSAdrian Chadd 	 * even if avp->av_set_tim is unset.
6712548a605dSAdrian Chadd 	 * It's currently set for hostap/ibss modes; but
6713548a605dSAdrian Chadd 	 * the same infrastructure is used for both STA
6714548a605dSAdrian Chadd 	 * and AP/IBSS node power save.
6715548a605dSAdrian Chadd 	 */
6716548a605dSAdrian Chadd 	if (avp->av_set_tim == NULL) {
67174bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6718548a605dSAdrian Chadd 		return (0);
6719548a605dSAdrian Chadd 	}
6720548a605dSAdrian Chadd 
6721548a605dSAdrian Chadd 	/*
6722548a605dSAdrian Chadd 	 * If setting the bit, always set it here.
6723548a605dSAdrian Chadd 	 * If clearing the bit, only clear it if the
6724548a605dSAdrian Chadd 	 * software queue is also empty.
6725548a605dSAdrian Chadd 	 *
6726548a605dSAdrian Chadd 	 * If the node has left power save, just clear the TIM
6727548a605dSAdrian Chadd 	 * bit regardless of the state of the power save queue.
6728548a605dSAdrian Chadd 	 *
6729548a605dSAdrian Chadd 	 * XXX TODO: although atomics are used, it's quite possible
6730548a605dSAdrian Chadd 	 * that a race will occur between this and setting/clearing
6731548a605dSAdrian Chadd 	 * in another thread.  TX completion will occur always in
6732548a605dSAdrian Chadd 	 * one thread, however setting/clearing the TIM bit can come
6733548a605dSAdrian Chadd 	 * from a variety of different process contexts!
6734548a605dSAdrian Chadd 	 */
6735548a605dSAdrian Chadd 	if (enable && an->an_tim_set == 1) {
6736548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
67379b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, tim_set=1, ignoring\n",
67389b48fb4bSAdrian Chadd 		    __func__,
67399b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
67409b48fb4bSAdrian Chadd 		    ":",
67419b48fb4bSAdrian Chadd 		    enable);
67424bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6743548a605dSAdrian Chadd 	} else if (enable) {
6744548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
67459b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, enabling TIM\n",
67469b48fb4bSAdrian Chadd 		    __func__,
67479b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
67489b48fb4bSAdrian Chadd 		    ":",
67499b48fb4bSAdrian Chadd 		    enable);
6750548a605dSAdrian Chadd 		an->an_tim_set = 1;
67514bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6752548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6753ba83edd4SAdrian Chadd 	} else if (an->an_swq_depth == 0) {
6754548a605dSAdrian Chadd 		/* disable */
6755548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
67569b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_swq_depth == 0, disabling\n",
67579b48fb4bSAdrian Chadd 		    __func__,
67589b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
67599b48fb4bSAdrian Chadd 		    ":",
67609b48fb4bSAdrian Chadd 		    enable);
6761548a605dSAdrian Chadd 		an->an_tim_set = 0;
67624bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6763548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6764548a605dSAdrian Chadd 	} else if (! an->an_is_powersave) {
6765548a605dSAdrian Chadd 		/*
6766548a605dSAdrian Chadd 		 * disable regardless; the node isn't in powersave now
6767548a605dSAdrian Chadd 		 */
6768548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
67699b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_pwrsave=0, disabling\n",
67709b48fb4bSAdrian Chadd 		    __func__,
67719b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
67729b48fb4bSAdrian Chadd 		    ":",
67739b48fb4bSAdrian Chadd 		    enable);
6774548a605dSAdrian Chadd 		an->an_tim_set = 0;
67754bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6776548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6777548a605dSAdrian Chadd 	} else {
6778548a605dSAdrian Chadd 		/*
6779548a605dSAdrian Chadd 		 * psq disable, node is currently in powersave, node
6780548a605dSAdrian Chadd 		 * software queue isn't empty, so don't clear the TIM bit
6781548a605dSAdrian Chadd 		 * for now.
6782548a605dSAdrian Chadd 		 */
67834bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6784548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
67859b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_swq_depth > 0, ignoring\n",
67869b48fb4bSAdrian Chadd 		    __func__,
67879b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
67889b48fb4bSAdrian Chadd 		    ":",
67899b48fb4bSAdrian Chadd 		    enable);
6790548a605dSAdrian Chadd 		changed = 0;
6791548a605dSAdrian Chadd 	}
6792548a605dSAdrian Chadd 
6793548a605dSAdrian Chadd 	return (changed);
6794bdbb6e5bSAdrian Chadd #else
6795bdbb6e5bSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6796bdbb6e5bSAdrian Chadd 
679760328038SAdrian Chadd 	/*
6798661c81c3SBaptiste Daroussin 	 * Some operating modes don't set av_set_tim(), so don't
679960328038SAdrian Chadd 	 * update it here.
680060328038SAdrian Chadd 	 */
680160328038SAdrian Chadd 	if (avp->av_set_tim == NULL)
680260328038SAdrian Chadd 		return (0);
680360328038SAdrian Chadd 
6804bdbb6e5bSAdrian Chadd 	return (avp->av_set_tim(ni, enable));
6805bdbb6e5bSAdrian Chadd #endif /* ATH_SW_PSQ */
6806548a605dSAdrian Chadd }
6807548a605dSAdrian Chadd 
6808548a605dSAdrian Chadd /*
6809548a605dSAdrian Chadd  * Set or update the TIM from the software queue.
6810548a605dSAdrian Chadd  *
6811548a605dSAdrian Chadd  * Check the software queue depth before attempting to do lock
6812548a605dSAdrian Chadd  * anything; that avoids trying to obtain the lock.  Then,
6813548a605dSAdrian Chadd  * re-check afterwards to ensure nothing has changed in the
6814548a605dSAdrian Chadd  * meantime.
6815548a605dSAdrian Chadd  *
6816548a605dSAdrian Chadd  * set:   This is designed to be called from the TX path, after
6817548a605dSAdrian Chadd  *        a frame has been queued; to see if the swq > 0.
6818548a605dSAdrian Chadd  *
6819548a605dSAdrian Chadd  * clear: This is designed to be called from the buffer completion point
6820548a605dSAdrian Chadd  *        (right now it's ath_tx_default_comp()) where the state of
6821548a605dSAdrian Chadd  *        a software queue has changed.
6822548a605dSAdrian Chadd  *
6823548a605dSAdrian Chadd  * It makes sense to place it at buffer free / completion rather
6824548a605dSAdrian Chadd  * than after each software queue operation, as there's no real
6825548a605dSAdrian Chadd  * point in churning the TIM bit as the last frames in the software
6826548a605dSAdrian Chadd  * queue are transmitted.  If they fail and we retry them, we'd
6827548a605dSAdrian Chadd  * just be setting the TIM bit again anyway.
6828548a605dSAdrian Chadd  */
6829548a605dSAdrian Chadd void
6830548a605dSAdrian Chadd ath_tx_update_tim(struct ath_softc *sc, struct ieee80211_node *ni,
6831548a605dSAdrian Chadd      int enable)
6832548a605dSAdrian Chadd {
6833bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
6834548a605dSAdrian Chadd 	struct ath_node *an;
6835548a605dSAdrian Chadd 	struct ath_vap *avp;
6836548a605dSAdrian Chadd 
6837548a605dSAdrian Chadd 	/* Don't do this for broadcast/etc frames */
6838548a605dSAdrian Chadd 	if (ni == NULL)
6839548a605dSAdrian Chadd 		return;
6840548a605dSAdrian Chadd 
6841548a605dSAdrian Chadd 	an = ATH_NODE(ni);
6842548a605dSAdrian Chadd 	avp = ATH_VAP(ni->ni_vap);
6843548a605dSAdrian Chadd 
6844548a605dSAdrian Chadd 	/*
6845548a605dSAdrian Chadd 	 * And for operating modes without the TIM handler set, let's
6846548a605dSAdrian Chadd 	 * just skip those.
6847548a605dSAdrian Chadd 	 */
6848548a605dSAdrian Chadd 	if (avp->av_set_tim == NULL)
6849548a605dSAdrian Chadd 		return;
6850548a605dSAdrian Chadd 
68514bed2b67SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
6852548a605dSAdrian Chadd 
6853548a605dSAdrian Chadd 	if (enable) {
6854548a605dSAdrian Chadd 		if (an->an_is_powersave &&
6855548a605dSAdrian Chadd 		    an->an_tim_set == 0 &&
6856ba83edd4SAdrian Chadd 		    an->an_swq_depth != 0) {
6857548a605dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
68589b48fb4bSAdrian Chadd 			    "%s: %6D: swq_depth>0, tim_set=0, set!\n",
68599b48fb4bSAdrian Chadd 			    __func__,
68609b48fb4bSAdrian Chadd 			    ni->ni_macaddr,
68619b48fb4bSAdrian Chadd 			    ":");
6862548a605dSAdrian Chadd 			an->an_tim_set = 1;
6863548a605dSAdrian Chadd 			(void) avp->av_set_tim(ni, 1);
6864548a605dSAdrian Chadd 		}
6865548a605dSAdrian Chadd 	} else {
6866548a605dSAdrian Chadd 		/*
6867548a605dSAdrian Chadd 		 * Don't bother grabbing the lock unless the queue is empty.
6868548a605dSAdrian Chadd 		 */
6869ba83edd4SAdrian Chadd 		if (&an->an_swq_depth != 0)
6870548a605dSAdrian Chadd 			return;
6871548a605dSAdrian Chadd 
6872548a605dSAdrian Chadd 		if (an->an_is_powersave &&
6873548a605dSAdrian Chadd 		    an->an_stack_psq == 0 &&
6874548a605dSAdrian Chadd 		    an->an_tim_set == 1 &&
6875ba83edd4SAdrian Chadd 		    an->an_swq_depth == 0) {
6876548a605dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
687722a3aee6SAdrian Chadd 			    "%s: %6D: swq_depth=0, tim_set=1, psq_set=0,"
6878548a605dSAdrian Chadd 			    " clear!\n",
687922a3aee6SAdrian Chadd 			    __func__,
688022a3aee6SAdrian Chadd 			    ni->ni_macaddr,
688122a3aee6SAdrian Chadd 			    ":");
6882548a605dSAdrian Chadd 			an->an_tim_set = 0;
6883548a605dSAdrian Chadd 			(void) avp->av_set_tim(ni, 0);
6884548a605dSAdrian Chadd 		}
6885548a605dSAdrian Chadd 	}
6886bdbb6e5bSAdrian Chadd #else
6887bdbb6e5bSAdrian Chadd 	return;
6888bdbb6e5bSAdrian Chadd #endif	/* ATH_SW_PSQ */
6889548a605dSAdrian Chadd }
68900eb81626SAdrian Chadd 
689122a3aee6SAdrian Chadd /*
689222a3aee6SAdrian Chadd  * Received a ps-poll frame from net80211.
689322a3aee6SAdrian Chadd  *
689422a3aee6SAdrian Chadd  * Here we get a chance to serve out a software-queued frame ourselves
689522a3aee6SAdrian Chadd  * before we punt it to net80211 to transmit us one itself - either
689622a3aee6SAdrian Chadd  * because there's traffic in the net80211 psq, or a NULL frame to
689722a3aee6SAdrian Chadd  * indicate there's nothing else.
689822a3aee6SAdrian Chadd  */
689922a3aee6SAdrian Chadd static void
690022a3aee6SAdrian Chadd ath_node_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m)
690122a3aee6SAdrian Chadd {
690222a3aee6SAdrian Chadd #ifdef	ATH_SW_PSQ
690322a3aee6SAdrian Chadd 	struct ath_node *an;
690422a3aee6SAdrian Chadd 	struct ath_vap *avp;
690522a3aee6SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
690622a3aee6SAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
690722a3aee6SAdrian Chadd 	int tid;
690822a3aee6SAdrian Chadd 
690922a3aee6SAdrian Chadd 	/* Just paranoia */
691022a3aee6SAdrian Chadd 	if (ni == NULL)
691122a3aee6SAdrian Chadd 		return;
691222a3aee6SAdrian Chadd 
691322a3aee6SAdrian Chadd 	/*
691422a3aee6SAdrian Chadd 	 * Unassociated (temporary node) station.
691522a3aee6SAdrian Chadd 	 */
691622a3aee6SAdrian Chadd 	if (ni->ni_associd == 0)
691722a3aee6SAdrian Chadd 		return;
691822a3aee6SAdrian Chadd 
691922a3aee6SAdrian Chadd 	/*
692022a3aee6SAdrian Chadd 	 * We do have an active node, so let's begin looking into it.
692122a3aee6SAdrian Chadd 	 */
692222a3aee6SAdrian Chadd 	an = ATH_NODE(ni);
692322a3aee6SAdrian Chadd 	avp = ATH_VAP(ni->ni_vap);
692422a3aee6SAdrian Chadd 
692522a3aee6SAdrian Chadd 	/*
692622a3aee6SAdrian Chadd 	 * For now, we just call the original ps-poll method.
692722a3aee6SAdrian Chadd 	 * Once we're ready to flip this on:
692822a3aee6SAdrian Chadd 	 *
692922a3aee6SAdrian Chadd 	 * + Set leak to 1, as no matter what we're going to have
693022a3aee6SAdrian Chadd 	 *   to send a frame;
693122a3aee6SAdrian Chadd 	 * + Check the software queue and if there's something in it,
693222a3aee6SAdrian Chadd 	 *   schedule the highest TID thas has traffic from this node.
693322a3aee6SAdrian Chadd 	 *   Then make sure we schedule the software scheduler to
693422a3aee6SAdrian Chadd 	 *   run so it picks up said frame.
693522a3aee6SAdrian Chadd 	 *
693622a3aee6SAdrian Chadd 	 * That way whatever happens, we'll at least send _a_ frame
693722a3aee6SAdrian Chadd 	 * to the given node.
693822a3aee6SAdrian Chadd 	 *
693922a3aee6SAdrian Chadd 	 * Again, yes, it's crappy QoS if the node has multiple
694022a3aee6SAdrian Chadd 	 * TIDs worth of traffic - but let's get it working first
694122a3aee6SAdrian Chadd 	 * before we optimise it.
694222a3aee6SAdrian Chadd 	 *
694322a3aee6SAdrian Chadd 	 * Also yes, there's definitely latency here - we're not
694422a3aee6SAdrian Chadd 	 * direct dispatching to the hardware in this path (and
694522a3aee6SAdrian Chadd 	 * we're likely being called from the packet receive path,
694622a3aee6SAdrian Chadd 	 * so going back into TX may be a little hairy!) but again
694722a3aee6SAdrian Chadd 	 * I'd like to get this working first before optimising
694822a3aee6SAdrian Chadd 	 * turn-around time.
694922a3aee6SAdrian Chadd 	 */
695022a3aee6SAdrian Chadd 
695122a3aee6SAdrian Chadd 	ATH_TX_LOCK(sc);
695222a3aee6SAdrian Chadd 
695322a3aee6SAdrian Chadd 	/*
695422a3aee6SAdrian Chadd 	 * Legacy - we're called and the node isn't asleep.
695522a3aee6SAdrian Chadd 	 * Immediately punt.
695622a3aee6SAdrian Chadd 	 */
695722a3aee6SAdrian Chadd 	if (! an->an_is_powersave) {
695883bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
695922a3aee6SAdrian Chadd 		    "%s: %6D: not in powersave?\n",
696022a3aee6SAdrian Chadd 		    __func__,
696122a3aee6SAdrian Chadd 		    ni->ni_macaddr,
696222a3aee6SAdrian Chadd 		    ":");
696322a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
696422a3aee6SAdrian Chadd 		avp->av_recv_pspoll(ni, m);
696522a3aee6SAdrian Chadd 		return;
696622a3aee6SAdrian Chadd 	}
696722a3aee6SAdrian Chadd 
696822a3aee6SAdrian Chadd 	/*
696922a3aee6SAdrian Chadd 	 * We're in powersave.
697022a3aee6SAdrian Chadd 	 *
697122a3aee6SAdrian Chadd 	 * Leak a frame.
697222a3aee6SAdrian Chadd 	 */
697322a3aee6SAdrian Chadd 	an->an_leak_count = 1;
697422a3aee6SAdrian Chadd 
697522a3aee6SAdrian Chadd 	/*
697622a3aee6SAdrian Chadd 	 * Now, if there's no frames in the node, just punt to
697722a3aee6SAdrian Chadd 	 * recv_pspoll.
697822a3aee6SAdrian Chadd 	 *
697922a3aee6SAdrian Chadd 	 * Don't bother checking if the TIM bit is set, we really
698022a3aee6SAdrian Chadd 	 * only care if there are any frames here!
698122a3aee6SAdrian Chadd 	 */
698222a3aee6SAdrian Chadd 	if (an->an_swq_depth == 0) {
698322a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
698422a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
698522a3aee6SAdrian Chadd 		    "%s: %6D: SWQ empty; punting to net80211\n",
698622a3aee6SAdrian Chadd 		    __func__,
698722a3aee6SAdrian Chadd 		    ni->ni_macaddr,
698822a3aee6SAdrian Chadd 		    ":");
698922a3aee6SAdrian Chadd 		avp->av_recv_pspoll(ni, m);
699022a3aee6SAdrian Chadd 		return;
699122a3aee6SAdrian Chadd 	}
699222a3aee6SAdrian Chadd 
699322a3aee6SAdrian Chadd 	/*
699422a3aee6SAdrian Chadd 	 * Ok, let's schedule the highest TID that has traffic
699522a3aee6SAdrian Chadd 	 * and then schedule something.
699622a3aee6SAdrian Chadd 	 */
699722a3aee6SAdrian Chadd 	for (tid = IEEE80211_TID_SIZE - 1; tid >= 0; tid--) {
699822a3aee6SAdrian Chadd 		struct ath_tid *atid = &an->an_tid[tid];
699922a3aee6SAdrian Chadd 		/*
700022a3aee6SAdrian Chadd 		 * No frames? Skip.
700122a3aee6SAdrian Chadd 		 */
700222a3aee6SAdrian Chadd 		if (atid->axq_depth == 0)
700322a3aee6SAdrian Chadd 			continue;
700422a3aee6SAdrian Chadd 		ath_tx_tid_sched(sc, atid);
700522a3aee6SAdrian Chadd 		/*
700622a3aee6SAdrian Chadd 		 * XXX we could do a direct call to the TXQ
700722a3aee6SAdrian Chadd 		 * scheduler code here to optimise latency
700822a3aee6SAdrian Chadd 		 * at the expense of a REALLY deep callstack.
700922a3aee6SAdrian Chadd 		 */
701022a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
701122a3aee6SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_txqtask);
701222a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
701322a3aee6SAdrian Chadd 		    "%s: %6D: leaking frame to TID %d\n",
701422a3aee6SAdrian Chadd 		    __func__,
701522a3aee6SAdrian Chadd 		    ni->ni_macaddr,
701622a3aee6SAdrian Chadd 		    ":",
701722a3aee6SAdrian Chadd 		    tid);
701822a3aee6SAdrian Chadd 		return;
701922a3aee6SAdrian Chadd 	}
702022a3aee6SAdrian Chadd 
702122a3aee6SAdrian Chadd 	ATH_TX_UNLOCK(sc);
702222a3aee6SAdrian Chadd 
702322a3aee6SAdrian Chadd 	/*
702422a3aee6SAdrian Chadd 	 * XXX nothing in the TIDs at this point? Eek.
702522a3aee6SAdrian Chadd 	 */
702683bbd5ebSRui Paulo 	DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
702783bbd5ebSRui Paulo 	    "%s: %6D: TIDs empty, but ath_node showed traffic?!\n",
702822a3aee6SAdrian Chadd 	    __func__,
702922a3aee6SAdrian Chadd 	    ni->ni_macaddr,
703022a3aee6SAdrian Chadd 	    ":");
703122a3aee6SAdrian Chadd 	avp->av_recv_pspoll(ni, m);
703222a3aee6SAdrian Chadd #else
703322a3aee6SAdrian Chadd 	avp->av_recv_pspoll(ni, m);
703422a3aee6SAdrian Chadd #endif	/* ATH_SW_PSQ */
703522a3aee6SAdrian Chadd }
703622a3aee6SAdrian Chadd 
7037dba9c859SAdrian Chadd MODULE_VERSION(if_ath, 1);
7038dba9c859SAdrian Chadd MODULE_DEPEND(if_ath, wlan, 1, 1, 1);          /* 802.11 media layer */
703958816f3fSAdrian Chadd #if	defined(IEEE80211_ALQ) || defined(AH_DEBUG_ALQ)
704058816f3fSAdrian Chadd MODULE_DEPEND(if_ath, alq, 1, 1, 1);
704158816f3fSAdrian Chadd #endif
7042