xref: /freebsd/sys/dev/ath/if_ath.c (revision f50e4ebf6a779dfa0b4d5aaeaabde41bb19296f3)
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 *);
1547a79cebfSGleb Smirnoff static int	ath_init(struct ath_softc *);
1557a79cebfSGleb Smirnoff static void	ath_stop(struct ath_softc *);
156b032f27cSSam Leffler static int	ath_reset_vap(struct ieee80211vap *, u_long);
1577a79cebfSGleb Smirnoff static int	ath_transmit(struct ieee80211com *, struct mbuf *);
1585591b213SSam Leffler static int	ath_media_change(struct ifnet *);
1592e986da5SSam Leffler static void	ath_watchdog(void *);
1607a79cebfSGleb Smirnoff static int	ath_ioctl(struct ieee80211com *, u_long, void *);
1617a79cebfSGleb Smirnoff static void	ath_parent(struct ieee80211com *);
1625591b213SSam Leffler static void	ath_fatal_proc(void *, int);
163b032f27cSSam Leffler static void	ath_bmiss_vap(struct ieee80211vap *);
1645591b213SSam Leffler static void	ath_bmiss_proc(void *, int);
165b032f27cSSam Leffler static void	ath_key_update_begin(struct ieee80211vap *);
166b032f27cSSam Leffler static void	ath_key_update_end(struct ieee80211vap *);
167e5bd159eSAdrian Chadd static void	ath_update_mcast_hw(struct ath_softc *);
168272f6adeSGleb Smirnoff static void	ath_update_mcast(struct ieee80211com *);
169272f6adeSGleb Smirnoff static void	ath_update_promisc(struct ieee80211com *);
170272f6adeSGleb Smirnoff static void	ath_updateslot(struct ieee80211com *);
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 */
242af3b2549SHans Petter Selasky SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RWTUN, &ath_rxbuf,
243e2d787faSSam Leffler 	    0, "rx buffers allocated");
2443d184db2SAdrian Chadd int ath_txbuf = ATH_TXBUF;		/* # tx buffers to allocate */
245af3b2549SHans Petter Selasky SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RWTUN, &ath_txbuf,
246e2d787faSSam Leffler 	    0, "tx buffers allocated");
2473d184db2SAdrian Chadd int ath_txbuf_mgmt = ATH_MGMT_TXBUF;	/* # mgmt tx buffers to allocate */
248af3b2549SHans Petter Selasky SYSCTL_INT(_hw_ath, OID_AUTO, txbuf_mgmt, CTLFLAG_RWTUN, &ath_txbuf_mgmt,
249af33d486SAdrian Chadd 	    0, "tx (mgmt) buffers allocated");
250e2d787faSSam Leffler 
251a35dae8dSAdrian Chadd int ath_bstuck_threshold = 4;		/* max missed beacons */
252a32ac9d3SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, bstuck, CTLFLAG_RW, &ath_bstuck_threshold,
253a32ac9d3SSam Leffler 	    0, "max missed beacon xmits before chip reset");
254a32ac9d3SSam Leffler 
2556b349e5aSAdrian Chadd MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
256c42a7b7eSSam Leffler 
257f8418db5SAdrian Chadd void
258f8418db5SAdrian Chadd ath_legacy_attach_comp_func(struct ath_softc *sc)
259f8418db5SAdrian Chadd {
260f8418db5SAdrian Chadd 
261f8418db5SAdrian Chadd 	/*
262f8418db5SAdrian Chadd 	 * Special case certain configurations.  Note the
263f8418db5SAdrian Chadd 	 * CAB queue is handled by these specially so don't
264f8418db5SAdrian Chadd 	 * include them when checking the txq setup mask.
265f8418db5SAdrian Chadd 	 */
266f8418db5SAdrian Chadd 	switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
267f8418db5SAdrian Chadd 	case 0x01:
268f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
269f8418db5SAdrian Chadd 		break;
270f8418db5SAdrian Chadd 	case 0x0f:
271f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
272f8418db5SAdrian Chadd 		break;
273f8418db5SAdrian Chadd 	default:
274f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
275f8418db5SAdrian Chadd 		break;
276f8418db5SAdrian Chadd 	}
277f8418db5SAdrian Chadd }
278f8418db5SAdrian Chadd 
279f5c30c4eSAdrian Chadd /*
280f5c30c4eSAdrian Chadd  * Set the target power mode.
281f5c30c4eSAdrian Chadd  *
282f5c30c4eSAdrian Chadd  * If this is called during a point in time where
283f5c30c4eSAdrian Chadd  * the hardware is being programmed elsewhere, it will
284f5c30c4eSAdrian Chadd  * simply store it away and update it when all current
285f5c30c4eSAdrian Chadd  * uses of the hardware are completed.
286f5c30c4eSAdrian Chadd  */
287f5c30c4eSAdrian Chadd void
288f5c30c4eSAdrian Chadd _ath_power_setpower(struct ath_softc *sc, int power_state, const char *file, int line)
289f5c30c4eSAdrian Chadd {
290f5c30c4eSAdrian Chadd 	ATH_LOCK_ASSERT(sc);
291f5c30c4eSAdrian Chadd 
292f5c30c4eSAdrian Chadd 	sc->sc_target_powerstate = power_state;
293f5c30c4eSAdrian Chadd 
294f5c30c4eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) state=%d, refcnt=%d\n",
295f5c30c4eSAdrian Chadd 	    __func__,
296f5c30c4eSAdrian Chadd 	    file,
297f5c30c4eSAdrian Chadd 	    line,
298f5c30c4eSAdrian Chadd 	    power_state,
299f5c30c4eSAdrian Chadd 	    sc->sc_powersave_refcnt);
300f5c30c4eSAdrian Chadd 
301f5c30c4eSAdrian Chadd 	if (sc->sc_powersave_refcnt == 0 &&
302f5c30c4eSAdrian Chadd 	    power_state != sc->sc_cur_powerstate) {
303f5c30c4eSAdrian Chadd 		sc->sc_cur_powerstate = power_state;
304f5c30c4eSAdrian Chadd 		ath_hal_setpower(sc->sc_ah, power_state);
3057d567ed6SAdrian Chadd 
3067d567ed6SAdrian Chadd 		/*
3077d567ed6SAdrian Chadd 		 * If the NIC is force-awake, then set the
3087d567ed6SAdrian Chadd 		 * self-gen frame state appropriately.
3097d567ed6SAdrian Chadd 		 *
3107d567ed6SAdrian Chadd 		 * If the nic is in network sleep or full-sleep,
3117d567ed6SAdrian Chadd 		 * we let the above call leave the self-gen
3127d567ed6SAdrian Chadd 		 * state as "sleep".
3137d567ed6SAdrian Chadd 		 */
3147d567ed6SAdrian Chadd 		if (sc->sc_cur_powerstate == HAL_PM_AWAKE &&
3157d567ed6SAdrian Chadd 		    sc->sc_target_selfgen_state != HAL_PM_AWAKE) {
3167d567ed6SAdrian Chadd 			ath_hal_setselfgenpower(sc->sc_ah,
3177d567ed6SAdrian Chadd 			    sc->sc_target_selfgen_state);
3187d567ed6SAdrian Chadd 		}
3197d567ed6SAdrian Chadd 	}
3207d567ed6SAdrian Chadd }
3217d567ed6SAdrian Chadd 
3227d567ed6SAdrian Chadd /*
3237d567ed6SAdrian Chadd  * Set the current self-generated frames state.
3247d567ed6SAdrian Chadd  *
3257d567ed6SAdrian Chadd  * This is separate from the target power mode.  The chip may be
3267d567ed6SAdrian Chadd  * awake but the desired state is "sleep", so frames sent to the
3277d567ed6SAdrian Chadd  * destination has PWRMGT=1 in the 802.11 header.  The NIC also
3287d567ed6SAdrian Chadd  * needs to know to set PWRMGT=1 in self-generated frames.
3297d567ed6SAdrian Chadd  */
3307d567ed6SAdrian Chadd void
3317d567ed6SAdrian Chadd _ath_power_set_selfgen(struct ath_softc *sc, int power_state, const char *file, int line)
3327d567ed6SAdrian Chadd {
3337d567ed6SAdrian Chadd 
3347d567ed6SAdrian Chadd 	ATH_LOCK_ASSERT(sc);
3357d567ed6SAdrian Chadd 
3367d567ed6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) state=%d, refcnt=%d\n",
3377d567ed6SAdrian Chadd 	    __func__,
3387d567ed6SAdrian Chadd 	    file,
3397d567ed6SAdrian Chadd 	    line,
3407d567ed6SAdrian Chadd 	    power_state,
3417d567ed6SAdrian Chadd 	    sc->sc_target_selfgen_state);
3427d567ed6SAdrian Chadd 
3437d567ed6SAdrian Chadd 	sc->sc_target_selfgen_state = power_state;
3447d567ed6SAdrian Chadd 
3457d567ed6SAdrian Chadd 	/*
3467d567ed6SAdrian Chadd 	 * If the NIC is force-awake, then set the power state.
3477d567ed6SAdrian Chadd 	 * Network-state and full-sleep will already transition it to
3487d567ed6SAdrian Chadd 	 * mark self-gen frames as sleeping - and we can't
3497d567ed6SAdrian Chadd 	 * guarantee the NIC is awake to program the self-gen frame
3507d567ed6SAdrian Chadd 	 * setting anyway.
3517d567ed6SAdrian Chadd 	 */
3527d567ed6SAdrian Chadd 	if (sc->sc_cur_powerstate == HAL_PM_AWAKE) {
3537d567ed6SAdrian Chadd 		ath_hal_setselfgenpower(sc->sc_ah, power_state);
354f5c30c4eSAdrian Chadd 	}
355f5c30c4eSAdrian Chadd }
356f5c30c4eSAdrian Chadd 
357f5c30c4eSAdrian Chadd /*
358f5c30c4eSAdrian Chadd  * Set the hardware power mode and take a reference.
359f5c30c4eSAdrian Chadd  *
360f5c30c4eSAdrian Chadd  * This doesn't update the target power mode in the driver;
361f5c30c4eSAdrian Chadd  * it just updates the hardware power state.
362f5c30c4eSAdrian Chadd  *
363f5c30c4eSAdrian Chadd  * XXX it should only ever force the hardware awake; it should
364f5c30c4eSAdrian Chadd  * never be called to set it asleep.
365f5c30c4eSAdrian Chadd  */
366f5c30c4eSAdrian Chadd void
367f5c30c4eSAdrian Chadd _ath_power_set_power_state(struct ath_softc *sc, int power_state, const char *file, int line)
368f5c30c4eSAdrian Chadd {
369f5c30c4eSAdrian Chadd 	ATH_LOCK_ASSERT(sc);
370f5c30c4eSAdrian Chadd 
371f5c30c4eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) state=%d, refcnt=%d\n",
372f5c30c4eSAdrian Chadd 	    __func__,
373f5c30c4eSAdrian Chadd 	    file,
374f5c30c4eSAdrian Chadd 	    line,
375f5c30c4eSAdrian Chadd 	    power_state,
376f5c30c4eSAdrian Chadd 	    sc->sc_powersave_refcnt);
377f5c30c4eSAdrian Chadd 
378f5c30c4eSAdrian Chadd 	sc->sc_powersave_refcnt++;
379f5c30c4eSAdrian Chadd 
380f5c30c4eSAdrian Chadd 	if (power_state != sc->sc_cur_powerstate) {
381f5c30c4eSAdrian Chadd 		ath_hal_setpower(sc->sc_ah, power_state);
382f5c30c4eSAdrian Chadd 		sc->sc_cur_powerstate = power_state;
3837d567ed6SAdrian Chadd 
3847d567ed6SAdrian Chadd 		/*
3857d567ed6SAdrian Chadd 		 * Adjust the self-gen powerstate if appropriate.
3867d567ed6SAdrian Chadd 		 */
3877d567ed6SAdrian Chadd 		if (sc->sc_cur_powerstate == HAL_PM_AWAKE &&
3887d567ed6SAdrian Chadd 		    sc->sc_target_selfgen_state != HAL_PM_AWAKE) {
3897d567ed6SAdrian Chadd 			ath_hal_setselfgenpower(sc->sc_ah,
3907d567ed6SAdrian Chadd 			    sc->sc_target_selfgen_state);
3917d567ed6SAdrian Chadd 		}
3927d567ed6SAdrian Chadd 
393f5c30c4eSAdrian Chadd 	}
394f5c30c4eSAdrian Chadd }
395f5c30c4eSAdrian Chadd 
396f5c30c4eSAdrian Chadd /*
397f5c30c4eSAdrian Chadd  * Restore the power save mode to what it once was.
398f5c30c4eSAdrian Chadd  *
399f5c30c4eSAdrian Chadd  * This will decrement the reference counter and once it hits
400f5c30c4eSAdrian Chadd  * zero, it'll restore the powersave state.
401f5c30c4eSAdrian Chadd  */
402f5c30c4eSAdrian Chadd void
403f5c30c4eSAdrian Chadd _ath_power_restore_power_state(struct ath_softc *sc, const char *file, int line)
404f5c30c4eSAdrian Chadd {
405f5c30c4eSAdrian Chadd 
406f5c30c4eSAdrian Chadd 	ATH_LOCK_ASSERT(sc);
407f5c30c4eSAdrian Chadd 
408f5c30c4eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) refcnt=%d, target state=%d\n",
409f5c30c4eSAdrian Chadd 	    __func__,
410f5c30c4eSAdrian Chadd 	    file,
411f5c30c4eSAdrian Chadd 	    line,
412f5c30c4eSAdrian Chadd 	    sc->sc_powersave_refcnt,
413f5c30c4eSAdrian Chadd 	    sc->sc_target_powerstate);
414f5c30c4eSAdrian Chadd 
415f5c30c4eSAdrian Chadd 	if (sc->sc_powersave_refcnt == 0)
416f5c30c4eSAdrian Chadd 		device_printf(sc->sc_dev, "%s: refcnt=0?\n", __func__);
417f5c30c4eSAdrian Chadd 	else
418f5c30c4eSAdrian Chadd 		sc->sc_powersave_refcnt--;
419f5c30c4eSAdrian Chadd 
420f5c30c4eSAdrian Chadd 	if (sc->sc_powersave_refcnt == 0 &&
421f5c30c4eSAdrian Chadd 	    sc->sc_target_powerstate != sc->sc_cur_powerstate) {
422f5c30c4eSAdrian Chadd 		sc->sc_cur_powerstate = sc->sc_target_powerstate;
423f5c30c4eSAdrian Chadd 		ath_hal_setpower(sc->sc_ah, sc->sc_target_powerstate);
424f5c30c4eSAdrian Chadd 	}
4257d567ed6SAdrian Chadd 
4267d567ed6SAdrian Chadd 	/*
4277d567ed6SAdrian Chadd 	 * Adjust the self-gen powerstate if appropriate.
4287d567ed6SAdrian Chadd 	 */
4297d567ed6SAdrian Chadd 	if (sc->sc_cur_powerstate == HAL_PM_AWAKE &&
4307d567ed6SAdrian Chadd 	    sc->sc_target_selfgen_state != HAL_PM_AWAKE) {
4317d567ed6SAdrian Chadd 		ath_hal_setselfgenpower(sc->sc_ah,
4327d567ed6SAdrian Chadd 		    sc->sc_target_selfgen_state);
4337d567ed6SAdrian Chadd 	}
4347d567ed6SAdrian Chadd 
435f5c30c4eSAdrian Chadd }
436f5c30c4eSAdrian Chadd 
4379389d5a9SAdrian Chadd /*
4389389d5a9SAdrian Chadd  * Configure the initial HAL configuration values based on bus
4399389d5a9SAdrian Chadd  * specific parameters.
4409389d5a9SAdrian Chadd  *
4419389d5a9SAdrian Chadd  * Some PCI IDs and other information may need tweaking.
4429389d5a9SAdrian Chadd  *
4439389d5a9SAdrian Chadd  * XXX TODO: ath9k and the Atheros HAL only program comm2g_switch_enable
4449389d5a9SAdrian Chadd  * if BT antenna diversity isn't enabled.
4459389d5a9SAdrian Chadd  *
4469389d5a9SAdrian Chadd  * So, let's also figure out how to enable BT diversity for AR9485.
4479389d5a9SAdrian Chadd  */
4489389d5a9SAdrian Chadd static void
4499389d5a9SAdrian Chadd ath_setup_hal_config(struct ath_softc *sc, HAL_OPS_CONFIG *ah_config)
4509389d5a9SAdrian Chadd {
4519389d5a9SAdrian Chadd 	/* XXX TODO: only for PCI devices? */
4529389d5a9SAdrian Chadd 
4539389d5a9SAdrian Chadd 	if (sc->sc_pci_devinfo & (ATH_PCI_CUS198 | ATH_PCI_CUS230)) {
4549389d5a9SAdrian Chadd 		ah_config->ath_hal_ext_lna_ctl_gpio = 0x200; /* bit 9 */
4559389d5a9SAdrian Chadd 		ah_config->ath_hal_ext_atten_margin_cfg = AH_TRUE;
4569389d5a9SAdrian Chadd 		ah_config->ath_hal_min_gainidx = AH_TRUE;
4579389d5a9SAdrian Chadd 		ah_config->ath_hal_ant_ctrl_comm2g_switch_enable = 0x000bbb88;
4589389d5a9SAdrian Chadd 		/* XXX low_rssi_thresh */
4599389d5a9SAdrian Chadd 		/* XXX fast_div_bias */
4609389d5a9SAdrian Chadd 		device_printf(sc->sc_dev, "configuring for %s\n",
4619389d5a9SAdrian Chadd 		    (sc->sc_pci_devinfo & ATH_PCI_CUS198) ?
4629389d5a9SAdrian Chadd 		    "CUS198" : "CUS230");
4639389d5a9SAdrian Chadd 	}
4649389d5a9SAdrian Chadd 
4659389d5a9SAdrian Chadd 	if (sc->sc_pci_devinfo & ATH_PCI_CUS217)
4669389d5a9SAdrian Chadd 		device_printf(sc->sc_dev, "CUS217 card detected\n");
4679389d5a9SAdrian Chadd 
4689389d5a9SAdrian Chadd 	if (sc->sc_pci_devinfo & ATH_PCI_CUS252)
4699389d5a9SAdrian Chadd 		device_printf(sc->sc_dev, "CUS252 card detected\n");
4709389d5a9SAdrian Chadd 
4719389d5a9SAdrian Chadd 	if (sc->sc_pci_devinfo & ATH_PCI_AR9565_1ANT)
4729389d5a9SAdrian Chadd 		device_printf(sc->sc_dev, "WB335 1-ANT card detected\n");
4739389d5a9SAdrian Chadd 
4749389d5a9SAdrian Chadd 	if (sc->sc_pci_devinfo & ATH_PCI_AR9565_2ANT)
4759389d5a9SAdrian Chadd 		device_printf(sc->sc_dev, "WB335 2-ANT card detected\n");
4769389d5a9SAdrian Chadd 
4779389d5a9SAdrian Chadd 	if (sc->sc_pci_devinfo & ATH_PCI_KILLER)
4789389d5a9SAdrian Chadd 		device_printf(sc->sc_dev, "Killer Wireless card detected\n");
4799389d5a9SAdrian Chadd 
4809389d5a9SAdrian Chadd #if 0
4819389d5a9SAdrian Chadd         /*
4829389d5a9SAdrian Chadd          * Some WB335 cards do not support antenna diversity. Since
4839389d5a9SAdrian Chadd          * we use a hardcoded value for AR9565 instead of using the
4849389d5a9SAdrian Chadd          * EEPROM/OTP data, remove the combining feature from
4859389d5a9SAdrian Chadd          * the HW capabilities bitmap.
4869389d5a9SAdrian Chadd          */
4879389d5a9SAdrian Chadd         if (sc->sc_pci_devinfo & (ATH9K_PCI_AR9565_1ANT | ATH9K_PCI_AR9565_2ANT)) {
4889389d5a9SAdrian Chadd                 if (!(sc->sc_pci_devinfo & ATH9K_PCI_BT_ANT_DIV))
4899389d5a9SAdrian Chadd                         pCap->hw_caps &= ~ATH9K_HW_CAP_ANT_DIV_COMB;
4909389d5a9SAdrian Chadd         }
4919389d5a9SAdrian Chadd 
4929389d5a9SAdrian Chadd         if (sc->sc_pci_devinfo & ATH9K_PCI_BT_ANT_DIV) {
4939389d5a9SAdrian Chadd                 pCap->hw_caps |= ATH9K_HW_CAP_BT_ANT_DIV;
4949389d5a9SAdrian Chadd                 device_printf(sc->sc_dev, "Set BT/WLAN RX diversity capability\n");
4959389d5a9SAdrian Chadd         }
4969389d5a9SAdrian Chadd #endif
4979389d5a9SAdrian Chadd 
4989389d5a9SAdrian Chadd         if (sc->sc_pci_devinfo & ATH_PCI_D3_L1_WAR) {
4999389d5a9SAdrian Chadd                 ah_config->ath_hal_pcie_waen = 0x0040473b;
5009389d5a9SAdrian Chadd                 device_printf(sc->sc_dev, "Enable WAR for ASPM D3/L1\n");
5019389d5a9SAdrian Chadd         }
5029389d5a9SAdrian Chadd 
5039389d5a9SAdrian Chadd #if 0
5049389d5a9SAdrian Chadd         if (sc->sc_pci_devinfo & ATH9K_PCI_NO_PLL_PWRSAVE) {
5059389d5a9SAdrian Chadd                 ah->config.no_pll_pwrsave = true;
5069389d5a9SAdrian Chadd                 device_printf(sc->sc_dev, "Disable PLL PowerSave\n");
5079389d5a9SAdrian Chadd         }
5089389d5a9SAdrian Chadd #endif
5099389d5a9SAdrian Chadd 
5109389d5a9SAdrian Chadd }
5119389d5a9SAdrian Chadd 
512240b1f1dSAdrian Chadd /*
513240b1f1dSAdrian Chadd  * Attempt to fetch the MAC address from the kernel environment.
514240b1f1dSAdrian Chadd  *
515240b1f1dSAdrian Chadd  * Returns 0, macaddr in macaddr if successful; -1 otherwise.
516240b1f1dSAdrian Chadd  */
517240b1f1dSAdrian Chadd static int
518240b1f1dSAdrian Chadd ath_fetch_mac_kenv(struct ath_softc *sc, uint8_t *macaddr)
519240b1f1dSAdrian Chadd {
520240b1f1dSAdrian Chadd 	char devid_str[32];
521240b1f1dSAdrian Chadd 	int local_mac = 0;
522240b1f1dSAdrian Chadd 	char *local_macstr;
523240b1f1dSAdrian Chadd 
524240b1f1dSAdrian Chadd 	/*
525240b1f1dSAdrian Chadd 	 * Fetch from the kenv rather than using hints.
526240b1f1dSAdrian Chadd 	 *
527240b1f1dSAdrian Chadd 	 * Hints would be nice but the transition to dynamic
528240b1f1dSAdrian Chadd 	 * hints/kenv doesn't happen early enough for this
529240b1f1dSAdrian Chadd 	 * to work reliably (eg on anything embedded.)
530240b1f1dSAdrian Chadd 	 */
531240b1f1dSAdrian Chadd 	snprintf(devid_str, 32, "hint.%s.%d.macaddr",
532240b1f1dSAdrian Chadd 	    device_get_name(sc->sc_dev),
533240b1f1dSAdrian Chadd 	    device_get_unit(sc->sc_dev));
534240b1f1dSAdrian Chadd 
535240b1f1dSAdrian Chadd 	if ((local_macstr = kern_getenv(devid_str)) != NULL) {
536240b1f1dSAdrian Chadd 		uint32_t tmpmac[ETHER_ADDR_LEN];
537240b1f1dSAdrian Chadd 		int count;
538240b1f1dSAdrian Chadd 		int i;
539240b1f1dSAdrian Chadd 
540240b1f1dSAdrian Chadd 		/* Have a MAC address; should use it */
541240b1f1dSAdrian Chadd 		device_printf(sc->sc_dev,
542240b1f1dSAdrian Chadd 		    "Overriding MAC address from environment: '%s'\n",
543240b1f1dSAdrian Chadd 		    local_macstr);
544240b1f1dSAdrian Chadd 
545240b1f1dSAdrian Chadd 		/* Extract out the MAC address */
546240b1f1dSAdrian Chadd 		count = sscanf(local_macstr, "%x%*c%x%*c%x%*c%x%*c%x%*c%x",
547240b1f1dSAdrian Chadd 		    &tmpmac[0], &tmpmac[1],
548240b1f1dSAdrian Chadd 		    &tmpmac[2], &tmpmac[3],
549240b1f1dSAdrian Chadd 		    &tmpmac[4], &tmpmac[5]);
550240b1f1dSAdrian Chadd 		if (count == 6) {
551240b1f1dSAdrian Chadd 			/* Valid! */
552240b1f1dSAdrian Chadd 			local_mac = 1;
553240b1f1dSAdrian Chadd 			for (i = 0; i < ETHER_ADDR_LEN; i++)
554240b1f1dSAdrian Chadd 				macaddr[i] = tmpmac[i];
555240b1f1dSAdrian Chadd 		}
556240b1f1dSAdrian Chadd 		/* Done! */
557240b1f1dSAdrian Chadd 		freeenv(local_macstr);
558240b1f1dSAdrian Chadd 		local_macstr = NULL;
559240b1f1dSAdrian Chadd 	}
560240b1f1dSAdrian Chadd 
561240b1f1dSAdrian Chadd 	if (local_mac)
562240b1f1dSAdrian Chadd 		return (0);
563240b1f1dSAdrian Chadd 	return (-1);
564240b1f1dSAdrian Chadd }
565240b1f1dSAdrian Chadd 
56667397d39SAdrian Chadd #define	HAL_MODE_HT20 (HAL_MODE_11NG_HT20 | HAL_MODE_11NA_HT20)
56767397d39SAdrian Chadd #define	HAL_MODE_HT40 \
56867397d39SAdrian Chadd 	(HAL_MODE_11NG_HT40PLUS | HAL_MODE_11NG_HT40MINUS | \
56967397d39SAdrian Chadd 	HAL_MODE_11NA_HT40PLUS | HAL_MODE_11NA_HT40MINUS)
5705591b213SSam Leffler int
5715591b213SSam Leffler ath_attach(u_int16_t devid, struct ath_softc *sc)
5725591b213SSam Leffler {
5737a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
574fc74a9f9SBrooks Davis 	struct ath_hal *ah = NULL;
5755591b213SSam Leffler 	HAL_STATUS status;
576c42a7b7eSSam Leffler 	int error = 0, i;
577411373ebSSam Leffler 	u_int wmodes;
578a865860dSAdrian Chadd 	int rx_chainmask, tx_chainmask;
5799389d5a9SAdrian Chadd 	HAL_OPS_CONFIG ah_config;
5805591b213SSam Leffler 
581c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
5825591b213SSam Leffler 
58359686fe9SGleb Smirnoff 	ic->ic_softc = sc;
584c8550c02SGleb Smirnoff 	ic->ic_name = device_get_nameunit(sc->sc_dev);
585fc74a9f9SBrooks Davis 
5869389d5a9SAdrian Chadd 	/*
5879389d5a9SAdrian Chadd 	 * Configure the initial configuration data.
5889389d5a9SAdrian Chadd 	 *
5899389d5a9SAdrian Chadd 	 * This is stuff that may be needed early during attach
5909389d5a9SAdrian Chadd 	 * rather than done via configuration calls later.
5919389d5a9SAdrian Chadd 	 */
5929389d5a9SAdrian Chadd 	bzero(&ah_config, sizeof(ah_config));
5939389d5a9SAdrian Chadd 	ath_setup_hal_config(sc, &ah_config);
5949389d5a9SAdrian Chadd 
5957e97436bSAdrian Chadd 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh,
5969389d5a9SAdrian Chadd 	    sc->sc_eepromdata, &ah_config, &status);
5975591b213SSam Leffler 	if (ah == NULL) {
59876e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
59976e6fd5dSGleb Smirnoff 		    "unable to attach hardware; HAL status %u\n", status);
6005591b213SSam Leffler 		error = ENXIO;
6015591b213SSam Leffler 		goto bad;
6025591b213SSam Leffler 	}
6035591b213SSam Leffler 	sc->sc_ah = ah;
604b58b3803SSam Leffler 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
6053297be13SSam Leffler #ifdef	ATH_DEBUG
6063297be13SSam Leffler 	sc->sc_debug = ath_debug;
6073297be13SSam Leffler #endif
6085591b213SSam Leffler 
6095591b213SSam Leffler 	/*
610f8cc9b09SAdrian Chadd 	 * Setup the DMA/EDMA functions based on the current
611f8cc9b09SAdrian Chadd 	 * hardware support.
612f8cc9b09SAdrian Chadd 	 *
613f8cc9b09SAdrian Chadd 	 * This is required before the descriptors are allocated.
614f8cc9b09SAdrian Chadd 	 */
6153d184db2SAdrian Chadd 	if (ath_hal_hasedma(sc->sc_ah)) {
6163d184db2SAdrian Chadd 		sc->sc_isedma = 1;
617f8cc9b09SAdrian Chadd 		ath_recv_setup_edma(sc);
6183fdfc330SAdrian Chadd 		ath_xmit_setup_edma(sc);
6193fdfc330SAdrian Chadd 	} else {
620f8cc9b09SAdrian Chadd 		ath_recv_setup_legacy(sc);
6213fdfc330SAdrian Chadd 		ath_xmit_setup_legacy(sc);
6223fdfc330SAdrian Chadd 	}
623f8cc9b09SAdrian Chadd 
624f5c30c4eSAdrian Chadd 	if (ath_hal_hasmybeacon(sc->sc_ah)) {
625f5c30c4eSAdrian Chadd 		sc->sc_do_mybeacon = 1;
626f5c30c4eSAdrian Chadd 	}
627f5c30c4eSAdrian Chadd 
628f8cc9b09SAdrian Chadd 	/*
629c42a7b7eSSam Leffler 	 * Check if the MAC has multi-rate retry support.
630c42a7b7eSSam Leffler 	 * We do this by trying to setup a fake extended
631c42a7b7eSSam Leffler 	 * descriptor.  MAC's that don't have support will
632c42a7b7eSSam Leffler 	 * return false w/o doing anything.  MAC's that do
633c42a7b7eSSam Leffler 	 * support it will return true w/o doing anything.
634c42a7b7eSSam Leffler 	 */
635c42a7b7eSSam Leffler 	sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
636c42a7b7eSSam Leffler 
637c42a7b7eSSam Leffler 	/*
638c42a7b7eSSam Leffler 	 * Check if the device has hardware counters for PHY
639c42a7b7eSSam Leffler 	 * errors.  If so we need to enable the MIB interrupt
640c42a7b7eSSam Leffler 	 * so we can act on stat triggers.
641c42a7b7eSSam Leffler 	 */
642c42a7b7eSSam Leffler 	if (ath_hal_hwphycounters(ah))
643c42a7b7eSSam Leffler 		sc->sc_needmib = 1;
644c42a7b7eSSam Leffler 
645c42a7b7eSSam Leffler 	/*
646c42a7b7eSSam Leffler 	 * Get the hardware key cache size.
647c42a7b7eSSam Leffler 	 */
648c42a7b7eSSam Leffler 	sc->sc_keymax = ath_hal_keycachesize(ah);
649e8fd88a3SSam Leffler 	if (sc->sc_keymax > ATH_KEYMAX) {
65076e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
65176e6fd5dSGleb Smirnoff 		    "Warning, using only %u of %u key cache slots\n",
652e8fd88a3SSam Leffler 		    ATH_KEYMAX, sc->sc_keymax);
653e8fd88a3SSam Leffler 		sc->sc_keymax = ATH_KEYMAX;
654c42a7b7eSSam Leffler 	}
655c42a7b7eSSam Leffler 	/*
656c42a7b7eSSam Leffler 	 * Reset the key cache since some parts do not
657c42a7b7eSSam Leffler 	 * reset the contents on initial power up.
658c42a7b7eSSam Leffler 	 */
659c42a7b7eSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
660c42a7b7eSSam Leffler 		ath_hal_keyreset(ah, i);
661c42a7b7eSSam Leffler 
662c42a7b7eSSam Leffler 	/*
663b032f27cSSam Leffler 	 * Collect the default channel list.
6645591b213SSam Leffler 	 */
665b032f27cSSam Leffler 	error = ath_getchannels(sc);
6665591b213SSam Leffler 	if (error != 0)
6675591b213SSam Leffler 		goto bad;
6685591b213SSam Leffler 
6695591b213SSam Leffler 	/*
6705591b213SSam Leffler 	 * Setup rate tables for all potential media types.
6715591b213SSam Leffler 	 */
6725591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11A);
6735591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11B);
6745591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11G);
675c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
676c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
67768e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_STURBO_A);
67868e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NA);
67968e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NG);
680724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_HALF);
681724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_QUARTER);
682aaa70f2fSSam Leffler 
683c42a7b7eSSam Leffler 	/* NB: setup here so ath_rate_update is happy */
684c42a7b7eSSam Leffler 	ath_setcurmode(sc, IEEE80211_MODE_11A);
6855591b213SSam Leffler 
686c42a7b7eSSam Leffler 	/*
6873fdfc330SAdrian Chadd 	 * Allocate TX descriptors and populate the lists.
688c42a7b7eSSam Leffler 	 */
6895591b213SSam Leffler 	error = ath_desc_alloc(sc);
6905591b213SSam Leffler 	if (error != 0) {
69176e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
69276e6fd5dSGleb Smirnoff 		    "failed to allocate TX descriptors: %d\n", error);
6933fdfc330SAdrian Chadd 		goto bad;
6943fdfc330SAdrian Chadd 	}
6953fdfc330SAdrian Chadd 	error = ath_txdma_setup(sc);
6963fdfc330SAdrian Chadd 	if (error != 0) {
69776e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
69876e6fd5dSGleb Smirnoff 		    "failed to allocate TX descriptors: %d\n", error);
6995591b213SSam Leffler 		goto bad;
7005591b213SSam Leffler 	}
7013d184db2SAdrian Chadd 
7023fdfc330SAdrian Chadd 	/*
7033fdfc330SAdrian Chadd 	 * Allocate RX descriptors and populate the lists.
7043fdfc330SAdrian Chadd 	 */
7053d184db2SAdrian Chadd 	error = ath_rxdma_setup(sc);
7063d184db2SAdrian Chadd 	if (error != 0) {
70776e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
70876e6fd5dSGleb Smirnoff 		     "failed to allocate RX descriptors: %d\n", error);
7093d184db2SAdrian Chadd 		goto bad;
7103d184db2SAdrian Chadd 	}
7113d184db2SAdrian Chadd 
712adcdc8f2SAdrian Chadd 	callout_init_mtx(&sc->sc_cal_ch, &sc->sc_mtx, 0);
713adcdc8f2SAdrian Chadd 	callout_init_mtx(&sc->sc_wd_ch, &sc->sc_mtx, 0);
7145591b213SSam Leffler 
715f0b2a0beSSam Leffler 	ATH_TXBUF_LOCK_INIT(sc);
7165591b213SSam Leffler 
7170bbf5441SSam Leffler 	sc->sc_tq = taskqueue_create("ath_taskq", M_NOWAIT,
7180bbf5441SSam Leffler 		taskqueue_thread_enqueue, &sc->sc_tq);
7197a79cebfSGleb Smirnoff 	taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, "%s taskq",
7207a79cebfSGleb Smirnoff 	    device_get_nameunit(sc->sc_dev));
7210bbf5441SSam Leffler 
722f8cc9b09SAdrian Chadd 	TASK_INIT(&sc->sc_rxtask, 0, sc->sc_rx.recv_tasklet, sc);
7235591b213SSam Leffler 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
724c42a7b7eSSam Leffler 	TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc);
725d52f7132SAdrian Chadd 	TASK_INIT(&sc->sc_resettask,0, ath_reset_proc, sc);
72603e9308fSAdrian Chadd 	TASK_INIT(&sc->sc_txqtask, 0, ath_txq_sched_tasklet, sc);
727f846cf42SAdrian Chadd 	TASK_INIT(&sc->sc_fataltask, 0, ath_fatal_proc, sc);
7285591b213SSam Leffler 
7295591b213SSam Leffler 	/*
730c42a7b7eSSam Leffler 	 * Allocate hardware transmit queues: one queue for
731c42a7b7eSSam Leffler 	 * beacon frames and one data queue for each QoS
7324fa8d4efSDaniel Eischen 	 * priority.  Note that the hal handles resetting
733c42a7b7eSSam Leffler 	 * these queues at the needed time.
734c42a7b7eSSam Leffler 	 *
735c42a7b7eSSam Leffler 	 * XXX PS-Poll
7365591b213SSam Leffler 	 */
737e1252ce1SAdrian Chadd 	sc->sc_bhalq = ath_beaconq_setup(sc);
7385591b213SSam Leffler 	if (sc->sc_bhalq == (u_int) -1) {
73976e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
74076e6fd5dSGleb Smirnoff 		    "unable to setup a beacon xmit queue!\n");
741c42a7b7eSSam Leffler 		error = EIO;
742b28b4653SSam Leffler 		goto bad2;
7435591b213SSam Leffler 	}
744c42a7b7eSSam Leffler 	sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
745c42a7b7eSSam Leffler 	if (sc->sc_cabq == NULL) {
74676e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "unable to setup CAB xmit queue!\n");
747c42a7b7eSSam Leffler 		error = EIO;
748c42a7b7eSSam Leffler 		goto bad2;
749c42a7b7eSSam Leffler 	}
750c42a7b7eSSam Leffler 	/* NB: insure BK queue is the lowest priority h/w queue */
751c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
75276e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
75376e6fd5dSGleb Smirnoff 		    "unable to setup xmit queue for %s traffic!\n",
754c42a7b7eSSam Leffler 		    ieee80211_wme_acnames[WME_AC_BK]);
755c42a7b7eSSam Leffler 		error = EIO;
756c42a7b7eSSam Leffler 		goto bad2;
757c42a7b7eSSam Leffler 	}
758c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
759c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
760c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
761c42a7b7eSSam Leffler 		/*
762c42a7b7eSSam Leffler 		 * Not enough hardware tx queues to properly do WME;
763c42a7b7eSSam Leffler 		 * just punt and assign them all to the same h/w queue.
764c42a7b7eSSam Leffler 		 * We could do a better job of this if, for example,
765c42a7b7eSSam Leffler 		 * we allocate queues when we switch from station to
766c42a7b7eSSam Leffler 		 * AP mode.
767c42a7b7eSSam Leffler 		 */
768c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_VI] != NULL)
769c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
770c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_BE] != NULL)
771c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
772c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
773c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
774c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
775c42a7b7eSSam Leffler 	}
776c42a7b7eSSam Leffler 
777c42a7b7eSSam Leffler 	/*
778f8418db5SAdrian Chadd 	 * Attach the TX completion function.
779f8418db5SAdrian Chadd 	 *
780f8418db5SAdrian Chadd 	 * The non-EDMA chips may have some special case optimisations;
781f8418db5SAdrian Chadd 	 * this method gives everyone a chance to attach cleanly.
782c42a7b7eSSam Leffler 	 */
783f8418db5SAdrian Chadd 	sc->sc_tx.xmit_attach_comp_func(sc);
784c42a7b7eSSam Leffler 
785c42a7b7eSSam Leffler 	/*
786c42a7b7eSSam Leffler 	 * Setup rate control.  Some rate control modules
787c42a7b7eSSam Leffler 	 * call back to change the anntena state so expose
788c42a7b7eSSam Leffler 	 * the necessary entry points.
789c42a7b7eSSam Leffler 	 * XXX maybe belongs in struct ath_ratectrl?
790c42a7b7eSSam Leffler 	 */
791c42a7b7eSSam Leffler 	sc->sc_setdefantenna = ath_setdefantenna;
792c42a7b7eSSam Leffler 	sc->sc_rc = ath_rate_attach(sc);
793c42a7b7eSSam Leffler 	if (sc->sc_rc == NULL) {
794c42a7b7eSSam Leffler 		error = EIO;
795c42a7b7eSSam Leffler 		goto bad2;
796c42a7b7eSSam Leffler 	}
797c42a7b7eSSam Leffler 
79848237774SAdrian Chadd 	/* Attach DFS module */
79948237774SAdrian Chadd 	if (! ath_dfs_attach(sc)) {
8007e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
8017e97436bSAdrian Chadd 		    "%s: unable to attach DFS\n", __func__);
80248237774SAdrian Chadd 		error = EIO;
80348237774SAdrian Chadd 		goto bad2;
80448237774SAdrian Chadd 	}
80548237774SAdrian Chadd 
8069af351f9SAdrian Chadd 	/* Attach spectral module */
8079af351f9SAdrian Chadd 	if (ath_spectral_attach(sc) < 0) {
8089af351f9SAdrian Chadd 		device_printf(sc->sc_dev,
8099af351f9SAdrian Chadd 		    "%s: unable to attach spectral\n", __func__);
8109af351f9SAdrian Chadd 		error = EIO;
8119af351f9SAdrian Chadd 		goto bad2;
8129af351f9SAdrian Chadd 	}
8139af351f9SAdrian Chadd 
814b70f530bSAdrian Chadd 	/* Attach bluetooth coexistence module */
815b70f530bSAdrian Chadd 	if (ath_btcoex_attach(sc) < 0) {
816b70f530bSAdrian Chadd 		device_printf(sc->sc_dev,
817b70f530bSAdrian Chadd 		    "%s: unable to attach bluetooth coexistence\n", __func__);
818b70f530bSAdrian Chadd 		error = EIO;
819b70f530bSAdrian Chadd 		goto bad2;
820b70f530bSAdrian Chadd 	}
821b70f530bSAdrian Chadd 
822216ca234SAdrian Chadd 	/* Attach LNA diversity module */
823216ca234SAdrian Chadd 	if (ath_lna_div_attach(sc) < 0) {
824216ca234SAdrian Chadd 		device_printf(sc->sc_dev,
825216ca234SAdrian Chadd 		    "%s: unable to attach LNA diversity\n", __func__);
826216ca234SAdrian Chadd 		error = EIO;
827216ca234SAdrian Chadd 		goto bad2;
828216ca234SAdrian Chadd 	}
829216ca234SAdrian Chadd 
83048237774SAdrian Chadd 	/* Start DFS processing tasklet */
83148237774SAdrian Chadd 	TASK_INIT(&sc->sc_dfstask, 0, ath_dfs_tasklet, sc);
83248237774SAdrian Chadd 
8333440495aSAdrian Chadd 	/* Configure LED state */
8343e50ec2cSSam Leffler 	sc->sc_blinking = 0;
835c42a7b7eSSam Leffler 	sc->sc_ledstate = 1;
8363e50ec2cSSam Leffler 	sc->sc_ledon = 0;			/* low true */
8373e50ec2cSSam Leffler 	sc->sc_ledidle = (2700*hz)/1000;	/* 2.7sec */
838fd90e2edSJung-uk Kim 	callout_init(&sc->sc_ledtimer, 1);
8393440495aSAdrian Chadd 
8403440495aSAdrian Chadd 	/*
8413440495aSAdrian Chadd 	 * Don't setup hardware-based blinking.
8423440495aSAdrian Chadd 	 *
8433440495aSAdrian Chadd 	 * Although some NICs may have this configured in the
8443440495aSAdrian Chadd 	 * default reset register values, the user may wish
8453440495aSAdrian Chadd 	 * to alter which pins have which function.
8463440495aSAdrian Chadd 	 *
8473440495aSAdrian Chadd 	 * The reference driver attaches the MAC network LED to GPIO1 and
8483440495aSAdrian Chadd 	 * the MAC power LED to GPIO2.  However, the DWA-552 cardbus
8493440495aSAdrian Chadd 	 * NIC has these reversed.
8503440495aSAdrian Chadd 	 */
8513440495aSAdrian Chadd 	sc->sc_hardled = (1 == 0);
8523440495aSAdrian Chadd 	sc->sc_led_net_pin = -1;
8533440495aSAdrian Chadd 	sc->sc_led_pwr_pin = -1;
854c42a7b7eSSam Leffler 	/*
855c42a7b7eSSam Leffler 	 * Auto-enable soft led processing for IBM cards and for
856c42a7b7eSSam Leffler 	 * 5211 minipci cards.  Users can also manually enable/disable
857c42a7b7eSSam Leffler 	 * support with a sysctl.
858c42a7b7eSSam Leffler 	 */
859c42a7b7eSSam Leffler 	sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
8606558ffd9SAdrian Chadd 	ath_led_config(sc);
861a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
8625591b213SSam Leffler 
8635591b213SSam Leffler 	/* XXX not right but it's not used anywhere important */
8645591b213SSam Leffler 	ic->ic_phytype = IEEE80211_T_OFDM;
8655591b213SSam Leffler 	ic->ic_opmode = IEEE80211_M_STA;
866c42a7b7eSSam Leffler 	ic->ic_caps =
867c43feedeSSam Leffler 		  IEEE80211_C_STA		/* station mode */
868c43feedeSSam Leffler 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
869fe32c3efSSam Leffler 		| IEEE80211_C_HOSTAP		/* hostap mode */
870fe32c3efSSam Leffler 		| IEEE80211_C_MONITOR		/* monitor mode */
8717a04dc27SSam Leffler 		| IEEE80211_C_AHDEMO		/* adhoc demo mode */
872b032f27cSSam Leffler 		| IEEE80211_C_WDS		/* 4-address traffic works */
87359aa14a9SRui Paulo 		| IEEE80211_C_MBSS		/* mesh point link mode */
874fe32c3efSSam Leffler 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
875c42a7b7eSSam Leffler 		| IEEE80211_C_SHSLOT		/* short slot time supported */
876c42a7b7eSSam Leffler 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
8773b324f57SAdrian Chadd #ifndef	ATH_ENABLE_11N
87868e8e04eSSam Leffler 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
8793b324f57SAdrian Chadd #endif
88068e8e04eSSam Leffler 		| IEEE80211_C_TXFRAG		/* handle tx frags */
88110dc8de4SAdrian Chadd #ifdef	ATH_ENABLE_DFS
8827e97436bSAdrian Chadd 		| IEEE80211_C_DFS		/* Enable radar detection */
88310dc8de4SAdrian Chadd #endif
884f5c30c4eSAdrian Chadd 		| IEEE80211_C_PMGT		/* Station side power mgmt */
885f5c30c4eSAdrian Chadd 		| IEEE80211_C_SWSLEEP
88601e7e035SSam Leffler 		;
887c42a7b7eSSam Leffler 	/*
888c42a7b7eSSam Leffler 	 * Query the hal to figure out h/w crypto support.
889c42a7b7eSSam Leffler 	 */
890c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
891b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
892c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
893b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_OCB;
894c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
895b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_CCM;
896c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
897b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_CKIP;
898c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
899b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIP;
900c42a7b7eSSam Leffler 		/*
901c42a7b7eSSam Leffler 		 * Check if h/w does the MIC and/or whether the
902c42a7b7eSSam Leffler 		 * separate key cache entries are required to
903c42a7b7eSSam Leffler 		 * handle both tx+rx MIC keys.
904c42a7b7eSSam Leffler 		 */
905c42a7b7eSSam Leffler 		if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
906b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
9075901d2d3SSam Leffler 		/*
9085901d2d3SSam Leffler 		 * If the h/w supports storing tx+rx MIC keys
9095901d2d3SSam Leffler 		 * in one cache slot automatically enable use.
9105901d2d3SSam Leffler 		 */
9115901d2d3SSam Leffler 		if (ath_hal_hastkipsplit(ah) ||
9125901d2d3SSam Leffler 		    !ath_hal_settkipsplit(ah, AH_FALSE))
913c42a7b7eSSam Leffler 			sc->sc_splitmic = 1;
914b032f27cSSam Leffler 		/*
915b032f27cSSam Leffler 		 * If the h/w can do TKIP MIC together with WME then
916b032f27cSSam Leffler 		 * we use it; otherwise we force the MIC to be done
917b032f27cSSam Leffler 		 * in software by the net80211 layer.
918b032f27cSSam Leffler 		 */
919b032f27cSSam Leffler 		if (ath_hal_haswmetkipmic(ah))
920b032f27cSSam Leffler 			sc->sc_wmetkipmic = 1;
921c42a7b7eSSam Leffler 	}
922e8fd88a3SSam Leffler 	sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR);
9239ac01d39SRui Paulo 	/*
9241ac5dac2SRui Paulo 	 * Check for multicast key search support.
9259ac01d39SRui Paulo 	 */
9269ac01d39SRui Paulo 	if (ath_hal_hasmcastkeysearch(sc->sc_ah) &&
9279ac01d39SRui Paulo 	    !ath_hal_getmcastkeysearch(sc->sc_ah)) {
9289ac01d39SRui Paulo 		ath_hal_setmcastkeysearch(sc->sc_ah, 1);
9299ac01d39SRui Paulo 	}
930e8fd88a3SSam Leffler 	sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah);
931c42a7b7eSSam Leffler 	/*
9325901d2d3SSam Leffler 	 * Mark key cache slots associated with global keys
9335901d2d3SSam Leffler 	 * as in use.  If we knew TKIP was not to be used we
9345901d2d3SSam Leffler 	 * could leave the +32, +64, and +32+64 slots free.
9355901d2d3SSam Leffler 	 */
9365901d2d3SSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
9375901d2d3SSam Leffler 		setbit(sc->sc_keymap, i);
9385901d2d3SSam Leffler 		setbit(sc->sc_keymap, i+64);
9395901d2d3SSam Leffler 		if (sc->sc_splitmic) {
9405901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32);
9415901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32+64);
9425901d2d3SSam Leffler 		}
9435901d2d3SSam Leffler 	}
9445901d2d3SSam Leffler 	/*
945c42a7b7eSSam Leffler 	 * TPC support can be done either with a global cap or
946c42a7b7eSSam Leffler 	 * per-packet support.  The latter is not available on
947c42a7b7eSSam Leffler 	 * all parts.  We're a bit pedantic here as all parts
948c42a7b7eSSam Leffler 	 * support a global cap.
949c42a7b7eSSam Leffler 	 */
950c59005e9SSam Leffler 	if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah))
951c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TXPMGT;
952c42a7b7eSSam Leffler 
953c42a7b7eSSam Leffler 	/*
954c42a7b7eSSam Leffler 	 * Mark WME capability only if we have sufficient
955c42a7b7eSSam Leffler 	 * hardware queues to do proper priority scheduling.
956c42a7b7eSSam Leffler 	 */
957c42a7b7eSSam Leffler 	if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
958c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_WME;
959c42a7b7eSSam Leffler 	/*
960e8fd88a3SSam Leffler 	 * Check for misc other capabilities.
961c42a7b7eSSam Leffler 	 */
962c42a7b7eSSam Leffler 	if (ath_hal_hasbursting(ah))
963c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_BURST;
964b032f27cSSam Leffler 	sc->sc_hasbmask = ath_hal_hasbssidmask(ah);
96559aa14a9SRui Paulo 	sc->sc_hasbmatch = ath_hal_hasbssidmatch(ah);
966b032f27cSSam Leffler 	sc->sc_hastsfadd = ath_hal_hastsfadjust(ah);
9678a2a6beeSAdrian Chadd 	sc->sc_rxslink = ath_hal_self_linked_final_rxdesc(ah);
968fc4de9b7SAdrian Chadd 	sc->sc_rxtsf32 = ath_hal_has_long_rxdesc_tsf(ah);
969dd6a574eSAdrian Chadd 	sc->sc_hasenforcetxop = ath_hal_hasenforcetxop(ah);
9703df7a8abSAdrian Chadd 	sc->sc_rx_lnamixer = ath_hal_hasrxlnamixer(ah);
971216ca234SAdrian Chadd 	sc->sc_hasdivcomb = ath_hal_hasdivantcomb(ah);
972216ca234SAdrian Chadd 
97368e8e04eSSam Leffler 	if (ath_hal_hasfastframes(ah))
97468e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_FF;
97559efa8b5SSam Leffler 	wmodes = ath_hal_getwirelessmodes(ah);
976411373ebSSam Leffler 	if (wmodes & (HAL_MODE_108G|HAL_MODE_TURBO))
97768e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TURBOP;
978584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
97910ad9a77SSam Leffler 	if (ath_hal_macversion(ah) > 0x78) {
98010ad9a77SSam Leffler 		ic->ic_caps |= IEEE80211_C_TDMA; /* capable of TDMA */
98110ad9a77SSam Leffler 		ic->ic_tdma_update = ath_tdma_update;
98210ad9a77SSam Leffler 	}
98310ad9a77SSam Leffler #endif
98467397d39SAdrian Chadd 
98567397d39SAdrian Chadd 	/*
9869c85ff91SAdrian Chadd 	 * TODO: enforce that at least this many frames are available
9879c85ff91SAdrian Chadd 	 * in the txbuf list before allowing data frames (raw or
9889c85ff91SAdrian Chadd 	 * otherwise) to be transmitted.
9899c85ff91SAdrian Chadd 	 */
9909c85ff91SAdrian Chadd 	sc->sc_txq_data_minfree = 10;
9919c85ff91SAdrian Chadd 	/*
9929c85ff91SAdrian Chadd 	 * Leave this as default to maintain legacy behaviour.
9939c85ff91SAdrian Chadd 	 * Shortening the cabq/mcastq may end up causing some
9949c85ff91SAdrian Chadd 	 * undesirable behaviour.
9959c85ff91SAdrian Chadd 	 */
9969c85ff91SAdrian Chadd 	sc->sc_txq_mcastq_maxdepth = ath_txbuf;
9979c85ff91SAdrian Chadd 
9987dcb2beaSAdrian Chadd 	/*
99922a3aee6SAdrian Chadd 	 * How deep can the node software TX queue get whilst it's asleep.
100022a3aee6SAdrian Chadd 	 */
100122a3aee6SAdrian Chadd 	sc->sc_txq_node_psq_maxdepth = 16;
100222a3aee6SAdrian Chadd 
100322a3aee6SAdrian Chadd 	/*
10047dcb2beaSAdrian Chadd 	 * Default the maximum queue depth for a given node
10057dcb2beaSAdrian Chadd 	 * to 1/4'th the TX buffers, or 64, whichever
10067dcb2beaSAdrian Chadd 	 * is larger.
10077dcb2beaSAdrian Chadd 	 */
10087dcb2beaSAdrian Chadd 	sc->sc_txq_node_maxdepth = MAX(64, ath_txbuf / 4);
10097dcb2beaSAdrian Chadd 
1010b837332dSAdrian Chadd 	/* Enable CABQ by default */
1011b837332dSAdrian Chadd 	sc->sc_cabq_enable = 1;
1012b837332dSAdrian Chadd 
10139c85ff91SAdrian Chadd 	/*
1014a865860dSAdrian Chadd 	 * Allow the TX and RX chainmasks to be overridden by
1015a865860dSAdrian Chadd 	 * environment variables and/or device.hints.
1016a865860dSAdrian Chadd 	 *
1017a865860dSAdrian Chadd 	 * This must be done early - before the hardware is
1018a865860dSAdrian Chadd 	 * calibrated or before the 802.11n stream calculation
1019a865860dSAdrian Chadd 	 * is done.
1020a865860dSAdrian Chadd 	 */
1021a865860dSAdrian Chadd 	if (resource_int_value(device_get_name(sc->sc_dev),
1022a865860dSAdrian Chadd 	    device_get_unit(sc->sc_dev), "rx_chainmask",
1023a865860dSAdrian Chadd 	    &rx_chainmask) == 0) {
1024a865860dSAdrian Chadd 		device_printf(sc->sc_dev, "Setting RX chainmask to 0x%x\n",
1025a865860dSAdrian Chadd 		    rx_chainmask);
1026a865860dSAdrian Chadd 		(void) ath_hal_setrxchainmask(sc->sc_ah, rx_chainmask);
1027a865860dSAdrian Chadd 	}
1028a865860dSAdrian Chadd 	if (resource_int_value(device_get_name(sc->sc_dev),
1029a865860dSAdrian Chadd 	    device_get_unit(sc->sc_dev), "tx_chainmask",
1030a865860dSAdrian Chadd 	    &tx_chainmask) == 0) {
1031a865860dSAdrian Chadd 		device_printf(sc->sc_dev, "Setting TX chainmask to 0x%x\n",
1032a865860dSAdrian Chadd 		    tx_chainmask);
1033dc8552d5SAdrian Chadd 		(void) ath_hal_settxchainmask(sc->sc_ah, tx_chainmask);
1034a865860dSAdrian Chadd 	}
1035a865860dSAdrian Chadd 
1036af017101SAdrian Chadd 	/*
1037ff5b5634SAdrian Chadd 	 * Query the TX/RX chainmask configuration.
1038ff5b5634SAdrian Chadd 	 *
1039ff5b5634SAdrian Chadd 	 * This is only relevant for 11n devices.
1040ff5b5634SAdrian Chadd 	 */
1041ff5b5634SAdrian Chadd 	ath_hal_getrxchainmask(ah, &sc->sc_rxchainmask);
1042ff5b5634SAdrian Chadd 	ath_hal_gettxchainmask(ah, &sc->sc_txchainmask);
1043ff5b5634SAdrian Chadd 
1044ff5b5634SAdrian Chadd 	/*
1045af017101SAdrian Chadd 	 * Disable MRR with protected frames by default.
1046af017101SAdrian Chadd 	 * Only 802.11n series NICs can handle this.
1047af017101SAdrian Chadd 	 */
1048af017101SAdrian Chadd 	sc->sc_mrrprot = 0;	/* XXX should be a capability */
1049af017101SAdrian Chadd 
10505540369bSAdrian Chadd 	/*
10515540369bSAdrian Chadd 	 * Query the enterprise mode information the HAL.
10525540369bSAdrian Chadd 	 */
10535540369bSAdrian Chadd 	if (ath_hal_getcapability(ah, HAL_CAP_ENTERPRISE_MODE, 0,
10545540369bSAdrian Chadd 	    &sc->sc_ent_cfg) == HAL_OK)
10555540369bSAdrian Chadd 		sc->sc_use_ent = 1;
10565540369bSAdrian Chadd 
10578fd67f92SAdrian Chadd #ifdef	ATH_ENABLE_11N
105867397d39SAdrian Chadd 	/*
105967397d39SAdrian Chadd 	 * Query HT capabilities
106067397d39SAdrian Chadd 	 */
106167397d39SAdrian Chadd 	if (ath_hal_getcapability(ah, HAL_CAP_HT, 0, NULL) == HAL_OK &&
106267397d39SAdrian Chadd 	    (wmodes & (HAL_MODE_HT20 | HAL_MODE_HT40))) {
10636f4fb2d8SAdrian Chadd 		uint32_t rxs, txs;
106467397d39SAdrian Chadd 
106567397d39SAdrian Chadd 		device_printf(sc->sc_dev, "[HT] enabling HT modes\n");
1066af017101SAdrian Chadd 
1067af017101SAdrian Chadd 		sc->sc_mrrprot = 1;	/* XXX should be a capability */
1068af017101SAdrian Chadd 
106967397d39SAdrian Chadd 		ic->ic_htcaps = IEEE80211_HTC_HT	/* HT operation */
107067397d39SAdrian Chadd 			    | IEEE80211_HTC_AMPDU	/* A-MPDU tx/rx */
107167397d39SAdrian Chadd 			    | IEEE80211_HTC_AMSDU	/* A-MSDU tx/rx */
10727e97436bSAdrian Chadd 			    | IEEE80211_HTCAP_MAXAMSDU_3839
10737e97436bSAdrian Chadd 			    				/* max A-MSDU length */
107467397d39SAdrian Chadd 			    | IEEE80211_HTCAP_SMPS_OFF;	/* SM power save off */
107567397d39SAdrian Chadd 			;
107667397d39SAdrian Chadd 
107776355edbSAdrian Chadd 		/*
107876355edbSAdrian Chadd 		 * Enable short-GI for HT20 only if the hardware
107976355edbSAdrian Chadd 		 * advertises support.
108076355edbSAdrian Chadd 		 * Notably, anything earlier than the AR9287 doesn't.
108176355edbSAdrian Chadd 		 */
108276355edbSAdrian Chadd 		if ((ath_hal_getcapability(ah,
108376355edbSAdrian Chadd 		    HAL_CAP_HT20_SGI, 0, NULL) == HAL_OK) &&
108476355edbSAdrian Chadd 		    (wmodes & HAL_MODE_HT20)) {
108576355edbSAdrian Chadd 			device_printf(sc->sc_dev,
108676355edbSAdrian Chadd 			    "[HT] enabling short-GI in 20MHz mode\n");
108776355edbSAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
108876355edbSAdrian Chadd 		}
108976355edbSAdrian Chadd 
109067397d39SAdrian Chadd 		if (wmodes & HAL_MODE_HT40)
109167397d39SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40
109267397d39SAdrian Chadd 			    |  IEEE80211_HTCAP_SHORTGI40;
109367397d39SAdrian Chadd 
109467397d39SAdrian Chadd 		/*
10957e97436bSAdrian Chadd 		 * TX/RX streams need to be taken into account when
10967e97436bSAdrian Chadd 		 * negotiating which MCS rates it'll receive and
109767397d39SAdrian Chadd 		 * what MCS rates are available for TX.
109867397d39SAdrian Chadd 		 */
109954517070SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 0, &txs);
110054517070SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 1, &rxs);
110167397d39SAdrian Chadd 		ic->ic_txstream = txs;
110267397d39SAdrian Chadd 		ic->ic_rxstream = rxs;
110367397d39SAdrian Chadd 
11046606ba81SAdrian Chadd 		/*
11056606ba81SAdrian Chadd 		 * Setup TX and RX STBC based on what the HAL allows and
11066606ba81SAdrian Chadd 		 * the currently configured chainmask set.
11076606ba81SAdrian Chadd 		 * Ie - don't enable STBC TX if only one chain is enabled.
11086606ba81SAdrian Chadd 		 * STBC RX is fine on a single RX chain; it just won't
11096606ba81SAdrian Chadd 		 * provide any real benefit.
11106606ba81SAdrian Chadd 		 */
11116606ba81SAdrian Chadd 		if (ath_hal_getcapability(ah, HAL_CAP_RX_STBC, 0,
11126606ba81SAdrian Chadd 		    NULL) == HAL_OK) {
11136606ba81SAdrian Chadd 			sc->sc_rx_stbc = 1;
11146606ba81SAdrian Chadd 			device_printf(sc->sc_dev,
11156606ba81SAdrian Chadd 			    "[HT] 1 stream STBC receive enabled\n");
11166606ba81SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_1STREAM;
11176606ba81SAdrian Chadd 		}
11186606ba81SAdrian Chadd 		if (txs > 1 && ath_hal_getcapability(ah, HAL_CAP_TX_STBC, 0,
11196606ba81SAdrian Chadd 		    NULL) == HAL_OK) {
11206606ba81SAdrian Chadd 			sc->sc_tx_stbc = 1;
11216606ba81SAdrian Chadd 			device_printf(sc->sc_dev,
11226606ba81SAdrian Chadd 			    "[HT] 1 stream STBC transmit enabled\n");
11236606ba81SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
11246606ba81SAdrian Chadd 		}
11256606ba81SAdrian Chadd 
1126ce656facSAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_RTS_AGGR_LIMIT, 1,
1127ce656facSAdrian Chadd 		    &sc->sc_rts_aggr_limit);
1128ce656facSAdrian Chadd 		if (sc->sc_rts_aggr_limit != (64 * 1024))
1129ce656facSAdrian Chadd 			device_printf(sc->sc_dev,
1130ce656facSAdrian Chadd 			    "[HT] RTS aggregates limited to %d KiB\n",
1131ce656facSAdrian Chadd 			    sc->sc_rts_aggr_limit / 1024);
1132ce656facSAdrian Chadd 
11337e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
11347e97436bSAdrian Chadd 		    "[HT] %d RX streams; %d TX streams\n", rxs, txs);
113567397d39SAdrian Chadd 	}
113667397d39SAdrian Chadd #endif
113767397d39SAdrian Chadd 
1138c42a7b7eSSam Leffler 	/*
1139f8aa9fd5SAdrian Chadd 	 * Initial aggregation settings.
1140f8aa9fd5SAdrian Chadd 	 */
114172910f03SAdrian Chadd 	sc->sc_hwq_limit_aggr = ATH_AGGR_MIN_QDEPTH;
114272910f03SAdrian Chadd 	sc->sc_hwq_limit_nonaggr = ATH_NONAGGR_MIN_QDEPTH;
1143f8aa9fd5SAdrian Chadd 	sc->sc_tid_hwq_lo = ATH_AGGR_SCHED_LOW;
1144f8aa9fd5SAdrian Chadd 	sc->sc_tid_hwq_hi = ATH_AGGR_SCHED_HIGH;
11454a502c33SAdrian Chadd 	sc->sc_aggr_limit = ATH_AGGR_MAXSIZE;
1146a54ecf78SAdrian Chadd 	sc->sc_delim_min_pad = 0;
1147f8aa9fd5SAdrian Chadd 
1148f8aa9fd5SAdrian Chadd 	/*
1149ddbe3036SAdrian Chadd 	 * Check if the hardware requires PCI register serialisation.
1150ddbe3036SAdrian Chadd 	 * Some of the Owl based MACs require this.
1151ddbe3036SAdrian Chadd 	 */
1152ddbe3036SAdrian Chadd 	if (mp_ncpus > 1 &&
1153ddbe3036SAdrian Chadd 	    ath_hal_getcapability(ah, HAL_CAP_SERIALISE_WAR,
1154ddbe3036SAdrian Chadd 	     0, NULL) == HAL_OK) {
1155ddbe3036SAdrian Chadd 		sc->sc_ah->ah_config.ah_serialise_reg_war = 1;
11567e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
11577e97436bSAdrian Chadd 		    "Enabling register serialisation\n");
1158ddbe3036SAdrian Chadd 	}
1159ddbe3036SAdrian Chadd 
1160ddbe3036SAdrian Chadd 	/*
1161f0db652cSAdrian Chadd 	 * Initialise the deferred completed RX buffer list.
1162f0db652cSAdrian Chadd 	 */
11635d4dedadSAdrian Chadd 	TAILQ_INIT(&sc->sc_rx_rxlist[HAL_RX_QUEUE_HP]);
11645d4dedadSAdrian Chadd 	TAILQ_INIT(&sc->sc_rx_rxlist[HAL_RX_QUEUE_LP]);
1165f0db652cSAdrian Chadd 
1166f0db652cSAdrian Chadd 	/*
1167c42a7b7eSSam Leffler 	 * Indicate we need the 802.11 header padded to a
1168c42a7b7eSSam Leffler 	 * 32-bit boundary for 4-address and QoS frames.
1169c42a7b7eSSam Leffler 	 */
1170c42a7b7eSSam Leffler 	ic->ic_flags |= IEEE80211_F_DATAPAD;
1171c42a7b7eSSam Leffler 
1172c42a7b7eSSam Leffler 	/*
1173c42a7b7eSSam Leffler 	 * Query the hal about antenna support.
1174c42a7b7eSSam Leffler 	 */
1175c42a7b7eSSam Leffler 	sc->sc_defant = ath_hal_getdefantenna(ah);
1176c42a7b7eSSam Leffler 
1177c42a7b7eSSam Leffler 	/*
1178c42a7b7eSSam Leffler 	 * Not all chips have the VEOL support we want to
1179c42a7b7eSSam Leffler 	 * use with IBSS beacons; check here for it.
1180c42a7b7eSSam Leffler 	 */
1181c42a7b7eSSam Leffler 	sc->sc_hasveol = ath_hal_hasveol(ah);
11825591b213SSam Leffler 
1183240b1f1dSAdrian Chadd 	/* get mac address from kenv first, then hardware */
11847a79cebfSGleb Smirnoff 	if (ath_fetch_mac_kenv(sc, ic->ic_macaddr) == 0) {
11859cecaef7SAdrian Chadd 		/* Tell the HAL now about the new MAC */
11867a79cebfSGleb Smirnoff 		ath_hal_setmac(ah, ic->ic_macaddr);
11879cecaef7SAdrian Chadd 	} else {
11887a79cebfSGleb Smirnoff 		ath_hal_getmac(ah, ic->ic_macaddr);
11899cecaef7SAdrian Chadd 	}
1190240b1f1dSAdrian Chadd 
1191b032f27cSSam Leffler 	if (sc->sc_hasbmask)
1192b032f27cSSam Leffler 		ath_hal_getbssidmask(ah, sc->sc_hwbssidmask);
11935591b213SSam Leffler 
1194b032f27cSSam Leffler 	/* NB: used to size node table key mapping array */
1195b032f27cSSam Leffler 	ic->ic_max_keyix = sc->sc_keymax;
11965591b213SSam Leffler 	/* call MI attach routine. */
11977a79cebfSGleb Smirnoff 	ieee80211_ifattach(ic);
1198b032f27cSSam Leffler 	ic->ic_setregdomain = ath_setregdomain;
1199b032f27cSSam Leffler 	ic->ic_getradiocaps = ath_getradiocaps;
1200b032f27cSSam Leffler 	sc->sc_opmode = HAL_M_STA;
1201b032f27cSSam Leffler 
12025591b213SSam Leffler 	/* override default methods */
12037a79cebfSGleb Smirnoff 	ic->ic_ioctl = ath_ioctl;
12047a79cebfSGleb Smirnoff 	ic->ic_parent = ath_parent;
12057a79cebfSGleb Smirnoff 	ic->ic_transmit = ath_transmit;
1206b032f27cSSam Leffler 	ic->ic_newassoc = ath_newassoc;
1207b032f27cSSam Leffler 	ic->ic_updateslot = ath_updateslot;
1208b032f27cSSam Leffler 	ic->ic_wme.wme_update = ath_wme_update;
1209b032f27cSSam Leffler 	ic->ic_vap_create = ath_vap_create;
1210b032f27cSSam Leffler 	ic->ic_vap_delete = ath_vap_delete;
1211b032f27cSSam Leffler 	ic->ic_raw_xmit = ath_raw_xmit;
1212b032f27cSSam Leffler 	ic->ic_update_mcast = ath_update_mcast;
1213b032f27cSSam Leffler 	ic->ic_update_promisc = ath_update_promisc;
12145591b213SSam Leffler 	ic->ic_node_alloc = ath_node_alloc;
12151e774079SSam Leffler 	sc->sc_node_free = ic->ic_node_free;
12165591b213SSam Leffler 	ic->ic_node_free = ath_node_free;
12174afa805eSAdrian Chadd 	sc->sc_node_cleanup = ic->ic_node_cleanup;
12184afa805eSAdrian Chadd 	ic->ic_node_cleanup = ath_node_cleanup;
121968e8e04eSSam Leffler 	ic->ic_node_getsignal = ath_node_getsignal;
122068e8e04eSSam Leffler 	ic->ic_scan_start = ath_scan_start;
122168e8e04eSSam Leffler 	ic->ic_scan_end = ath_scan_end;
122268e8e04eSSam Leffler 	ic->ic_set_channel = ath_set_channel;
1223fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
1224eb6f0de0SAdrian Chadd 	/* 802.11n specific - but just override anyway */
1225eb6f0de0SAdrian Chadd 	sc->sc_addba_request = ic->ic_addba_request;
1226eb6f0de0SAdrian Chadd 	sc->sc_addba_response = ic->ic_addba_response;
1227eb6f0de0SAdrian Chadd 	sc->sc_addba_stop = ic->ic_addba_stop;
1228eb6f0de0SAdrian Chadd 	sc->sc_bar_response = ic->ic_bar_response;
1229eb6f0de0SAdrian Chadd 	sc->sc_addba_response_timeout = ic->ic_addba_response_timeout;
1230eb6f0de0SAdrian Chadd 
1231eb6f0de0SAdrian Chadd 	ic->ic_addba_request = ath_addba_request;
1232eb6f0de0SAdrian Chadd 	ic->ic_addba_response = ath_addba_response;
1233eb6f0de0SAdrian Chadd 	ic->ic_addba_response_timeout = ath_addba_response_timeout;
1234eb6f0de0SAdrian Chadd 	ic->ic_addba_stop = ath_addba_stop;
1235eb6f0de0SAdrian Chadd 	ic->ic_bar_response = ath_bar_response;
1236eb6f0de0SAdrian Chadd 
1237fdd72b4aSAdrian Chadd 	ic->ic_update_chw = ath_update_chw;
1238fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
1239fdd72b4aSAdrian Chadd 
1240e1b5ab97SAdrian Chadd #ifdef	ATH_ENABLE_RADIOTAP_VENDOR_EXT
1241e1b5ab97SAdrian Chadd 	/*
1242e1b5ab97SAdrian Chadd 	 * There's one vendor bitmap entry in the RX radiotap
1243e1b5ab97SAdrian Chadd 	 * header; make sure that's taken into account.
1244e1b5ab97SAdrian Chadd 	 */
1245e1b5ab97SAdrian Chadd 	ieee80211_radiotap_attachv(ic,
1246e1b5ab97SAdrian Chadd 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th), 0,
1247e1b5ab97SAdrian Chadd 		ATH_TX_RADIOTAP_PRESENT,
1248e1b5ab97SAdrian Chadd 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th), 1,
1249e1b5ab97SAdrian Chadd 		ATH_RX_RADIOTAP_PRESENT);
1250e1b5ab97SAdrian Chadd #else
1251e1b5ab97SAdrian Chadd 	/*
1252e1b5ab97SAdrian Chadd 	 * No vendor bitmap/extensions are present.
1253e1b5ab97SAdrian Chadd 	 */
12545463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
12555463c4a4SSam Leffler 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
12565463c4a4SSam Leffler 		ATH_TX_RADIOTAP_PRESENT,
12575463c4a4SSam Leffler 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
12585463c4a4SSam Leffler 		ATH_RX_RADIOTAP_PRESENT);
1259e1b5ab97SAdrian Chadd #endif	/* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
12605463c4a4SSam Leffler 
12614866e6c2SSam Leffler 	/*
1262bdbb6e5bSAdrian Chadd 	 * Setup the ALQ logging if required
1263bdbb6e5bSAdrian Chadd 	 */
126489d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
1265bdbb6e5bSAdrian Chadd 	if_ath_alq_init(&sc->sc_alq, device_get_nameunit(sc->sc_dev));
1266bb327d28SAdrian Chadd 	if_ath_alq_setcfg(&sc->sc_alq,
1267bb327d28SAdrian Chadd 	    sc->sc_ah->ah_macVersion,
1268bb327d28SAdrian Chadd 	    sc->sc_ah->ah_macRev,
1269bb327d28SAdrian Chadd 	    sc->sc_ah->ah_phyRev,
1270bb327d28SAdrian Chadd 	    sc->sc_ah->ah_magic);
1271bdbb6e5bSAdrian Chadd #endif
1272bdbb6e5bSAdrian Chadd 
1273bdbb6e5bSAdrian Chadd 	/*
12744866e6c2SSam Leffler 	 * Setup dynamic sysctl's now that country code and
12754866e6c2SSam Leffler 	 * regdomain are available from the hal.
12764866e6c2SSam Leffler 	 */
12774866e6c2SSam Leffler 	ath_sysctlattach(sc);
1278e8dabfbeSAdrian Chadd 	ath_sysctl_stats_attach(sc);
127937931a35SAdrian Chadd 	ath_sysctl_hal_attach(sc);
128073454c73SSam Leffler 
1281c42a7b7eSSam Leffler 	if (bootverbose)
1282c42a7b7eSSam Leffler 		ieee80211_announce(ic);
1283c42a7b7eSSam Leffler 	ath_announce(sc);
1284f5c30c4eSAdrian Chadd 
1285f5c30c4eSAdrian Chadd 	/*
1286f5c30c4eSAdrian Chadd 	 * Put it to sleep for now.
1287f5c30c4eSAdrian Chadd 	 */
1288f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1289f5c30c4eSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_FULL_SLEEP);
1290f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1291f5c30c4eSAdrian Chadd 
12925591b213SSam Leffler 	return 0;
1293b28b4653SSam Leffler bad2:
1294c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
1295b28b4653SSam Leffler 	ath_desc_free(sc);
12963fdfc330SAdrian Chadd 	ath_txdma_teardown(sc);
12973d184db2SAdrian Chadd 	ath_rxdma_teardown(sc);
12985591b213SSam Leffler bad:
12995591b213SSam Leffler 	if (ah)
13005591b213SSam Leffler 		ath_hal_detach(ah);
13015591b213SSam Leffler 	sc->sc_invalid = 1;
13025591b213SSam Leffler 	return error;
13035591b213SSam Leffler }
13045591b213SSam Leffler 
13055591b213SSam Leffler int
13065591b213SSam Leffler ath_detach(struct ath_softc *sc)
13075591b213SSam Leffler {
13085591b213SSam Leffler 
1309c42a7b7eSSam Leffler 	/*
1310c42a7b7eSSam Leffler 	 * NB: the order of these is important:
131171b85077SSam Leffler 	 * o stop the chip so no more interrupts will fire
1312c42a7b7eSSam Leffler 	 * o call the 802.11 layer before detaching the hal to
1313c42a7b7eSSam Leffler 	 *   insure callbacks into the driver to delete global
1314c42a7b7eSSam Leffler 	 *   key cache entries can be handled
131571b85077SSam Leffler 	 * o free the taskqueue which drains any pending tasks
1316c42a7b7eSSam Leffler 	 * o reclaim the tx queue data structures after calling
1317c42a7b7eSSam Leffler 	 *   the 802.11 layer as we'll get called back to reclaim
1318c42a7b7eSSam Leffler 	 *   node state and potentially want to use them
1319c42a7b7eSSam Leffler 	 * o to cleanup the tx queues the hal is called, so detach
1320c42a7b7eSSam Leffler 	 *   it last
1321c42a7b7eSSam Leffler 	 * Other than that, it's straightforward...
1322c42a7b7eSSam Leffler 	 */
1323f5c30c4eSAdrian Chadd 
1324f5c30c4eSAdrian Chadd 	/*
1325f5c30c4eSAdrian Chadd 	 * XXX Wake the hardware up first.  ath_stop() will still
1326f5c30c4eSAdrian Chadd 	 * wake it up first, but I'd rather do it here just to
1327f5c30c4eSAdrian Chadd 	 * ensure it's awake.
1328f5c30c4eSAdrian Chadd 	 */
1329f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1330f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1331f5c30c4eSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_AWAKE);
1332f5c30c4eSAdrian Chadd 
1333f5c30c4eSAdrian Chadd 	/*
1334f5c30c4eSAdrian Chadd 	 * Stop things cleanly.
1335f5c30c4eSAdrian Chadd 	 */
13367a79cebfSGleb Smirnoff 	ath_stop(sc);
13377a79cebfSGleb Smirnoff 	ATH_UNLOCK(sc);
1338f5c30c4eSAdrian Chadd 
13397a79cebfSGleb Smirnoff 	ieee80211_ifdetach(&sc->sc_ic);
134071b85077SSam Leffler 	taskqueue_free(sc->sc_tq);
134186e07743SSam Leffler #ifdef ATH_TX99_DIAG
134286e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
134386e07743SSam Leffler 		sc->sc_tx99->detach(sc->sc_tx99);
134486e07743SSam Leffler #endif
1345c42a7b7eSSam Leffler 	ath_rate_detach(sc->sc_rc);
134689d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
1347bdbb6e5bSAdrian Chadd 	if_ath_alq_tidyup(&sc->sc_alq);
1348bdbb6e5bSAdrian Chadd #endif
1349216ca234SAdrian Chadd 	ath_lna_div_detach(sc);
1350b70f530bSAdrian Chadd 	ath_btcoex_detach(sc);
13519af351f9SAdrian Chadd 	ath_spectral_detach(sc);
135248237774SAdrian Chadd 	ath_dfs_detach(sc);
13535591b213SSam Leffler 	ath_desc_free(sc);
13544bf404eaSAdrian Chadd 	ath_txdma_teardown(sc);
13553d184db2SAdrian Chadd 	ath_rxdma_teardown(sc);
1356c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
135771b85077SSam Leffler 	ath_hal_detach(sc->sc_ah);	/* NB: sets chip in full sleep */
1358a93c5097SAdrian Chadd 
13595591b213SSam Leffler 	return 0;
13605591b213SSam Leffler }
13615591b213SSam Leffler 
1362b032f27cSSam Leffler /*
1363b032f27cSSam Leffler  * MAC address handling for multiple BSS on the same radio.
1364b032f27cSSam Leffler  * The first vap uses the MAC address from the EEPROM.  For
1365b032f27cSSam Leffler  * subsequent vap's we set the U/L bit (bit 1) in the MAC
1366b032f27cSSam Leffler  * address and use the next six bits as an index.
1367b032f27cSSam Leffler  */
1368b032f27cSSam Leffler static void
1369b032f27cSSam Leffler assign_address(struct ath_softc *sc, uint8_t mac[IEEE80211_ADDR_LEN], int clone)
1370b032f27cSSam Leffler {
1371b032f27cSSam Leffler 	int i;
1372b032f27cSSam Leffler 
1373b032f27cSSam Leffler 	if (clone && sc->sc_hasbmask) {
1374b032f27cSSam Leffler 		/* NB: we only do this if h/w supports multiple bssid */
1375b032f27cSSam Leffler 		for (i = 0; i < 8; i++)
1376b032f27cSSam Leffler 			if ((sc->sc_bssidmask & (1<<i)) == 0)
1377b032f27cSSam Leffler 				break;
1378b032f27cSSam Leffler 		if (i != 0)
1379b032f27cSSam Leffler 			mac[0] |= (i << 2)|0x2;
1380b032f27cSSam Leffler 	} else
1381b032f27cSSam Leffler 		i = 0;
1382b032f27cSSam Leffler 	sc->sc_bssidmask |= 1<<i;
1383b032f27cSSam Leffler 	sc->sc_hwbssidmask[0] &= ~mac[0];
1384b032f27cSSam Leffler 	if (i == 0)
1385b032f27cSSam Leffler 		sc->sc_nbssid0++;
1386b032f27cSSam Leffler }
1387b032f27cSSam Leffler 
1388b032f27cSSam Leffler static void
1389b032f27cSSam Leffler reclaim_address(struct ath_softc *sc, const uint8_t mac[IEEE80211_ADDR_LEN])
1390b032f27cSSam Leffler {
1391b032f27cSSam Leffler 	int i = mac[0] >> 2;
1392b032f27cSSam Leffler 	uint8_t mask;
1393b032f27cSSam Leffler 
1394b032f27cSSam Leffler 	if (i != 0 || --sc->sc_nbssid0 == 0) {
1395b032f27cSSam Leffler 		sc->sc_bssidmask &= ~(1<<i);
1396b032f27cSSam Leffler 		/* recalculate bssid mask from remaining addresses */
1397b032f27cSSam Leffler 		mask = 0xff;
1398b032f27cSSam Leffler 		for (i = 1; i < 8; i++)
1399b032f27cSSam Leffler 			if (sc->sc_bssidmask & (1<<i))
1400b032f27cSSam Leffler 				mask &= ~((i<<2)|0x2);
1401b032f27cSSam Leffler 		sc->sc_hwbssidmask[0] |= mask;
1402b032f27cSSam Leffler 	}
1403b032f27cSSam Leffler }
1404b032f27cSSam Leffler 
1405b032f27cSSam Leffler /*
1406b032f27cSSam Leffler  * Assign a beacon xmit slot.  We try to space out
1407b032f27cSSam Leffler  * assignments so when beacons are staggered the
1408b032f27cSSam Leffler  * traffic coming out of the cab q has maximal time
1409b032f27cSSam Leffler  * to go out before the next beacon is scheduled.
1410b032f27cSSam Leffler  */
1411b032f27cSSam Leffler static int
1412b032f27cSSam Leffler assign_bslot(struct ath_softc *sc)
1413b032f27cSSam Leffler {
1414b032f27cSSam Leffler 	u_int slot, free;
1415b032f27cSSam Leffler 
1416b032f27cSSam Leffler 	free = 0;
1417b032f27cSSam Leffler 	for (slot = 0; slot < ATH_BCBUF; slot++)
1418b032f27cSSam Leffler 		if (sc->sc_bslot[slot] == NULL) {
1419b032f27cSSam Leffler 			if (sc->sc_bslot[(slot+1)%ATH_BCBUF] == NULL &&
1420b032f27cSSam Leffler 			    sc->sc_bslot[(slot-1)%ATH_BCBUF] == NULL)
1421b032f27cSSam Leffler 				return slot;
1422b032f27cSSam Leffler 			free = slot;
1423b032f27cSSam Leffler 			/* NB: keep looking for a double slot */
1424b032f27cSSam Leffler 		}
1425b032f27cSSam Leffler 	return free;
1426b032f27cSSam Leffler }
1427b032f27cSSam Leffler 
1428b032f27cSSam Leffler static struct ieee80211vap *
1429fcd9500fSBernhard Schmidt ath_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
1430fcd9500fSBernhard Schmidt     enum ieee80211_opmode opmode, int flags,
1431b032f27cSSam Leffler     const uint8_t bssid[IEEE80211_ADDR_LEN],
1432b032f27cSSam Leffler     const uint8_t mac0[IEEE80211_ADDR_LEN])
1433b032f27cSSam Leffler {
14343797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
1435b032f27cSSam Leffler 	struct ath_vap *avp;
1436b032f27cSSam Leffler 	struct ieee80211vap *vap;
1437b032f27cSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN];
1438fcd9500fSBernhard Schmidt 	int needbeacon, error;
1439fcd9500fSBernhard Schmidt 	enum ieee80211_opmode ic_opmode;
1440b032f27cSSam Leffler 
14418aabf601SKevin Lo 	avp = malloc(sizeof(struct ath_vap), M_80211_VAP, M_WAITOK | M_ZERO);
1442b032f27cSSam Leffler 	needbeacon = 0;
1443b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(mac, mac0);
1444b032f27cSSam Leffler 
1445b032f27cSSam Leffler 	ATH_LOCK(sc);
1446a8962181SSam Leffler 	ic_opmode = opmode;		/* default to opmode of new vap */
1447b032f27cSSam Leffler 	switch (opmode) {
1448b032f27cSSam Leffler 	case IEEE80211_M_STA:
1449a8962181SSam Leffler 		if (sc->sc_nstavaps != 0) {	/* XXX only 1 for now */
1450b032f27cSSam Leffler 			device_printf(sc->sc_dev, "only 1 sta vap supported\n");
1451b032f27cSSam Leffler 			goto bad;
1452b032f27cSSam Leffler 		}
1453b032f27cSSam Leffler 		if (sc->sc_nvaps) {
1454b032f27cSSam Leffler 			/*
1455a8962181SSam Leffler 			 * With multiple vaps we must fall back
1456a8962181SSam Leffler 			 * to s/w beacon miss handling.
1457b032f27cSSam Leffler 			 */
1458b032f27cSSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
1459b032f27cSSam Leffler 		}
1460a8962181SSam Leffler 		if (flags & IEEE80211_CLONE_NOBEACONS) {
1461a8962181SSam Leffler 			/*
1462a8962181SSam Leffler 			 * Station mode w/o beacons are implemented w/ AP mode.
1463a8962181SSam Leffler 			 */
1464b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
1465a8962181SSam Leffler 		}
1466b032f27cSSam Leffler 		break;
1467b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1468b032f27cSSam Leffler 		if (sc->sc_nvaps != 0) {	/* XXX only 1 for now */
1469b032f27cSSam Leffler 			device_printf(sc->sc_dev,
1470b032f27cSSam Leffler 			    "only 1 ibss vap supported\n");
1471b032f27cSSam Leffler 			goto bad;
1472b032f27cSSam Leffler 		}
1473b032f27cSSam Leffler 		needbeacon = 1;
1474b032f27cSSam Leffler 		break;
1475b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1476584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
147710ad9a77SSam Leffler 		if (flags & IEEE80211_CLONE_TDMA) {
1478a8962181SSam Leffler 			if (sc->sc_nvaps != 0) {
1479a8962181SSam Leffler 				device_printf(sc->sc_dev,
1480a8962181SSam Leffler 				    "only 1 tdma vap supported\n");
1481a8962181SSam Leffler 				goto bad;
1482a8962181SSam Leffler 			}
148310ad9a77SSam Leffler 			needbeacon = 1;
148410ad9a77SSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
148510ad9a77SSam Leffler 		}
1486b032f27cSSam Leffler 		/* fall thru... */
148710ad9a77SSam Leffler #endif
1488b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1489b032f27cSSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode != opmode) {
1490a8962181SSam Leffler 			/*
1491a8962181SSam Leffler 			 * Adopt existing mode.  Adding a monitor or ahdemo
1492a8962181SSam Leffler 			 * vap to an existing configuration is of dubious
1493a8962181SSam Leffler 			 * value but should be ok.
1494a8962181SSam Leffler 			 */
1495b032f27cSSam Leffler 			/* XXX not right for monitor mode */
1496b032f27cSSam Leffler 			ic_opmode = ic->ic_opmode;
1497a8962181SSam Leffler 		}
1498b032f27cSSam Leffler 		break;
1499b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
150059aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1501b032f27cSSam Leffler 		needbeacon = 1;
1502a8962181SSam Leffler 		break;
1503b032f27cSSam Leffler 	case IEEE80211_M_WDS:
1504a8962181SSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode == IEEE80211_M_STA) {
1505b032f27cSSam Leffler 			device_printf(sc->sc_dev,
1506b032f27cSSam Leffler 			    "wds not supported in sta mode\n");
1507b032f27cSSam Leffler 			goto bad;
1508b032f27cSSam Leffler 		}
1509b032f27cSSam Leffler 		/*
1510b032f27cSSam Leffler 		 * Silently remove any request for a unique
1511b032f27cSSam Leffler 		 * bssid; WDS vap's always share the local
1512b032f27cSSam Leffler 		 * mac address.
1513b032f27cSSam Leffler 		 */
1514b032f27cSSam Leffler 		flags &= ~IEEE80211_CLONE_BSSID;
1515a8962181SSam Leffler 		if (sc->sc_nvaps == 0)
1516b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
1517a8962181SSam Leffler 		else
1518a8962181SSam Leffler 			ic_opmode = ic->ic_opmode;
15197d261891SRui Paulo 		break;
1520b032f27cSSam Leffler 	default:
1521b032f27cSSam Leffler 		device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
1522b032f27cSSam Leffler 		goto bad;
1523b032f27cSSam Leffler 	}
1524b032f27cSSam Leffler 	/*
1525b032f27cSSam Leffler 	 * Check that a beacon buffer is available; the code below assumes it.
1526b032f27cSSam Leffler 	 */
15276b349e5aSAdrian Chadd 	if (needbeacon & TAILQ_EMPTY(&sc->sc_bbuf)) {
1528b032f27cSSam Leffler 		device_printf(sc->sc_dev, "no beacon buffer available\n");
1529b032f27cSSam Leffler 		goto bad;
1530b032f27cSSam Leffler 	}
1531b032f27cSSam Leffler 
1532b032f27cSSam Leffler 	/* STA, AHDEMO? */
153359aa14a9SRui Paulo 	if (opmode == IEEE80211_M_HOSTAP || opmode == IEEE80211_M_MBSS) {
1534b032f27cSSam Leffler 		assign_address(sc, mac, flags & IEEE80211_CLONE_BSSID);
1535b032f27cSSam Leffler 		ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1536b032f27cSSam Leffler 	}
1537b032f27cSSam Leffler 
1538b032f27cSSam Leffler 	vap = &avp->av_vap;
1539b032f27cSSam Leffler 	/* XXX can't hold mutex across if_alloc */
1540b032f27cSSam Leffler 	ATH_UNLOCK(sc);
15417a79cebfSGleb Smirnoff 	error = ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
1542b032f27cSSam Leffler 	ATH_LOCK(sc);
1543b032f27cSSam Leffler 	if (error != 0) {
1544b032f27cSSam Leffler 		device_printf(sc->sc_dev, "%s: error %d creating vap\n",
1545b032f27cSSam Leffler 		    __func__, error);
1546b032f27cSSam Leffler 		goto bad2;
1547b032f27cSSam Leffler 	}
1548b032f27cSSam Leffler 
1549b032f27cSSam Leffler 	/* h/w crypto support */
1550b032f27cSSam Leffler 	vap->iv_key_alloc = ath_key_alloc;
1551b032f27cSSam Leffler 	vap->iv_key_delete = ath_key_delete;
1552b032f27cSSam Leffler 	vap->iv_key_set = ath_key_set;
1553b032f27cSSam Leffler 	vap->iv_key_update_begin = ath_key_update_begin;
1554b032f27cSSam Leffler 	vap->iv_key_update_end = ath_key_update_end;
1555b032f27cSSam Leffler 
1556b032f27cSSam Leffler 	/* override various methods */
1557b032f27cSSam Leffler 	avp->av_recv_mgmt = vap->iv_recv_mgmt;
1558b032f27cSSam Leffler 	vap->iv_recv_mgmt = ath_recv_mgmt;
1559b032f27cSSam Leffler 	vap->iv_reset = ath_reset_vap;
1560b032f27cSSam Leffler 	vap->iv_update_beacon = ath_beacon_update;
1561b032f27cSSam Leffler 	avp->av_newstate = vap->iv_newstate;
1562b032f27cSSam Leffler 	vap->iv_newstate = ath_newstate;
1563b032f27cSSam Leffler 	avp->av_bmiss = vap->iv_bmiss;
1564b032f27cSSam Leffler 	vap->iv_bmiss = ath_bmiss_vap;
1565b032f27cSSam Leffler 
15660eb81626SAdrian Chadd 	avp->av_node_ps = vap->iv_node_ps;
15670eb81626SAdrian Chadd 	vap->iv_node_ps = ath_node_powersave;
15680eb81626SAdrian Chadd 
1569548a605dSAdrian Chadd 	avp->av_set_tim = vap->iv_set_tim;
1570548a605dSAdrian Chadd 	vap->iv_set_tim = ath_node_set_tim;
1571548a605dSAdrian Chadd 
157222a3aee6SAdrian Chadd 	avp->av_recv_pspoll = vap->iv_recv_pspoll;
157322a3aee6SAdrian Chadd 	vap->iv_recv_pspoll = ath_node_recv_pspoll;
157422a3aee6SAdrian Chadd 
15759be25f4aSAdrian Chadd 	/* Set default parameters */
15769be25f4aSAdrian Chadd 
15779be25f4aSAdrian Chadd 	/*
15789be25f4aSAdrian Chadd 	 * Anything earlier than some AR9300 series MACs don't
15799be25f4aSAdrian Chadd 	 * support a smaller MPDU density.
15809be25f4aSAdrian Chadd 	 */
15819be25f4aSAdrian Chadd 	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_8;
15829be25f4aSAdrian Chadd 	/*
15839be25f4aSAdrian Chadd 	 * All NICs can handle the maximum size, however
15849be25f4aSAdrian Chadd 	 * AR5416 based MACs can only TX aggregates w/ RTS
15859be25f4aSAdrian Chadd 	 * protection when the total aggregate size is <= 8k.
15869be25f4aSAdrian Chadd 	 * However, for now that's enforced by the TX path.
15879be25f4aSAdrian Chadd 	 */
15889be25f4aSAdrian Chadd 	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K;
15899be25f4aSAdrian Chadd 
1590b032f27cSSam Leffler 	avp->av_bslot = -1;
1591b032f27cSSam Leffler 	if (needbeacon) {
1592b032f27cSSam Leffler 		/*
1593b032f27cSSam Leffler 		 * Allocate beacon state and setup the q for buffered
1594b032f27cSSam Leffler 		 * multicast frames.  We know a beacon buffer is
1595b032f27cSSam Leffler 		 * available because we checked above.
1596b032f27cSSam Leffler 		 */
15976b349e5aSAdrian Chadd 		avp->av_bcbuf = TAILQ_FIRST(&sc->sc_bbuf);
15986b349e5aSAdrian Chadd 		TAILQ_REMOVE(&sc->sc_bbuf, avp->av_bcbuf, bf_list);
1599b032f27cSSam Leffler 		if (opmode != IEEE80211_M_IBSS || !sc->sc_hasveol) {
1600b032f27cSSam Leffler 			/*
1601b032f27cSSam Leffler 			 * Assign the vap to a beacon xmit slot.  As above
1602b032f27cSSam Leffler 			 * this cannot fail to find a free one.
1603b032f27cSSam Leffler 			 */
1604b032f27cSSam Leffler 			avp->av_bslot = assign_bslot(sc);
1605b032f27cSSam Leffler 			KASSERT(sc->sc_bslot[avp->av_bslot] == NULL,
1606b032f27cSSam Leffler 			    ("beacon slot %u not empty", avp->av_bslot));
1607b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = vap;
1608b032f27cSSam Leffler 			sc->sc_nbcnvaps++;
1609b032f27cSSam Leffler 		}
1610b032f27cSSam Leffler 		if (sc->sc_hastsfadd && sc->sc_nbcnvaps > 0) {
1611b032f27cSSam Leffler 			/*
1612b032f27cSSam Leffler 			 * Multple vaps are to transmit beacons and we
1613b032f27cSSam Leffler 			 * have h/w support for TSF adjusting; enable
1614b032f27cSSam Leffler 			 * use of staggered beacons.
1615b032f27cSSam Leffler 			 */
1616b032f27cSSam Leffler 			sc->sc_stagbeacons = 1;
1617b032f27cSSam Leffler 		}
1618b032f27cSSam Leffler 		ath_txq_init(sc, &avp->av_mcastq, ATH_TXQ_SWQ);
1619b032f27cSSam Leffler 	}
1620b032f27cSSam Leffler 
1621b032f27cSSam Leffler 	ic->ic_opmode = ic_opmode;
1622b032f27cSSam Leffler 	if (opmode != IEEE80211_M_WDS) {
1623b032f27cSSam Leffler 		sc->sc_nvaps++;
1624b032f27cSSam Leffler 		if (opmode == IEEE80211_M_STA)
1625b032f27cSSam Leffler 			sc->sc_nstavaps++;
1626fe0dd789SSam Leffler 		if (opmode == IEEE80211_M_MBSS)
1627fe0dd789SSam Leffler 			sc->sc_nmeshvaps++;
1628b032f27cSSam Leffler 	}
1629b032f27cSSam Leffler 	switch (ic_opmode) {
1630b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1631b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_IBSS;
1632b032f27cSSam Leffler 		break;
1633b032f27cSSam Leffler 	case IEEE80211_M_STA:
1634b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_STA;
1635b032f27cSSam Leffler 		break;
1636b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1637584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
163810ad9a77SSam Leffler 		if (vap->iv_caps & IEEE80211_C_TDMA) {
163910ad9a77SSam Leffler 			sc->sc_tdma = 1;
164010ad9a77SSam Leffler 			/* NB: disable tsf adjust */
164110ad9a77SSam Leffler 			sc->sc_stagbeacons = 0;
164210ad9a77SSam Leffler 		}
164310ad9a77SSam Leffler 		/*
164410ad9a77SSam Leffler 		 * NB: adhoc demo mode is a pseudo mode; to the hal it's
164510ad9a77SSam Leffler 		 * just ap mode.
164610ad9a77SSam Leffler 		 */
164710ad9a77SSam Leffler 		/* fall thru... */
164810ad9a77SSam Leffler #endif
1649b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
165059aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1651b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_HOSTAP;
1652b032f27cSSam Leffler 		break;
1653b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1654b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_MONITOR;
1655b032f27cSSam Leffler 		break;
1656b032f27cSSam Leffler 	default:
1657b032f27cSSam Leffler 		/* XXX should not happen */
1658b032f27cSSam Leffler 		break;
1659b032f27cSSam Leffler 	}
1660b032f27cSSam Leffler 	if (sc->sc_hastsfadd) {
1661b032f27cSSam Leffler 		/*
1662b032f27cSSam Leffler 		 * Configure whether or not TSF adjust should be done.
1663b032f27cSSam Leffler 		 */
1664b032f27cSSam Leffler 		ath_hal_settsfadjust(sc->sc_ah, sc->sc_stagbeacons);
1665b032f27cSSam Leffler 	}
166610ad9a77SSam Leffler 	if (flags & IEEE80211_CLONE_NOBEACONS) {
166710ad9a77SSam Leffler 		/*
166810ad9a77SSam Leffler 		 * Enable s/w beacon miss handling.
166910ad9a77SSam Leffler 		 */
167010ad9a77SSam Leffler 		sc->sc_swbmiss = 1;
167110ad9a77SSam Leffler 	}
1672b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1673b032f27cSSam Leffler 
1674b032f27cSSam Leffler 	/* complete setup */
16757a79cebfSGleb Smirnoff 	ieee80211_vap_attach(vap, ath_media_change, ieee80211_media_status,
16767a79cebfSGleb Smirnoff 	    mac);
1677b032f27cSSam Leffler 	return vap;
1678b032f27cSSam Leffler bad2:
1679b032f27cSSam Leffler 	reclaim_address(sc, mac);
1680b032f27cSSam Leffler 	ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1681b032f27cSSam Leffler bad:
1682b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1683b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1684b032f27cSSam Leffler 	return NULL;
1685b032f27cSSam Leffler }
1686b032f27cSSam Leffler 
1687b032f27cSSam Leffler static void
1688b032f27cSSam Leffler ath_vap_delete(struct ieee80211vap *vap)
1689b032f27cSSam Leffler {
1690b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
16913797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
1692b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1693b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
1694b032f27cSSam Leffler 
1695f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1696f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1697f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1698f5c30c4eSAdrian Chadd 
1699f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
17007a79cebfSGleb Smirnoff 	if (sc->sc_running) {
1701b032f27cSSam Leffler 		/*
1702b032f27cSSam Leffler 		 * Quiesce the hardware while we remove the vap.  In
1703b032f27cSSam Leffler 		 * particular we need to reclaim all references to
1704b032f27cSSam Leffler 		 * the vap state by any frames pending on the tx queues.
1705b032f27cSSam Leffler 		 */
1706b032f27cSSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
1707517526efSAdrian Chadd 		/* XXX Do all frames from all vaps/nodes need draining here? */
17089a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* stop recv side */
1709062cf7d9SAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);		/* stop hw xmit side */
1710b032f27cSSam Leffler 	}
1711b032f27cSSam Leffler 
1712f5c30c4eSAdrian Chadd 	/* .. leave the hardware awake for now. */
1713f5c30c4eSAdrian Chadd 
1714b032f27cSSam Leffler 	ieee80211_vap_detach(vap);
171516d4de92SAdrian Chadd 
171616d4de92SAdrian Chadd 	/*
171716d4de92SAdrian Chadd 	 * XXX Danger Will Robinson! Danger!
171816d4de92SAdrian Chadd 	 *
171916d4de92SAdrian Chadd 	 * Because ieee80211_vap_detach() can queue a frame (the station
172016d4de92SAdrian Chadd 	 * diassociate message?) after we've drained the TXQ and
172116d4de92SAdrian Chadd 	 * flushed the software TXQ, we will end up with a frame queued
172216d4de92SAdrian Chadd 	 * to a node whose vap is about to be freed.
172316d4de92SAdrian Chadd 	 *
172416d4de92SAdrian Chadd 	 * To work around this, flush the hardware/software again.
172516d4de92SAdrian Chadd 	 * This may be racy - the ath task may be running and the packet
172616d4de92SAdrian Chadd 	 * may be being scheduled between sw->hw txq. Tsk.
172716d4de92SAdrian Chadd 	 *
172816d4de92SAdrian Chadd 	 * TODO: figure out why a new node gets allocated somewhere around
17297a79cebfSGleb Smirnoff 	 * here (after the ath_tx_swq() call; and after an ath_stop()
173016d4de92SAdrian Chadd 	 * call!)
173116d4de92SAdrian Chadd 	 */
173216d4de92SAdrian Chadd 
173316d4de92SAdrian Chadd 	ath_draintxq(sc, ATH_RESET_DEFAULT);
173416d4de92SAdrian Chadd 
1735b032f27cSSam Leffler 	ATH_LOCK(sc);
1736b032f27cSSam Leffler 	/*
1737b032f27cSSam Leffler 	 * Reclaim beacon state.  Note this must be done before
1738b032f27cSSam Leffler 	 * the vap instance is reclaimed as we may have a reference
1739b032f27cSSam Leffler 	 * to it in the buffer for the beacon frame.
1740b032f27cSSam Leffler 	 */
1741b032f27cSSam Leffler 	if (avp->av_bcbuf != NULL) {
1742b032f27cSSam Leffler 		if (avp->av_bslot != -1) {
1743b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = NULL;
1744b032f27cSSam Leffler 			sc->sc_nbcnvaps--;
1745b032f27cSSam Leffler 		}
1746b032f27cSSam Leffler 		ath_beacon_return(sc, avp->av_bcbuf);
1747b032f27cSSam Leffler 		avp->av_bcbuf = NULL;
1748b032f27cSSam Leffler 		if (sc->sc_nbcnvaps == 0) {
1749b032f27cSSam Leffler 			sc->sc_stagbeacons = 0;
1750b032f27cSSam Leffler 			if (sc->sc_hastsfadd)
1751b032f27cSSam Leffler 				ath_hal_settsfadjust(sc->sc_ah, 0);
1752b032f27cSSam Leffler 		}
1753b032f27cSSam Leffler 		/*
1754b032f27cSSam Leffler 		 * Reclaim any pending mcast frames for the vap.
1755b032f27cSSam Leffler 		 */
1756b032f27cSSam Leffler 		ath_tx_draintxq(sc, &avp->av_mcastq);
1757b032f27cSSam Leffler 	}
1758b032f27cSSam Leffler 	/*
1759b032f27cSSam Leffler 	 * Update bookkeeping.
1760b032f27cSSam Leffler 	 */
1761b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
1762b032f27cSSam Leffler 		sc->sc_nstavaps--;
1763b032f27cSSam Leffler 		if (sc->sc_nstavaps == 0 && sc->sc_swbmiss)
1764b032f27cSSam Leffler 			sc->sc_swbmiss = 0;
176559aa14a9SRui Paulo 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
176659aa14a9SRui Paulo 	    vap->iv_opmode == IEEE80211_M_MBSS) {
1767b032f27cSSam Leffler 		reclaim_address(sc, vap->iv_myaddr);
1768b032f27cSSam Leffler 		ath_hal_setbssidmask(ah, sc->sc_hwbssidmask);
1769fe0dd789SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_MBSS)
1770fe0dd789SSam Leffler 			sc->sc_nmeshvaps--;
1771b032f27cSSam Leffler 	}
1772b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_WDS)
1773b032f27cSSam Leffler 		sc->sc_nvaps--;
1774584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
177510ad9a77SSam Leffler 	/* TDMA operation ceases when the last vap is destroyed */
177610ad9a77SSam Leffler 	if (sc->sc_tdma && sc->sc_nvaps == 0) {
177710ad9a77SSam Leffler 		sc->sc_tdma = 0;
177810ad9a77SSam Leffler 		sc->sc_swbmiss = 0;
177910ad9a77SSam Leffler 	}
178010ad9a77SSam Leffler #endif
1781b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1782b032f27cSSam Leffler 
17837a79cebfSGleb Smirnoff 	if (sc->sc_running) {
1784b032f27cSSam Leffler 		/*
1785b032f27cSSam Leffler 		 * Restart rx+tx machines if still running (RUNNING will
1786b032f27cSSam Leffler 		 * be reset if we just destroyed the last vap).
1787b032f27cSSam Leffler 		 */
1788b032f27cSSam Leffler 		if (ath_startrecv(sc) != 0)
178976e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev,
179076e6fd5dSGleb Smirnoff 			    "%s: unable to restart recv logic\n", __func__);
1791c89b957aSSam Leffler 		if (sc->sc_beacons) {		/* restart beacons */
1792c89b957aSSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
1793c89b957aSSam Leffler 			if (sc->sc_tdma)
1794c89b957aSSam Leffler 				ath_tdma_config(sc, NULL);
1795c89b957aSSam Leffler 			else
1796c89b957aSSam Leffler #endif
1797b032f27cSSam Leffler 				ath_beacon_config(sc, NULL);
1798c89b957aSSam Leffler 		}
1799b032f27cSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
1800b032f27cSSam Leffler 	}
1801f5c30c4eSAdrian Chadd 
1802f5c30c4eSAdrian Chadd 	/* Ok, let the hardware asleep. */
1803f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
180416d4de92SAdrian Chadd 	ATH_UNLOCK(sc);
1805b032f27cSSam Leffler }
1806b032f27cSSam Leffler 
18075591b213SSam Leffler void
18085591b213SSam Leffler ath_suspend(struct ath_softc *sc)
18095591b213SSam Leffler {
18107a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
18115591b213SSam Leffler 
18127a79cebfSGleb Smirnoff 	sc->sc_resume_up = ic->ic_nrunning != 0;
1813d1328898SAdrian Chadd 
1814d3ac945bSSam Leffler 	ieee80211_suspend_all(ic);
1815d3ac945bSSam Leffler 	/*
1816d3ac945bSSam Leffler 	 * NB: don't worry about putting the chip in low power
1817d3ac945bSSam Leffler 	 * mode; pci will power off our socket on suspend and
1818f29b8b7fSWarner Losh 	 * CardBus detaches the device.
181917bb5fd1SAdrian Chadd 	 *
182017bb5fd1SAdrian Chadd 	 * XXX TODO: well, that's great, except for non-cardbus
182117bb5fd1SAdrian Chadd 	 * devices!
1822d3ac945bSSam Leffler 	 */
1823d73df6d5SAdrian Chadd 
1824ae2a0aa4SAdrian Chadd 	/*
182517bb5fd1SAdrian Chadd 	 * XXX This doesn't wait until all pending taskqueue
182617bb5fd1SAdrian Chadd 	 * items and parallel transmit/receive/other threads
182717bb5fd1SAdrian Chadd 	 * are running!
182817bb5fd1SAdrian Chadd 	 */
182917bb5fd1SAdrian Chadd 	ath_hal_intrset(sc->sc_ah, 0);
183017bb5fd1SAdrian Chadd 	taskqueue_block(sc->sc_tq);
18317707f31dSAdrian Chadd 
18327707f31dSAdrian Chadd 	ATH_LOCK(sc);
18337707f31dSAdrian Chadd 	callout_stop(&sc->sc_cal_ch);
18347707f31dSAdrian Chadd 	ATH_UNLOCK(sc);
183517bb5fd1SAdrian Chadd 
183617bb5fd1SAdrian Chadd 	/*
1837ae2a0aa4SAdrian Chadd 	 * XXX ensure sc_invalid is 1
1838ae2a0aa4SAdrian Chadd 	 */
1839ae2a0aa4SAdrian Chadd 
1840ae2a0aa4SAdrian Chadd 	/* Disable the PCIe PHY, complete with workarounds */
1841ae2a0aa4SAdrian Chadd 	ath_hal_enablepcie(sc->sc_ah, 1, 1);
1842d3ac945bSSam Leffler }
1843d3ac945bSSam Leffler 
1844d3ac945bSSam Leffler /*
1845d3ac945bSSam Leffler  * Reset the key cache since some parts do not reset the
1846d3ac945bSSam Leffler  * contents on resume.  First we clear all entries, then
1847d3ac945bSSam Leffler  * re-load keys that the 802.11 layer assumes are setup
1848d3ac945bSSam Leffler  * in h/w.
1849d3ac945bSSam Leffler  */
1850d3ac945bSSam Leffler static void
1851d3ac945bSSam Leffler ath_reset_keycache(struct ath_softc *sc)
1852d3ac945bSSam Leffler {
18537a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
1854d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1855d3ac945bSSam Leffler 	int i;
1856d3ac945bSSam Leffler 
1857f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1858f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1859d3ac945bSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
1860d3ac945bSSam Leffler 		ath_hal_keyreset(ah, i);
1861f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
1862f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1863d3ac945bSSam Leffler 	ieee80211_crypto_reload_keys(ic);
18645591b213SSam Leffler }
18655591b213SSam Leffler 
18666322256bSAdrian Chadd /*
18676322256bSAdrian Chadd  * Fetch the current chainmask configuration based on the current
18686322256bSAdrian Chadd  * operating channel and options.
18696322256bSAdrian Chadd  */
18706322256bSAdrian Chadd static void
18716322256bSAdrian Chadd ath_update_chainmasks(struct ath_softc *sc, struct ieee80211_channel *chan)
18726322256bSAdrian Chadd {
18736322256bSAdrian Chadd 
18746322256bSAdrian Chadd 	/*
18756322256bSAdrian Chadd 	 * Set TX chainmask to the currently configured chainmask;
18766322256bSAdrian Chadd 	 * the TX chainmask depends upon the current operating mode.
18776322256bSAdrian Chadd 	 */
18786322256bSAdrian Chadd 	sc->sc_cur_rxchainmask = sc->sc_rxchainmask;
18796322256bSAdrian Chadd 	if (IEEE80211_IS_CHAN_HT(chan)) {
18806322256bSAdrian Chadd 		sc->sc_cur_txchainmask = sc->sc_txchainmask;
18816322256bSAdrian Chadd 	} else {
18826322256bSAdrian Chadd 		sc->sc_cur_txchainmask = 1;
18836322256bSAdrian Chadd 	}
18847904f516SAdrian Chadd 
18857904f516SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
18867904f516SAdrian Chadd 	    "%s: TX chainmask is now 0x%x, RX is now 0x%x\n",
18877904f516SAdrian Chadd 	    __func__,
18887904f516SAdrian Chadd 	    sc->sc_cur_txchainmask,
18897904f516SAdrian Chadd 	    sc->sc_cur_rxchainmask);
18906322256bSAdrian Chadd }
18916322256bSAdrian Chadd 
18925591b213SSam Leffler void
18935591b213SSam Leffler ath_resume(struct ath_softc *sc)
18945591b213SSam Leffler {
18957a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
1896d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1897d3ac945bSSam Leffler 	HAL_STATUS status;
18985591b213SSam Leffler 
1899ae2a0aa4SAdrian Chadd 	ath_hal_enablepcie(ah, 0, 0);
1900d73df6d5SAdrian Chadd 
1901d3ac945bSSam Leffler 	/*
1902d3ac945bSSam Leffler 	 * Must reset the chip before we reload the
1903d3ac945bSSam Leffler 	 * keycache as we were powered down on suspend.
1904d3ac945bSSam Leffler 	 */
19056322256bSAdrian Chadd 	ath_update_chainmasks(sc,
19066322256bSAdrian Chadd 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan);
19076322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
19086322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
1909f5c30c4eSAdrian Chadd 
1910f5c30c4eSAdrian Chadd 	/* Ensure we set the current power state to on */
1911f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
19127d567ed6SAdrian Chadd 	ath_power_setselfgen(sc, HAL_PM_AWAKE);
1913f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1914f5c30c4eSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_AWAKE);
1915f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1916f5c30c4eSAdrian Chadd 
1917054d7b69SSam Leffler 	ath_hal_reset(ah, sc->sc_opmode,
1918054d7b69SSam Leffler 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan,
1919*f50e4ebfSAdrian Chadd 	    AH_FALSE, HAL_RESET_NORMAL, &status);
1920d3ac945bSSam Leffler 	ath_reset_keycache(sc);
19217e5eb44dSAdrian Chadd 
192217bb5fd1SAdrian Chadd 	ATH_RX_LOCK(sc);
192317bb5fd1SAdrian Chadd 	sc->sc_rx_stopped = 1;
192417bb5fd1SAdrian Chadd 	sc->sc_rx_resetted = 1;
192517bb5fd1SAdrian Chadd 	ATH_RX_UNLOCK(sc);
192617bb5fd1SAdrian Chadd 
19277e5eb44dSAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
19287e5eb44dSAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
19297e5eb44dSAdrian Chadd 
19309af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
19319af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
19329af351f9SAdrian Chadd 
1933dd6a574eSAdrian Chadd 	/*
1934b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
1935b70f530bSAdrian Chadd 	 */
1936b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
1937b70f530bSAdrian Chadd 
1938b70f530bSAdrian Chadd 	/*
1939dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
1940dd6a574eSAdrian Chadd 	 * support it.
1941dd6a574eSAdrian Chadd 	 */
1942dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
1943dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
1944dd6a574eSAdrian Chadd 	else
1945dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
1946dd6a574eSAdrian Chadd 
1947a497cd88SAdrian Chadd 	/* Restore the LED configuration */
1948a497cd88SAdrian Chadd 	ath_led_config(sc);
1949a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
1950a497cd88SAdrian Chadd 
1951d1328898SAdrian Chadd 	if (sc->sc_resume_up)
1952021a0db5SAdrian Chadd 		ieee80211_resume_all(ic);
19532fd9aabbSAdrian Chadd 
1954f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1955f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
1956f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1957f5c30c4eSAdrian Chadd 
19582fd9aabbSAdrian Chadd 	/* XXX beacons ? */
19596b59f5e3SSam Leffler }
19605591b213SSam Leffler 
19615591b213SSam Leffler void
19625591b213SSam Leffler ath_shutdown(struct ath_softc *sc)
19635591b213SSam Leffler {
19645591b213SSam Leffler 
19657a79cebfSGleb Smirnoff 	ATH_LOCK(sc);
19667a79cebfSGleb Smirnoff 	ath_stop(sc);
19677a79cebfSGleb Smirnoff 	ATH_UNLOCK(sc);
1968d3ac945bSSam Leffler 	/* NB: no point powering down chip as we're about to reboot */
19695591b213SSam Leffler }
19705591b213SSam Leffler 
1971c42a7b7eSSam Leffler /*
1972c42a7b7eSSam Leffler  * Interrupt handler.  Most of the actual processing is deferred.
1973c42a7b7eSSam Leffler  */
19745591b213SSam Leffler void
19755591b213SSam Leffler ath_intr(void *arg)
19765591b213SSam Leffler {
19775591b213SSam Leffler 	struct ath_softc *sc = arg;
19785591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
19796f5fe81eSAdrian Chadd 	HAL_INT status = 0;
19808f939e79SAdrian Chadd 	uint32_t txqs;
19815591b213SSam Leffler 
1982ef27340cSAdrian Chadd 	/*
1983ef27340cSAdrian Chadd 	 * If we're inside a reset path, just print a warning and
1984ef27340cSAdrian Chadd 	 * clear the ISR. The reset routine will finish it for us.
1985ef27340cSAdrian Chadd 	 */
1986ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
1987ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt) {
1988ef27340cSAdrian Chadd 		HAL_INT status;
1989ef27340cSAdrian Chadd 		ath_hal_getisr(ah, &status);	/* clear ISR */
1990ef27340cSAdrian Chadd 		ath_hal_intrset(ah, 0);		/* disable further intr's */
1991ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_ANY,
1992ef27340cSAdrian Chadd 		    "%s: in reset, ignoring: status=0x%x\n",
1993ef27340cSAdrian Chadd 		    __func__, status);
1994ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
1995ef27340cSAdrian Chadd 		return;
1996ef27340cSAdrian Chadd 	}
1997ef27340cSAdrian Chadd 
19985591b213SSam Leffler 	if (sc->sc_invalid) {
19995591b213SSam Leffler 		/*
2000b58b3803SSam Leffler 		 * The hardware is not ready/present, don't touch anything.
2001b58b3803SSam Leffler 		 * Note this can happen early on if the IRQ is shared.
20025591b213SSam Leffler 		 */
2003c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
2004ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
20055591b213SSam Leffler 		return;
20065591b213SSam Leffler 	}
2007ef27340cSAdrian Chadd 	if (!ath_hal_intrpend(ah)) {		/* shared irq, not for us */
2008ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2009fdd758d4SSam Leffler 		return;
2010ef27340cSAdrian Chadd 	}
2011ef27340cSAdrian Chadd 
2012f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2013f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2014f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2015f5c30c4eSAdrian Chadd 
20167a79cebfSGleb Smirnoff 	if (sc->sc_ic.ic_nrunning == 0 && sc->sc_running == 0) {
201768e8e04eSSam Leffler 		HAL_INT status;
201868e8e04eSSam Leffler 
20197a79cebfSGleb Smirnoff 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: ic_nrunning %d sc_running %d\n",
20207a79cebfSGleb Smirnoff 		    __func__, sc->sc_ic.ic_nrunning, sc->sc_running);
20215591b213SSam Leffler 		ath_hal_getisr(ah, &status);	/* clear ISR */
20225591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable further intr's */
2023ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2024f5c30c4eSAdrian Chadd 
2025f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
2026f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
2027f5c30c4eSAdrian Chadd 		ATH_UNLOCK(sc);
20285591b213SSam Leffler 		return;
20295591b213SSam Leffler 	}
2030ef27340cSAdrian Chadd 
2031c42a7b7eSSam Leffler 	/*
2032c42a7b7eSSam Leffler 	 * Figure out the reason(s) for the interrupt.  Note
2033c42a7b7eSSam Leffler 	 * that the hal returns a pseudo-ISR that may include
2034c42a7b7eSSam Leffler 	 * bits we haven't explicitly enabled so we mask the
2035c42a7b7eSSam Leffler 	 * value to insure we only process bits we requested.
2036c42a7b7eSSam Leffler 	 */
20375591b213SSam Leffler 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
2038c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
203903682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 1, "ath_intr: mask=0x%.8x", status);
2040a26f3327SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
2041a26f3327SAdrian Chadd 	if_ath_alq_post_intr(&sc->sc_alq, status, ah->ah_intrstate,
2042a26f3327SAdrian Chadd 	    ah->ah_syncstate);
2043a26f3327SAdrian Chadd #endif	/* ATH_DEBUG_ALQ */
204431fdf3d6SAdrian Chadd #ifdef	ATH_KTR_INTR_DEBUG
204503682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 5,
2046f52d3452SAdrian Chadd 	    "ath_intr: ISR=0x%.8x, ISR_S0=0x%.8x, ISR_S1=0x%.8x, ISR_S2=0x%.8x, ISR_S5=0x%.8x",
2047f52d3452SAdrian Chadd 	    ah->ah_intrstate[0],
2048f52d3452SAdrian Chadd 	    ah->ah_intrstate[1],
2049f52d3452SAdrian Chadd 	    ah->ah_intrstate[2],
2050f52d3452SAdrian Chadd 	    ah->ah_intrstate[3],
2051f52d3452SAdrian Chadd 	    ah->ah_intrstate[6]);
205231fdf3d6SAdrian Chadd #endif
20539467e3f3SAdrian Chadd 
20549467e3f3SAdrian Chadd 	/* Squirrel away SYNC interrupt debugging */
20559467e3f3SAdrian Chadd 	if (ah->ah_syncstate != 0) {
20569467e3f3SAdrian Chadd 		int i;
20579467e3f3SAdrian Chadd 		for (i = 0; i < 32; i++)
20589467e3f3SAdrian Chadd 			if (ah->ah_syncstate & (i << i))
20599467e3f3SAdrian Chadd 				sc->sc_intr_stats.sync_intr[i]++;
20609467e3f3SAdrian Chadd 	}
20619467e3f3SAdrian Chadd 
2062ecddff40SSam Leffler 	status &= sc->sc_imask;			/* discard unasked for bits */
20636f5fe81eSAdrian Chadd 
20646f5fe81eSAdrian Chadd 	/* Short-circuit un-handled interrupts */
2065ef27340cSAdrian Chadd 	if (status == 0x0) {
2066ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2067f5c30c4eSAdrian Chadd 
2068f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
2069f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
2070f5c30c4eSAdrian Chadd 		ATH_UNLOCK(sc);
2071f5c30c4eSAdrian Chadd 
20726f5fe81eSAdrian Chadd 		return;
2073ef27340cSAdrian Chadd 	}
20746f5fe81eSAdrian Chadd 
2075ef27340cSAdrian Chadd 	/*
2076ef27340cSAdrian Chadd 	 * Take a note that we're inside the interrupt handler, so
2077ef27340cSAdrian Chadd 	 * the reset routines know to wait.
2078ef27340cSAdrian Chadd 	 */
2079ef27340cSAdrian Chadd 	sc->sc_intr_cnt++;
2080ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2081ef27340cSAdrian Chadd 
2082ef27340cSAdrian Chadd 	/*
2083ef27340cSAdrian Chadd 	 * Handle the interrupt. We won't run concurrent with the reset
2084ef27340cSAdrian Chadd 	 * or channel change routines as they'll wait for sc_intr_cnt
2085ef27340cSAdrian Chadd 	 * to be 0 before continuing.
2086ef27340cSAdrian Chadd 	 */
20875591b213SSam Leffler 	if (status & HAL_INT_FATAL) {
20885591b213SSam Leffler 		sc->sc_stats.ast_hardware++;
20895591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
2090f846cf42SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_fataltask);
20915591b213SSam Leffler 	} else {
2092c42a7b7eSSam Leffler 		if (status & HAL_INT_SWBA) {
2093c42a7b7eSSam Leffler 			/*
2094c42a7b7eSSam Leffler 			 * Software beacon alert--time to send a beacon.
2095c42a7b7eSSam Leffler 			 * Handle beacon transmission directly; deferring
2096c42a7b7eSSam Leffler 			 * this is too slow to meet timing constraints
2097c42a7b7eSSam Leffler 			 * under load.
2098c42a7b7eSSam Leffler 			 */
2099584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
210010ad9a77SSam Leffler 			if (sc->sc_tdma) {
210110ad9a77SSam Leffler 				if (sc->sc_tdmaswba == 0) {
21027a79cebfSGleb Smirnoff 					struct ieee80211com *ic = &sc->sc_ic;
210310ad9a77SSam Leffler 					struct ieee80211vap *vap =
210410ad9a77SSam Leffler 					    TAILQ_FIRST(&ic->ic_vaps);
210510ad9a77SSam Leffler 					ath_tdma_beacon_send(sc, vap);
210610ad9a77SSam Leffler 					sc->sc_tdmaswba =
210710ad9a77SSam Leffler 					    vap->iv_tdma->tdma_bintval;
210810ad9a77SSam Leffler 				} else
210910ad9a77SSam Leffler 					sc->sc_tdmaswba--;
211010ad9a77SSam Leffler 			} else
211110ad9a77SSam Leffler #endif
2112339ccfb3SSam Leffler 			{
2113c42a7b7eSSam Leffler 				ath_beacon_proc(sc, 0);
2114339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
2115339ccfb3SSam Leffler 				/*
2116339ccfb3SSam Leffler 				 * Schedule the rx taskq in case there's no
2117339ccfb3SSam Leffler 				 * traffic so any frames held on the staging
2118339ccfb3SSam Leffler 				 * queue are aged and potentially flushed.
2119339ccfb3SSam Leffler 				 */
2120f0db652cSAdrian Chadd 				sc->sc_rx.recv_sched(sc, 1);
2121339ccfb3SSam Leffler #endif
2122339ccfb3SSam Leffler 			}
2123c42a7b7eSSam Leffler 		}
21245591b213SSam Leffler 		if (status & HAL_INT_RXEOL) {
21258f939e79SAdrian Chadd 			int imask;
212603682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXEOL");
212717bb5fd1SAdrian Chadd 			if (! sc->sc_isedma) {
2128ef27340cSAdrian Chadd 				ATH_PCU_LOCK(sc);
21295591b213SSam Leffler 				/*
21305591b213SSam Leffler 				 * NB: the hardware should re-read the link when
21315591b213SSam Leffler 				 *     RXE bit is written, but it doesn't work at
21325591b213SSam Leffler 				 *     least on older hardware revs.
21335591b213SSam Leffler 				 */
21345591b213SSam Leffler 				sc->sc_stats.ast_rxeol++;
213573f895fcSAdrian Chadd 				/*
213673f895fcSAdrian Chadd 				 * Disable RXEOL/RXORN - prevent an interrupt
213773f895fcSAdrian Chadd 				 * storm until the PCU logic can be reset.
21381fdadc0fSAdrian Chadd 				 * In case the interface is reset some other
21391fdadc0fSAdrian Chadd 				 * way before "sc_kickpcu" is called, don't
21401fdadc0fSAdrian Chadd 				 * modify sc_imask - that way if it is reset
21411fdadc0fSAdrian Chadd 				 * by a call to ath_reset() somehow, the
21421fdadc0fSAdrian Chadd 				 * interrupt mask will be correctly reprogrammed.
214373f895fcSAdrian Chadd 				 */
21448f939e79SAdrian Chadd 				imask = sc->sc_imask;
21451fdadc0fSAdrian Chadd 				imask &= ~(HAL_INT_RXEOL | HAL_INT_RXORN);
21461fdadc0fSAdrian Chadd 				ath_hal_intrset(ah, imask);
21471fdadc0fSAdrian Chadd 				/*
21488f939e79SAdrian Chadd 				 * Only blank sc_rxlink if we've not yet kicked
21498f939e79SAdrian Chadd 				 * the PCU.
21508f939e79SAdrian Chadd 				 *
21518f939e79SAdrian Chadd 				 * This isn't entirely correct - the correct solution
21528f939e79SAdrian Chadd 				 * would be to have a PCU lock and engage that for
21538f939e79SAdrian Chadd 				 * the duration of the PCU fiddling; which would include
21548f939e79SAdrian Chadd 				 * running the RX process. Otherwise we could end up
21558f939e79SAdrian Chadd 				 * messing up the RX descriptor chain and making the
21568f939e79SAdrian Chadd 				 * RX desc list much shorter.
21578f939e79SAdrian Chadd 				 */
21588f939e79SAdrian Chadd 				if (! sc->sc_kickpcu)
21598f939e79SAdrian Chadd 					sc->sc_rxlink = NULL;
21608f939e79SAdrian Chadd 				sc->sc_kickpcu = 1;
2161f0db652cSAdrian Chadd 				ATH_PCU_UNLOCK(sc);
216217bb5fd1SAdrian Chadd 			}
21638f939e79SAdrian Chadd 			/*
216417bb5fd1SAdrian Chadd 			 * Enqueue an RX proc to handle whatever
21651fdadc0fSAdrian Chadd 			 * is in the RX queue.
216617bb5fd1SAdrian Chadd 			 * This will then kick the PCU if required.
21671fdadc0fSAdrian Chadd 			 */
2168f0db652cSAdrian Chadd 			sc->sc_rx.recv_sched(sc, 1);
21695591b213SSam Leffler 		}
21705591b213SSam Leffler 		if (status & HAL_INT_TXURN) {
21715591b213SSam Leffler 			sc->sc_stats.ast_txurn++;
21725591b213SSam Leffler 			/* bump tx trigger level */
21735591b213SSam Leffler 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
21745591b213SSam Leffler 		}
2175bcbb08ceSAdrian Chadd 		/*
2176bcbb08ceSAdrian Chadd 		 * Handle both the legacy and RX EDMA interrupt bits.
2177bcbb08ceSAdrian Chadd 		 * Note that HAL_INT_RXLP is also HAL_INT_RXDESC.
2178bcbb08ceSAdrian Chadd 		 */
2179bcbb08ceSAdrian Chadd 		if (status & (HAL_INT_RX | HAL_INT_RXHP | HAL_INT_RXLP)) {
21808f939e79SAdrian Chadd 			sc->sc_stats.ast_rx_intr++;
2181f0db652cSAdrian Chadd 			sc->sc_rx.recv_sched(sc, 1);
21828f939e79SAdrian Chadd 		}
21838f939e79SAdrian Chadd 		if (status & HAL_INT_TX) {
21848f939e79SAdrian Chadd 			sc->sc_stats.ast_tx_intr++;
21858f939e79SAdrian Chadd 			/*
21868f939e79SAdrian Chadd 			 * Grab all the currently set bits in the HAL txq bitmap
21878f939e79SAdrian Chadd 			 * and blank them. This is the only place we should be
21888f939e79SAdrian Chadd 			 * doing this.
21898f939e79SAdrian Chadd 			 */
2190bad98824SAdrian Chadd 			if (! sc->sc_isedma) {
2191ef27340cSAdrian Chadd 				ATH_PCU_LOCK(sc);
21928f939e79SAdrian Chadd 				txqs = 0xffffffff;
21938f939e79SAdrian Chadd 				ath_hal_gettxintrtxqs(sc->sc_ah, &txqs);
219403682514SAdrian Chadd 				ATH_KTR(sc, ATH_KTR_INTERRUPTS, 3,
219503682514SAdrian Chadd 				    "ath_intr: TX; txqs=0x%08x, txq_active was 0x%08x, now 0x%08x",
219603682514SAdrian Chadd 				    txqs,
219703682514SAdrian Chadd 				    sc->sc_txq_active,
219803682514SAdrian Chadd 				    sc->sc_txq_active | txqs);
21998f939e79SAdrian Chadd 				sc->sc_txq_active |= txqs;
2200ef27340cSAdrian Chadd 				ATH_PCU_UNLOCK(sc);
22018f939e79SAdrian Chadd 			}
2202bad98824SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask);
2203bad98824SAdrian Chadd 		}
22045591b213SSam Leffler 		if (status & HAL_INT_BMISS) {
22055591b213SSam Leffler 			sc->sc_stats.ast_bmiss++;
22060bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask);
22075591b213SSam Leffler 		}
22086ad02dbaSAdrian Chadd 		if (status & HAL_INT_GTT)
22096ad02dbaSAdrian Chadd 			sc->sc_stats.ast_tx_timeout++;
22105594f5c0SAdrian Chadd 		if (status & HAL_INT_CST)
22115594f5c0SAdrian Chadd 			sc->sc_stats.ast_tx_cst++;
2212c42a7b7eSSam Leffler 		if (status & HAL_INT_MIB) {
2213c42a7b7eSSam Leffler 			sc->sc_stats.ast_mib++;
2214ef27340cSAdrian Chadd 			ATH_PCU_LOCK(sc);
2215c42a7b7eSSam Leffler 			/*
2216c42a7b7eSSam Leffler 			 * Disable interrupts until we service the MIB
2217c42a7b7eSSam Leffler 			 * interrupt; otherwise it will continue to fire.
2218c42a7b7eSSam Leffler 			 */
2219c42a7b7eSSam Leffler 			ath_hal_intrset(ah, 0);
2220c42a7b7eSSam Leffler 			/*
2221c42a7b7eSSam Leffler 			 * Let the hal handle the event.  We assume it will
2222c42a7b7eSSam Leffler 			 * clear whatever condition caused the interrupt.
2223c42a7b7eSSam Leffler 			 */
2224ffa2cab6SSam Leffler 			ath_hal_mibevent(ah, &sc->sc_halstats);
22258f939e79SAdrian Chadd 			/*
22268f939e79SAdrian Chadd 			 * Don't reset the interrupt if we've just
22278f939e79SAdrian Chadd 			 * kicked the PCU, or we may get a nested
22288f939e79SAdrian Chadd 			 * RXEOL before the rxproc has had a chance
22298f939e79SAdrian Chadd 			 * to run.
22308f939e79SAdrian Chadd 			 */
22318f939e79SAdrian Chadd 			if (sc->sc_kickpcu == 0)
2232c42a7b7eSSam Leffler 				ath_hal_intrset(ah, sc->sc_imask);
2233ef27340cSAdrian Chadd 			ATH_PCU_UNLOCK(sc);
2234c42a7b7eSSam Leffler 		}
22359c4fc1e8SSam Leffler 		if (status & HAL_INT_RXORN) {
22369c4fc1e8SSam Leffler 			/* NB: hal marks HAL_INT_FATAL when RXORN is fatal */
223703682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXORN");
22389c4fc1e8SSam Leffler 			sc->sc_stats.ast_rxorn++;
22399c4fc1e8SSam Leffler 		}
2240f5c30c4eSAdrian Chadd 		if (status & HAL_INT_TSFOOR) {
2241f5c30c4eSAdrian Chadd 			device_printf(sc->sc_dev, "%s: TSFOOR\n", __func__);
2242f5c30c4eSAdrian Chadd 			sc->sc_syncbeacon = 1;
2243f5c30c4eSAdrian Chadd 		}
22445591b213SSam Leffler 	}
2245ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2246ef27340cSAdrian Chadd 	sc->sc_intr_cnt--;
2247ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2248f5c30c4eSAdrian Chadd 
2249f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2250f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2251f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
22525591b213SSam Leffler }
22535591b213SSam Leffler 
22545591b213SSam Leffler static void
22555591b213SSam Leffler ath_fatal_proc(void *arg, int pending)
22565591b213SSam Leffler {
22575591b213SSam Leffler 	struct ath_softc *sc = arg;
225816c8acaaSSam Leffler 	u_int32_t *state;
225916c8acaaSSam Leffler 	u_int32_t len;
226068e8e04eSSam Leffler 	void *sp;
22615591b213SSam Leffler 
226270c81b20SAdrian Chadd 	if (sc->sc_invalid)
226370c81b20SAdrian Chadd 		return;
226470c81b20SAdrian Chadd 
226576e6fd5dSGleb Smirnoff 	device_printf(sc->sc_dev, "hardware error; resetting\n");
226616c8acaaSSam Leffler 	/*
226716c8acaaSSam Leffler 	 * Fatal errors are unrecoverable.  Typically these
226816c8acaaSSam Leffler 	 * are caused by DMA errors.  Collect h/w state from
226916c8acaaSSam Leffler 	 * the hal so we can diagnose what's going on.
227016c8acaaSSam Leffler 	 */
227168e8e04eSSam Leffler 	if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) {
227216c8acaaSSam Leffler 		KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len));
227368e8e04eSSam Leffler 		state = sp;
227476e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
227576e6fd5dSGleb Smirnoff 		    "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n", state[0],
227676e6fd5dSGleb Smirnoff 		    state[1] , state[2], state[3], state[4], state[5]);
227716c8acaaSSam Leffler 	}
22787a79cebfSGleb Smirnoff 	ath_reset(sc, ATH_RESET_NOLOSS);
22795591b213SSam Leffler }
22805591b213SSam Leffler 
22815591b213SSam Leffler static void
2282b032f27cSSam Leffler ath_bmiss_vap(struct ieee80211vap *vap)
22835591b213SSam Leffler {
22843797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
2285f5c30c4eSAdrian Chadd 
228659fbb257SSam Leffler 	/*
228759fbb257SSam Leffler 	 * Workaround phantom bmiss interrupts by sanity-checking
228859fbb257SSam Leffler 	 * the time of our last rx'd frame.  If it is within the
228959fbb257SSam Leffler 	 * beacon miss interval then ignore the interrupt.  If it's
229059fbb257SSam Leffler 	 * truly a bmiss we'll get another interrupt soon and that'll
229159fbb257SSam Leffler 	 * be dispatched up for processing.  Note this applies only
229259fbb257SSam Leffler 	 * for h/w beacon miss events.
229359fbb257SSam Leffler 	 */
2294f5c30c4eSAdrian Chadd 
2295f5c30c4eSAdrian Chadd 	/*
2296f5c30c4eSAdrian Chadd 	 * XXX TODO: Just read the TSF during the interrupt path;
2297f5c30c4eSAdrian Chadd 	 * that way we don't have to wake up again just to read it
2298f5c30c4eSAdrian Chadd 	 * again.
2299f5c30c4eSAdrian Chadd 	 */
2300f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2301f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2302f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2303f5c30c4eSAdrian Chadd 
230459fbb257SSam Leffler 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
2305d7736e13SSam Leffler 		u_int64_t lastrx = sc->sc_lastrx;
2306d7736e13SSam Leffler 		u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah);
230780767531SAdrian Chadd 		/* XXX should take a locked ref to iv_bss */
2308d7736e13SSam Leffler 		u_int bmisstimeout =
2309b032f27cSSam Leffler 			vap->iv_bmissthreshold * vap->iv_bss->ni_intval * 1024;
2310d7736e13SSam Leffler 
2311d7736e13SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2312d7736e13SSam Leffler 		    "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n",
2313d7736e13SSam Leffler 		    __func__, (unsigned long long) tsf,
2314d7736e13SSam Leffler 		    (unsigned long long)(tsf - lastrx),
2315d7736e13SSam Leffler 		    (unsigned long long) lastrx, bmisstimeout);
231659fbb257SSam Leffler 
231759fbb257SSam Leffler 		if (tsf - lastrx <= bmisstimeout) {
2318d7736e13SSam Leffler 			sc->sc_stats.ast_bmiss_phantom++;
2319f5c30c4eSAdrian Chadd 
2320f5c30c4eSAdrian Chadd 			ATH_LOCK(sc);
2321f5c30c4eSAdrian Chadd 			ath_power_restore_power_state(sc);
2322f5c30c4eSAdrian Chadd 			ATH_UNLOCK(sc);
2323f5c30c4eSAdrian Chadd 
232459fbb257SSam Leffler 			return;
232559fbb257SSam Leffler 		}
232659fbb257SSam Leffler 	}
2327f5c30c4eSAdrian Chadd 
2328f5c30c4eSAdrian Chadd 	/*
2329f5c30c4eSAdrian Chadd 	 * There's no need to keep the hardware awake during the call
2330f5c30c4eSAdrian Chadd 	 * to av_bmiss().
2331f5c30c4eSAdrian Chadd 	 */
2332f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2333f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2334f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2335f5c30c4eSAdrian Chadd 
2336f5c30c4eSAdrian Chadd 	/*
2337f5c30c4eSAdrian Chadd 	 * Attempt to force a beacon resync.
2338f5c30c4eSAdrian Chadd 	 */
2339f5c30c4eSAdrian Chadd 	sc->sc_syncbeacon = 1;
2340f5c30c4eSAdrian Chadd 
234159fbb257SSam Leffler 	ATH_VAP(vap)->av_bmiss(vap);
2342e585d188SSam Leffler }
2343b032f27cSSam Leffler 
2344f5c30c4eSAdrian Chadd /* XXX this needs a force wakeup! */
2345b837332dSAdrian Chadd int
2346459bc4f0SSam Leffler ath_hal_gethangstate(struct ath_hal *ah, uint32_t mask, uint32_t *hangs)
2347459bc4f0SSam Leffler {
2348459bc4f0SSam Leffler 	uint32_t rsize;
2349459bc4f0SSam Leffler 	void *sp;
2350459bc4f0SSam Leffler 
235125c96056SAdrian Chadd 	if (!ath_hal_getdiagstate(ah, HAL_DIAG_CHECK_HANGS, &mask, sizeof(mask), &sp, &rsize))
2352459bc4f0SSam Leffler 		return 0;
2353459bc4f0SSam Leffler 	KASSERT(rsize == sizeof(uint32_t), ("resultsize %u", rsize));
2354459bc4f0SSam Leffler 	*hangs = *(uint32_t *)sp;
2355459bc4f0SSam Leffler 	return 1;
2356459bc4f0SSam Leffler }
2357459bc4f0SSam Leffler 
2358b032f27cSSam Leffler static void
2359b032f27cSSam Leffler ath_bmiss_proc(void *arg, int pending)
2360b032f27cSSam Leffler {
2361b032f27cSSam Leffler 	struct ath_softc *sc = arg;
2362459bc4f0SSam Leffler 	uint32_t hangs;
2363b032f27cSSam Leffler 
2364b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
2365459bc4f0SSam Leffler 
2366f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2367f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2368f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2369f5c30c4eSAdrian Chadd 
2370f5c30c4eSAdrian Chadd 	ath_beacon_miss(sc);
2371f5c30c4eSAdrian Chadd 
2372a74ebfe5SAdrian Chadd 	/*
2373a74ebfe5SAdrian Chadd 	 * Do a reset upon any becaon miss event.
2374a74ebfe5SAdrian Chadd 	 *
2375a74ebfe5SAdrian Chadd 	 * It may be a non-recognised RX clear hang which needs a reset
2376a74ebfe5SAdrian Chadd 	 * to clear.
2377a74ebfe5SAdrian Chadd 	 */
2378459bc4f0SSam Leffler 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) {
23797a79cebfSGleb Smirnoff 		ath_reset(sc, ATH_RESET_NOLOSS);
238076e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
238176e6fd5dSGleb Smirnoff 		    "bb hang detected (0x%x), resetting\n", hangs);
2382a74ebfe5SAdrian Chadd 	} else {
23837a79cebfSGleb Smirnoff 		ath_reset(sc, ATH_RESET_NOLOSS);
23847a79cebfSGleb Smirnoff 		ieee80211_beacon_miss(&sc->sc_ic);
23855591b213SSam Leffler 	}
2386f5c30c4eSAdrian Chadd 
2387f5c30c4eSAdrian Chadd 	/* Force a beacon resync, in case they've drifted */
2388f5c30c4eSAdrian Chadd 	sc->sc_syncbeacon = 1;
2389f5c30c4eSAdrian Chadd 
2390f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2391f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2392f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2393a74ebfe5SAdrian Chadd }
23945591b213SSam Leffler 
2395724c193aSSam Leffler /*
2396b032f27cSSam Leffler  * Handle TKIP MIC setup to deal hardware that doesn't do MIC
2397b032f27cSSam Leffler  * calcs together with WME.  If necessary disable the crypto
2398b032f27cSSam Leffler  * hardware and mark the 802.11 state so keys will be setup
2399b032f27cSSam Leffler  * with the MIC work done in software.
2400b032f27cSSam Leffler  */
2401b032f27cSSam Leffler static void
2402b032f27cSSam Leffler ath_settkipmic(struct ath_softc *sc)
2403b032f27cSSam Leffler {
24047a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
2405b032f27cSSam Leffler 
2406b032f27cSSam Leffler 	if ((ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIP) && !sc->sc_wmetkipmic) {
2407b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WME) {
2408b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_FALSE);
2409b032f27cSSam Leffler 			ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC;
2410b032f27cSSam Leffler 		} else {
2411b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_TRUE);
2412b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
2413b032f27cSSam Leffler 		}
2414b032f27cSSam Leffler 	}
2415b032f27cSSam Leffler }
2416b032f27cSSam Leffler 
24177a79cebfSGleb Smirnoff static int
24187a79cebfSGleb Smirnoff ath_init(struct ath_softc *sc)
24195591b213SSam Leffler {
24207a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
24215591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
24225591b213SSam Leffler 	HAL_STATUS status;
24235591b213SSam Leffler 
24247a79cebfSGleb Smirnoff 	ATH_LOCK_ASSERT(sc);
24255591b213SSam Leffler 
24265591b213SSam Leffler 	/*
2427f5c30c4eSAdrian Chadd 	 * Force the sleep state awake.
2428f5c30c4eSAdrian Chadd 	 */
24297d567ed6SAdrian Chadd 	ath_power_setselfgen(sc, HAL_PM_AWAKE);
2430f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2431f5c30c4eSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_AWAKE);
2432f5c30c4eSAdrian Chadd 
2433f5c30c4eSAdrian Chadd 	/*
24345591b213SSam Leffler 	 * Stop anything previously setup.  This is safe
24355591b213SSam Leffler 	 * whether this is the first time through or not.
24365591b213SSam Leffler 	 */
24377a79cebfSGleb Smirnoff 	ath_stop(sc);
24385591b213SSam Leffler 
24395591b213SSam Leffler 	/*
24405591b213SSam Leffler 	 * The basic interface to setting the hardware in a good
24415591b213SSam Leffler 	 * state is ``reset''.  On return the hardware is known to
24425591b213SSam Leffler 	 * be powered up and with interrupts disabled.  This must
24435591b213SSam Leffler 	 * be followed by initialization of the appropriate bits
24445591b213SSam Leffler 	 * and then setup of the interrupt mask.
24455591b213SSam Leffler 	 */
2446b032f27cSSam Leffler 	ath_settkipmic(sc);
24476322256bSAdrian Chadd 	ath_update_chainmasks(sc, ic->ic_curchan);
24486322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
24496322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
2450f5c30c4eSAdrian Chadd 
245176e6fd5dSGleb Smirnoff 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_FALSE,
2452*f50e4ebfSAdrian Chadd 	    HAL_RESET_NORMAL, &status)) {
245376e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
245476e6fd5dSGleb Smirnoff 		    "unable to reset hardware; hal status %u\n", status);
24557a79cebfSGleb Smirnoff 		return (ENODEV);
24565591b213SSam Leffler 	}
245717bb5fd1SAdrian Chadd 
245817bb5fd1SAdrian Chadd 	ATH_RX_LOCK(sc);
245917bb5fd1SAdrian Chadd 	sc->sc_rx_stopped = 1;
246017bb5fd1SAdrian Chadd 	sc->sc_rx_resetted = 1;
246117bb5fd1SAdrian Chadd 	ATH_RX_UNLOCK(sc);
246217bb5fd1SAdrian Chadd 
2463b032f27cSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
24645591b213SSam Leffler 
246548237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
246648237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
246748237774SAdrian Chadd 
24689af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
24699af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
24709af351f9SAdrian Chadd 
24715591b213SSam Leffler 	/*
2472b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
2473b70f530bSAdrian Chadd 	 */
2474b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
2475b70f530bSAdrian Chadd 
2476b70f530bSAdrian Chadd 	/*
2477dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
2478dd6a574eSAdrian Chadd 	 * support it.
2479dd6a574eSAdrian Chadd 	 */
2480dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
2481dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
2482dd6a574eSAdrian Chadd 	else
2483dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
2484dd6a574eSAdrian Chadd 
2485dd6a574eSAdrian Chadd 	/*
2486c59005e9SSam Leffler 	 * Likewise this is set during reset so update
2487c59005e9SSam Leffler 	 * state cached in the driver.
2488c59005e9SSam Leffler 	 */
2489c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
24909bbfde1eSAdrian Chadd 	sc->sc_lastlongcal = ticks;
24912dc7fcc4SSam Leffler 	sc->sc_resetcal = 1;
24922dc7fcc4SSam Leffler 	sc->sc_lastcalreset = 0;
24939bbfde1eSAdrian Chadd 	sc->sc_lastani = ticks;
24949bbfde1eSAdrian Chadd 	sc->sc_lastshortcal = ticks;
2495a108ab63SAdrian Chadd 	sc->sc_doresetcal = AH_FALSE;
24962fd9aabbSAdrian Chadd 	/*
24972fd9aabbSAdrian Chadd 	 * Beacon timers were cleared here; give ath_newstate()
24982fd9aabbSAdrian Chadd 	 * a hint that the beacon timers should be poked when
24992fd9aabbSAdrian Chadd 	 * things transition to the RUN state.
25002fd9aabbSAdrian Chadd 	 */
25012fd9aabbSAdrian Chadd 	sc->sc_beacons = 0;
2502c42a7b7eSSam Leffler 
2503c42a7b7eSSam Leffler 	/*
25045591b213SSam Leffler 	 * Setup the hardware after reset: the key cache
25055591b213SSam Leffler 	 * is filled as needed and the receive engine is
25065591b213SSam Leffler 	 * set going.  Frame transmit is handled entirely
25075591b213SSam Leffler 	 * in the frame output path; there's nothing to do
25085591b213SSam Leffler 	 * here except setup the interrupt mask.
25095591b213SSam Leffler 	 */
25105591b213SSam Leffler 	if (ath_startrecv(sc) != 0) {
251176e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "unable to start recv logic\n");
2512f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
25137a79cebfSGleb Smirnoff 		return (ENODEV);
25145591b213SSam Leffler 	}
25155591b213SSam Leffler 
25165591b213SSam Leffler 	/*
25175591b213SSam Leffler 	 * Enable interrupts.
25185591b213SSam Leffler 	 */
25195591b213SSam Leffler 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
252017bb5fd1SAdrian Chadd 		  | HAL_INT_RXORN | HAL_INT_TXURN
25215591b213SSam Leffler 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
2522bcbb08ceSAdrian Chadd 
2523bcbb08ceSAdrian Chadd 	/*
2524bcbb08ceSAdrian Chadd 	 * Enable RX EDMA bits.  Note these overlap with
2525bcbb08ceSAdrian Chadd 	 * HAL_INT_RX and HAL_INT_RXDESC respectively.
2526bcbb08ceSAdrian Chadd 	 */
2527bcbb08ceSAdrian Chadd 	if (sc->sc_isedma)
2528bcbb08ceSAdrian Chadd 		sc->sc_imask |= (HAL_INT_RXHP | HAL_INT_RXLP);
2529bcbb08ceSAdrian Chadd 
2530c42a7b7eSSam Leffler 	/*
253117bb5fd1SAdrian Chadd 	 * If we're an EDMA NIC, we don't care about RXEOL.
253217bb5fd1SAdrian Chadd 	 * Writing a new descriptor in will simply restart
253317bb5fd1SAdrian Chadd 	 * RX DMA.
253417bb5fd1SAdrian Chadd 	 */
253517bb5fd1SAdrian Chadd 	if (! sc->sc_isedma)
253617bb5fd1SAdrian Chadd 		sc->sc_imask |= HAL_INT_RXEOL;
253717bb5fd1SAdrian Chadd 
253817bb5fd1SAdrian Chadd 	/*
2539c42a7b7eSSam Leffler 	 * Enable MIB interrupts when there are hardware phy counters.
2540c42a7b7eSSam Leffler 	 * Note we only do this (at the moment) for station mode.
2541c42a7b7eSSam Leffler 	 */
2542c42a7b7eSSam Leffler 	if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
2543c42a7b7eSSam Leffler 		sc->sc_imask |= HAL_INT_MIB;
25445591b213SSam Leffler 
2545f5c30c4eSAdrian Chadd 	/*
2546f5c30c4eSAdrian Chadd 	 * XXX add capability for this.
2547f5c30c4eSAdrian Chadd 	 *
2548f5c30c4eSAdrian Chadd 	 * If we're in STA mode (and maybe IBSS?) then register for
2549f5c30c4eSAdrian Chadd 	 * TSFOOR interrupts.
2550f5c30c4eSAdrian Chadd 	 */
2551f5c30c4eSAdrian Chadd 	if (ic->ic_opmode == IEEE80211_M_STA)
2552f5c30c4eSAdrian Chadd 		sc->sc_imask |= HAL_INT_TSFOOR;
2553f5c30c4eSAdrian Chadd 
25545594f5c0SAdrian Chadd 	/* Enable global TX timeout and carrier sense timeout if available */
25556ad02dbaSAdrian Chadd 	if (ath_hal_gtxto_supported(ah))
25563788ebedSAdrian Chadd 		sc->sc_imask |= HAL_INT_GTT;
2557d0a0ebc6SAdrian Chadd 
2558d0a0ebc6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: imask=0x%x\n",
2559d0a0ebc6SAdrian Chadd 		__func__, sc->sc_imask);
25606ad02dbaSAdrian Chadd 
25617a79cebfSGleb Smirnoff 	sc->sc_running = 1;
25622e986da5SSam Leffler 	callout_reset(&sc->sc_wd_ch, hz, ath_watchdog, sc);
2563b032f27cSSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
25645591b213SSam Leffler 
2565f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2566b032f27cSSam Leffler 
25677a79cebfSGleb Smirnoff 	return (0);
25685591b213SSam Leffler }
25695591b213SSam Leffler 
25705591b213SSam Leffler static void
25717a79cebfSGleb Smirnoff ath_stop(struct ath_softc *sc)
25725591b213SSam Leffler {
25735591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
25745591b213SSam Leffler 
2575c42a7b7eSSam Leffler 	ATH_LOCK_ASSERT(sc);
2576f5c30c4eSAdrian Chadd 
2577f5c30c4eSAdrian Chadd 	/*
2578f5c30c4eSAdrian Chadd 	 * Wake the hardware up before fiddling with it.
2579f5c30c4eSAdrian Chadd 	 */
2580f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2581f5c30c4eSAdrian Chadd 
25827a79cebfSGleb Smirnoff 	if (sc->sc_running) {
25835591b213SSam Leffler 		/*
25845591b213SSam Leffler 		 * Shutdown the hardware and driver:
2585c42a7b7eSSam Leffler 		 *    reset 802.11 state machine
25865591b213SSam Leffler 		 *    turn off timers
2587c42a7b7eSSam Leffler 		 *    disable interrupts
2588c42a7b7eSSam Leffler 		 *    turn off the radio
25895591b213SSam Leffler 		 *    clear transmit machinery
25905591b213SSam Leffler 		 *    clear receive machinery
25915591b213SSam Leffler 		 *    drain and release tx queues
25925591b213SSam Leffler 		 *    reclaim beacon resources
25935591b213SSam Leffler 		 *    power down hardware
25945591b213SSam Leffler 		 *
25955591b213SSam Leffler 		 * Note that some of this work is not possible if the
25965591b213SSam Leffler 		 * hardware is gone (invalid).
25975591b213SSam Leffler 		 */
259886e07743SSam Leffler #ifdef ATH_TX99_DIAG
259986e07743SSam Leffler 		if (sc->sc_tx99 != NULL)
260086e07743SSam Leffler 			sc->sc_tx99->stop(sc->sc_tx99);
260186e07743SSam Leffler #endif
26022e986da5SSam Leffler 		callout_stop(&sc->sc_wd_ch);
26032e986da5SSam Leffler 		sc->sc_wd_timer = 0;
26047a79cebfSGleb Smirnoff 		sc->sc_running = 0;
2605c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
26063e50ec2cSSam Leffler 			if (sc->sc_softled) {
26073e50ec2cSSam Leffler 				callout_stop(&sc->sc_ledtimer);
26083e50ec2cSSam Leffler 				ath_hal_gpioset(ah, sc->sc_ledpin,
26093e50ec2cSSam Leffler 					!sc->sc_ledon);
26103e50ec2cSSam Leffler 				sc->sc_blinking = 0;
26113e50ec2cSSam Leffler 			}
26125591b213SSam Leffler 			ath_hal_intrset(ah, 0);
2613c42a7b7eSSam Leffler 		}
2614062cf7d9SAdrian Chadd 		/* XXX we should stop RX regardless of whether it's valid */
2615c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
26169a842e8bSAdrian Chadd 			ath_stoprecv(sc, 1);
2617c42a7b7eSSam Leffler 			ath_hal_phydisable(ah);
2618c42a7b7eSSam Leffler 		} else
26195591b213SSam Leffler 			sc->sc_rxlink = NULL;
2620062cf7d9SAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);
2621b032f27cSSam Leffler 		ath_beacon_free(sc);	/* XXX not needed */
2622c42a7b7eSSam Leffler 	}
2623f5c30c4eSAdrian Chadd 
2624f5c30c4eSAdrian Chadd 	/* And now, restore the current power state */
2625f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2626c42a7b7eSSam Leffler }
2627c42a7b7eSSam Leffler 
2628f5c30c4eSAdrian Chadd /*
2629f5c30c4eSAdrian Chadd  * Wait until all pending TX/RX has completed.
2630f5c30c4eSAdrian Chadd  *
2631f5c30c4eSAdrian Chadd  * This waits until all existing transmit, receive and interrupts
2632f5c30c4eSAdrian Chadd  * have completed.  It's assumed that the caller has first
2633f5c30c4eSAdrian Chadd  * grabbed the reset lock so it doesn't try to do overlapping
2634f5c30c4eSAdrian Chadd  * chip resets.
2635f5c30c4eSAdrian Chadd  */
2636f5c30c4eSAdrian Chadd #define	MAX_TXRX_ITERATIONS	100
2637ef27340cSAdrian Chadd static void
263821008bf1SAdrian Chadd ath_txrx_stop_locked(struct ath_softc *sc)
2639ef27340cSAdrian Chadd {
2640ef27340cSAdrian Chadd 	int i = MAX_TXRX_ITERATIONS;
2641ef27340cSAdrian Chadd 
2642ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
264321008bf1SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
264421008bf1SAdrian Chadd 
2645ef27340cSAdrian Chadd 	/*
2646ef27340cSAdrian Chadd 	 * Sleep until all the pending operations have completed.
2647ef27340cSAdrian Chadd 	 *
2648ef27340cSAdrian Chadd 	 * The caller must ensure that reset has been incremented
2649ef27340cSAdrian Chadd 	 * or the pending operations may continue being queued.
2650ef27340cSAdrian Chadd 	 */
2651ef27340cSAdrian Chadd 	while (sc->sc_rxproc_cnt || sc->sc_txproc_cnt ||
2652ef27340cSAdrian Chadd 	    sc->sc_txstart_cnt || sc->sc_intr_cnt) {
2653ef27340cSAdrian Chadd 		if (i <= 0)
2654ef27340cSAdrian Chadd 			break;
2655f5c30c4eSAdrian Chadd 		msleep(sc, &sc->sc_pcu_mtx, 0, "ath_txrx_stop",
2656f5c30c4eSAdrian Chadd 		    msecs_to_ticks(10));
2657ef27340cSAdrian Chadd 		i--;
2658ef27340cSAdrian Chadd 	}
2659ef27340cSAdrian Chadd 
2660ef27340cSAdrian Chadd 	if (i <= 0)
2661ef27340cSAdrian Chadd 		device_printf(sc->sc_dev,
2662ef27340cSAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2663ef27340cSAdrian Chadd 		    __func__, MAX_TXRX_ITERATIONS);
2664ef27340cSAdrian Chadd }
2665ef27340cSAdrian Chadd #undef	MAX_TXRX_ITERATIONS
2666ef27340cSAdrian Chadd 
2667e78719adSAdrian Chadd #if 0
2668ef27340cSAdrian Chadd static void
266921008bf1SAdrian Chadd ath_txrx_stop(struct ath_softc *sc)
267021008bf1SAdrian Chadd {
267121008bf1SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
267221008bf1SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
267321008bf1SAdrian Chadd 
267421008bf1SAdrian Chadd 	ATH_PCU_LOCK(sc);
267521008bf1SAdrian Chadd 	ath_txrx_stop_locked(sc);
267621008bf1SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
267721008bf1SAdrian Chadd }
2678e78719adSAdrian Chadd #endif
267921008bf1SAdrian Chadd 
268021008bf1SAdrian Chadd static void
2681ef27340cSAdrian Chadd ath_txrx_start(struct ath_softc *sc)
2682ef27340cSAdrian Chadd {
2683ef27340cSAdrian Chadd 
2684ef27340cSAdrian Chadd 	taskqueue_unblock(sc->sc_tq);
2685ef27340cSAdrian Chadd }
2686ef27340cSAdrian Chadd 
2687ee321975SAdrian Chadd /*
2688ee321975SAdrian Chadd  * Grab the reset lock, and wait around until noone else
2689ee321975SAdrian Chadd  * is trying to do anything with it.
2690ee321975SAdrian Chadd  *
2691ee321975SAdrian Chadd  * This is totally horrible but we can't hold this lock for
2692ee321975SAdrian Chadd  * long enough to do TX/RX or we end up with net80211/ip stack
2693ee321975SAdrian Chadd  * LORs and eventual deadlock.
2694ee321975SAdrian Chadd  *
2695ee321975SAdrian Chadd  * "dowait" signals whether to spin, waiting for the reset
2696ee321975SAdrian Chadd  * lock count to reach 0. This should (for now) only be used
2697ee321975SAdrian Chadd  * during the reset path, as the rest of the code may not
2698ee321975SAdrian Chadd  * be locking-reentrant enough to behave correctly.
2699ee321975SAdrian Chadd  *
2700ee321975SAdrian Chadd  * Another, cleaner way should be found to serialise all of
2701ee321975SAdrian Chadd  * these operations.
2702ee321975SAdrian Chadd  */
2703f5c30c4eSAdrian Chadd #define	MAX_RESET_ITERATIONS	25
2704ee321975SAdrian Chadd static int
2705ee321975SAdrian Chadd ath_reset_grablock(struct ath_softc *sc, int dowait)
2706ee321975SAdrian Chadd {
2707ee321975SAdrian Chadd 	int w = 0;
2708ee321975SAdrian Chadd 	int i = MAX_RESET_ITERATIONS;
2709ee321975SAdrian Chadd 
2710ee321975SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
2711ee321975SAdrian Chadd 	do {
2712ee321975SAdrian Chadd 		if (sc->sc_inreset_cnt == 0) {
2713ee321975SAdrian Chadd 			w = 1;
2714ee321975SAdrian Chadd 			break;
2715ee321975SAdrian Chadd 		}
2716ee321975SAdrian Chadd 		if (dowait == 0) {
2717ee321975SAdrian Chadd 			w = 0;
2718ee321975SAdrian Chadd 			break;
2719ee321975SAdrian Chadd 		}
2720ee321975SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2721f5c30c4eSAdrian Chadd 		/*
2722f5c30c4eSAdrian Chadd 		 * 1 tick is likely not enough time for long calibrations
2723f5c30c4eSAdrian Chadd 		 * to complete.  So we should wait quite a while.
2724f5c30c4eSAdrian Chadd 		 */
2725f5c30c4eSAdrian Chadd 		pause("ath_reset_grablock", msecs_to_ticks(100));
2726ee321975SAdrian Chadd 		i--;
2727ee321975SAdrian Chadd 		ATH_PCU_LOCK(sc);
2728ee321975SAdrian Chadd 	} while (i > 0);
2729ee321975SAdrian Chadd 
2730ee321975SAdrian Chadd 	/*
2731ee321975SAdrian Chadd 	 * We always increment the refcounter, regardless
2732ee321975SAdrian Chadd 	 * of whether we succeeded to get it in an exclusive
2733ee321975SAdrian Chadd 	 * way.
2734ee321975SAdrian Chadd 	 */
2735ee321975SAdrian Chadd 	sc->sc_inreset_cnt++;
2736ee321975SAdrian Chadd 
2737ee321975SAdrian Chadd 	if (i <= 0)
2738ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2739ee321975SAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2740ee321975SAdrian Chadd 		    __func__, MAX_RESET_ITERATIONS);
2741ee321975SAdrian Chadd 
2742ee321975SAdrian Chadd 	if (w == 0)
2743ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2744ee321975SAdrian Chadd 		    "%s: warning, recursive reset path!\n",
2745ee321975SAdrian Chadd 		    __func__);
2746ee321975SAdrian Chadd 
2747ee321975SAdrian Chadd 	return w;
2748ee321975SAdrian Chadd }
2749ee321975SAdrian Chadd #undef MAX_RESET_ITERATIONS
2750ee321975SAdrian Chadd 
2751ee321975SAdrian Chadd /*
27525591b213SSam Leffler  * Reset the hardware w/o losing operational state.  This is
27535591b213SSam Leffler  * basically a more efficient way of doing ath_stop, ath_init,
27545591b213SSam Leffler  * followed by state transitions to the current 802.11
2755c42a7b7eSSam Leffler  * operational state.  Used to recover from various errors and
2756c42a7b7eSSam Leffler  * to reset or reload hardware state.
27575591b213SSam Leffler  */
27586079fdbeSAdrian Chadd int
27597a79cebfSGleb Smirnoff ath_reset(struct ath_softc *sc, ATH_RESET_TYPE reset_type)
27605591b213SSam Leffler {
27617a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
27625591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
27635591b213SSam Leffler 	HAL_STATUS status;
2764ef27340cSAdrian Chadd 	int i;
27655591b213SSam Leffler 
2766f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
276716d4de92SAdrian Chadd 
2768ee321975SAdrian Chadd 	/* Ensure ATH_LOCK isn't held; ath_rx_proc can't be locked */
2769ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
2770ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
2771ef27340cSAdrian Chadd 
2772d52f7132SAdrian Chadd 	/* Try to (stop any further TX/RX from occuring */
2773d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
2774d52f7132SAdrian Chadd 
2775f5c30c4eSAdrian Chadd 	/*
2776f5c30c4eSAdrian Chadd 	 * Wake the hardware up.
2777f5c30c4eSAdrian Chadd 	 */
2778f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2779f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2780f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2781f5c30c4eSAdrian Chadd 
2782ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2783904e385eSAdrian Chadd 
2784904e385eSAdrian Chadd 	/*
2785904e385eSAdrian Chadd 	 * Grab the reset lock before TX/RX is stopped.
2786904e385eSAdrian Chadd 	 *
2787904e385eSAdrian Chadd 	 * This is needed to ensure that when the TX/RX actually does finish,
2788904e385eSAdrian Chadd 	 * no further TX/RX/reset runs in parallel with this.
2789904e385eSAdrian Chadd 	 */
2790ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
2791ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
2792ef27340cSAdrian Chadd 		    __func__);
2793ef27340cSAdrian Chadd 	}
2794904e385eSAdrian Chadd 
2795904e385eSAdrian Chadd 	/* disable interrupts */
2796904e385eSAdrian Chadd 	ath_hal_intrset(ah, 0);
2797904e385eSAdrian Chadd 
2798904e385eSAdrian Chadd 	/*
2799904e385eSAdrian Chadd 	 * Now, ensure that any in progress TX/RX completes before we
2800904e385eSAdrian Chadd 	 * continue.
2801904e385eSAdrian Chadd 	 */
2802904e385eSAdrian Chadd 	ath_txrx_stop_locked(sc);
2803904e385eSAdrian Chadd 
2804ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2805ef27340cSAdrian Chadd 
2806f52d3452SAdrian Chadd 	/*
2807ef27340cSAdrian Chadd 	 * Regardless of whether we're doing a no-loss flush or
2808ef27340cSAdrian Chadd 	 * not, stop the PCU and handle what's in the RX queue.
2809ef27340cSAdrian Chadd 	 * That way frames aren't dropped which shouldn't be.
2810ef27340cSAdrian Chadd 	 */
28119a842e8bSAdrian Chadd 	ath_stoprecv(sc, (reset_type != ATH_RESET_NOLOSS));
2812f8cc9b09SAdrian Chadd 	ath_rx_flush(sc);
2813ef27340cSAdrian Chadd 
2814062cf7d9SAdrian Chadd 	/*
2815062cf7d9SAdrian Chadd 	 * Should now wait for pending TX/RX to complete
2816062cf7d9SAdrian Chadd 	 * and block future ones from occuring. This needs to be
2817062cf7d9SAdrian Chadd 	 * done before the TX queue is drained.
2818062cf7d9SAdrian Chadd 	 */
2819062cf7d9SAdrian Chadd 	ath_draintxq(sc, reset_type);	/* stop xmit side */
2820062cf7d9SAdrian Chadd 
2821b032f27cSSam Leffler 	ath_settkipmic(sc);		/* configure TKIP MIC handling */
28225591b213SSam Leffler 	/* NB: indicate channel change so we do a full reset */
28236322256bSAdrian Chadd 	ath_update_chainmasks(sc, ic->ic_curchan);
28246322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
28256322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
2826*f50e4ebfSAdrian Chadd 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_TRUE,
2827*f50e4ebfSAdrian Chadd 	    HAL_RESET_NORMAL, &status))
282876e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
282976e6fd5dSGleb Smirnoff 		    "%s: unable to reset hardware; hal status %u\n",
28305591b213SSam Leffler 		    __func__, status);
2831c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
283248237774SAdrian Chadd 
283317bb5fd1SAdrian Chadd 	ATH_RX_LOCK(sc);
283417bb5fd1SAdrian Chadd 	sc->sc_rx_stopped = 1;
283517bb5fd1SAdrian Chadd 	sc->sc_rx_resetted = 1;
283617bb5fd1SAdrian Chadd 	ATH_RX_UNLOCK(sc);
283717bb5fd1SAdrian Chadd 
283848237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
283948237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
284048237774SAdrian Chadd 
28419af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
28429af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
28439af351f9SAdrian Chadd 
2844dd6a574eSAdrian Chadd 	/*
2845b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
2846b70f530bSAdrian Chadd 	 */
2847b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
2848b70f530bSAdrian Chadd 
2849b70f530bSAdrian Chadd 	/*
2850dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
2851dd6a574eSAdrian Chadd 	 * support it.
2852dd6a574eSAdrian Chadd 	 */
2853dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
2854dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
2855dd6a574eSAdrian Chadd 	else
2856dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
2857dd6a574eSAdrian Chadd 
285868e8e04eSSam Leffler 	if (ath_startrecv(sc) != 0)	/* restart recv */
285976e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
286076e6fd5dSGleb Smirnoff 		    "%s: unable to start recv logic\n", __func__);
2861c42a7b7eSSam Leffler 	/*
2862c42a7b7eSSam Leffler 	 * We may be doing a reset in response to an ioctl
2863c42a7b7eSSam Leffler 	 * that changes the channel so update any state that
2864c42a7b7eSSam Leffler 	 * might change as a result.
2865c42a7b7eSSam Leffler 	 */
2866724c193aSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
2867c89b957aSSam Leffler 	if (sc->sc_beacons) {		/* restart beacons */
2868584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
286910ad9a77SSam Leffler 		if (sc->sc_tdma)
287010ad9a77SSam Leffler 			ath_tdma_config(sc, NULL);
287110ad9a77SSam Leffler 		else
287210ad9a77SSam Leffler #endif
2873c89b957aSSam Leffler 			ath_beacon_config(sc, NULL);
287410ad9a77SSam Leffler 	}
2875c42a7b7eSSam Leffler 
2876ef27340cSAdrian Chadd 	/*
2877ef27340cSAdrian Chadd 	 * Release the reset lock and re-enable interrupts here.
2878ef27340cSAdrian Chadd 	 * If an interrupt was being processed in ath_intr(),
2879ef27340cSAdrian Chadd 	 * it would disable interrupts at this point. So we have
2880ef27340cSAdrian Chadd 	 * to atomically enable interrupts and decrement the
2881ef27340cSAdrian Chadd 	 * reset counter - this way ath_intr() doesn't end up
2882ef27340cSAdrian Chadd 	 * disabling interrupts without a corresponding enable
2883ef27340cSAdrian Chadd 	 * in the rest or channel change path.
2884f5c30c4eSAdrian Chadd 	 *
2885f5c30c4eSAdrian Chadd 	 * Grab the TX reference in case we need to transmit.
2886f5c30c4eSAdrian Chadd 	 * That way a parallel transmit doesn't.
2887ef27340cSAdrian Chadd 	 */
2888ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2889ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
2890f5c30c4eSAdrian Chadd 	sc->sc_txstart_cnt++;
2891ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
2892ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
2893ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2894ef27340cSAdrian Chadd 
2895ef27340cSAdrian Chadd 	/*
2896ef27340cSAdrian Chadd 	 * TX and RX can be started here. If it were started with
2897ef27340cSAdrian Chadd 	 * sc_inreset_cnt > 0, the TX and RX path would abort.
2898ef27340cSAdrian Chadd 	 * Thus if this is a nested call through the reset or
2899ef27340cSAdrian Chadd 	 * channel change code, TX completion will occur but
2900ef27340cSAdrian Chadd 	 * RX completion and ath_start / ath_tx_start will not
2901ef27340cSAdrian Chadd 	 * run.
2902ef27340cSAdrian Chadd 	 */
2903ef27340cSAdrian Chadd 
2904ef27340cSAdrian Chadd 	/* Restart TX/RX as needed */
2905ef27340cSAdrian Chadd 	ath_txrx_start(sc);
2906ef27340cSAdrian Chadd 
2907f5c30c4eSAdrian Chadd 	/* XXX TODO: we need to hold the tx refcount here! */
2908f5c30c4eSAdrian Chadd 
2909375307d4SAdrian Chadd 	/* Restart TX completion and pending TX */
2910ef27340cSAdrian Chadd 	if (reset_type == ATH_RESET_NOLOSS) {
2911ef27340cSAdrian Chadd 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
2912ef27340cSAdrian Chadd 			if (ATH_TXQ_SETUP(sc, i)) {
2913b837332dSAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
2914ef27340cSAdrian Chadd 				ath_txq_restart_dma(sc, &sc->sc_txq[i]);
2915b837332dSAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
2916b837332dSAdrian Chadd 
2917b837332dSAdrian Chadd 				ATH_TX_LOCK(sc);
2918ef27340cSAdrian Chadd 				ath_txq_sched(sc, &sc->sc_txq[i]);
2919375307d4SAdrian Chadd 				ATH_TX_UNLOCK(sc);
2920ef27340cSAdrian Chadd 			}
2921b837332dSAdrian Chadd 		}
2922b837332dSAdrian Chadd 	}
2923ef27340cSAdrian Chadd 
2924f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2925f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2926f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2927f5c30c4eSAdrian Chadd 
2928f5c30c4eSAdrian Chadd 	ATH_PCU_LOCK(sc);
2929f5c30c4eSAdrian Chadd 	sc->sc_txstart_cnt--;
2930f5c30c4eSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2931f5c30c4eSAdrian Chadd 
2932ef27340cSAdrian Chadd 	/* Handle any frames in the TX queue */
2933ef27340cSAdrian Chadd 	/*
2934ef27340cSAdrian Chadd 	 * XXX should this be done by the caller, rather than
2935ef27340cSAdrian Chadd 	 * ath_reset() ?
2936ef27340cSAdrian Chadd 	 */
29378e739394SAdrian Chadd 	ath_tx_kick(sc);		/* restart xmit */
2938c42a7b7eSSam Leffler 	return 0;
29395591b213SSam Leffler }
29405591b213SSam Leffler 
294168e8e04eSSam Leffler static int
2942b032f27cSSam Leffler ath_reset_vap(struct ieee80211vap *vap, u_long cmd)
2943b032f27cSSam Leffler {
29444b54a231SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
29453797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
29464b54a231SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
29474b54a231SSam Leffler 
29484b54a231SSam Leffler 	switch (cmd) {
29494b54a231SSam Leffler 	case IEEE80211_IOC_TXPOWER:
29504b54a231SSam Leffler 		/*
29514b54a231SSam Leffler 		 * If per-packet TPC is enabled, then we have nothing
29524b54a231SSam Leffler 		 * to do; otherwise we need to force the global limit.
29534b54a231SSam Leffler 		 * All this can happen directly; no need to reset.
29544b54a231SSam Leffler 		 */
29554b54a231SSam Leffler 		if (!ath_hal_gettpc(ah))
29564b54a231SSam Leffler 			ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
29574b54a231SSam Leffler 		return 0;
29584b54a231SSam Leffler 	}
2959517526efSAdrian Chadd 	/* XXX? Full or NOLOSS? */
29607a79cebfSGleb Smirnoff 	return ath_reset(sc, ATH_RESET_FULL);
2961b032f27cSSam Leffler }
2962b032f27cSSam Leffler 
2963b8e788a5SAdrian Chadd struct ath_buf *
2964af33d486SAdrian Chadd _ath_getbuf_locked(struct ath_softc *sc, ath_buf_type_t btype)
296510ad9a77SSam Leffler {
296610ad9a77SSam Leffler 	struct ath_buf *bf;
296710ad9a77SSam Leffler 
296810ad9a77SSam Leffler 	ATH_TXBUF_LOCK_ASSERT(sc);
296910ad9a77SSam Leffler 
2970af33d486SAdrian Chadd 	if (btype == ATH_BUFTYPE_MGMT)
2971af33d486SAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_txbuf_mgmt);
2972af33d486SAdrian Chadd 	else
29736b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_txbuf);
2974af33d486SAdrian Chadd 
2975e346b073SAdrian Chadd 	if (bf == NULL) {
2976e346b073SAdrian Chadd 		sc->sc_stats.ast_tx_getnobuf++;
2977e346b073SAdrian Chadd 	} else {
2978e346b073SAdrian Chadd 		if (bf->bf_flags & ATH_BUF_BUSY) {
2979e346b073SAdrian Chadd 			sc->sc_stats.ast_tx_getbusybuf++;
2980e346b073SAdrian Chadd 			bf = NULL;
2981e346b073SAdrian Chadd 		}
2982e346b073SAdrian Chadd 	}
2983e346b073SAdrian Chadd 
2984af33d486SAdrian Chadd 	if (bf != NULL && (bf->bf_flags & ATH_BUF_BUSY) == 0) {
2985af33d486SAdrian Chadd 		if (btype == ATH_BUFTYPE_MGMT)
2986af33d486SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_txbuf_mgmt, bf, bf_list);
298723ced6c1SAdrian Chadd 		else {
2988af33d486SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
298923ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt--;
299023ced6c1SAdrian Chadd 
299123ced6c1SAdrian Chadd 			/*
299223ced6c1SAdrian Chadd 			 * This shuldn't happen; however just to be
299323ced6c1SAdrian Chadd 			 * safe print a warning and fudge the txbuf
299423ced6c1SAdrian Chadd 			 * count.
299523ced6c1SAdrian Chadd 			 */
299623ced6c1SAdrian Chadd 			if (sc->sc_txbuf_cnt < 0) {
299723ced6c1SAdrian Chadd 				device_printf(sc->sc_dev,
299823ced6c1SAdrian Chadd 				    "%s: sc_txbuf_cnt < 0?\n",
299923ced6c1SAdrian Chadd 				    __func__);
300023ced6c1SAdrian Chadd 				sc->sc_txbuf_cnt = 0;
300123ced6c1SAdrian Chadd 			}
300223ced6c1SAdrian Chadd 		}
3003af33d486SAdrian Chadd 	} else
300410ad9a77SSam Leffler 		bf = NULL;
3005e346b073SAdrian Chadd 
300610ad9a77SSam Leffler 	if (bf == NULL) {
3007af33d486SAdrian Chadd 		/* XXX should check which list, mgmt or otherwise */
300810ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: %s\n", __func__,
30096b349e5aSAdrian Chadd 		    TAILQ_FIRST(&sc->sc_txbuf) == NULL ?
301010ad9a77SSam Leffler 			"out of xmit buffers" : "xmit buffer busy");
3011e346b073SAdrian Chadd 		return NULL;
301210ad9a77SSam Leffler 	}
3013e346b073SAdrian Chadd 
3014af33d486SAdrian Chadd 	/* XXX TODO: should do this at buffer list initialisation */
3015af33d486SAdrian Chadd 	/* XXX (then, ensure the buffer has the right flag set) */
30163feffbd7SAdrian Chadd 	bf->bf_flags = 0;
3017af33d486SAdrian Chadd 	if (btype == ATH_BUFTYPE_MGMT)
3018af33d486SAdrian Chadd 		bf->bf_flags |= ATH_BUF_MGMT;
3019af33d486SAdrian Chadd 	else
3020af33d486SAdrian Chadd 		bf->bf_flags &= (~ATH_BUF_MGMT);
3021af33d486SAdrian Chadd 
3022e346b073SAdrian Chadd 	/* Valid bf here; clear some basic fields */
3023e346b073SAdrian Chadd 	bf->bf_next = NULL;	/* XXX just to be sure */
3024e346b073SAdrian Chadd 	bf->bf_last = NULL;	/* XXX again, just to be sure */
3025e346b073SAdrian Chadd 	bf->bf_comp = NULL;	/* XXX again, just to be sure */
3026e346b073SAdrian Chadd 	bzero(&bf->bf_state, sizeof(bf->bf_state));
3027e346b073SAdrian Chadd 
302885bf9bc3SAdrian Chadd 	/*
302985bf9bc3SAdrian Chadd 	 * Track the descriptor ID only if doing EDMA
303085bf9bc3SAdrian Chadd 	 */
303185bf9bc3SAdrian Chadd 	if (sc->sc_isedma) {
303285bf9bc3SAdrian Chadd 		bf->bf_descid = sc->sc_txbuf_descid;
303385bf9bc3SAdrian Chadd 		sc->sc_txbuf_descid++;
303485bf9bc3SAdrian Chadd 	}
303585bf9bc3SAdrian Chadd 
303610ad9a77SSam Leffler 	return bf;
303710ad9a77SSam Leffler }
303810ad9a77SSam Leffler 
3039e346b073SAdrian Chadd /*
3040e346b073SAdrian Chadd  * When retrying a software frame, buffers marked ATH_BUF_BUSY
3041e346b073SAdrian Chadd  * can't be thrown back on the queue as they could still be
3042e346b073SAdrian Chadd  * in use by the hardware.
3043e346b073SAdrian Chadd  *
3044e346b073SAdrian Chadd  * This duplicates the buffer, or returns NULL.
3045e346b073SAdrian Chadd  *
3046e346b073SAdrian Chadd  * The descriptor is also copied but the link pointers and
3047e346b073SAdrian Chadd  * the DMA segments aren't copied; this frame should thus
3048e346b073SAdrian Chadd  * be again passed through the descriptor setup/chain routines
3049e346b073SAdrian Chadd  * so the link is correct.
3050e346b073SAdrian Chadd  *
3051e346b073SAdrian Chadd  * The caller must free the buffer using ath_freebuf().
3052e346b073SAdrian Chadd  */
3053e346b073SAdrian Chadd struct ath_buf *
30543f3a5dbdSAdrian Chadd ath_buf_clone(struct ath_softc *sc, struct ath_buf *bf)
3055e346b073SAdrian Chadd {
3056e346b073SAdrian Chadd 	struct ath_buf *tbf;
3057e346b073SAdrian Chadd 
3058af33d486SAdrian Chadd 	tbf = ath_getbuf(sc,
3059af33d486SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_MGMT) ?
3060af33d486SAdrian Chadd 	     ATH_BUFTYPE_MGMT : ATH_BUFTYPE_NORMAL);
3061e346b073SAdrian Chadd 	if (tbf == NULL)
3062e346b073SAdrian Chadd 		return NULL;	/* XXX failure? Why? */
3063e346b073SAdrian Chadd 
3064e346b073SAdrian Chadd 	/* Copy basics */
3065e346b073SAdrian Chadd 	tbf->bf_next = NULL;
3066e346b073SAdrian Chadd 	tbf->bf_nseg = bf->bf_nseg;
30673feffbd7SAdrian Chadd 	tbf->bf_flags = bf->bf_flags & ATH_BUF_FLAGS_CLONE;
3068e346b073SAdrian Chadd 	tbf->bf_status = bf->bf_status;
3069e346b073SAdrian Chadd 	tbf->bf_m = bf->bf_m;
3070e346b073SAdrian Chadd 	tbf->bf_node = bf->bf_node;
3071f5c30c4eSAdrian Chadd 	KASSERT((bf->bf_node != NULL), ("%s: bf_node=NULL!", __func__));
3072e346b073SAdrian Chadd 	/* will be setup by the chain/setup function */
3073e346b073SAdrian Chadd 	tbf->bf_lastds = NULL;
3074e346b073SAdrian Chadd 	/* for now, last == self */
3075e346b073SAdrian Chadd 	tbf->bf_last = tbf;
3076e346b073SAdrian Chadd 	tbf->bf_comp = bf->bf_comp;
3077e346b073SAdrian Chadd 
3078e346b073SAdrian Chadd 	/* NOTE: DMA segments will be setup by the setup/chain functions */
3079e346b073SAdrian Chadd 
3080e346b073SAdrian Chadd 	/* The caller has to re-init the descriptor + links */
3081e346b073SAdrian Chadd 
30823f3a5dbdSAdrian Chadd 	/*
30833f3a5dbdSAdrian Chadd 	 * Free the DMA mapping here, before we NULL the mbuf.
30843f3a5dbdSAdrian Chadd 	 * We must only call bus_dmamap_unload() once per mbuf chain
30853f3a5dbdSAdrian Chadd 	 * or behaviour is undefined.
30863f3a5dbdSAdrian Chadd 	 */
30873f3a5dbdSAdrian Chadd 	if (bf->bf_m != NULL) {
30883f3a5dbdSAdrian Chadd 		/*
30893f3a5dbdSAdrian Chadd 		 * XXX is this POSTWRITE call required?
30903f3a5dbdSAdrian Chadd 		 */
30913f3a5dbdSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
30923f3a5dbdSAdrian Chadd 		    BUS_DMASYNC_POSTWRITE);
30933f3a5dbdSAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
30943f3a5dbdSAdrian Chadd 	}
30953f3a5dbdSAdrian Chadd 
30963f3a5dbdSAdrian Chadd 	bf->bf_m = NULL;
30973f3a5dbdSAdrian Chadd 	bf->bf_node = NULL;
30983f3a5dbdSAdrian Chadd 
3099e346b073SAdrian Chadd 	/* Copy state */
3100e346b073SAdrian Chadd 	memcpy(&tbf->bf_state, &bf->bf_state, sizeof(bf->bf_state));
3101e346b073SAdrian Chadd 
3102e346b073SAdrian Chadd 	return tbf;
3103e346b073SAdrian Chadd }
3104e346b073SAdrian Chadd 
3105b8e788a5SAdrian Chadd struct ath_buf *
3106af33d486SAdrian Chadd ath_getbuf(struct ath_softc *sc, ath_buf_type_t btype)
310710ad9a77SSam Leffler {
310810ad9a77SSam Leffler 	struct ath_buf *bf;
310910ad9a77SSam Leffler 
311010ad9a77SSam Leffler 	ATH_TXBUF_LOCK(sc);
3111af33d486SAdrian Chadd 	bf = _ath_getbuf_locked(sc, btype);
3112af33d486SAdrian Chadd 	/*
3113af33d486SAdrian Chadd 	 * If a mgmt buffer was requested but we're out of those,
3114af33d486SAdrian Chadd 	 * try requesting a normal one.
3115af33d486SAdrian Chadd 	 */
3116af33d486SAdrian Chadd 	if (bf == NULL && btype == ATH_BUFTYPE_MGMT)
3117af33d486SAdrian Chadd 		bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL);
3118e4e7938aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
311910ad9a77SSam Leffler 	if (bf == NULL) {
312010ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: stop queue\n", __func__);
312110ad9a77SSam Leffler 		sc->sc_stats.ast_tx_qstop++;
312210ad9a77SSam Leffler 	}
312310ad9a77SSam Leffler 	return bf;
312410ad9a77SSam Leffler }
312510ad9a77SSam Leffler 
31267dcb2beaSAdrian Chadd /*
3127cd7dffd0SAdrian Chadd  * Transmit a single frame.
3128cd7dffd0SAdrian Chadd  *
3129cd7dffd0SAdrian Chadd  * net80211 will free the node reference if the transmit
3130cd7dffd0SAdrian Chadd  * fails, so don't free the node reference here.
31317dcb2beaSAdrian Chadd  */
3132cd7dffd0SAdrian Chadd static int
31337a79cebfSGleb Smirnoff ath_transmit(struct ieee80211com *ic, struct mbuf *m)
3134cd7dffd0SAdrian Chadd {
31353797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
3136cd7dffd0SAdrian Chadd 	struct ieee80211_node *ni;
3137cd7dffd0SAdrian Chadd 	struct mbuf *next;
3138cd7dffd0SAdrian Chadd 	struct ath_buf *bf;
3139cd7dffd0SAdrian Chadd 	ath_bufhead frags;
3140cd7dffd0SAdrian Chadd 	int retval = 0;
3141cd7dffd0SAdrian Chadd 
3142cd7dffd0SAdrian Chadd 	/*
3143cd7dffd0SAdrian Chadd 	 * Tell the reset path that we're currently transmitting.
3144cd7dffd0SAdrian Chadd 	 */
3145cd7dffd0SAdrian Chadd 	ATH_PCU_LOCK(sc);
3146cd7dffd0SAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
314783bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
3148cd7dffd0SAdrian Chadd 		    "%s: sc_inreset_cnt > 0; bailing\n", __func__);
3149cd7dffd0SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
3150cd7dffd0SAdrian Chadd 		sc->sc_stats.ast_tx_qstop++;
3151cd7dffd0SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TX, 0, "ath_start_task: OACTIVE, finish");
3152cd7dffd0SAdrian Chadd 		return (ENOBUFS);	/* XXX should be EINVAL or? */
3153cd7dffd0SAdrian Chadd 	}
3154cd7dffd0SAdrian Chadd 	sc->sc_txstart_cnt++;
3155cd7dffd0SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3156cd7dffd0SAdrian Chadd 
3157f5c30c4eSAdrian Chadd 	/* Wake the hardware up already */
3158f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3159f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3160f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3161f5c30c4eSAdrian Chadd 
3162cd7dffd0SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_transmit: start");
3163cd7dffd0SAdrian Chadd 	/*
3164cd7dffd0SAdrian Chadd 	 * Grab the TX lock - it's ok to do this here; we haven't
3165cd7dffd0SAdrian Chadd 	 * yet started transmitting.
3166cd7dffd0SAdrian Chadd 	 */
3167cd7dffd0SAdrian Chadd 	ATH_TX_LOCK(sc);
3168cd7dffd0SAdrian Chadd 
3169cd7dffd0SAdrian Chadd 	/*
3170cd7dffd0SAdrian Chadd 	 * Node reference, if there's one.
3171cd7dffd0SAdrian Chadd 	 */
31727dcb2beaSAdrian Chadd 	ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
31737dcb2beaSAdrian Chadd 
31747dcb2beaSAdrian Chadd 	/*
31757dcb2beaSAdrian Chadd 	 * Enforce how deep a node queue can get.
31767dcb2beaSAdrian Chadd 	 *
31777dcb2beaSAdrian Chadd 	 * XXX it would be nicer if we kept an mbuf queue per
31787dcb2beaSAdrian Chadd 	 * node and only whacked them into ath_bufs when we
31797dcb2beaSAdrian Chadd 	 * are ready to schedule some traffic from them.
31807dcb2beaSAdrian Chadd 	 * .. that may come later.
31817dcb2beaSAdrian Chadd 	 *
31827dcb2beaSAdrian Chadd 	 * XXX we should also track the per-node hardware queue
31837dcb2beaSAdrian Chadd 	 * depth so it is easy to limit the _SUM_ of the swq and
31847dcb2beaSAdrian Chadd 	 * hwq frames.  Since we only schedule two HWQ frames
31857dcb2beaSAdrian Chadd 	 * at a time, this should be OK for now.
31867dcb2beaSAdrian Chadd 	 */
31877dcb2beaSAdrian Chadd 	if ((!(m->m_flags & M_EAPOL)) &&
31887dcb2beaSAdrian Chadd 	    (ATH_NODE(ni)->an_swq_depth > sc->sc_txq_node_maxdepth)) {
31897dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nodeq_overflow++;
3190cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
3191cd7dffd0SAdrian Chadd 		goto finish;
31927dcb2beaSAdrian Chadd 	}
31937dcb2beaSAdrian Chadd 
31947dcb2beaSAdrian Chadd 	/*
31957dcb2beaSAdrian Chadd 	 * Check how many TX buffers are available.
31967dcb2beaSAdrian Chadd 	 *
31977dcb2beaSAdrian Chadd 	 * If this is for non-EAPOL traffic, just leave some
31987dcb2beaSAdrian Chadd 	 * space free in order for buffer cloning and raw
31997dcb2beaSAdrian Chadd 	 * frame transmission to occur.
32007dcb2beaSAdrian Chadd 	 *
32017dcb2beaSAdrian Chadd 	 * If it's for EAPOL traffic, ignore this for now.
32027dcb2beaSAdrian Chadd 	 * Management traffic will be sent via the raw transmit
32037dcb2beaSAdrian Chadd 	 * method which bypasses this check.
32047dcb2beaSAdrian Chadd 	 *
32057dcb2beaSAdrian Chadd 	 * This is needed to ensure that EAPOL frames during
32067dcb2beaSAdrian Chadd 	 * (re) keying have a chance to go out.
32077dcb2beaSAdrian Chadd 	 *
32087dcb2beaSAdrian Chadd 	 * See kern/138379 for more information.
32097dcb2beaSAdrian Chadd 	 */
32107dcb2beaSAdrian Chadd 	if ((!(m->m_flags & M_EAPOL)) &&
32117dcb2beaSAdrian Chadd 	    (sc->sc_txbuf_cnt <= sc->sc_txq_data_minfree)) {
32127dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
3213cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
3214cd7dffd0SAdrian Chadd 		goto finish;
321523ced6c1SAdrian Chadd 	}
321623ced6c1SAdrian Chadd 
32175591b213SSam Leffler 	/*
32185591b213SSam Leffler 	 * Grab a TX buffer and associated resources.
32197dcb2beaSAdrian Chadd 	 *
32207dcb2beaSAdrian Chadd 	 * If it's an EAPOL frame, allocate a MGMT ath_buf.
32217dcb2beaSAdrian Chadd 	 * That way even with temporary buffer exhaustion due to
32227dcb2beaSAdrian Chadd 	 * the data path doesn't leave us without the ability
32237dcb2beaSAdrian Chadd 	 * to transmit management frames.
32247dcb2beaSAdrian Chadd 	 *
32257dcb2beaSAdrian Chadd 	 * Otherwise allocate a normal buffer.
32265591b213SSam Leffler 	 */
32277dcb2beaSAdrian Chadd 	if (m->m_flags & M_EAPOL)
32287dcb2beaSAdrian Chadd 		bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT);
32297dcb2beaSAdrian Chadd 	else
3230af33d486SAdrian Chadd 		bf = ath_getbuf(sc, ATH_BUFTYPE_NORMAL);
32311a85141aSAdrian Chadd 
32327dcb2beaSAdrian Chadd 	if (bf == NULL) {
32337dcb2beaSAdrian Chadd 		/*
3234cd7dffd0SAdrian Chadd 		 * If we failed to allocate a buffer, fail.
32357dcb2beaSAdrian Chadd 		 *
32367dcb2beaSAdrian Chadd 		 * We shouldn't fail normally, due to the check
32377dcb2beaSAdrian Chadd 		 * above.
32387dcb2beaSAdrian Chadd 		 */
32397dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
3240cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
3241cd7dffd0SAdrian Chadd 		goto finish;
3242b032f27cSSam Leffler 	}
32437dcb2beaSAdrian Chadd 
3244cd7dffd0SAdrian Chadd 	/*
3245cd7dffd0SAdrian Chadd 	 * At this point we have a buffer; so we need to free it
3246cd7dffd0SAdrian Chadd 	 * if we hit any error conditions.
3247cd7dffd0SAdrian Chadd 	 */
32487dcb2beaSAdrian Chadd 
324968e8e04eSSam Leffler 	/*
325068e8e04eSSam Leffler 	 * Check for fragmentation.  If this frame
325168e8e04eSSam Leffler 	 * has been broken up verify we have enough
325268e8e04eSSam Leffler 	 * buffers to send all the fragments so all
325368e8e04eSSam Leffler 	 * go out or none...
325468e8e04eSSam Leffler 	 */
32556b349e5aSAdrian Chadd 	TAILQ_INIT(&frags);
32561a85141aSAdrian Chadd 	if ((m->m_flags & M_FRAG) &&
32571a85141aSAdrian Chadd 	    !ath_txfrag_setup(sc, &frags, m, ni)) {
325868e8e04eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT,
325968e8e04eSSam Leffler 		    "%s: out of txfrag buffers\n", __func__);
326036c6be9aSSam Leffler 		sc->sc_stats.ast_tx_nofrag++;
32617a79cebfSGleb Smirnoff 		if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
32627a79cebfSGleb Smirnoff 		/*
32637a79cebfSGleb Smirnoff 		 * XXXGL: is mbuf valid after ath_txfrag_setup? If yes,
32647a79cebfSGleb Smirnoff 		 * we shouldn't free it but return back.
32657a79cebfSGleb Smirnoff 		 */
3266d07be335SAdrian Chadd 		ieee80211_free_mbuf(m);
32677a79cebfSGleb Smirnoff 		m = NULL;
326868e8e04eSSam Leffler 		goto bad;
326968e8e04eSSam Leffler 	}
3270cd7dffd0SAdrian Chadd 
3271cd7dffd0SAdrian Chadd 	/*
3272cd7dffd0SAdrian Chadd 	 * At this point if we have any TX fragments, then we will
3273cd7dffd0SAdrian Chadd 	 * have bumped the node reference once for each of those.
3274cd7dffd0SAdrian Chadd 	 */
3275cd7dffd0SAdrian Chadd 
3276cd7dffd0SAdrian Chadd 	/*
3277cd7dffd0SAdrian Chadd 	 * XXX Is there anything actually _enforcing_ that the
3278cd7dffd0SAdrian Chadd 	 * fragments are being transmitted in one hit, rather than
3279cd7dffd0SAdrian Chadd 	 * being interleaved with other transmissions on that
3280cd7dffd0SAdrian Chadd 	 * hardware queue?
3281cd7dffd0SAdrian Chadd 	 *
3282cd7dffd0SAdrian Chadd 	 * The ATH TX output lock is the only thing serialising this
3283cd7dffd0SAdrian Chadd 	 * right now.
3284cd7dffd0SAdrian Chadd 	 */
3285cd7dffd0SAdrian Chadd 
3286cd7dffd0SAdrian Chadd 	/*
3287cd7dffd0SAdrian Chadd 	 * Calculate the "next fragment" length field in ath_buf
3288cd7dffd0SAdrian Chadd 	 * in order to let the transmit path know enough about
3289cd7dffd0SAdrian Chadd 	 * what to next write to the hardware.
3290cd7dffd0SAdrian Chadd 	 */
3291cd7dffd0SAdrian Chadd 	if (m->m_flags & M_FRAG) {
3292cd7dffd0SAdrian Chadd 		struct ath_buf *fbf = bf;
3293cd7dffd0SAdrian Chadd 		struct ath_buf *n_fbf = NULL;
3294cd7dffd0SAdrian Chadd 		struct mbuf *fm = m->m_nextpkt;
3295cd7dffd0SAdrian Chadd 
3296cd7dffd0SAdrian Chadd 		/*
3297cd7dffd0SAdrian Chadd 		 * We need to walk the list of fragments and set
3298cd7dffd0SAdrian Chadd 		 * the next size to the following buffer.
3299cd7dffd0SAdrian Chadd 		 * However, the first buffer isn't in the frag
3300cd7dffd0SAdrian Chadd 		 * list, so we have to do some gymnastics here.
3301cd7dffd0SAdrian Chadd 		 */
3302cd7dffd0SAdrian Chadd 		TAILQ_FOREACH(n_fbf, &frags, bf_list) {
3303cd7dffd0SAdrian Chadd 			fbf->bf_nextfraglen = fm->m_pkthdr.len;
3304cd7dffd0SAdrian Chadd 			fbf = n_fbf;
3305cd7dffd0SAdrian Chadd 			fm = fm->m_nextpkt;
3306cd7dffd0SAdrian Chadd 		}
3307cd7dffd0SAdrian Chadd 	}
3308cd7dffd0SAdrian Chadd 
33091a85141aSAdrian Chadd nextfrag:
331068e8e04eSSam Leffler 	/*
33111a85141aSAdrian Chadd 	 * Pass the frame to the h/w for transmission.
33121a85141aSAdrian Chadd 	 * Fragmented frames have each frag chained together
33131a85141aSAdrian Chadd 	 * with m_nextpkt.  We know there are sufficient ath_buf's
33141a85141aSAdrian Chadd 	 * to send all the frags because of work done by
33151a85141aSAdrian Chadd 	 * ath_txfrag_setup.  We leave m_nextpkt set while
33161a85141aSAdrian Chadd 	 * calling ath_tx_start so it can use it to extend the
33171a85141aSAdrian Chadd 	 * the tx duration to cover the subsequent frag and
33181a85141aSAdrian Chadd 	 * so it can reclaim all the mbufs in case of an error;
33191a85141aSAdrian Chadd 	 * ath_tx_start clears m_nextpkt once it commits to
33201a85141aSAdrian Chadd 	 * handing the frame to the hardware.
3321cd7dffd0SAdrian Chadd 	 *
3322cd7dffd0SAdrian Chadd 	 * Note: if this fails, then the mbufs are freed but
3323cd7dffd0SAdrian Chadd 	 * not the node reference.
3324da4552abSAdrian Chadd 	 *
3325da4552abSAdrian Chadd 	 * So, we now have to free the node reference ourselves here
3326da4552abSAdrian Chadd 	 * and return OK up to the stack.
332768e8e04eSSam Leffler 	 */
33281a85141aSAdrian Chadd 	next = m->m_nextpkt;
33291a85141aSAdrian Chadd 	if (ath_tx_start(sc, ni, bf, m)) {
33305591b213SSam Leffler bad:
33317a79cebfSGleb Smirnoff 		if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
33321a85141aSAdrian Chadd reclaim:
333368e8e04eSSam Leffler 		bf->bf_m = NULL;
333468e8e04eSSam Leffler 		bf->bf_node = NULL;
3335c42a7b7eSSam Leffler 		ATH_TXBUF_LOCK(sc);
3336e1a50456SAdrian Chadd 		ath_returnbuf_head(sc, bf);
3337cd7dffd0SAdrian Chadd 		/*
3338cd7dffd0SAdrian Chadd 		 * Free the rest of the node references and
3339cd7dffd0SAdrian Chadd 		 * buffers for the fragment list.
3340cd7dffd0SAdrian Chadd 		 */
334168e8e04eSSam Leffler 		ath_txfrag_cleanup(sc, &frags, ni);
3342c42a7b7eSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
3343da4552abSAdrian Chadd 
3344da4552abSAdrian Chadd 		/*
3345da4552abSAdrian Chadd 		 * XXX: And free the node/return OK; ath_tx_start() may have
3346da4552abSAdrian Chadd 		 *      modified the buffer.  We currently have no way to
3347da4552abSAdrian Chadd 		 *      signify that the mbuf was freed but there was an error.
3348da4552abSAdrian Chadd 		 */
3349da4552abSAdrian Chadd 		ieee80211_free_node(ni);
3350da4552abSAdrian Chadd 		retval = 0;
3351cd7dffd0SAdrian Chadd 		goto finish;
33521a85141aSAdrian Chadd 	}
33531a85141aSAdrian Chadd 
3354548a605dSAdrian Chadd 	/*
3355548a605dSAdrian Chadd 	 * Check here if the node is in power save state.
3356548a605dSAdrian Chadd 	 */
3357548a605dSAdrian Chadd 	ath_tx_update_tim(sc, ni, 1);
3358548a605dSAdrian Chadd 
33591a85141aSAdrian Chadd 	if (next != NULL) {
336068e8e04eSSam Leffler 		/*
33611a85141aSAdrian Chadd 		 * Beware of state changing between frags.
33621a85141aSAdrian Chadd 		 * XXX check sta power-save state?
336368e8e04eSSam Leffler 		 */
33641a85141aSAdrian Chadd 		if (ni->ni_vap->iv_state != IEEE80211_S_RUN) {
3365c5239edbSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
33661a85141aSAdrian Chadd 			    "%s: flush fragmented packet, state %s\n",
33671a85141aSAdrian Chadd 			    __func__,
33681a85141aSAdrian Chadd 			    ieee80211_state_name[ni->ni_vap->iv_state]);
3369a91ab3c0SAdrian Chadd 			/* XXX dmamap */
3370d07be335SAdrian Chadd 			ieee80211_free_mbuf(next);
33711a85141aSAdrian Chadd 			goto reclaim;
3372c5239edbSAdrian Chadd 		}
33731a85141aSAdrian Chadd 		m = next;
33741a85141aSAdrian Chadd 		bf = TAILQ_FIRST(&frags);
33751a85141aSAdrian Chadd 		KASSERT(bf != NULL, ("no buf for txfrag"));
33761a85141aSAdrian Chadd 		TAILQ_REMOVE(&frags, bf, bf_list);
33771a85141aSAdrian Chadd 		goto nextfrag;
3378c5239edbSAdrian Chadd 	}
3379c5239edbSAdrian Chadd 
3380cd7dffd0SAdrian Chadd 	/*
3381cd7dffd0SAdrian Chadd 	 * Bump watchdog timer.
3382cd7dffd0SAdrian Chadd 	 */
33831a85141aSAdrian Chadd 	sc->sc_wd_timer = 5;
3384cd7dffd0SAdrian Chadd 
3385cd7dffd0SAdrian Chadd finish:
3386cd7dffd0SAdrian Chadd 	ATH_TX_UNLOCK(sc);
3387cd7dffd0SAdrian Chadd 
3388cd7dffd0SAdrian Chadd 	/*
3389cd7dffd0SAdrian Chadd 	 * Finished transmitting!
3390cd7dffd0SAdrian Chadd 	 */
3391cd7dffd0SAdrian Chadd 	ATH_PCU_LOCK(sc);
3392cd7dffd0SAdrian Chadd 	sc->sc_txstart_cnt--;
3393cd7dffd0SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3394cd7dffd0SAdrian Chadd 
3395f5c30c4eSAdrian Chadd 	/* Sleep the hardware if required */
3396f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3397f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3398f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3399f5c30c4eSAdrian Chadd 
3400cd7dffd0SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_transmit: finished");
3401cd7dffd0SAdrian Chadd 
3402cd7dffd0SAdrian Chadd 	return (retval);
34035591b213SSam Leffler }
3404cd7dffd0SAdrian Chadd 
34055591b213SSam Leffler static int
34065591b213SSam Leffler ath_media_change(struct ifnet *ifp)
34075591b213SSam Leffler {
3408b032f27cSSam Leffler 	int error = ieee80211_media_change(ifp);
3409b032f27cSSam Leffler 	/* NB: only the fixed rate can change and that doesn't need a reset */
3410b032f27cSSam Leffler 	return (error == ENETRESET ? 0 : error);
34115591b213SSam Leffler }
34125591b213SSam Leffler 
3413c42a7b7eSSam Leffler /*
3414c42a7b7eSSam Leffler  * Block/unblock tx+rx processing while a key change is done.
3415c42a7b7eSSam Leffler  * We assume the caller serializes key management operations
3416c42a7b7eSSam Leffler  * so we only need to worry about synchronization with other
3417c42a7b7eSSam Leffler  * uses that originate in the driver.
3418c42a7b7eSSam Leffler  */
3419c42a7b7eSSam Leffler static void
3420b032f27cSSam Leffler ath_key_update_begin(struct ieee80211vap *vap)
3421c42a7b7eSSam Leffler {
34223797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
3423c42a7b7eSSam Leffler 
3424c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
3425b032f27cSSam Leffler 	taskqueue_block(sc->sc_tq);
3426c42a7b7eSSam Leffler }
3427c42a7b7eSSam Leffler 
3428c42a7b7eSSam Leffler static void
3429b032f27cSSam Leffler ath_key_update_end(struct ieee80211vap *vap)
3430c42a7b7eSSam Leffler {
34313797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
3432c42a7b7eSSam Leffler 
3433c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
3434b032f27cSSam Leffler 	taskqueue_unblock(sc->sc_tq);
3435c42a7b7eSSam Leffler }
34365591b213SSam Leffler 
3437b032f27cSSam Leffler static void
3438272f6adeSGleb Smirnoff ath_update_promisc(struct ieee80211com *ic)
3439b032f27cSSam Leffler {
3440272f6adeSGleb Smirnoff 	struct ath_softc *sc = ic->ic_softc;
3441b032f27cSSam Leffler 	u_int32_t rfilt;
3442b032f27cSSam Leffler 
3443b032f27cSSam Leffler 	/* configure rx filter */
3444f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3445f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3446b032f27cSSam Leffler 	rfilt = ath_calcrxfilter(sc);
3447b032f27cSSam Leffler 	ath_hal_setrxfilter(sc->sc_ah, rfilt);
3448f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3449f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3450b032f27cSSam Leffler 
3451b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x\n", __func__, rfilt);
3452b032f27cSSam Leffler }
3453b032f27cSSam Leffler 
3454e5bd159eSAdrian Chadd /*
3455e5bd159eSAdrian Chadd  * Driver-internal mcast update call.
3456e5bd159eSAdrian Chadd  *
3457e5bd159eSAdrian Chadd  * Assumes the hardware is already awake.
3458e5bd159eSAdrian Chadd  */
3459b032f27cSSam Leffler static void
3460e5bd159eSAdrian Chadd ath_update_mcast_hw(struct ath_softc *sc)
3461b032f27cSSam Leffler {
34627a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
3463b032f27cSSam Leffler 	u_int32_t mfilt[2];
3464b032f27cSSam Leffler 
3465b032f27cSSam Leffler 	/* calculate and install multicast filter */
34667a79cebfSGleb Smirnoff 	if (ic->ic_allmulti == 0) {
34677a79cebfSGleb Smirnoff 		struct ieee80211vap *vap;
34687a79cebfSGleb Smirnoff 		struct ifnet *ifp;
3469b032f27cSSam Leffler 		struct ifmultiaddr *ifma;
34707a79cebfSGleb Smirnoff 
3471b032f27cSSam Leffler 		/*
3472b032f27cSSam Leffler 		 * Merge multicast addresses to form the hardware filter.
3473b032f27cSSam Leffler 		 */
3474b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = 0;
34757a79cebfSGleb Smirnoff 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
34767a79cebfSGleb Smirnoff 			ifp = vap->iv_ifp;
34777a79cebfSGleb Smirnoff 			if_maddr_rlock(ifp);
3478b032f27cSSam Leffler 			TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3479b032f27cSSam Leffler 				caddr_t dl;
34807a79cebfSGleb Smirnoff 				uint32_t val;
34817a79cebfSGleb Smirnoff 				uint8_t pos;
3482b032f27cSSam Leffler 
3483b032f27cSSam Leffler 				/* calculate XOR of eight 6bit values */
34847a79cebfSGleb Smirnoff 				dl = LLADDR((struct sockaddr_dl *)
34857a79cebfSGleb Smirnoff 				    ifma->ifma_addr);
3486b032f27cSSam Leffler 				val = LE_READ_4(dl + 0);
34877a79cebfSGleb Smirnoff 				pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^
34887a79cebfSGleb Smirnoff 				    val;
3489b032f27cSSam Leffler 				val = LE_READ_4(dl + 3);
34907a79cebfSGleb Smirnoff 				pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^
34917a79cebfSGleb Smirnoff 				    val;
3492b032f27cSSam Leffler 				pos &= 0x3f;
3493b032f27cSSam Leffler 				mfilt[pos / 32] |= (1 << (pos % 32));
3494b032f27cSSam Leffler 			}
3495eb956cd0SRobert Watson 			if_maddr_runlock(ifp);
34967a79cebfSGleb Smirnoff 		}
3497b032f27cSSam Leffler 	} else
3498b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = ~0;
3499e5bd159eSAdrian Chadd 
3500b032f27cSSam Leffler 	ath_hal_setmcastfilter(sc->sc_ah, mfilt[0], mfilt[1]);
3501e5bd159eSAdrian Chadd 
3502b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: MC filter %08x:%08x\n",
3503b032f27cSSam Leffler 		__func__, mfilt[0], mfilt[1]);
35044bc0e754SSam Leffler }
35054bc0e754SSam Leffler 
3506e5bd159eSAdrian Chadd /*
3507e5bd159eSAdrian Chadd  * Called from the net80211 layer - force the hardware
3508e5bd159eSAdrian Chadd  * awake before operating.
3509e5bd159eSAdrian Chadd  */
3510e5bd159eSAdrian Chadd static void
3511272f6adeSGleb Smirnoff ath_update_mcast(struct ieee80211com *ic)
3512e5bd159eSAdrian Chadd {
3513272f6adeSGleb Smirnoff 	struct ath_softc *sc = ic->ic_softc;
3514e5bd159eSAdrian Chadd 
3515e5bd159eSAdrian Chadd 	ATH_LOCK(sc);
3516e5bd159eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3517e5bd159eSAdrian Chadd 	ATH_UNLOCK(sc);
3518e5bd159eSAdrian Chadd 
3519e5bd159eSAdrian Chadd 	ath_update_mcast_hw(sc);
3520e5bd159eSAdrian Chadd 
3521e5bd159eSAdrian Chadd 	ATH_LOCK(sc);
3522e5bd159eSAdrian Chadd 	ath_power_restore_power_state(sc);
3523e5bd159eSAdrian Chadd 	ATH_UNLOCK(sc);
3524e5bd159eSAdrian Chadd }
3525e5bd159eSAdrian Chadd 
3526e60c4fc2SAdrian Chadd void
35275591b213SSam Leffler ath_mode_init(struct ath_softc *sc)
35285591b213SSam Leffler {
35297a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
3530b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3531b032f27cSSam Leffler 	u_int32_t rfilt;
35325591b213SSam Leffler 
35334bc0e754SSam Leffler 	/* configure rx filter */
353468e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
35354bc0e754SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
35364bc0e754SSam Leffler 
35375591b213SSam Leffler 	/* configure operational mode */
3538c42a7b7eSSam Leffler 	ath_hal_setopmode(ah);
3539c42a7b7eSSam Leffler 
354029aca940SSam Leffler 	/* handle any link-level address change */
35417a79cebfSGleb Smirnoff 	ath_hal_setmac(ah, ic->ic_macaddr);
35425591b213SSam Leffler 
35435591b213SSam Leffler 	/* calculate and install multicast filter */
3544e5bd159eSAdrian Chadd 	ath_update_mcast_hw(sc);
35455591b213SSam Leffler }
35465591b213SSam Leffler 
3547c42a7b7eSSam Leffler /*
3548c42a7b7eSSam Leffler  * Set the slot time based on the current setting.
3549c42a7b7eSSam Leffler  */
3550ba5c15d9SAdrian Chadd void
3551c42a7b7eSSam Leffler ath_setslottime(struct ath_softc *sc)
3552c42a7b7eSSam Leffler {
35537a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
3554c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3555aaa70f2fSSam Leffler 	u_int usec;
3556c42a7b7eSSam Leffler 
3557aaa70f2fSSam Leffler 	if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan))
3558aaa70f2fSSam Leffler 		usec = 13;
3559aaa70f2fSSam Leffler 	else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan))
3560aaa70f2fSSam Leffler 		usec = 21;
3561724c193aSSam Leffler 	else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) {
3562724c193aSSam Leffler 		/* honor short/long slot time only in 11g */
3563724c193aSSam Leffler 		/* XXX shouldn't honor on pure g or turbo g channel */
3564724c193aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
3565aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_9;
3566aaa70f2fSSam Leffler 		else
3567aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_20;
3568724c193aSSam Leffler 	} else
3569724c193aSSam Leffler 		usec = HAL_SLOT_TIME_9;
3570aaa70f2fSSam Leffler 
3571aaa70f2fSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
3572aaa70f2fSSam Leffler 	    "%s: chan %u MHz flags 0x%x %s slot, %u usec\n",
3573aaa70f2fSSam Leffler 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
3574aaa70f2fSSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec);
3575aaa70f2fSSam Leffler 
3576f5c30c4eSAdrian Chadd 	/* Wake up the hardware first before updating the slot time */
3577f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3578f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3579aaa70f2fSSam Leffler 	ath_hal_setslottime(ah, usec);
3580f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3581c42a7b7eSSam Leffler 	sc->sc_updateslot = OK;
3582f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3583c42a7b7eSSam Leffler }
3584c42a7b7eSSam Leffler 
3585c42a7b7eSSam Leffler /*
3586c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update the
3587c42a7b7eSSam Leffler  * slot time based on the current setting.
3588c42a7b7eSSam Leffler  */
3589c42a7b7eSSam Leffler static void
3590272f6adeSGleb Smirnoff ath_updateslot(struct ieee80211com *ic)
3591c42a7b7eSSam Leffler {
3592272f6adeSGleb Smirnoff 	struct ath_softc *sc = ic->ic_softc;
3593c42a7b7eSSam Leffler 
3594c42a7b7eSSam Leffler 	/*
3595c42a7b7eSSam Leffler 	 * When not coordinating the BSS, change the hardware
3596c42a7b7eSSam Leffler 	 * immediately.  For other operation we defer the change
3597c42a7b7eSSam Leffler 	 * until beacon updates have propagated to the stations.
3598f5c30c4eSAdrian Chadd 	 *
3599f5c30c4eSAdrian Chadd 	 * XXX sc_updateslot isn't changed behind a lock?
3600c42a7b7eSSam Leffler 	 */
360159aa14a9SRui Paulo 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
360259aa14a9SRui Paulo 	    ic->ic_opmode == IEEE80211_M_MBSS)
3603c42a7b7eSSam Leffler 		sc->sc_updateslot = UPDATE;
3604c42a7b7eSSam Leffler 	else
3605c42a7b7eSSam Leffler 		ath_setslottime(sc);
3606c42a7b7eSSam Leffler }
3607c42a7b7eSSam Leffler 
3608c42a7b7eSSam Leffler /*
3609622b3fd2SSam Leffler  * Append the contents of src to dst; both queues
3610622b3fd2SSam Leffler  * are assumed to be locked.
3611622b3fd2SSam Leffler  */
3612ba5c15d9SAdrian Chadd void
3613622b3fd2SSam Leffler ath_txqmove(struct ath_txq *dst, struct ath_txq *src)
3614622b3fd2SSam Leffler {
3615e86fd7a7SAdrian Chadd 
3616b837332dSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(src);
3617b837332dSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(dst);
3618b837332dSAdrian Chadd 
36196b349e5aSAdrian Chadd 	TAILQ_CONCAT(&dst->axq_q, &src->axq_q, bf_list);
3620622b3fd2SSam Leffler 	dst->axq_link = src->axq_link;
3621622b3fd2SSam Leffler 	src->axq_link = NULL;
3622622b3fd2SSam Leffler 	dst->axq_depth += src->axq_depth;
36236edf1dc7SAdrian Chadd 	dst->axq_aggr_depth += src->axq_aggr_depth;
3624622b3fd2SSam Leffler 	src->axq_depth = 0;
36256edf1dc7SAdrian Chadd 	src->axq_aggr_depth = 0;
3626622b3fd2SSam Leffler }
3627622b3fd2SSam Leffler 
3628622b3fd2SSam Leffler /*
3629d52f7132SAdrian Chadd  * Reset the hardware, with no loss.
3630d52f7132SAdrian Chadd  *
3631d52f7132SAdrian Chadd  * This can't be used for a general case reset.
3632d52f7132SAdrian Chadd  */
3633d52f7132SAdrian Chadd static void
3634d52f7132SAdrian Chadd ath_reset_proc(void *arg, int pending)
3635d52f7132SAdrian Chadd {
3636d52f7132SAdrian Chadd 	struct ath_softc *sc = arg;
3637d52f7132SAdrian Chadd 
3638d52f7132SAdrian Chadd #if 0
363976e6fd5dSGleb Smirnoff 	device_printf(sc->sc_dev, "%s: resetting\n", __func__);
3640d52f7132SAdrian Chadd #endif
36417a79cebfSGleb Smirnoff 	ath_reset(sc, ATH_RESET_NOLOSS);
3642d52f7132SAdrian Chadd }
3643d52f7132SAdrian Chadd 
3644d52f7132SAdrian Chadd /*
3645c42a7b7eSSam Leffler  * Reset the hardware after detecting beacons have stopped.
3646c42a7b7eSSam Leffler  */
3647c42a7b7eSSam Leffler static void
3648c42a7b7eSSam Leffler ath_bstuck_proc(void *arg, int pending)
3649c42a7b7eSSam Leffler {
3650c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
365116d4de92SAdrian Chadd 	uint32_t hangs = 0;
365216d4de92SAdrian Chadd 
365316d4de92SAdrian Chadd 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0)
365476e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "bb hang detected (0x%x)\n", hangs);
3655c42a7b7eSSam Leffler 
3656370f81faSAdrian Chadd #ifdef	ATH_DEBUG_ALQ
3657370f81faSAdrian Chadd 	if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_STUCK_BEACON))
3658370f81faSAdrian Chadd 		if_ath_alq_post(&sc->sc_alq, ATH_ALQ_STUCK_BEACON, 0, NULL);
3659370f81faSAdrian Chadd #endif
3660370f81faSAdrian Chadd 
366176e6fd5dSGleb Smirnoff 	device_printf(sc->sc_dev, "stuck beacon; resetting (bmiss count %u)\n",
3662c42a7b7eSSam Leffler 	    sc->sc_bmisscount);
3663c2e34459SSam Leffler 	sc->sc_stats.ast_bstuck++;
366416d4de92SAdrian Chadd 	/*
366516d4de92SAdrian Chadd 	 * This assumes that there's no simultaneous channel mode change
366616d4de92SAdrian Chadd 	 * occuring.
366716d4de92SAdrian Chadd 	 */
36687a79cebfSGleb Smirnoff 	ath_reset(sc, ATH_RESET_NOLOSS);
3669c42a7b7eSSam Leffler }
3670c42a7b7eSSam Leffler 
36715591b213SSam Leffler static void
36725591b213SSam Leffler ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
36735591b213SSam Leffler {
36745591b213SSam Leffler 	bus_addr_t *paddr = (bus_addr_t*) arg;
3675d77367bfSSam Leffler 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
36765591b213SSam Leffler 	*paddr = segs->ds_addr;
36775591b213SSam Leffler }
36785591b213SSam Leffler 
3679c9f78537SAdrian Chadd /*
3680c9f78537SAdrian Chadd  * Allocate the descriptors and appropriate DMA tag/setup.
3681c9f78537SAdrian Chadd  *
3682c9f78537SAdrian Chadd  * For some situations (eg EDMA TX completion), there isn't a requirement
3683c9f78537SAdrian Chadd  * for the ath_buf entries to be allocated.
3684c9f78537SAdrian Chadd  */
36853d184db2SAdrian Chadd int
3686c9f78537SAdrian Chadd ath_descdma_alloc_desc(struct ath_softc *sc,
3687c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head,
3688b39722d6SAdrian Chadd 	const char *name, int ds_size, int ndesc)
3689c42a7b7eSSam Leffler {
3690c42a7b7eSSam Leffler #define	DS2PHYS(_dd, _ds) \
3691c42a7b7eSSam Leffler 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
369245abcd6cSAdrian Chadd #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
369345abcd6cSAdrian Chadd 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
3694c9f78537SAdrian Chadd 	int error;
369545abcd6cSAdrian Chadd 
36961006fc0cSAdrian Chadd 	dd->dd_descsize = ds_size;
3697c42a7b7eSSam Leffler 
36983d9b1596SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
3699b39722d6SAdrian Chadd 	    "%s: %s DMA: %u desc, %d bytes per descriptor\n",
3700b39722d6SAdrian Chadd 	    __func__, name, ndesc, dd->dd_descsize);
3701c42a7b7eSSam Leffler 
3702c42a7b7eSSam Leffler 	dd->dd_name = name;
3703b39722d6SAdrian Chadd 	dd->dd_desc_len = dd->dd_descsize * ndesc;
370445abcd6cSAdrian Chadd 
370545abcd6cSAdrian Chadd 	/*
370645abcd6cSAdrian Chadd 	 * Merlin work-around:
370745abcd6cSAdrian Chadd 	 * Descriptors that cross the 4KB boundary can't be used.
370845abcd6cSAdrian Chadd 	 * Assume one skipped descriptor per 4KB page.
370945abcd6cSAdrian Chadd 	 */
371045abcd6cSAdrian Chadd 	if (! ath_hal_split4ktrans(sc->sc_ah)) {
3711b39722d6SAdrian Chadd 		int numpages = dd->dd_desc_len / 4096;
3712b39722d6SAdrian Chadd 		dd->dd_desc_len += ds_size * numpages;
371345abcd6cSAdrian Chadd 	}
3714c42a7b7eSSam Leffler 
3715c42a7b7eSSam Leffler 	/*
3716c42a7b7eSSam Leffler 	 * Setup DMA descriptor area.
3717a91ab3c0SAdrian Chadd 	 *
3718a91ab3c0SAdrian Chadd 	 * BUS_DMA_ALLOCNOW is not used; we never use bounce
3719a91ab3c0SAdrian Chadd 	 * buffers for the descriptors themselves.
3720c42a7b7eSSam Leffler 	 */
3721c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev),	/* parent */
3722c42a7b7eSSam Leffler 		       PAGE_SIZE, 0,		/* alignment, bounds */
3723c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
3724c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR,	/* highaddr */
3725c42a7b7eSSam Leffler 		       NULL, NULL,		/* filter, filterarg */
3726c42a7b7eSSam Leffler 		       dd->dd_desc_len,		/* maxsize */
3727c42a7b7eSSam Leffler 		       1,			/* nsegments */
37286ccb8ea7SSam Leffler 		       dd->dd_desc_len,		/* maxsegsize */
3729a91ab3c0SAdrian Chadd 		       0,			/* flags */
3730c42a7b7eSSam Leffler 		       NULL,			/* lockfunc */
3731c42a7b7eSSam Leffler 		       NULL,			/* lockarg */
3732c42a7b7eSSam Leffler 		       &dd->dd_dmat);
3733c42a7b7eSSam Leffler 	if (error != 0) {
373476e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
373576e6fd5dSGleb Smirnoff 		    "cannot allocate %s DMA tag\n", dd->dd_name);
3736c42a7b7eSSam Leffler 		return error;
3737c42a7b7eSSam Leffler 	}
3738c42a7b7eSSam Leffler 
3739c42a7b7eSSam Leffler 	/* allocate descriptors */
3740c42a7b7eSSam Leffler 	error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
37410553a01fSSam Leffler 				 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
37420553a01fSSam Leffler 				 &dd->dd_dmamap);
3743c42a7b7eSSam Leffler 	if (error != 0) {
374476e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
374576e6fd5dSGleb Smirnoff 		    "unable to alloc memory for %u %s descriptors, error %u\n",
374676e6fd5dSGleb Smirnoff 		    ndesc, dd->dd_name, error);
3747c42a7b7eSSam Leffler 		goto fail1;
3748c42a7b7eSSam Leffler 	}
3749c42a7b7eSSam Leffler 
3750c42a7b7eSSam Leffler 	error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
3751c42a7b7eSSam Leffler 				dd->dd_desc, dd->dd_desc_len,
3752c42a7b7eSSam Leffler 				ath_load_cb, &dd->dd_desc_paddr,
3753c42a7b7eSSam Leffler 				BUS_DMA_NOWAIT);
3754c42a7b7eSSam Leffler 	if (error != 0) {
375576e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
375676e6fd5dSGleb Smirnoff 		    "unable to map %s descriptors, error %u\n",
3757c42a7b7eSSam Leffler 		    dd->dd_name, error);
3758c42a7b7eSSam Leffler 		goto fail2;
3759c42a7b7eSSam Leffler 	}
3760c42a7b7eSSam Leffler 
3761c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
3762c9f78537SAdrian Chadd 	    __func__, dd->dd_name, (uint8_t *) dd->dd_desc,
3763c9f78537SAdrian Chadd 	    (u_long) dd->dd_desc_len, (caddr_t) dd->dd_desc_paddr,
3764c9f78537SAdrian Chadd 	    /*XXX*/ (u_long) dd->dd_desc_len);
3765c9f78537SAdrian Chadd 
3766c9f78537SAdrian Chadd 	return (0);
3767c9f78537SAdrian Chadd 
3768c9f78537SAdrian Chadd fail2:
3769c9f78537SAdrian Chadd 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3770c9f78537SAdrian Chadd fail1:
3771c9f78537SAdrian Chadd 	bus_dma_tag_destroy(dd->dd_dmat);
3772c9f78537SAdrian Chadd 	memset(dd, 0, sizeof(*dd));
3773c9f78537SAdrian Chadd 	return error;
3774c9f78537SAdrian Chadd #undef DS2PHYS
3775c9f78537SAdrian Chadd #undef ATH_DESC_4KB_BOUND_CHECK
3776c9f78537SAdrian Chadd }
3777c9f78537SAdrian Chadd 
3778c9f78537SAdrian Chadd int
3779c9f78537SAdrian Chadd ath_descdma_setup(struct ath_softc *sc,
3780c9f78537SAdrian Chadd 	struct ath_descdma *dd, ath_bufhead *head,
3781c9f78537SAdrian Chadd 	const char *name, int ds_size, int nbuf, int ndesc)
3782c9f78537SAdrian Chadd {
3783c9f78537SAdrian Chadd #define	DS2PHYS(_dd, _ds) \
3784c9f78537SAdrian Chadd 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
3785c9f78537SAdrian Chadd #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
3786c9f78537SAdrian Chadd 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
3787c9f78537SAdrian Chadd 	uint8_t *ds;
3788c9f78537SAdrian Chadd 	struct ath_buf *bf;
3789c9f78537SAdrian Chadd 	int i, bsize, error;
3790c9f78537SAdrian Chadd 
3791c9f78537SAdrian Chadd 	/* Allocate descriptors */
3792c9f78537SAdrian Chadd 	error = ath_descdma_alloc_desc(sc, dd, head, name, ds_size,
3793b39722d6SAdrian Chadd 	    nbuf * ndesc);
3794c9f78537SAdrian Chadd 
3795c9f78537SAdrian Chadd 	/* Assume any errors during allocation were dealt with */
3796c9f78537SAdrian Chadd 	if (error != 0) {
3797c9f78537SAdrian Chadd 		return (error);
3798c9f78537SAdrian Chadd 	}
3799c9f78537SAdrian Chadd 
3800c9f78537SAdrian Chadd 	ds = (uint8_t *) dd->dd_desc;
3801c42a7b7eSSam Leffler 
3802ebecf802SSam Leffler 	/* allocate rx buffers */
3803c42a7b7eSSam Leffler 	bsize = sizeof(struct ath_buf) * nbuf;
3804c42a7b7eSSam Leffler 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
3805c42a7b7eSSam Leffler 	if (bf == NULL) {
380676e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
380776e6fd5dSGleb Smirnoff 		    "malloc of %s buffers failed, size %u\n",
3808c42a7b7eSSam Leffler 		    dd->dd_name, bsize);
3809c42a7b7eSSam Leffler 		goto fail3;
3810c42a7b7eSSam Leffler 	}
3811c42a7b7eSSam Leffler 	dd->dd_bufptr = bf;
3812c42a7b7eSSam Leffler 
38136b349e5aSAdrian Chadd 	TAILQ_INIT(head);
38143d9b1596SAdrian Chadd 	for (i = 0; i < nbuf; i++, bf++, ds += (ndesc * dd->dd_descsize)) {
381545abcd6cSAdrian Chadd 		bf->bf_desc = (struct ath_desc *) ds;
3816c42a7b7eSSam Leffler 		bf->bf_daddr = DS2PHYS(dd, ds);
381745abcd6cSAdrian Chadd 		if (! ath_hal_split4ktrans(sc->sc_ah)) {
381845abcd6cSAdrian Chadd 			/*
381945abcd6cSAdrian Chadd 			 * Merlin WAR: Skip descriptor addresses which
382045abcd6cSAdrian Chadd 			 * cause 4KB boundary crossing along any point
382145abcd6cSAdrian Chadd 			 * in the descriptor.
382245abcd6cSAdrian Chadd 			 */
382345abcd6cSAdrian Chadd 			 if (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr,
38247ef7f613SAdrian Chadd 			     dd->dd_descsize)) {
382545abcd6cSAdrian Chadd 				/* Start at the next page */
382645abcd6cSAdrian Chadd 				ds += 0x1000 - (bf->bf_daddr & 0xFFF);
382745abcd6cSAdrian Chadd 				bf->bf_desc = (struct ath_desc *) ds;
382845abcd6cSAdrian Chadd 				bf->bf_daddr = DS2PHYS(dd, ds);
382945abcd6cSAdrian Chadd 			}
383045abcd6cSAdrian Chadd 		}
3831c42a7b7eSSam Leffler 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
3832c42a7b7eSSam Leffler 				&bf->bf_dmamap);
3833c42a7b7eSSam Leffler 		if (error != 0) {
383476e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev, "unable to create dmamap "
383576e6fd5dSGleb Smirnoff 			    "for %s buffer %u, error %u\n",
383676e6fd5dSGleb Smirnoff 			    dd->dd_name, i, error);
3837c42a7b7eSSam Leffler 			ath_descdma_cleanup(sc, dd, head);
3838c42a7b7eSSam Leffler 			return error;
3839c42a7b7eSSam Leffler 		}
38406edf1dc7SAdrian Chadd 		bf->bf_lastds = bf->bf_desc;	/* Just an initial value */
38416b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(head, bf, bf_list);
3842c42a7b7eSSam Leffler 	}
38437ef7f613SAdrian Chadd 
38447ef7f613SAdrian Chadd 	/*
38457ef7f613SAdrian Chadd 	 * XXX TODO: ensure that ds doesn't overflow the descriptor
38467ef7f613SAdrian Chadd 	 * allocation otherwise weird stuff will occur and crash your
38477ef7f613SAdrian Chadd 	 * machine.
38487ef7f613SAdrian Chadd 	 */
3849c42a7b7eSSam Leffler 	return 0;
3850c9f78537SAdrian Chadd 	/* XXX this should likely just call ath_descdma_cleanup() */
3851c42a7b7eSSam Leffler fail3:
3852c42a7b7eSSam Leffler 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3853c42a7b7eSSam Leffler 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3854c42a7b7eSSam Leffler 	bus_dma_tag_destroy(dd->dd_dmat);
3855c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3856c42a7b7eSSam Leffler 	return error;
3857c42a7b7eSSam Leffler #undef DS2PHYS
385845abcd6cSAdrian Chadd #undef ATH_DESC_4KB_BOUND_CHECK
3859c42a7b7eSSam Leffler }
3860c42a7b7eSSam Leffler 
386139abbd9bSAdrian Chadd /*
386239abbd9bSAdrian Chadd  * Allocate ath_buf entries but no descriptor contents.
386339abbd9bSAdrian Chadd  *
386439abbd9bSAdrian Chadd  * This is for RX EDMA where the descriptors are the header part of
386539abbd9bSAdrian Chadd  * the RX buffer.
386639abbd9bSAdrian Chadd  */
386739abbd9bSAdrian Chadd int
386839abbd9bSAdrian Chadd ath_descdma_setup_rx_edma(struct ath_softc *sc,
386939abbd9bSAdrian Chadd 	struct ath_descdma *dd, ath_bufhead *head,
387039abbd9bSAdrian Chadd 	const char *name, int nbuf, int rx_status_len)
387139abbd9bSAdrian Chadd {
387239abbd9bSAdrian Chadd 	struct ath_buf *bf;
387339abbd9bSAdrian Chadd 	int i, bsize, error;
387439abbd9bSAdrian Chadd 
387539abbd9bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers\n",
387639abbd9bSAdrian Chadd 	    __func__, name, nbuf);
387739abbd9bSAdrian Chadd 
387839abbd9bSAdrian Chadd 	dd->dd_name = name;
387939abbd9bSAdrian Chadd 	/*
388039abbd9bSAdrian Chadd 	 * This is (mostly) purely for show.  We're not allocating any actual
388139abbd9bSAdrian Chadd 	 * descriptors here as EDMA RX has the descriptor be part
388239abbd9bSAdrian Chadd 	 * of the RX buffer.
388339abbd9bSAdrian Chadd 	 *
388439abbd9bSAdrian Chadd 	 * However, dd_desc_len is used by ath_descdma_free() to determine
388539abbd9bSAdrian Chadd 	 * whether we have already freed this DMA mapping.
388639abbd9bSAdrian Chadd 	 */
38873d9b1596SAdrian Chadd 	dd->dd_desc_len = rx_status_len * nbuf;
38883d9b1596SAdrian Chadd 	dd->dd_descsize = rx_status_len;
388939abbd9bSAdrian Chadd 
389039abbd9bSAdrian Chadd 	/* allocate rx buffers */
389139abbd9bSAdrian Chadd 	bsize = sizeof(struct ath_buf) * nbuf;
389239abbd9bSAdrian Chadd 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
389339abbd9bSAdrian Chadd 	if (bf == NULL) {
389476e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
389576e6fd5dSGleb Smirnoff 		    "malloc of %s buffers failed, size %u\n",
389639abbd9bSAdrian Chadd 		    dd->dd_name, bsize);
3897b5b60f35SAdrian Chadd 		error = ENOMEM;
389839abbd9bSAdrian Chadd 		goto fail3;
389939abbd9bSAdrian Chadd 	}
390039abbd9bSAdrian Chadd 	dd->dd_bufptr = bf;
390139abbd9bSAdrian Chadd 
390239abbd9bSAdrian Chadd 	TAILQ_INIT(head);
390339abbd9bSAdrian Chadd 	for (i = 0; i < nbuf; i++, bf++) {
390439abbd9bSAdrian Chadd 		bf->bf_desc = NULL;
390539abbd9bSAdrian Chadd 		bf->bf_daddr = 0;
390639abbd9bSAdrian Chadd 		bf->bf_lastds = NULL;	/* Just an initial value */
390739abbd9bSAdrian Chadd 
390839abbd9bSAdrian Chadd 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
390939abbd9bSAdrian Chadd 				&bf->bf_dmamap);
391039abbd9bSAdrian Chadd 		if (error != 0) {
391176e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev, "unable to create dmamap "
391276e6fd5dSGleb Smirnoff 			    "for %s buffer %u, error %u\n",
391376e6fd5dSGleb Smirnoff 			    dd->dd_name, i, error);
391439abbd9bSAdrian Chadd 			ath_descdma_cleanup(sc, dd, head);
391539abbd9bSAdrian Chadd 			return error;
391639abbd9bSAdrian Chadd 		}
391739abbd9bSAdrian Chadd 		TAILQ_INSERT_TAIL(head, bf, bf_list);
391839abbd9bSAdrian Chadd 	}
391939abbd9bSAdrian Chadd 	return 0;
392039abbd9bSAdrian Chadd fail3:
392139abbd9bSAdrian Chadd 	memset(dd, 0, sizeof(*dd));
392239abbd9bSAdrian Chadd 	return error;
392339abbd9bSAdrian Chadd }
392439abbd9bSAdrian Chadd 
39253d184db2SAdrian Chadd void
3926c42a7b7eSSam Leffler ath_descdma_cleanup(struct ath_softc *sc,
3927c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head)
3928c42a7b7eSSam Leffler {
3929c42a7b7eSSam Leffler 	struct ath_buf *bf;
3930c42a7b7eSSam Leffler 	struct ieee80211_node *ni;
3931a91ab3c0SAdrian Chadd 	int do_warning = 0;
3932c42a7b7eSSam Leffler 
39338d467c41SAdrian Chadd 	if (dd->dd_dmamap != 0) {
3934c42a7b7eSSam Leffler 		bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3935c42a7b7eSSam Leffler 		bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3936c42a7b7eSSam Leffler 		bus_dma_tag_destroy(dd->dd_dmat);
39378d467c41SAdrian Chadd 	}
3938c42a7b7eSSam Leffler 
39399ed9f02bSAdrian Chadd 	if (head != NULL) {
39406b349e5aSAdrian Chadd 		TAILQ_FOREACH(bf, head, bf_list) {
3941c42a7b7eSSam Leffler 			if (bf->bf_m) {
3942a91ab3c0SAdrian Chadd 				/*
3943a91ab3c0SAdrian Chadd 				 * XXX warn if there's buffers here.
3944a91ab3c0SAdrian Chadd 				 * XXX it should have been freed by the
3945a91ab3c0SAdrian Chadd 				 * owner!
3946a91ab3c0SAdrian Chadd 				 */
3947a91ab3c0SAdrian Chadd 
3948a91ab3c0SAdrian Chadd 				if (do_warning == 0) {
3949a91ab3c0SAdrian Chadd 					do_warning = 1;
3950a91ab3c0SAdrian Chadd 					device_printf(sc->sc_dev,
3951a91ab3c0SAdrian Chadd 					    "%s: %s: mbuf should've been"
3952a91ab3c0SAdrian Chadd 					    " unmapped/freed!\n",
3953a91ab3c0SAdrian Chadd 					    __func__,
3954a91ab3c0SAdrian Chadd 					    dd->dd_name);
3955a91ab3c0SAdrian Chadd 				}
3956a91ab3c0SAdrian Chadd 				bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
3957a91ab3c0SAdrian Chadd 				    BUS_DMASYNC_POSTREAD);
3958a91ab3c0SAdrian Chadd 				bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3959c42a7b7eSSam Leffler 				m_freem(bf->bf_m);
3960c42a7b7eSSam Leffler 				bf->bf_m = NULL;
3961c42a7b7eSSam Leffler 			}
3962c42a7b7eSSam Leffler 			if (bf->bf_dmamap != NULL) {
3963c42a7b7eSSam Leffler 				bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
3964c42a7b7eSSam Leffler 				bf->bf_dmamap = NULL;
3965c42a7b7eSSam Leffler 			}
3966c42a7b7eSSam Leffler 			ni = bf->bf_node;
3967c42a7b7eSSam Leffler 			bf->bf_node = NULL;
3968c42a7b7eSSam Leffler 			if (ni != NULL) {
3969c42a7b7eSSam Leffler 				/*
3970c42a7b7eSSam Leffler 				 * Reclaim node reference.
3971c42a7b7eSSam Leffler 				 */
3972c42a7b7eSSam Leffler 				ieee80211_free_node(ni);
3973c42a7b7eSSam Leffler 			}
3974c42a7b7eSSam Leffler 		}
39759ed9f02bSAdrian Chadd 	}
3976c42a7b7eSSam Leffler 
39779ed9f02bSAdrian Chadd 	if (head != NULL)
39786b349e5aSAdrian Chadd 		TAILQ_INIT(head);
39799ed9f02bSAdrian Chadd 
39809ed9f02bSAdrian Chadd 	if (dd->dd_bufptr != NULL)
3981c42a7b7eSSam Leffler 		free(dd->dd_bufptr, M_ATHDEV);
3982c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3983c42a7b7eSSam Leffler }
3984c42a7b7eSSam Leffler 
3985c42a7b7eSSam Leffler static int
39865591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc)
39875591b213SSam Leffler {
3988c42a7b7eSSam Leffler 	int error;
39895591b213SSam Leffler 
3990c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
399109067b6eSAdrian Chadd 		    "tx", sc->sc_tx_desclen, ath_txbuf, ATH_MAX_SCATTER);
3992c42a7b7eSSam Leffler 	if (error != 0) {
39935591b213SSam Leffler 		return error;
3994c42a7b7eSSam Leffler 	}
399523ced6c1SAdrian Chadd 	sc->sc_txbuf_cnt = ath_txbuf;
3996c42a7b7eSSam Leffler 
3997af33d486SAdrian Chadd 	error = ath_descdma_setup(sc, &sc->sc_txdma_mgmt, &sc->sc_txbuf_mgmt,
39981006fc0cSAdrian Chadd 		    "tx_mgmt", sc->sc_tx_desclen, ath_txbuf_mgmt,
39991006fc0cSAdrian Chadd 		    ATH_TXDESC);
4000af33d486SAdrian Chadd 	if (error != 0) {
4001af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
4002af33d486SAdrian Chadd 		return error;
4003af33d486SAdrian Chadd 	}
4004af33d486SAdrian Chadd 
4005af33d486SAdrian Chadd 	/*
4006af33d486SAdrian Chadd 	 * XXX mark txbuf_mgmt frames with ATH_BUF_MGMT, so the
4007af33d486SAdrian Chadd 	 * flag doesn't have to be set in ath_getbuf_locked().
4008af33d486SAdrian Chadd 	 */
4009af33d486SAdrian Chadd 
4010c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
40111006fc0cSAdrian Chadd 			"beacon", sc->sc_tx_desclen, ATH_BCBUF, 1);
4012c42a7b7eSSam Leffler 	if (error != 0) {
4013af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
4014af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
4015af33d486SAdrian Chadd 		    &sc->sc_txbuf_mgmt);
4016c42a7b7eSSam Leffler 		return error;
4017c42a7b7eSSam Leffler 	}
40185591b213SSam Leffler 	return 0;
40195591b213SSam Leffler }
40205591b213SSam Leffler 
40215591b213SSam Leffler static void
40225591b213SSam Leffler ath_desc_free(struct ath_softc *sc)
40235591b213SSam Leffler {
40245591b213SSam Leffler 
4025c42a7b7eSSam Leffler 	if (sc->sc_bdma.dd_desc_len != 0)
4026c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
4027c42a7b7eSSam Leffler 	if (sc->sc_txdma.dd_desc_len != 0)
4028c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
4029af33d486SAdrian Chadd 	if (sc->sc_txdma_mgmt.dd_desc_len != 0)
4030af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
4031af33d486SAdrian Chadd 		    &sc->sc_txbuf_mgmt);
40325591b213SSam Leffler }
40335591b213SSam Leffler 
40345591b213SSam Leffler static struct ieee80211_node *
403538c208f8SSam Leffler ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
40365591b213SSam Leffler {
403738c208f8SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
40383797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
4039c42a7b7eSSam Leffler 	const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
4040c42a7b7eSSam Leffler 	struct ath_node *an;
4041c42a7b7eSSam Leffler 
4042c42a7b7eSSam Leffler 	an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
4043c42a7b7eSSam Leffler 	if (an == NULL) {
4044c42a7b7eSSam Leffler 		/* XXX stat+msg */
4045de5af704SSam Leffler 		return NULL;
40465591b213SSam Leffler 	}
4047c42a7b7eSSam Leffler 	ath_rate_node_init(sc, an);
40485591b213SSam Leffler 
40493dd85b26SAdrian Chadd 	/* Setup the mutex - there's no associd yet so set the name to NULL */
40503dd85b26SAdrian Chadd 	snprintf(an->an_name, sizeof(an->an_name), "%s: node %p",
40513dd85b26SAdrian Chadd 	    device_get_nameunit(sc->sc_dev), an);
40523dd85b26SAdrian Chadd 	mtx_init(&an->an_mtx, an->an_name, NULL, MTX_DEF);
40533dd85b26SAdrian Chadd 
4054eb6f0de0SAdrian Chadd 	/* XXX setup ath_tid */
4055eb6f0de0SAdrian Chadd 	ath_tx_tid_init(sc, an);
4056eb6f0de0SAdrian Chadd 
40579b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__, mac, ":", an);
4058c42a7b7eSSam Leffler 	return &an->an_node;
4059c42a7b7eSSam Leffler }
4060c42a7b7eSSam Leffler 
40615591b213SSam Leffler static void
40624afa805eSAdrian Chadd ath_node_cleanup(struct ieee80211_node *ni)
40634afa805eSAdrian Chadd {
40644afa805eSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
40653797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
40664afa805eSAdrian Chadd 
40679b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__,
40689b48fb4bSAdrian Chadd 	    ni->ni_macaddr, ":", ATH_NODE(ni));
40699b48fb4bSAdrian Chadd 
40704afa805eSAdrian Chadd 	/* Cleanup ath_tid, free unused bufs, unlink bufs in TXQ */
4071eb6f0de0SAdrian Chadd 	ath_tx_node_flush(sc, ATH_NODE(ni));
40724afa805eSAdrian Chadd 	ath_rate_node_cleanup(sc, ATH_NODE(ni));
40734afa805eSAdrian Chadd 	sc->sc_node_cleanup(ni);
40744afa805eSAdrian Chadd }
40754afa805eSAdrian Chadd 
40764afa805eSAdrian Chadd static void
4077c42a7b7eSSam Leffler ath_node_free(struct ieee80211_node *ni)
40785591b213SSam Leffler {
4079c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
40803797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
40811e774079SSam Leffler 
40829b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__,
40839b48fb4bSAdrian Chadd 	    ni->ni_macaddr, ":", ATH_NODE(ni));
40843dd85b26SAdrian Chadd 	mtx_destroy(&ATH_NODE(ni)->an_mtx);
4085c42a7b7eSSam Leffler 	sc->sc_node_free(ni);
40865591b213SSam Leffler }
40875591b213SSam Leffler 
408868e8e04eSSam Leffler static void
408968e8e04eSSam Leffler ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
409068e8e04eSSam Leffler {
409168e8e04eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
40923797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
409368e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
409468e8e04eSSam Leffler 
4095b032f27cSSam Leffler 	*rssi = ic->ic_node_getrssi(ni);
409659efa8b5SSam Leffler 	if (ni->ni_chan != IEEE80211_CHAN_ANYC)
409759efa8b5SSam Leffler 		*noise = ath_hal_getchannoise(ah, ni->ni_chan);
409859efa8b5SSam Leffler 	else
409968e8e04eSSam Leffler 		*noise = -95;		/* nominally correct */
410068e8e04eSSam Leffler }
410168e8e04eSSam Leffler 
4102c42a7b7eSSam Leffler /*
4103c42a7b7eSSam Leffler  * Set the default antenna.
4104c42a7b7eSSam Leffler  */
4105e60c4fc2SAdrian Chadd void
4106c42a7b7eSSam Leffler ath_setdefantenna(struct ath_softc *sc, u_int antenna)
4107c42a7b7eSSam Leffler {
4108c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4109c42a7b7eSSam Leffler 
4110c42a7b7eSSam Leffler 	/* XXX block beacon interrupts */
4111c42a7b7eSSam Leffler 	ath_hal_setdefantenna(ah, antenna);
4112c42a7b7eSSam Leffler 	if (sc->sc_defant != antenna)
4113c42a7b7eSSam Leffler 		sc->sc_stats.ast_ant_defswitch++;
4114c42a7b7eSSam Leffler 	sc->sc_defant = antenna;
4115c42a7b7eSSam Leffler 	sc->sc_rxotherant = 0;
4116c42a7b7eSSam Leffler }
4117c42a7b7eSSam Leffler 
41185463c4a4SSam Leffler static void
4119622b3fd2SSam Leffler ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum)
4120622b3fd2SSam Leffler {
4121622b3fd2SSam Leffler 	txq->axq_qnum = qnum;
4122339ccfb3SSam Leffler 	txq->axq_ac = 0;
4123622b3fd2SSam Leffler 	txq->axq_depth = 0;
412416d4de92SAdrian Chadd 	txq->axq_aggr_depth = 0;
4125622b3fd2SSam Leffler 	txq->axq_intrcnt = 0;
4126622b3fd2SSam Leffler 	txq->axq_link = NULL;
41276b349e5aSAdrian Chadd 	txq->axq_softc = sc;
41286b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_q);
41296b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_tidq);
41303feffbd7SAdrian Chadd 	TAILQ_INIT(&txq->fifo.axq_q);
4131b837332dSAdrian Chadd 	ATH_TXQ_LOCK_INIT(sc, txq);
4132622b3fd2SSam Leffler }
4133622b3fd2SSam Leffler 
41345591b213SSam Leffler /*
4135c42a7b7eSSam Leffler  * Setup a h/w transmit queue.
41365591b213SSam Leffler  */
4137c42a7b7eSSam Leffler static struct ath_txq *
4138c42a7b7eSSam Leffler ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
4139c42a7b7eSSam Leffler {
4140c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4141c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
4142c42a7b7eSSam Leffler 	int qnum;
4143c42a7b7eSSam Leffler 
4144c42a7b7eSSam Leffler 	memset(&qi, 0, sizeof(qi));
4145c42a7b7eSSam Leffler 	qi.tqi_subtype = subtype;
4146c42a7b7eSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
4147c42a7b7eSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
4148c42a7b7eSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
4149c42a7b7eSSam Leffler 	/*
4150c42a7b7eSSam Leffler 	 * Enable interrupts only for EOL and DESC conditions.
4151c42a7b7eSSam Leffler 	 * We mark tx descriptors to receive a DESC interrupt
4152c42a7b7eSSam Leffler 	 * when a tx queue gets deep; otherwise waiting for the
4153c42a7b7eSSam Leffler 	 * EOL to reap descriptors.  Note that this is done to
4154c42a7b7eSSam Leffler 	 * reduce interrupt load and this only defers reaping
4155c42a7b7eSSam Leffler 	 * descriptors, never transmitting frames.  Aside from
4156c42a7b7eSSam Leffler 	 * reducing interrupts this also permits more concurrency.
4157c42a7b7eSSam Leffler 	 * The only potential downside is if the tx queue backs
4158c42a7b7eSSam Leffler 	 * up in which case the top half of the kernel may backup
4159c42a7b7eSSam Leffler 	 * due to a lack of tx descriptors.
4160c42a7b7eSSam Leffler 	 */
41616961e9edSAdrian Chadd 	if (sc->sc_isedma)
41626961e9edSAdrian Chadd 		qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE |
41636961e9edSAdrian Chadd 		    HAL_TXQ_TXOKINT_ENABLE;
41646961e9edSAdrian Chadd 	else
41656961e9edSAdrian Chadd 		qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE |
41666961e9edSAdrian Chadd 		    HAL_TXQ_TXDESCINT_ENABLE;
41676961e9edSAdrian Chadd 
4168c42a7b7eSSam Leffler 	qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
4169c42a7b7eSSam Leffler 	if (qnum == -1) {
4170c42a7b7eSSam Leffler 		/*
4171c42a7b7eSSam Leffler 		 * NB: don't print a message, this happens
4172a614e076SSam Leffler 		 * normally on parts with too few tx queues
4173c42a7b7eSSam Leffler 		 */
4174c42a7b7eSSam Leffler 		return NULL;
4175c42a7b7eSSam Leffler 	}
4176d6166defSAdrian Chadd 	if (qnum >= nitems(sc->sc_txq)) {
41776891c875SPeter Wemm 		device_printf(sc->sc_dev,
41786891c875SPeter Wemm 			"hal qnum %u out of range, max %zu!\n",
4179d6166defSAdrian Chadd 			qnum, nitems(sc->sc_txq));
4180c42a7b7eSSam Leffler 		ath_hal_releasetxqueue(ah, qnum);
4181c42a7b7eSSam Leffler 		return NULL;
4182c42a7b7eSSam Leffler 	}
4183c42a7b7eSSam Leffler 	if (!ATH_TXQ_SETUP(sc, qnum)) {
4184622b3fd2SSam Leffler 		ath_txq_init(sc, &sc->sc_txq[qnum], qnum);
4185c42a7b7eSSam Leffler 		sc->sc_txqsetup |= 1<<qnum;
4186c42a7b7eSSam Leffler 	}
4187c42a7b7eSSam Leffler 	return &sc->sc_txq[qnum];
4188c42a7b7eSSam Leffler }
4189c42a7b7eSSam Leffler 
4190c42a7b7eSSam Leffler /*
4191c42a7b7eSSam Leffler  * Setup a hardware data transmit queue for the specified
4192c42a7b7eSSam Leffler  * access control.  The hal may not support all requested
4193c42a7b7eSSam Leffler  * queues in which case it will return a reference to a
4194c42a7b7eSSam Leffler  * previously setup queue.  We record the mapping from ac's
4195c42a7b7eSSam Leffler  * to h/w queues for use by ath_tx_start and also track
4196c42a7b7eSSam Leffler  * the set of h/w queues being used to optimize work in the
4197c42a7b7eSSam Leffler  * transmit interrupt handler and related routines.
4198c42a7b7eSSam Leffler  */
4199c42a7b7eSSam Leffler static int
4200c42a7b7eSSam Leffler ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
4201c42a7b7eSSam Leffler {
4202c42a7b7eSSam Leffler 	struct ath_txq *txq;
4203c42a7b7eSSam Leffler 
4204d6166defSAdrian Chadd 	if (ac >= nitems(sc->sc_ac2q)) {
42056891c875SPeter Wemm 		device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
4206d6166defSAdrian Chadd 			ac, nitems(sc->sc_ac2q));
4207c42a7b7eSSam Leffler 		return 0;
4208c42a7b7eSSam Leffler 	}
4209c42a7b7eSSam Leffler 	txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
4210c42a7b7eSSam Leffler 	if (txq != NULL) {
4211339ccfb3SSam Leffler 		txq->axq_ac = ac;
4212c42a7b7eSSam Leffler 		sc->sc_ac2q[ac] = txq;
4213c42a7b7eSSam Leffler 		return 1;
4214c42a7b7eSSam Leffler 	} else
4215c42a7b7eSSam Leffler 		return 0;
4216c42a7b7eSSam Leffler }
4217c42a7b7eSSam Leffler 
4218c42a7b7eSSam Leffler /*
4219c42a7b7eSSam Leffler  * Update WME parameters for a transmit queue.
4220c42a7b7eSSam Leffler  */
4221c42a7b7eSSam Leffler static int
4222c42a7b7eSSam Leffler ath_txq_update(struct ath_softc *sc, int ac)
4223c42a7b7eSSam Leffler {
4224c42a7b7eSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<v)-1)
42257a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
4226c42a7b7eSSam Leffler 	struct ath_txq *txq = sc->sc_ac2q[ac];
4227c42a7b7eSSam Leffler 	struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
4228c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4229c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
4230c42a7b7eSSam Leffler 
4231c42a7b7eSSam Leffler 	ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
4232584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
423310ad9a77SSam Leffler 	if (sc->sc_tdma) {
423410ad9a77SSam Leffler 		/*
423510ad9a77SSam Leffler 		 * AIFS is zero so there's no pre-transmit wait.  The
423610ad9a77SSam Leffler 		 * burst time defines the slot duration and is configured
423709be6601SSam Leffler 		 * through net80211.  The QCU is setup to not do post-xmit
423810ad9a77SSam Leffler 		 * back off, lockout all lower-priority QCU's, and fire
423910ad9a77SSam Leffler 		 * off the DMA beacon alert timer which is setup based
424010ad9a77SSam Leffler 		 * on the slot configuration.
424110ad9a77SSam Leffler 		 */
424210ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
424310ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
424410ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
424510ad9a77SSam Leffler 			      | HAL_TXQ_TXEOLINT_ENABLE
424610ad9a77SSam Leffler 			      | HAL_TXQ_DBA_GATED
424710ad9a77SSam Leffler 			      | HAL_TXQ_BACKOFF_DISABLE
424810ad9a77SSam Leffler 			      | HAL_TXQ_ARB_LOCKOUT_GLOBAL
424910ad9a77SSam Leffler 			      ;
425010ad9a77SSam Leffler 		qi.tqi_aifs = 0;
425110ad9a77SSam Leffler 		/* XXX +dbaprep? */
425210ad9a77SSam Leffler 		qi.tqi_readyTime = sc->sc_tdmaslotlen;
425310ad9a77SSam Leffler 		qi.tqi_burstTime = qi.tqi_readyTime;
425410ad9a77SSam Leffler 	} else {
425510ad9a77SSam Leffler #endif
425616d4de92SAdrian Chadd 		/*
425716d4de92SAdrian Chadd 		 * XXX shouldn't this just use the default flags
425816d4de92SAdrian Chadd 		 * used in the previous queue setup?
425916d4de92SAdrian Chadd 		 */
426010ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
426110ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
426210ad9a77SSam Leffler 			      | HAL_TXQ_TXDESCINT_ENABLE
426310ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
42641f25c0f7SAdrian Chadd 			      | HAL_TXQ_TXEOLINT_ENABLE
426510ad9a77SSam Leffler 			      ;
4266c42a7b7eSSam Leffler 		qi.tqi_aifs = wmep->wmep_aifsn;
4267c42a7b7eSSam Leffler 		qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
4268c42a7b7eSSam Leffler 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
426910ad9a77SSam Leffler 		qi.tqi_readyTime = 0;
4270d6166defSAdrian Chadd 		qi.tqi_burstTime = IEEE80211_TXOP_TO_US(wmep->wmep_txopLimit);
4271584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
427210ad9a77SSam Leffler 	}
427310ad9a77SSam Leffler #endif
427410ad9a77SSam Leffler 
427510ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
427610ad9a77SSam Leffler 	    "%s: Q%u qflags 0x%x aifs %u cwmin %u cwmax %u burstTime %u\n",
427710ad9a77SSam Leffler 	    __func__, txq->axq_qnum, qi.tqi_qflags,
427810ad9a77SSam Leffler 	    qi.tqi_aifs, qi.tqi_cwmin, qi.tqi_cwmax, qi.tqi_burstTime);
4279c42a7b7eSSam Leffler 
4280c42a7b7eSSam Leffler 	if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
428176e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "unable to update hardware queue "
428276e6fd5dSGleb Smirnoff 		    "parameters for %s traffic!\n", ieee80211_wme_acnames[ac]);
4283c42a7b7eSSam Leffler 		return 0;
4284c42a7b7eSSam Leffler 	} else {
4285c42a7b7eSSam Leffler 		ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
4286c42a7b7eSSam Leffler 		return 1;
4287c42a7b7eSSam Leffler 	}
4288c42a7b7eSSam Leffler #undef ATH_EXPONENT_TO_VALUE
4289c42a7b7eSSam Leffler }
4290c42a7b7eSSam Leffler 
4291c42a7b7eSSam Leffler /*
4292c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update WME parameters.
4293c42a7b7eSSam Leffler  */
4294a35dae8dSAdrian Chadd int
4295c42a7b7eSSam Leffler ath_wme_update(struct ieee80211com *ic)
4296c42a7b7eSSam Leffler {
42973797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
4298c42a7b7eSSam Leffler 
4299c42a7b7eSSam Leffler 	return !ath_txq_update(sc, WME_AC_BE) ||
4300c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_BK) ||
4301c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VI) ||
4302c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
4303c42a7b7eSSam Leffler }
4304c42a7b7eSSam Leffler 
4305c42a7b7eSSam Leffler /*
4306c42a7b7eSSam Leffler  * Reclaim resources for a setup queue.
4307c42a7b7eSSam Leffler  */
4308c42a7b7eSSam Leffler static void
4309c42a7b7eSSam Leffler ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
4310c42a7b7eSSam Leffler {
4311c42a7b7eSSam Leffler 
4312c42a7b7eSSam Leffler 	ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
4313c42a7b7eSSam Leffler 	sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
4314b837332dSAdrian Chadd 	ATH_TXQ_LOCK_DESTROY(txq);
4315c42a7b7eSSam Leffler }
4316c42a7b7eSSam Leffler 
4317c42a7b7eSSam Leffler /*
4318c42a7b7eSSam Leffler  * Reclaim all tx queue resources.
4319c42a7b7eSSam Leffler  */
4320c42a7b7eSSam Leffler static void
4321c42a7b7eSSam Leffler ath_tx_cleanup(struct ath_softc *sc)
4322c42a7b7eSSam Leffler {
4323c42a7b7eSSam Leffler 	int i;
4324c42a7b7eSSam Leffler 
4325c42a7b7eSSam Leffler 	ATH_TXBUF_LOCK_DESTROY(sc);
4326c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4327c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
4328c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, &sc->sc_txq[i]);
4329c42a7b7eSSam Leffler }
43305591b213SSam Leffler 
433199d258fdSSam Leffler /*
4332ab06fdf2SSam Leffler  * Return h/w rate index for an IEEE rate (w/o basic rate bit)
4333ab06fdf2SSam Leffler  * using the current rates in sc_rixmap.
43348b5341deSSam Leffler  */
4335b8e788a5SAdrian Chadd int
4336ab06fdf2SSam Leffler ath_tx_findrix(const struct ath_softc *sc, uint8_t rate)
43378b5341deSSam Leffler {
4338ab06fdf2SSam Leffler 	int rix = sc->sc_rixmap[rate];
4339ab06fdf2SSam Leffler 	/* NB: return lowest rix for invalid rate */
4340ab06fdf2SSam Leffler 	return (rix == 0xff ? 0 : rix);
43418b5341deSSam Leffler }
43428b5341deSSam Leffler 
43439352fb7aSAdrian Chadd static void
43449352fb7aSAdrian Chadd ath_tx_update_stats(struct ath_softc *sc, struct ath_tx_status *ts,
43459352fb7aSAdrian Chadd     struct ath_buf *bf)
43469352fb7aSAdrian Chadd {
43479352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
43487a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
43499352fb7aSAdrian Chadd 	int sr, lr, pri;
43509352fb7aSAdrian Chadd 
43519352fb7aSAdrian Chadd 	if (ts->ts_status == 0) {
43529352fb7aSAdrian Chadd 		u_int8_t txant = ts->ts_antenna;
43539352fb7aSAdrian Chadd 		sc->sc_stats.ast_ant_tx[txant]++;
43549352fb7aSAdrian Chadd 		sc->sc_ant_tx[txant]++;
43559352fb7aSAdrian Chadd 		if (ts->ts_finaltsi != 0)
43569352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_altrate++;
43579352fb7aSAdrian Chadd 		pri = M_WME_GETAC(bf->bf_m);
43589352fb7aSAdrian Chadd 		if (pri >= WME_AC_VO)
43599352fb7aSAdrian Chadd 			ic->ic_wme.wme_hipri_traffic++;
4360875a9451SAdrian Chadd 		if ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)
43619352fb7aSAdrian Chadd 			ni->ni_inact = ni->ni_inact_reload;
43629352fb7aSAdrian Chadd 	} else {
43639352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XRETRY)
43649352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xretries++;
43659352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FIFO)
43669352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_fifoerr++;
43679352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FILT)
43689352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_filtered++;
43699352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XTXOP)
43709352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xtxop++;
43719352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_TIMER_EXPIRED)
43729352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_timerexpired++;
43739352fb7aSAdrian Chadd 
43749352fb7aSAdrian Chadd 		if (bf->bf_m->m_flags & M_FF)
43759352fb7aSAdrian Chadd 			sc->sc_stats.ast_ff_txerr++;
43769352fb7aSAdrian Chadd 	}
43779352fb7aSAdrian Chadd 	/* XXX when is this valid? */
4378158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DESC_CFG_ERR)
43799352fb7aSAdrian Chadd 		sc->sc_stats.ast_tx_desccfgerr++;
4380158cb431SAdrian Chadd 	/*
4381158cb431SAdrian Chadd 	 * This can be valid for successful frame transmission!
4382158cb431SAdrian Chadd 	 * If there's a TX FIFO underrun during aggregate transmission,
4383158cb431SAdrian Chadd 	 * the MAC will pad the rest of the aggregate with delimiters.
4384158cb431SAdrian Chadd 	 * If a BA is returned, the frame is marked as "OK" and it's up
4385158cb431SAdrian Chadd 	 * to the TX completion code to notice which frames weren't
4386158cb431SAdrian Chadd 	 * successfully transmitted.
4387158cb431SAdrian Chadd 	 */
4388158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DATA_UNDERRUN)
4389158cb431SAdrian Chadd 		sc->sc_stats.ast_tx_data_underrun++;
4390158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DELIM_UNDERRUN)
4391158cb431SAdrian Chadd 		sc->sc_stats.ast_tx_delim_underrun++;
43929352fb7aSAdrian Chadd 
43939352fb7aSAdrian Chadd 	sr = ts->ts_shortretry;
43949352fb7aSAdrian Chadd 	lr = ts->ts_longretry;
43959352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_shortretry += sr;
43969352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_longretry += lr;
43979352fb7aSAdrian Chadd 
43989352fb7aSAdrian Chadd }
43999352fb7aSAdrian Chadd 
44009352fb7aSAdrian Chadd /*
44019352fb7aSAdrian Chadd  * The default completion. If fail is 1, this means
44029352fb7aSAdrian Chadd  * "please don't retry the frame, and just return -1 status
44039352fb7aSAdrian Chadd  * to the net80211 stack.
44049352fb7aSAdrian Chadd  */
44059352fb7aSAdrian Chadd void
44069352fb7aSAdrian Chadd ath_tx_default_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
44079352fb7aSAdrian Chadd {
44089352fb7aSAdrian Chadd 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
44099352fb7aSAdrian Chadd 	int st;
44109352fb7aSAdrian Chadd 
44119352fb7aSAdrian Chadd 	if (fail == 1)
44129352fb7aSAdrian Chadd 		st = -1;
44139352fb7aSAdrian Chadd 	else
4414875a9451SAdrian Chadd 		st = ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) ?
44159352fb7aSAdrian Chadd 		    ts->ts_status : HAL_TXERR_XRETRY;
44169352fb7aSAdrian Chadd 
4417ce597531SAdrian Chadd #if 0
44189352fb7aSAdrian Chadd 	if (bf->bf_state.bfs_dobaw)
44199352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
4420a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: dobaw should've been cleared!\n",
4421a66d5089SAdrian Chadd 		    __func__,
4422a66d5089SAdrian Chadd 		    bf,
4423a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
4424ce597531SAdrian Chadd #endif
44259352fb7aSAdrian Chadd 	if (bf->bf_next != NULL)
44269352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
4427a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: bf_next not NULL!\n",
4428a66d5089SAdrian Chadd 		    __func__,
4429a66d5089SAdrian Chadd 		    bf,
4430a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
44319352fb7aSAdrian Chadd 
44329352fb7aSAdrian Chadd 	/*
4433548a605dSAdrian Chadd 	 * Check if the node software queue is empty; if so
4434548a605dSAdrian Chadd 	 * then clear the TIM.
4435548a605dSAdrian Chadd 	 *
4436548a605dSAdrian Chadd 	 * This needs to be done before the buffer is freed as
4437548a605dSAdrian Chadd 	 * otherwise the node reference will have been released
4438548a605dSAdrian Chadd 	 * and the node may not actually exist any longer.
4439548a605dSAdrian Chadd 	 *
4440548a605dSAdrian Chadd 	 * XXX I don't like this belonging here, but it's cleaner
4441548a605dSAdrian Chadd 	 * to do it here right now then all the other places
4442548a605dSAdrian Chadd 	 * where ath_tx_default_comp() is called.
4443548a605dSAdrian Chadd 	 *
4444548a605dSAdrian Chadd 	 * XXX TODO: during drain, ensure that the callback is
4445548a605dSAdrian Chadd 	 * being called so we get a chance to update the TIM.
4446548a605dSAdrian Chadd 	 */
44474bed2b67SAdrian Chadd 	if (bf->bf_node) {
44484bed2b67SAdrian Chadd 		ATH_TX_LOCK(sc);
4449548a605dSAdrian Chadd 		ath_tx_update_tim(sc, bf->bf_node, 0);
44504bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
44514bed2b67SAdrian Chadd 	}
4452548a605dSAdrian Chadd 
4453548a605dSAdrian Chadd 	/*
44549352fb7aSAdrian Chadd 	 * Do any tx complete callback.  Note this must
44559352fb7aSAdrian Chadd 	 * be done before releasing the node reference.
44569352fb7aSAdrian Chadd 	 * This will free the mbuf, release the net80211
44579352fb7aSAdrian Chadd 	 * node and recycle the ath_buf.
44589352fb7aSAdrian Chadd 	 */
44599352fb7aSAdrian Chadd 	ath_tx_freebuf(sc, bf, st);
44609352fb7aSAdrian Chadd }
44619352fb7aSAdrian Chadd 
44629352fb7aSAdrian Chadd /*
4463eb6f0de0SAdrian Chadd  * Update rate control with the given completion status.
4464eb6f0de0SAdrian Chadd  */
4465eb6f0de0SAdrian Chadd void
4466eb6f0de0SAdrian Chadd ath_tx_update_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
4467eb6f0de0SAdrian Chadd     struct ath_rc_series *rc, struct ath_tx_status *ts, int frmlen,
4468eb6f0de0SAdrian Chadd     int nframes, int nbad)
4469eb6f0de0SAdrian Chadd {
4470eb6f0de0SAdrian Chadd 	struct ath_node *an;
4471eb6f0de0SAdrian Chadd 
4472eb6f0de0SAdrian Chadd 	/* Only for unicast frames */
4473eb6f0de0SAdrian Chadd 	if (ni == NULL)
4474eb6f0de0SAdrian Chadd 		return;
4475eb6f0de0SAdrian Chadd 
4476eb6f0de0SAdrian Chadd 	an = ATH_NODE(ni);
4477548a605dSAdrian Chadd 	ATH_NODE_UNLOCK_ASSERT(an);
4478eb6f0de0SAdrian Chadd 
4479eb6f0de0SAdrian Chadd 	if ((ts->ts_status & HAL_TXERR_FILT) == 0) {
4480eb6f0de0SAdrian Chadd 		ATH_NODE_LOCK(an);
4481eb6f0de0SAdrian Chadd 		ath_rate_tx_complete(sc, an, rc, ts, frmlen, nframes, nbad);
4482eb6f0de0SAdrian Chadd 		ATH_NODE_UNLOCK(an);
4483eb6f0de0SAdrian Chadd 	}
4484eb6f0de0SAdrian Chadd }
4485eb6f0de0SAdrian Chadd 
4486eb6f0de0SAdrian Chadd /*
4487bad98824SAdrian Chadd  * Process the completion of the given buffer.
4488bad98824SAdrian Chadd  *
4489bad98824SAdrian Chadd  * This calls the rate control update and then the buffer completion.
4490bad98824SAdrian Chadd  * This will either free the buffer or requeue it.  In any case, the
4491bad98824SAdrian Chadd  * bf pointer should be treated as invalid after this function is called.
4492bad98824SAdrian Chadd  */
4493bad98824SAdrian Chadd void
4494bad98824SAdrian Chadd ath_tx_process_buf_completion(struct ath_softc *sc, struct ath_txq *txq,
4495bad98824SAdrian Chadd     struct ath_tx_status *ts, struct ath_buf *bf)
4496bad98824SAdrian Chadd {
4497bad98824SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
4498bad98824SAdrian Chadd 
4499375307d4SAdrian Chadd 	ATH_TX_UNLOCK_ASSERT(sc);
45005e018508SAdrian Chadd 	ATH_TXQ_UNLOCK_ASSERT(txq);
4501bad98824SAdrian Chadd 
4502bad98824SAdrian Chadd 	/* If unicast frame, update general statistics */
4503bad98824SAdrian Chadd 	if (ni != NULL) {
4504bad98824SAdrian Chadd 		/* update statistics */
4505bad98824SAdrian Chadd 		ath_tx_update_stats(sc, ts, bf);
4506bad98824SAdrian Chadd 	}
4507bad98824SAdrian Chadd 
4508bad98824SAdrian Chadd 	/*
4509bad98824SAdrian Chadd 	 * Call the completion handler.
4510bad98824SAdrian Chadd 	 * The completion handler is responsible for
4511bad98824SAdrian Chadd 	 * calling the rate control code.
4512bad98824SAdrian Chadd 	 *
4513bad98824SAdrian Chadd 	 * Frames with no completion handler get the
4514bad98824SAdrian Chadd 	 * rate control code called here.
4515bad98824SAdrian Chadd 	 */
4516bad98824SAdrian Chadd 	if (bf->bf_comp == NULL) {
4517bad98824SAdrian Chadd 		if ((ts->ts_status & HAL_TXERR_FILT) == 0 &&
4518bad98824SAdrian Chadd 		    (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) {
4519bad98824SAdrian Chadd 			/*
4520bad98824SAdrian Chadd 			 * XXX assume this isn't an aggregate
4521bad98824SAdrian Chadd 			 * frame.
4522bad98824SAdrian Chadd 			 */
4523bad98824SAdrian Chadd 			ath_tx_update_ratectrl(sc, ni,
4524bad98824SAdrian Chadd 			     bf->bf_state.bfs_rc, ts,
4525bad98824SAdrian Chadd 			    bf->bf_state.bfs_pktlen, 1,
4526bad98824SAdrian Chadd 			    (ts->ts_status == 0 ? 0 : 1));
4527bad98824SAdrian Chadd 		}
4528bad98824SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4529bad98824SAdrian Chadd 	} else
4530bad98824SAdrian Chadd 		bf->bf_comp(sc, bf, 0);
4531bad98824SAdrian Chadd }
4532bad98824SAdrian Chadd 
4533bad98824SAdrian Chadd 
4534bad98824SAdrian Chadd 
4535bad98824SAdrian Chadd /*
4536c42a7b7eSSam Leffler  * Process completed xmit descriptors from the specified queue.
4537eb6f0de0SAdrian Chadd  * Kick the packet scheduler if needed. This can occur from this
4538eb6f0de0SAdrian Chadd  * particular task.
4539c42a7b7eSSam Leffler  */
4540788e6aa9SAdrian Chadd static int
4541788e6aa9SAdrian Chadd ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq, int dosched)
45425591b213SSam Leffler {
45435591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
45449352fb7aSAdrian Chadd 	struct ath_buf *bf;
45456edf1dc7SAdrian Chadd 	struct ath_desc *ds;
454665f9edeeSSam Leffler 	struct ath_tx_status *ts;
45475591b213SSam Leffler 	struct ieee80211_node *ni;
454853e98d5aSAdrian Chadd #ifdef	IEEE80211_SUPPORT_SUPERG
45497a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
455053e98d5aSAdrian Chadd #endif	/* IEEE80211_SUPPORT_SUPERG */
45519352fb7aSAdrian Chadd 	int nacked;
45525591b213SSam Leffler 	HAL_STATUS status;
45535591b213SSam Leffler 
4554c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
4555c42a7b7eSSam Leffler 		__func__, txq->axq_qnum,
4556c42a7b7eSSam Leffler 		(caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
4557c42a7b7eSSam Leffler 		txq->axq_link);
455803682514SAdrian Chadd 
455903682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 4,
456003682514SAdrian Chadd 	    "ath_tx_processq: txq=%u head %p link %p depth %p",
456103682514SAdrian Chadd 	    txq->axq_qnum,
456203682514SAdrian Chadd 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
456303682514SAdrian Chadd 	    txq->axq_link,
456403682514SAdrian Chadd 	    txq->axq_depth);
456503682514SAdrian Chadd 
4566d7736e13SSam Leffler 	nacked = 0;
45675591b213SSam Leffler 	for (;;) {
4568b837332dSAdrian Chadd 		ATH_TXQ_LOCK(txq);
4569c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;	/* reset periodic desc intr count */
45706b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&txq->axq_q);
45715591b213SSam Leffler 		if (bf == NULL) {
4572b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
45735591b213SSam Leffler 			break;
45745591b213SSam Leffler 		}
45756edf1dc7SAdrian Chadd 		ds = bf->bf_lastds;	/* XXX must be setup correctly! */
457665f9edeeSSam Leffler 		ts = &bf->bf_status.ds_txstat;
457703682514SAdrian Chadd 
457865f9edeeSSam Leffler 		status = ath_hal_txprocdesc(ah, ds, ts);
4579a585a9a1SSam Leffler #ifdef ATH_DEBUG
4580c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
45816902009eSSam Leffler 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
45826902009eSSam Leffler 			    status == HAL_OK);
458303682514SAdrian Chadd 		else if ((sc->sc_debug & ATH_DEBUG_RESET) && (dosched == 0))
4584d6b20023SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
4585d6b20023SAdrian Chadd 			    status == HAL_OK);
45865591b213SSam Leffler #endif
4587bb327d28SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
4588bb327d28SAdrian Chadd 		if (if_ath_alq_checkdebug(&sc->sc_alq,
4589bb327d28SAdrian Chadd 		    ATH_ALQ_EDMA_TXSTATUS)) {
4590bb327d28SAdrian Chadd 			if_ath_alq_post(&sc->sc_alq, ATH_ALQ_EDMA_TXSTATUS,
4591bb327d28SAdrian Chadd 			sc->sc_tx_statuslen,
4592bb327d28SAdrian Chadd 			(char *) ds);
4593bb327d28SAdrian Chadd 		}
4594bb327d28SAdrian Chadd #endif
459503682514SAdrian Chadd 
45965591b213SSam Leffler 		if (status == HAL_EINPROGRESS) {
459703682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_TXCOMP, 3,
459803682514SAdrian Chadd 			    "ath_tx_processq: txq=%u, bf=%p ds=%p, HAL_EINPROGRESS",
459903682514SAdrian Chadd 			    txq->axq_qnum, bf, ds);
4600b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
46015591b213SSam Leffler 			break;
46025591b213SSam Leffler 		}
46036b349e5aSAdrian Chadd 		ATH_TXQ_REMOVE(txq, bf, bf_list);
46045e018508SAdrian Chadd 
46055e018508SAdrian Chadd 		/*
46065e018508SAdrian Chadd 		 * Sanity check.
46075e018508SAdrian Chadd 		 */
46085e018508SAdrian Chadd 		if (txq->axq_qnum != bf->bf_state.bfs_tx_queue) {
46095e018508SAdrian Chadd 			device_printf(sc->sc_dev,
46105e018508SAdrian Chadd 			    "%s: TXQ=%d: bf=%p, bfs_tx_queue=%d\n",
46115e018508SAdrian Chadd 			    __func__,
46125e018508SAdrian Chadd 			    txq->axq_qnum,
46135e018508SAdrian Chadd 			    bf,
46145e018508SAdrian Chadd 			    bf->bf_state.bfs_tx_queue);
46155e018508SAdrian Chadd 		}
46165e018508SAdrian Chadd 		if (txq->axq_qnum != bf->bf_last->bf_state.bfs_tx_queue) {
46175e018508SAdrian Chadd 			device_printf(sc->sc_dev,
46185e018508SAdrian Chadd 			    "%s: TXQ=%d: bf_last=%p, bfs_tx_queue=%d\n",
46195e018508SAdrian Chadd 			    __func__,
46205e018508SAdrian Chadd 			    txq->axq_qnum,
46215e018508SAdrian Chadd 			    bf->bf_last,
46225e018508SAdrian Chadd 			    bf->bf_last->bf_state.bfs_tx_queue);
46235e018508SAdrian Chadd 		}
46245e018508SAdrian Chadd 
46255e018508SAdrian Chadd #if 0
4626d3731e4bSAdrian Chadd 		if (txq->axq_depth > 0) {
462710ad9a77SSam Leffler 			/*
4628d3731e4bSAdrian Chadd 			 * More frames follow.  Mark the buffer busy
4629d3731e4bSAdrian Chadd 			 * so it's not re-used while the hardware may
4630d3731e4bSAdrian Chadd 			 * still re-read the link field in the descriptor.
46316edf1dc7SAdrian Chadd 			 *
4632d3731e4bSAdrian Chadd 			 * Use the last buffer in an aggregate as that
4633d3731e4bSAdrian Chadd 			 * is where the hardware may be - intermediate
4634d3731e4bSAdrian Chadd 			 * descriptors won't be "busy".
463510ad9a77SSam Leffler 			 */
46366edf1dc7SAdrian Chadd 			bf->bf_last->bf_flags |= ATH_BUF_BUSY;
4637d3731e4bSAdrian Chadd 		} else
4638d3731e4bSAdrian Chadd 			txq->axq_link = NULL;
46395e018508SAdrian Chadd #else
46405e018508SAdrian Chadd 		bf->bf_last->bf_flags |= ATH_BUF_BUSY;
46415e018508SAdrian Chadd #endif
46426edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
46436edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
46445591b213SSam Leffler 
46455591b213SSam Leffler 		ni = bf->bf_node;
464603682514SAdrian Chadd 
464703682514SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TXCOMP, 5,
464803682514SAdrian Chadd 		    "ath_tx_processq: txq=%u, bf=%p, ds=%p, ni=%p, ts_status=0x%08x",
464903682514SAdrian Chadd 		    txq->axq_qnum, bf, ds, ni, ts->ts_status);
4650c42a7b7eSSam Leffler 		/*
46519352fb7aSAdrian Chadd 		 * If unicast frame was ack'd update RSSI,
465284784be1SSam Leffler 		 * including the last rx time used to
465384784be1SSam Leffler 		 * workaround phantom bmiss interrupts.
4654d7736e13SSam Leffler 		 */
46559352fb7aSAdrian Chadd 		if (ni != NULL && ts->ts_status == 0 &&
4656875a9451SAdrian Chadd 		    ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)) {
4657d7736e13SSam Leffler 			nacked++;
465884784be1SSam Leffler 			sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
465984784be1SSam Leffler 			ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi,
466084784be1SSam Leffler 				ts->ts_rssi);
466184784be1SSam Leffler 		}
4662b837332dSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
46639352fb7aSAdrian Chadd 
4664bad98824SAdrian Chadd 		/*
4665bad98824SAdrian Chadd 		 * Update statistics and call completion
4666bad98824SAdrian Chadd 		 */
4667bad98824SAdrian Chadd 		ath_tx_process_buf_completion(sc, txq, ts, bf);
4668548a605dSAdrian Chadd 
4669548a605dSAdrian Chadd 		/* XXX at this point, bf and ni may be totally invalid */
46705591b213SSam Leffler 	}
4671339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
467268e8e04eSSam Leffler 	/*
467368e8e04eSSam Leffler 	 * Flush fast-frame staging queue when traffic slows.
467468e8e04eSSam Leffler 	 */
467568e8e04eSSam Leffler 	if (txq->axq_depth <= 1)
467604f19fd6SSam Leffler 		ieee80211_ff_flush(ic, txq->axq_ac);
4677339ccfb3SSam Leffler #endif
4678eb6f0de0SAdrian Chadd 
467921bca442SAdrian Chadd 	/* Kick the software TXQ scheduler */
4680eb6f0de0SAdrian Chadd 	if (dosched) {
4681a40880adSAdrian Chadd 		ATH_TX_LOCK(sc);
4682a40880adSAdrian Chadd 		ath_txq_sched(sc, txq);
4683a40880adSAdrian Chadd 		ATH_TX_UNLOCK(sc);
4684eb6f0de0SAdrian Chadd 	}
4685eb6f0de0SAdrian Chadd 
468603682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
468703682514SAdrian Chadd 	    "ath_tx_processq: txq=%u: done",
468803682514SAdrian Chadd 	    txq->axq_qnum);
468903682514SAdrian Chadd 
4690d7736e13SSam Leffler 	return nacked;
4691d7736e13SSam Leffler }
4692d7736e13SSam Leffler 
46938f939e79SAdrian Chadd #define	TXQACTIVE(t, q)		( (t) & (1 << (q)))
4694c42a7b7eSSam Leffler 
4695c42a7b7eSSam Leffler /*
4696c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4697c42a7b7eSSam Leffler  * for a single hardware transmit queue (e.g. 5210 and 5211).
4698c42a7b7eSSam Leffler  */
4699c42a7b7eSSam Leffler static void
4700c42a7b7eSSam Leffler ath_tx_proc_q0(void *arg, int npending)
4701c42a7b7eSSam Leffler {
4702c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
47038f939e79SAdrian Chadd 	uint32_t txqs;
4704c42a7b7eSSam Leffler 
4705ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4706ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
47078f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
47088f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4709ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
47108f939e79SAdrian Chadd 
4711f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4712f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4713f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4714f5c30c4eSAdrian Chadd 
471503682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
471603682514SAdrian Chadd 	    "ath_tx_proc_q0: txqs=0x%08x", txqs);
471703682514SAdrian Chadd 
471896ff485dSAdrian Chadd 	if (TXQACTIVE(txqs, 0) && ath_tx_processq(sc, &sc->sc_txq[0], 1))
47198f939e79SAdrian Chadd 		/* XXX why is lastrx updated in tx code? */
4720d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
47218f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
472296ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
47232e986da5SSam Leffler 	sc->sc_wd_timer = 0;
47245591b213SSam Leffler 
47253e50ec2cSSam Leffler 	if (sc->sc_softled)
472646d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
47273e50ec2cSSam Leffler 
4728ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4729ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4730ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
47311a85141aSAdrian Chadd 
4732f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4733f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4734f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4735f5c30c4eSAdrian Chadd 
47361a85141aSAdrian Chadd 	ath_tx_kick(sc);
47375591b213SSam Leffler }
47385591b213SSam Leffler 
47395591b213SSam Leffler /*
4740c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4741c42a7b7eSSam Leffler  * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
47425591b213SSam Leffler  */
47435591b213SSam Leffler static void
4744c42a7b7eSSam Leffler ath_tx_proc_q0123(void *arg, int npending)
4745c42a7b7eSSam Leffler {
4746c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4747d7736e13SSam Leffler 	int nacked;
47488f939e79SAdrian Chadd 	uint32_t txqs;
47498f939e79SAdrian Chadd 
4750ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4751ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
47528f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
47538f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4754ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4755c42a7b7eSSam Leffler 
4756f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4757f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4758f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4759f5c30c4eSAdrian Chadd 
476003682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
476103682514SAdrian Chadd 	    "ath_tx_proc_q0123: txqs=0x%08x", txqs);
476203682514SAdrian Chadd 
4763c42a7b7eSSam Leffler 	/*
4764c42a7b7eSSam Leffler 	 * Process each active queue.
4765c42a7b7eSSam Leffler 	 */
4766d7736e13SSam Leffler 	nacked = 0;
47678f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 0))
476896ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[0], 1);
47698f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 1))
477096ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[1], 1);
47718f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 2))
477296ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[2], 1);
47738f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 3))
477496ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[3], 1);
47758f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
477696ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
4777d7736e13SSam Leffler 	if (nacked)
4778d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4779c42a7b7eSSam Leffler 
47802e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4781c42a7b7eSSam Leffler 
47823e50ec2cSSam Leffler 	if (sc->sc_softled)
478346d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
47843e50ec2cSSam Leffler 
4785ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4786ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4787ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
47881a85141aSAdrian Chadd 
4789f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4790f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4791f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4792f5c30c4eSAdrian Chadd 
47931a85141aSAdrian Chadd 	ath_tx_kick(sc);
4794c42a7b7eSSam Leffler }
4795c42a7b7eSSam Leffler 
4796c42a7b7eSSam Leffler /*
4797c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt.
4798c42a7b7eSSam Leffler  */
4799c42a7b7eSSam Leffler static void
4800c42a7b7eSSam Leffler ath_tx_proc(void *arg, int npending)
4801c42a7b7eSSam Leffler {
4802c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4803d7736e13SSam Leffler 	int i, nacked;
48048f939e79SAdrian Chadd 	uint32_t txqs;
48058f939e79SAdrian Chadd 
4806ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4807ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
48088f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
48098f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4810ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4811c42a7b7eSSam Leffler 
4812f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4813f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4814f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4815f5c30c4eSAdrian Chadd 
481603682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1, "ath_tx_proc: txqs=0x%08x", txqs);
481703682514SAdrian Chadd 
4818c42a7b7eSSam Leffler 	/*
4819c42a7b7eSSam Leffler 	 * Process each active queue.
4820c42a7b7eSSam Leffler 	 */
4821d7736e13SSam Leffler 	nacked = 0;
4822c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
48238f939e79SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i) && TXQACTIVE(txqs, i))
482496ff485dSAdrian Chadd 			nacked += ath_tx_processq(sc, &sc->sc_txq[i], 1);
4825d7736e13SSam Leffler 	if (nacked)
4826d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4827c42a7b7eSSam Leffler 
48282e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4829c42a7b7eSSam Leffler 
48303e50ec2cSSam Leffler 	if (sc->sc_softled)
483146d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
48323e50ec2cSSam Leffler 
4833ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4834ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4835ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
48361a85141aSAdrian Chadd 
4837f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4838f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4839f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4840f5c30c4eSAdrian Chadd 
48411a85141aSAdrian Chadd 	ath_tx_kick(sc);
4842c42a7b7eSSam Leffler }
484316d4de92SAdrian Chadd #undef	TXQACTIVE
4844c42a7b7eSSam Leffler 
48459352fb7aSAdrian Chadd /*
484603e9308fSAdrian Chadd  * Deferred processing of TXQ rescheduling.
484703e9308fSAdrian Chadd  */
484803e9308fSAdrian Chadd static void
484903e9308fSAdrian Chadd ath_txq_sched_tasklet(void *arg, int npending)
485003e9308fSAdrian Chadd {
485103e9308fSAdrian Chadd 	struct ath_softc *sc = arg;
485203e9308fSAdrian Chadd 	int i;
485303e9308fSAdrian Chadd 
485403e9308fSAdrian Chadd 	/* XXX is skipping ok? */
485503e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
485603e9308fSAdrian Chadd #if 0
485703e9308fSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
485803e9308fSAdrian Chadd 		device_printf(sc->sc_dev,
485903e9308fSAdrian Chadd 		    "%s: sc_inreset_cnt > 0; skipping\n", __func__);
486003e9308fSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
486103e9308fSAdrian Chadd 		return;
486203e9308fSAdrian Chadd 	}
486303e9308fSAdrian Chadd #endif
486403e9308fSAdrian Chadd 	sc->sc_txproc_cnt++;
486503e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
486603e9308fSAdrian Chadd 
4867f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4868f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4869f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4870f5c30c4eSAdrian Chadd 
4871375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
487203e9308fSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
4873b5a9dfd5SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
487403e9308fSAdrian Chadd 			ath_txq_sched(sc, &sc->sc_txq[i]);
4875b5a9dfd5SAdrian Chadd 		}
487603e9308fSAdrian Chadd 	}
4877375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
487803e9308fSAdrian Chadd 
4879f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4880f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4881f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4882f5c30c4eSAdrian Chadd 
488303e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
488403e9308fSAdrian Chadd 	sc->sc_txproc_cnt--;
488503e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
488603e9308fSAdrian Chadd }
488703e9308fSAdrian Chadd 
4888e1a50456SAdrian Chadd void
4889e1a50456SAdrian Chadd ath_returnbuf_tail(struct ath_softc *sc, struct ath_buf *bf)
4890e1a50456SAdrian Chadd {
4891e1a50456SAdrian Chadd 
4892e1a50456SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
4893e1a50456SAdrian Chadd 
4894af33d486SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_MGMT)
4895af33d486SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf_mgmt, bf, bf_list);
489623ced6c1SAdrian Chadd 	else {
4897e1a50456SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
489823ced6c1SAdrian Chadd 		sc->sc_txbuf_cnt++;
489923ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt > ath_txbuf) {
490023ced6c1SAdrian Chadd 			device_printf(sc->sc_dev,
490123ced6c1SAdrian Chadd 			    "%s: sc_txbuf_cnt > %d?\n",
490223ced6c1SAdrian Chadd 			    __func__,
490323ced6c1SAdrian Chadd 			    ath_txbuf);
490423ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt = ath_txbuf;
490523ced6c1SAdrian Chadd 		}
490623ced6c1SAdrian Chadd 	}
4907e1a50456SAdrian Chadd }
4908e1a50456SAdrian Chadd 
4909e1a50456SAdrian Chadd void
4910e1a50456SAdrian Chadd ath_returnbuf_head(struct ath_softc *sc, struct ath_buf *bf)
4911e1a50456SAdrian Chadd {
4912e1a50456SAdrian Chadd 
4913e1a50456SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
4914e1a50456SAdrian Chadd 
4915af33d486SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_MGMT)
4916af33d486SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf_mgmt, bf, bf_list);
491723ced6c1SAdrian Chadd 	else {
4918e1a50456SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
491923ced6c1SAdrian Chadd 		sc->sc_txbuf_cnt++;
492023ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt > ATH_TXBUF) {
492123ced6c1SAdrian Chadd 			device_printf(sc->sc_dev,
492223ced6c1SAdrian Chadd 			    "%s: sc_txbuf_cnt > %d?\n",
492323ced6c1SAdrian Chadd 			    __func__,
492423ced6c1SAdrian Chadd 			    ATH_TXBUF);
492523ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt = ATH_TXBUF;
492623ced6c1SAdrian Chadd 		}
492723ced6c1SAdrian Chadd 	}
4928e1a50456SAdrian Chadd }
4929e1a50456SAdrian Chadd 
493003e9308fSAdrian Chadd /*
4931629ce218SAdrian Chadd  * Free the holding buffer if it exists
4932629ce218SAdrian Chadd  */
49333feffbd7SAdrian Chadd void
4934629ce218SAdrian Chadd ath_txq_freeholdingbuf(struct ath_softc *sc, struct ath_txq *txq)
4935629ce218SAdrian Chadd {
49365e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK_ASSERT(sc);
49375e018508SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
4938629ce218SAdrian Chadd 
4939629ce218SAdrian Chadd 	if (txq->axq_holdingbf == NULL)
4940629ce218SAdrian Chadd 		return;
4941629ce218SAdrian Chadd 
4942629ce218SAdrian Chadd 	txq->axq_holdingbf->bf_flags &= ~ATH_BUF_BUSY;
49435e018508SAdrian Chadd 
49445e018508SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
4945629ce218SAdrian Chadd 	ath_returnbuf_tail(sc, txq->axq_holdingbf);
49465e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
49475e018508SAdrian Chadd 
4948629ce218SAdrian Chadd 	txq->axq_holdingbf = NULL;
4949629ce218SAdrian Chadd }
4950629ce218SAdrian Chadd 
4951629ce218SAdrian Chadd /*
4952629ce218SAdrian Chadd  * Add this buffer to the holding queue, freeing the previous
4953629ce218SAdrian Chadd  * one if it exists.
4954629ce218SAdrian Chadd  */
4955629ce218SAdrian Chadd static void
4956629ce218SAdrian Chadd ath_txq_addholdingbuf(struct ath_softc *sc, struct ath_buf *bf)
4957629ce218SAdrian Chadd {
4958629ce218SAdrian Chadd 	struct ath_txq *txq;
4959629ce218SAdrian Chadd 
49605e018508SAdrian Chadd 	txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue];
49615e018508SAdrian Chadd 
49625e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK_ASSERT(sc);
49635e018508SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
49645f2f0e61SAdrian Chadd 
4965629ce218SAdrian Chadd 	/* XXX assert ATH_BUF_BUSY is set */
4966629ce218SAdrian Chadd 
4967629ce218SAdrian Chadd 	/* XXX assert the tx queue is under the max number */
4968629ce218SAdrian Chadd 	if (bf->bf_state.bfs_tx_queue > HAL_NUM_TX_QUEUES) {
4969629ce218SAdrian Chadd 		device_printf(sc->sc_dev, "%s: bf=%p: invalid tx queue (%d)\n",
4970629ce218SAdrian Chadd 		    __func__,
4971629ce218SAdrian Chadd 		    bf,
4972629ce218SAdrian Chadd 		    bf->bf_state.bfs_tx_queue);
4973629ce218SAdrian Chadd 		bf->bf_flags &= ~ATH_BUF_BUSY;
4974629ce218SAdrian Chadd 		ath_returnbuf_tail(sc, bf);
4975629ce218SAdrian Chadd 		return;
4976629ce218SAdrian Chadd 	}
4977629ce218SAdrian Chadd 	ath_txq_freeholdingbuf(sc, txq);
4978629ce218SAdrian Chadd 	txq->axq_holdingbf = bf;
4979629ce218SAdrian Chadd }
4980629ce218SAdrian Chadd 
4981629ce218SAdrian Chadd /*
49829352fb7aSAdrian Chadd  * Return a buffer to the pool and update the 'busy' flag on the
49839352fb7aSAdrian Chadd  * previous 'tail' entry.
49849352fb7aSAdrian Chadd  *
49859352fb7aSAdrian Chadd  * This _must_ only be called when the buffer is involved in a completed
49869352fb7aSAdrian Chadd  * TX. The logic is that if it was part of an active TX, the previous
49879352fb7aSAdrian Chadd  * buffer on the list is now not involved in a halted TX DMA queue, waiting
49889352fb7aSAdrian Chadd  * for restart (eg for TDMA.)
49899352fb7aSAdrian Chadd  *
49909352fb7aSAdrian Chadd  * The caller must free the mbuf and recycle the node reference.
49915e018508SAdrian Chadd  *
49925e018508SAdrian Chadd  * XXX This method of handling busy / holding buffers is insanely stupid.
49935e018508SAdrian Chadd  * It requires bf_state.bfs_tx_queue to be correctly assigned.  It would
49945e018508SAdrian Chadd  * be much nicer if buffers in the processq() methods would instead be
49955e018508SAdrian Chadd  * always completed there (pushed onto a txq or ath_bufhead) so we knew
49965e018508SAdrian Chadd  * exactly what hardware queue they came from in the first place.
49979352fb7aSAdrian Chadd  */
49989352fb7aSAdrian Chadd void
49999352fb7aSAdrian Chadd ath_freebuf(struct ath_softc *sc, struct ath_buf *bf)
50009352fb7aSAdrian Chadd {
50015e018508SAdrian Chadd 	struct ath_txq *txq;
50025e018508SAdrian Chadd 
50035e018508SAdrian Chadd 	txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue];
50045e018508SAdrian Chadd 
50059352fb7aSAdrian Chadd 	KASSERT((bf->bf_node == NULL), ("%s: bf->bf_node != NULL\n", __func__));
50069352fb7aSAdrian Chadd 	KASSERT((bf->bf_m == NULL), ("%s: bf->bf_m != NULL\n", __func__));
50079352fb7aSAdrian Chadd 
5008629ce218SAdrian Chadd 	/*
50095e018508SAdrian Chadd 	 * If this buffer is busy, push it onto the holding queue.
5010629ce218SAdrian Chadd 	 */
5011629ce218SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_BUSY) {
50125e018508SAdrian Chadd 		ATH_TXQ_LOCK(txq);
5013629ce218SAdrian Chadd 		ath_txq_addholdingbuf(sc, bf);
50145e018508SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
5015629ce218SAdrian Chadd 		return;
5016629ce218SAdrian Chadd 	}
5017629ce218SAdrian Chadd 
5018629ce218SAdrian Chadd 	/*
5019629ce218SAdrian Chadd 	 * Not a busy buffer, so free normally
5020629ce218SAdrian Chadd 	 */
50219352fb7aSAdrian Chadd 	ATH_TXBUF_LOCK(sc);
5022e1a50456SAdrian Chadd 	ath_returnbuf_tail(sc, bf);
50239352fb7aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
50249352fb7aSAdrian Chadd }
50259352fb7aSAdrian Chadd 
50269352fb7aSAdrian Chadd /*
50279352fb7aSAdrian Chadd  * This is currently used by ath_tx_draintxq() and
50289352fb7aSAdrian Chadd  * ath_tx_tid_free_pkts().
50299352fb7aSAdrian Chadd  *
50309352fb7aSAdrian Chadd  * It recycles a single ath_buf.
50319352fb7aSAdrian Chadd  */
50329352fb7aSAdrian Chadd void
50339352fb7aSAdrian Chadd ath_tx_freebuf(struct ath_softc *sc, struct ath_buf *bf, int status)
50349352fb7aSAdrian Chadd {
50359352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
50369352fb7aSAdrian Chadd 	struct mbuf *m0 = bf->bf_m;
50379352fb7aSAdrian Chadd 
50383f3a5dbdSAdrian Chadd 	/*
50393f3a5dbdSAdrian Chadd 	 * Make sure that we only sync/unload if there's an mbuf.
50403f3a5dbdSAdrian Chadd 	 * If not (eg we cloned a buffer), the unload will have already
50413f3a5dbdSAdrian Chadd 	 * occured.
50423f3a5dbdSAdrian Chadd 	 */
50433f3a5dbdSAdrian Chadd 	if (bf->bf_m != NULL) {
50443f3a5dbdSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
50453f3a5dbdSAdrian Chadd 		    BUS_DMASYNC_POSTWRITE);
50463f3a5dbdSAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
50473f3a5dbdSAdrian Chadd 	}
50483f3a5dbdSAdrian Chadd 
50499352fb7aSAdrian Chadd 	bf->bf_node = NULL;
50509352fb7aSAdrian Chadd 	bf->bf_m = NULL;
50519352fb7aSAdrian Chadd 
50529352fb7aSAdrian Chadd 	/* Free the buffer, it's not needed any longer */
50539352fb7aSAdrian Chadd 	ath_freebuf(sc, bf);
50549352fb7aSAdrian Chadd 
5055e95f3424SAdrian Chadd 	/* Pass the buffer back to net80211 - completing it */
5056e95f3424SAdrian Chadd 	ieee80211_tx_complete(ni, m0, status);
50579352fb7aSAdrian Chadd }
50589352fb7aSAdrian Chadd 
50593feffbd7SAdrian Chadd static struct ath_buf *
50603feffbd7SAdrian Chadd ath_tx_draintxq_get_one(struct ath_softc *sc, struct ath_txq *txq)
50613feffbd7SAdrian Chadd {
50623feffbd7SAdrian Chadd 	struct ath_buf *bf;
50633feffbd7SAdrian Chadd 
50643feffbd7SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
50653feffbd7SAdrian Chadd 
50663feffbd7SAdrian Chadd 	/*
50673feffbd7SAdrian Chadd 	 * Drain the FIFO queue first, then if it's
50683feffbd7SAdrian Chadd 	 * empty, move to the normal frame queue.
50693feffbd7SAdrian Chadd 	 */
50703feffbd7SAdrian Chadd 	bf = TAILQ_FIRST(&txq->fifo.axq_q);
50713feffbd7SAdrian Chadd 	if (bf != NULL) {
50723feffbd7SAdrian Chadd 		/*
50733feffbd7SAdrian Chadd 		 * Is it the last buffer in this set?
50743feffbd7SAdrian Chadd 		 * Decrement the FIFO counter.
50753feffbd7SAdrian Chadd 		 */
50763feffbd7SAdrian Chadd 		if (bf->bf_flags & ATH_BUF_FIFOEND) {
50773feffbd7SAdrian Chadd 			if (txq->axq_fifo_depth == 0) {
50783feffbd7SAdrian Chadd 				device_printf(sc->sc_dev,
50793feffbd7SAdrian Chadd 				    "%s: Q%d: fifo_depth=0, fifo.axq_depth=%d?\n",
50803feffbd7SAdrian Chadd 				    __func__,
50813feffbd7SAdrian Chadd 				    txq->axq_qnum,
50823feffbd7SAdrian Chadd 				    txq->fifo.axq_depth);
50833feffbd7SAdrian Chadd 			} else
50843feffbd7SAdrian Chadd 				txq->axq_fifo_depth--;
50853feffbd7SAdrian Chadd 		}
50863feffbd7SAdrian Chadd 		ATH_TXQ_REMOVE(&txq->fifo, bf, bf_list);
50873feffbd7SAdrian Chadd 		return (bf);
50883feffbd7SAdrian Chadd 	}
50893feffbd7SAdrian Chadd 
50903feffbd7SAdrian Chadd 	/*
50913feffbd7SAdrian Chadd 	 * Debugging!
50923feffbd7SAdrian Chadd 	 */
50933feffbd7SAdrian Chadd 	if (txq->axq_fifo_depth != 0 || txq->fifo.axq_depth != 0) {
50943feffbd7SAdrian Chadd 		device_printf(sc->sc_dev,
50953feffbd7SAdrian Chadd 		    "%s: Q%d: fifo_depth=%d, fifo.axq_depth=%d\n",
50963feffbd7SAdrian Chadd 		    __func__,
50973feffbd7SAdrian Chadd 		    txq->axq_qnum,
50983feffbd7SAdrian Chadd 		    txq->axq_fifo_depth,
50993feffbd7SAdrian Chadd 		    txq->fifo.axq_depth);
51003feffbd7SAdrian Chadd 	}
51013feffbd7SAdrian Chadd 
51023feffbd7SAdrian Chadd 	/*
51033feffbd7SAdrian Chadd 	 * Now drain the pending queue.
51043feffbd7SAdrian Chadd 	 */
51053feffbd7SAdrian Chadd 	bf = TAILQ_FIRST(&txq->axq_q);
51063feffbd7SAdrian Chadd 	if (bf == NULL) {
51073feffbd7SAdrian Chadd 		txq->axq_link = NULL;
51083feffbd7SAdrian Chadd 		return (NULL);
51093feffbd7SAdrian Chadd 	}
51103feffbd7SAdrian Chadd 	ATH_TXQ_REMOVE(txq, bf, bf_list);
51113feffbd7SAdrian Chadd 	return (bf);
51123feffbd7SAdrian Chadd }
51133feffbd7SAdrian Chadd 
51149352fb7aSAdrian Chadd void
51151762ec94SAdrian Chadd ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
51165591b213SSam Leffler {
5117a585a9a1SSam Leffler #ifdef ATH_DEBUG
51185591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5119d2f6ed15SSam Leffler #endif
51205591b213SSam Leffler 	struct ath_buf *bf;
51217a4c5ed9SSam Leffler 	u_int ix;
51225591b213SSam Leffler 
5123c42a7b7eSSam Leffler 	/*
5124c42a7b7eSSam Leffler 	 * NB: this assumes output has been stopped and
51255d61b5e8SSam Leffler 	 *     we do not need to block ath_tx_proc
5126c42a7b7eSSam Leffler 	 */
51277a4c5ed9SSam Leffler 	for (ix = 0;; ix++) {
5128b837332dSAdrian Chadd 		ATH_TXQ_LOCK(txq);
51293feffbd7SAdrian Chadd 		bf = ath_tx_draintxq_get_one(sc, txq);
51305591b213SSam Leffler 		if (bf == NULL) {
5131b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
51325591b213SSam Leffler 			break;
51335591b213SSam Leffler 		}
51346edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
51356edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
5136a585a9a1SSam Leffler #ifdef ATH_DEBUG
51374a3ac3fcSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RESET) {
51387a79cebfSGleb Smirnoff 			struct ieee80211com *ic = &sc->sc_ic;
51391762ec94SAdrian Chadd 			int status = 0;
5140b032f27cSSam Leffler 
51411762ec94SAdrian Chadd 			/*
51421762ec94SAdrian Chadd 			 * EDMA operation has a TX completion FIFO
51431762ec94SAdrian Chadd 			 * separate from the TX descriptor, so this
51441762ec94SAdrian Chadd 			 * method of checking the "completion" status
51451762ec94SAdrian Chadd 			 * is wrong.
51461762ec94SAdrian Chadd 			 */
51471762ec94SAdrian Chadd 			if (! sc->sc_isedma) {
51481762ec94SAdrian Chadd 				status = (ath_hal_txprocdesc(ah,
51491762ec94SAdrian Chadd 				    bf->bf_lastds,
515065f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
51511762ec94SAdrian Chadd 			}
51521762ec94SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, ix, status);
5153e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ic, mtod(bf->bf_m, const uint8_t *),
51544a3ac3fcSSam Leffler 			    bf->bf_m->m_len, 0, -1);
51554a3ac3fcSSam Leffler 		}
5156a585a9a1SSam Leffler #endif /* ATH_DEBUG */
515723428eafSSam Leffler 		/*
51589352fb7aSAdrian Chadd 		 * Since we're now doing magic in the completion
51599352fb7aSAdrian Chadd 		 * functions, we -must- call it for aggregation
51609352fb7aSAdrian Chadd 		 * destinations or BAW tracking will get upset.
516123428eafSSam Leffler 		 */
51629352fb7aSAdrian Chadd 		/*
51639352fb7aSAdrian Chadd 		 * Clear ATH_BUF_BUSY; the completion handler
51649352fb7aSAdrian Chadd 		 * will free the buffer.
51659352fb7aSAdrian Chadd 		 */
5166b837332dSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
516710ad9a77SSam Leffler 		bf->bf_flags &= ~ATH_BUF_BUSY;
51689352fb7aSAdrian Chadd 		if (bf->bf_comp)
51699352fb7aSAdrian Chadd 			bf->bf_comp(sc, bf, 1);
51709352fb7aSAdrian Chadd 		else
51719352fb7aSAdrian Chadd 			ath_tx_default_comp(sc, bf, 1);
51725591b213SSam Leffler 	}
51739352fb7aSAdrian Chadd 
5174eb6f0de0SAdrian Chadd 	/*
5175629ce218SAdrian Chadd 	 * Free the holding buffer if it exists
5176629ce218SAdrian Chadd 	 */
51775e018508SAdrian Chadd 	ATH_TXQ_LOCK(txq);
5178629ce218SAdrian Chadd 	ath_txq_freeholdingbuf(sc, txq);
51795e018508SAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
5180629ce218SAdrian Chadd 
5181629ce218SAdrian Chadd 	/*
5182eb6f0de0SAdrian Chadd 	 * Drain software queued frames which are on
5183eb6f0de0SAdrian Chadd 	 * active TIDs.
5184eb6f0de0SAdrian Chadd 	 */
5185eb6f0de0SAdrian Chadd 	ath_tx_txq_drain(sc, txq);
5186c42a7b7eSSam Leffler }
5187c42a7b7eSSam Leffler 
5188c42a7b7eSSam Leffler static void
5189c42a7b7eSSam Leffler ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
5190c42a7b7eSSam Leffler {
5191c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5192c42a7b7eSSam Leffler 
51939be82a42SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
51949be82a42SAdrian Chadd 
51959d2a962bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
5196dfaf8de9SAdrian Chadd 	    "%s: tx queue [%u] %p, active=%d, hwpending=%d, flags 0x%08x, "
5197dfaf8de9SAdrian Chadd 	    "link %p, holdingbf=%p\n",
51989d2a962bSAdrian Chadd 	    __func__,
51999d2a962bSAdrian Chadd 	    txq->axq_qnum,
52006891c875SPeter Wemm 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
52018d060542SAdrian Chadd 	    (int) (!! ath_hal_txqenabled(ah, txq->axq_qnum)),
52028d060542SAdrian Chadd 	    (int) ath_hal_numtxpending(ah, txq->axq_qnum),
52039d2a962bSAdrian Chadd 	    txq->axq_flags,
5204dfaf8de9SAdrian Chadd 	    txq->axq_link,
5205dfaf8de9SAdrian Chadd 	    txq->axq_holdingbf);
5206dfaf8de9SAdrian Chadd 
52074a3ac3fcSSam Leffler 	(void) ath_hal_stoptxdma(ah, txq->axq_qnum);
52089be82a42SAdrian Chadd 	/* We've stopped TX DMA, so mark this as stopped. */
52099be82a42SAdrian Chadd 	txq->axq_flags &= ~ATH_TXQ_PUTRUNNING;
5210dfaf8de9SAdrian Chadd 
5211dfaf8de9SAdrian Chadd #ifdef	ATH_DEBUG
5212dfaf8de9SAdrian Chadd 	if ((sc->sc_debug & ATH_DEBUG_RESET)
5213dfaf8de9SAdrian Chadd 	    && (txq->axq_holdingbf != NULL)) {
5214dfaf8de9SAdrian Chadd 		ath_printtxbuf(sc, txq->axq_holdingbf, txq->axq_qnum, 0, 0);
5215dfaf8de9SAdrian Chadd 	}
5216dfaf8de9SAdrian Chadd #endif
5217c42a7b7eSSam Leffler }
5218c42a7b7eSSam Leffler 
5219bad98824SAdrian Chadd int
52202d433424SAdrian Chadd ath_stoptxdma(struct ath_softc *sc)
5221c42a7b7eSSam Leffler {
5222c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5223c42a7b7eSSam Leffler 	int i;
5224c42a7b7eSSam Leffler 
5225c42a7b7eSSam Leffler 	/* XXX return value */
52262d433424SAdrian Chadd 	if (sc->sc_invalid)
52272d433424SAdrian Chadd 		return 0;
52282d433424SAdrian Chadd 
5229c42a7b7eSSam Leffler 	if (!sc->sc_invalid) {
5230c42a7b7eSSam Leffler 		/* don't touch the hardware if marked invalid */
52314a3ac3fcSSam Leffler 		DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
52324a3ac3fcSSam Leffler 		    __func__, sc->sc_bhalq,
52334a3ac3fcSSam Leffler 		    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq),
52344a3ac3fcSSam Leffler 		    NULL);
52359be82a42SAdrian Chadd 
52369be82a42SAdrian Chadd 		/* stop the beacon queue */
5237c42a7b7eSSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
52389be82a42SAdrian Chadd 
52399be82a42SAdrian Chadd 		/* Stop the data queues */
52409be82a42SAdrian Chadd 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
52419be82a42SAdrian Chadd 			if (ATH_TXQ_SETUP(sc, i)) {
52429be82a42SAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
5243c42a7b7eSSam Leffler 				ath_tx_stopdma(sc, &sc->sc_txq[i]);
52449be82a42SAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
52459be82a42SAdrian Chadd 			}
52469be82a42SAdrian Chadd 		}
5247c42a7b7eSSam Leffler 	}
52482d433424SAdrian Chadd 
52492d433424SAdrian Chadd 	return 1;
52502d433424SAdrian Chadd }
52512d433424SAdrian Chadd 
525207187d11SAdrian Chadd #ifdef	ATH_DEBUG
52539be82a42SAdrian Chadd void
5254ed261a61SAdrian Chadd ath_tx_dump(struct ath_softc *sc, struct ath_txq *txq)
5255ed261a61SAdrian Chadd {
5256ed261a61SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
5257ed261a61SAdrian Chadd 	struct ath_buf *bf;
5258ed261a61SAdrian Chadd 	int i = 0;
5259ed261a61SAdrian Chadd 
5260ed261a61SAdrian Chadd 	if (! (sc->sc_debug & ATH_DEBUG_RESET))
5261ed261a61SAdrian Chadd 		return;
5262ed261a61SAdrian Chadd 
5263ed261a61SAdrian Chadd 	device_printf(sc->sc_dev, "%s: Q%d: begin\n",
5264ed261a61SAdrian Chadd 	    __func__, txq->axq_qnum);
5265ed261a61SAdrian Chadd 	TAILQ_FOREACH(bf, &txq->axq_q, bf_list) {
5266ed261a61SAdrian Chadd 		ath_printtxbuf(sc, bf, txq->axq_qnum, i,
5267ed261a61SAdrian Chadd 			ath_hal_txprocdesc(ah, bf->bf_lastds,
5268ed261a61SAdrian Chadd 			    &bf->bf_status.ds_txstat) == HAL_OK);
5269ed261a61SAdrian Chadd 		i++;
5270ed261a61SAdrian Chadd 	}
5271ed261a61SAdrian Chadd 	device_printf(sc->sc_dev, "%s: Q%d: end\n",
5272ed261a61SAdrian Chadd 	    __func__, txq->axq_qnum);
5273ed261a61SAdrian Chadd }
527407187d11SAdrian Chadd #endif /* ATH_DEBUG */
5275ed261a61SAdrian Chadd 
52762d433424SAdrian Chadd /*
52772d433424SAdrian Chadd  * Drain the transmit queues and reclaim resources.
52782d433424SAdrian Chadd  */
5279788e6aa9SAdrian Chadd void
5280788e6aa9SAdrian Chadd ath_legacy_tx_drain(struct ath_softc *sc, ATH_RESET_TYPE reset_type)
52812d433424SAdrian Chadd {
52822d433424SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
5283ba2c1fbcSAdrian Chadd 	struct ath_buf *bf_last;
52847a79cebfSGleb Smirnoff 	int i;
52852d433424SAdrian Chadd 
52862d433424SAdrian Chadd 	(void) ath_stoptxdma(sc);
52872d433424SAdrian Chadd 
5288ed261a61SAdrian Chadd 	/*
5289ed261a61SAdrian Chadd 	 * Dump the queue contents
5290ed261a61SAdrian Chadd 	 */
5291ef27340cSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
5292ef27340cSAdrian Chadd 		/*
5293ef27340cSAdrian Chadd 		 * XXX TODO: should we just handle the completed TX frames
5294ef27340cSAdrian Chadd 		 * here, whether or not the reset is a full one or not?
5295ef27340cSAdrian Chadd 		 */
5296ef27340cSAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
529707187d11SAdrian Chadd #ifdef	ATH_DEBUG
5298ed261a61SAdrian Chadd 			if (sc->sc_debug & ATH_DEBUG_RESET)
5299ed261a61SAdrian Chadd 				ath_tx_dump(sc, &sc->sc_txq[i]);
530007187d11SAdrian Chadd #endif	/* ATH_DEBUG */
53018328d6e4SAdrian Chadd 			if (reset_type == ATH_RESET_NOLOSS) {
5302ef27340cSAdrian Chadd 				ath_tx_processq(sc, &sc->sc_txq[i], 0);
53038328d6e4SAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
53048328d6e4SAdrian Chadd 				/*
53058328d6e4SAdrian Chadd 				 * Free the holding buffer; DMA is now
53068328d6e4SAdrian Chadd 				 * stopped.
53078328d6e4SAdrian Chadd 				 */
53088328d6e4SAdrian Chadd 				ath_txq_freeholdingbuf(sc, &sc->sc_txq[i]);
53098328d6e4SAdrian Chadd 				/*
53109be82a42SAdrian Chadd 				 * Setup the link pointer to be the
53119be82a42SAdrian Chadd 				 * _last_ buffer/descriptor in the list.
53129be82a42SAdrian Chadd 				 * If there's nothing in the list, set it
53139be82a42SAdrian Chadd 				 * to NULL.
53148328d6e4SAdrian Chadd 				 */
53159be82a42SAdrian Chadd 				bf_last = ATH_TXQ_LAST(&sc->sc_txq[i],
53169be82a42SAdrian Chadd 				    axq_q_s);
53179be82a42SAdrian Chadd 				if (bf_last != NULL) {
53189be82a42SAdrian Chadd 					ath_hal_gettxdesclinkptr(ah,
53199be82a42SAdrian Chadd 					    bf_last->bf_lastds,
53209be82a42SAdrian Chadd 					    &sc->sc_txq[i].axq_link);
53219be82a42SAdrian Chadd 				} else {
53228328d6e4SAdrian Chadd 					sc->sc_txq[i].axq_link = NULL;
53239be82a42SAdrian Chadd 				}
53248328d6e4SAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
53258328d6e4SAdrian Chadd 			} else
5326c42a7b7eSSam Leffler 				ath_tx_draintxq(sc, &sc->sc_txq[i]);
5327ef27340cSAdrian Chadd 		}
5328ef27340cSAdrian Chadd 	}
53294a3ac3fcSSam Leffler #ifdef ATH_DEBUG
53304a3ac3fcSSam Leffler 	if (sc->sc_debug & ATH_DEBUG_RESET) {
53316b349e5aSAdrian Chadd 		struct ath_buf *bf = TAILQ_FIRST(&sc->sc_bbuf);
53324a3ac3fcSSam Leffler 		if (bf != NULL && bf->bf_m != NULL) {
53336902009eSSam Leffler 			ath_printtxbuf(sc, bf, sc->sc_bhalq, 0,
53346edf1dc7SAdrian Chadd 				ath_hal_txprocdesc(ah, bf->bf_lastds,
533565f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
53367a79cebfSGleb Smirnoff 			ieee80211_dump_pkt(&sc->sc_ic,
5337e40b6ab1SSam Leffler 			    mtod(bf->bf_m, const uint8_t *), bf->bf_m->m_len,
5338e40b6ab1SSam Leffler 			    0, -1);
53394a3ac3fcSSam Leffler 		}
53404a3ac3fcSSam Leffler 	}
53414a3ac3fcSSam Leffler #endif /* ATH_DEBUG */
53422e986da5SSam Leffler 	sc->sc_wd_timer = 0;
53435591b213SSam Leffler }
53445591b213SSam Leffler 
53455591b213SSam Leffler /*
5346c42a7b7eSSam Leffler  * Update internal state after a channel change.
5347c42a7b7eSSam Leffler  */
5348c42a7b7eSSam Leffler static void
5349c42a7b7eSSam Leffler ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
5350c42a7b7eSSam Leffler {
5351c42a7b7eSSam Leffler 	enum ieee80211_phymode mode;
5352c42a7b7eSSam Leffler 
5353c42a7b7eSSam Leffler 	/*
5354c42a7b7eSSam Leffler 	 * Change channels and update the h/w rate map
5355c42a7b7eSSam Leffler 	 * if we're switching; e.g. 11a to 11b/g.
5356c42a7b7eSSam Leffler 	 */
535768e8e04eSSam Leffler 	mode = ieee80211_chan2mode(chan);
5358c42a7b7eSSam Leffler 	if (mode != sc->sc_curmode)
5359c42a7b7eSSam Leffler 		ath_setcurmode(sc, mode);
536059efa8b5SSam Leffler 	sc->sc_curchan = chan;
5361c42a7b7eSSam Leffler }
5362c42a7b7eSSam Leffler 
5363c42a7b7eSSam Leffler /*
53645591b213SSam Leffler  * Set/change channels.  If the channel is really being changed,
53654fa8d4efSDaniel Eischen  * it's done by resetting the chip.  To accomplish this we must
53665591b213SSam Leffler  * first cleanup any pending DMA, then restart stuff after a la
53675591b213SSam Leffler  * ath_init.
53685591b213SSam Leffler  */
53695591b213SSam Leffler static int
53705591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
53715591b213SSam Leffler {
53727a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
53735591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5374ef27340cSAdrian Chadd 	int ret = 0;
5375ef27340cSAdrian Chadd 
5376ef27340cSAdrian Chadd 	/* Treat this as an interface reset */
5377d52f7132SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
5378d52f7132SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
5379d52f7132SAdrian Chadd 
5380d52f7132SAdrian Chadd 	/* (Try to) stop TX/RX from occuring */
5381d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
5382d52f7132SAdrian Chadd 
5383ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5384904e385eSAdrian Chadd 
538517bb5fd1SAdrian Chadd 	/* Disable interrupts */
538617bb5fd1SAdrian Chadd 	ath_hal_intrset(ah, 0);
538717bb5fd1SAdrian Chadd 
5388904e385eSAdrian Chadd 	/* Stop new RX/TX/interrupt completion */
5389ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
5390ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
5391ef27340cSAdrian Chadd 		    __func__);
5392ee321975SAdrian Chadd 	}
5393904e385eSAdrian Chadd 
5394904e385eSAdrian Chadd 	/* Stop pending RX/TX completion */
5395904e385eSAdrian Chadd 	ath_txrx_stop_locked(sc);
5396904e385eSAdrian Chadd 
5397ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5398c42a7b7eSSam Leffler 
539959efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz, flags 0x%x)\n",
540059efa8b5SSam Leffler 	    __func__, ieee80211_chan2ieee(ic, chan),
540159efa8b5SSam Leffler 	    chan->ic_freq, chan->ic_flags);
540259efa8b5SSam Leffler 	if (chan != sc->sc_curchan) {
5403c42a7b7eSSam Leffler 		HAL_STATUS status;
54045591b213SSam Leffler 		/*
54055591b213SSam Leffler 		 * To switch channels clear any pending DMA operations;
54065591b213SSam Leffler 		 * wait long enough for the RX fifo to drain, reset the
54075591b213SSam Leffler 		 * hardware at the new frequency, and then re-enable
54085591b213SSam Leffler 		 * the relevant bits of the h/w.
54095591b213SSam Leffler 		 */
5410ef27340cSAdrian Chadd #if 0
54115591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
5412ef27340cSAdrian Chadd #endif
54139a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* turn off frame recv */
54149a842e8bSAdrian Chadd 		/*
54159a842e8bSAdrian Chadd 		 * First, handle completed TX/RX frames.
54169a842e8bSAdrian Chadd 		 */
5417f8cc9b09SAdrian Chadd 		ath_rx_flush(sc);
54189a842e8bSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_NOLOSS);
54199a842e8bSAdrian Chadd 		/*
54209a842e8bSAdrian Chadd 		 * Next, flush the non-scheduled frames.
54219a842e8bSAdrian Chadd 		 */
5422517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_FULL);	/* clear pending tx frames */
54239a842e8bSAdrian Chadd 
54246322256bSAdrian Chadd 		ath_update_chainmasks(sc, chan);
54256322256bSAdrian Chadd 		ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
54266322256bSAdrian Chadd 		    sc->sc_cur_rxchainmask);
5427*f50e4ebfSAdrian Chadd 		if (!ath_hal_reset(ah, sc->sc_opmode, chan, AH_TRUE,
5428*f50e4ebfSAdrian Chadd 		    HAL_RESET_NORMAL, &status)) {
542976e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev, "%s: unable to reset "
543079649302SGavin Atkinson 			    "channel %u (%u MHz, flags 0x%x), hal status %u\n",
543159efa8b5SSam Leffler 			    __func__, ieee80211_chan2ieee(ic, chan),
543259efa8b5SSam Leffler 			    chan->ic_freq, chan->ic_flags, status);
5433ef27340cSAdrian Chadd 			ret = EIO;
5434ef27340cSAdrian Chadd 			goto finish;
54355591b213SSam Leffler 		}
5436c59005e9SSam Leffler 		sc->sc_diversity = ath_hal_getdiversity(ah);
5437c42a7b7eSSam Leffler 
543817bb5fd1SAdrian Chadd 		ATH_RX_LOCK(sc);
543917bb5fd1SAdrian Chadd 		sc->sc_rx_stopped = 1;
544017bb5fd1SAdrian Chadd 		sc->sc_rx_resetted = 1;
544117bb5fd1SAdrian Chadd 		ATH_RX_UNLOCK(sc);
544217bb5fd1SAdrian Chadd 
544348237774SAdrian Chadd 		/* Let DFS at it in case it's a DFS channel */
5444398bca2eSAdrian Chadd 		ath_dfs_radar_enable(sc, chan);
544548237774SAdrian Chadd 
54469af351f9SAdrian Chadd 		/* Let spectral at in case spectral is enabled */
54479af351f9SAdrian Chadd 		ath_spectral_enable(sc, chan);
54489af351f9SAdrian Chadd 
54495591b213SSam Leffler 		/*
5450b70f530bSAdrian Chadd 		 * Let bluetooth coexistence at in case it's needed for this
5451b70f530bSAdrian Chadd 		 * channel
5452b70f530bSAdrian Chadd 		 */
5453b70f530bSAdrian Chadd 		ath_btcoex_enable(sc, ic->ic_curchan);
5454b70f530bSAdrian Chadd 
5455b70f530bSAdrian Chadd 		/*
5456dd6a574eSAdrian Chadd 		 * If we're doing TDMA, enforce the TXOP limitation for chips
5457dd6a574eSAdrian Chadd 		 * that support it.
5458dd6a574eSAdrian Chadd 		 */
5459dd6a574eSAdrian Chadd 		if (sc->sc_hasenforcetxop && sc->sc_tdma)
5460dd6a574eSAdrian Chadd 			ath_hal_setenforcetxop(sc->sc_ah, 1);
5461dd6a574eSAdrian Chadd 		else
5462dd6a574eSAdrian Chadd 			ath_hal_setenforcetxop(sc->sc_ah, 0);
5463dd6a574eSAdrian Chadd 
5464dd6a574eSAdrian Chadd 		/*
54655591b213SSam Leffler 		 * Re-enable rx framework.
54665591b213SSam Leffler 		 */
54675591b213SSam Leffler 		if (ath_startrecv(sc) != 0) {
546876e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev,
546976e6fd5dSGleb Smirnoff 			    "%s: unable to restart recv logic\n", __func__);
5470ef27340cSAdrian Chadd 			ret = EIO;
5471ef27340cSAdrian Chadd 			goto finish;
54725591b213SSam Leffler 		}
54735591b213SSam Leffler 
54745591b213SSam Leffler 		/*
54755591b213SSam Leffler 		 * Change channels and update the h/w rate map
54765591b213SSam Leffler 		 * if we're switching; e.g. 11a to 11b/g.
54775591b213SSam Leffler 		 */
5478c42a7b7eSSam Leffler 		ath_chan_change(sc, chan);
54790a915fadSSam Leffler 
54800a915fadSSam Leffler 		/*
54812fd9aabbSAdrian Chadd 		 * Reset clears the beacon timers; reset them
54822fd9aabbSAdrian Chadd 		 * here if needed.
54832fd9aabbSAdrian Chadd 		 */
54842fd9aabbSAdrian Chadd 		if (sc->sc_beacons) {		/* restart beacons */
54852fd9aabbSAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
54862fd9aabbSAdrian Chadd 			if (sc->sc_tdma)
54872fd9aabbSAdrian Chadd 				ath_tdma_config(sc, NULL);
54882fd9aabbSAdrian Chadd 			else
54892fd9aabbSAdrian Chadd #endif
54902fd9aabbSAdrian Chadd 			ath_beacon_config(sc, NULL);
54912fd9aabbSAdrian Chadd 		}
54922fd9aabbSAdrian Chadd 
54932fd9aabbSAdrian Chadd 		/*
54940a915fadSSam Leffler 		 * Re-enable interrupts.
54950a915fadSSam Leffler 		 */
5496e78719adSAdrian Chadd #if 0
54970a915fadSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
5498ef27340cSAdrian Chadd #endif
54995591b213SSam Leffler 	}
5500ef27340cSAdrian Chadd 
5501ef27340cSAdrian Chadd finish:
5502ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5503ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
5504ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
5505ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
5506ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5507ef27340cSAdrian Chadd 
5508ef27340cSAdrian Chadd 	ath_txrx_start(sc);
5509ef27340cSAdrian Chadd 	/* XXX ath_start? */
5510ef27340cSAdrian Chadd 
5511ef27340cSAdrian Chadd 	return ret;
55125591b213SSam Leffler }
55135591b213SSam Leffler 
55145591b213SSam Leffler /*
55155591b213SSam Leffler  * Periodically recalibrate the PHY to account
55165591b213SSam Leffler  * for temperature/environment changes.
55175591b213SSam Leffler  */
55185591b213SSam Leffler static void
55195591b213SSam Leffler ath_calibrate(void *arg)
55205591b213SSam Leffler {
55215591b213SSam Leffler 	struct ath_softc *sc = arg;
55225591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
55237a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
5524943e37a1SAdrian Chadd 	HAL_BOOL longCal, isCalDone = AH_TRUE;
5525a108ab63SAdrian Chadd 	HAL_BOOL aniCal, shortCal = AH_FALSE;
55262dc7fcc4SSam Leffler 	int nextcal;
55275591b213SSam Leffler 
5528adcdc8f2SAdrian Chadd 	ATH_LOCK_ASSERT(sc);
55297707f31dSAdrian Chadd 
5530f5c30c4eSAdrian Chadd 	/*
5531f5c30c4eSAdrian Chadd 	 * Force the hardware awake for ANI work.
5532f5c30c4eSAdrian Chadd 	 */
5533f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
5534f5c30c4eSAdrian Chadd 
5535f5c30c4eSAdrian Chadd 	/* Skip trying to do this if we're in reset */
5536f5c30c4eSAdrian Chadd 	if (sc->sc_inreset_cnt)
5537f5c30c4eSAdrian Chadd 		goto restart;
5538f5c30c4eSAdrian Chadd 
55398d91de92SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SCAN)	/* defer, off channel */
55408d91de92SSam Leffler 		goto restart;
55412dc7fcc4SSam Leffler 	longCal = (ticks - sc->sc_lastlongcal >= ath_longcalinterval*hz);
5542a108ab63SAdrian Chadd 	aniCal = (ticks - sc->sc_lastani >= ath_anicalinterval*hz/1000);
5543a108ab63SAdrian Chadd 	if (sc->sc_doresetcal)
5544a108ab63SAdrian Chadd 		shortCal = (ticks - sc->sc_lastshortcal >= ath_shortcalinterval*hz/1000);
5545a108ab63SAdrian Chadd 
5546a108ab63SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: shortCal=%d; longCal=%d; aniCal=%d\n", __func__, shortCal, longCal, aniCal);
5547a108ab63SAdrian Chadd 	if (aniCal) {
5548a108ab63SAdrian Chadd 		sc->sc_stats.ast_ani_cal++;
5549a108ab63SAdrian Chadd 		sc->sc_lastani = ticks;
5550a108ab63SAdrian Chadd 		ath_hal_ani_poll(ah, sc->sc_curchan);
5551a108ab63SAdrian Chadd 	}
5552a108ab63SAdrian Chadd 
55532dc7fcc4SSam Leffler 	if (longCal) {
55545591b213SSam Leffler 		sc->sc_stats.ast_per_cal++;
55558197f57eSAdrian Chadd 		sc->sc_lastlongcal = ticks;
55565591b213SSam Leffler 		if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
55575591b213SSam Leffler 			/*
55585591b213SSam Leffler 			 * Rfgain is out of bounds, reset the chip
55595591b213SSam Leffler 			 * to load new gain values.
55605591b213SSam Leffler 			 */
5561370572d9SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
5562370572d9SSam Leffler 				"%s: rfgain change\n", __func__);
55635591b213SSam Leffler 			sc->sc_stats.ast_per_rfgain++;
5564ef27340cSAdrian Chadd 			sc->sc_resetcal = 0;
5565ef27340cSAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
5566d52f7132SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
5567d52f7132SAdrian Chadd 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
5568f5c30c4eSAdrian Chadd 			ath_power_restore_power_state(sc);
5569ef27340cSAdrian Chadd 			return;
55705591b213SSam Leffler 		}
55712dc7fcc4SSam Leffler 		/*
55722dc7fcc4SSam Leffler 		 * If this long cal is after an idle period, then
55732dc7fcc4SSam Leffler 		 * reset the data collection state so we start fresh.
55742dc7fcc4SSam Leffler 		 */
55752dc7fcc4SSam Leffler 		if (sc->sc_resetcal) {
557659efa8b5SSam Leffler 			(void) ath_hal_calreset(ah, sc->sc_curchan);
55772dc7fcc4SSam Leffler 			sc->sc_lastcalreset = ticks;
5578a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
55792dc7fcc4SSam Leffler 			sc->sc_resetcal = 0;
5580a108ab63SAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
55812dc7fcc4SSam Leffler 		}
55822dc7fcc4SSam Leffler 	}
5583a108ab63SAdrian Chadd 
5584a108ab63SAdrian Chadd 	/* Only call if we're doing a short/long cal, not for ANI calibration */
5585a108ab63SAdrian Chadd 	if (shortCal || longCal) {
5586943e37a1SAdrian Chadd 		isCalDone = AH_FALSE;
558759efa8b5SSam Leffler 		if (ath_hal_calibrateN(ah, sc->sc_curchan, longCal, &isCalDone)) {
55882dc7fcc4SSam Leffler 			if (longCal) {
55892dc7fcc4SSam Leffler 				/*
55902dc7fcc4SSam Leffler 				 * Calibrate noise floor data again in case of change.
55912dc7fcc4SSam Leffler 				 */
55922dc7fcc4SSam Leffler 				ath_hal_process_noisefloor(ah);
55932dc7fcc4SSam Leffler 			}
55942dc7fcc4SSam Leffler 		} else {
5595c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
5596c42a7b7eSSam Leffler 				"%s: calibration of channel %u failed\n",
559759efa8b5SSam Leffler 				__func__, sc->sc_curchan->ic_freq);
55985591b213SSam Leffler 			sc->sc_stats.ast_per_calfail++;
55995591b213SSam Leffler 		}
5600a108ab63SAdrian Chadd 		if (shortCal)
5601a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
5602a108ab63SAdrian Chadd 	}
56032dc7fcc4SSam Leffler 	if (!isCalDone) {
56048d91de92SSam Leffler restart:
56057b0c77ecSSam Leffler 		/*
56062dc7fcc4SSam Leffler 		 * Use a shorter interval to potentially collect multiple
56072dc7fcc4SSam Leffler 		 * data samples required to complete calibration.  Once
56082dc7fcc4SSam Leffler 		 * we're told the work is done we drop back to a longer
56092dc7fcc4SSam Leffler 		 * interval between requests.  We're more aggressive doing
56102dc7fcc4SSam Leffler 		 * work when operating as an AP to improve operation right
56112dc7fcc4SSam Leffler 		 * after startup.
56127b0c77ecSSam Leffler 		 */
5613a108ab63SAdrian Chadd 		sc->sc_lastshortcal = ticks;
5614a108ab63SAdrian Chadd 		nextcal = ath_shortcalinterval*hz/1000;
56152dc7fcc4SSam Leffler 		if (sc->sc_opmode != HAL_M_HOSTAP)
56162dc7fcc4SSam Leffler 			nextcal *= 10;
5617a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_TRUE;
56182dc7fcc4SSam Leffler 	} else {
5619a108ab63SAdrian Chadd 		/* nextcal should be the shortest time for next event */
56202dc7fcc4SSam Leffler 		nextcal = ath_longcalinterval*hz;
56212dc7fcc4SSam Leffler 		if (sc->sc_lastcalreset == 0)
56222dc7fcc4SSam Leffler 			sc->sc_lastcalreset = sc->sc_lastlongcal;
56232dc7fcc4SSam Leffler 		else if (ticks - sc->sc_lastcalreset >= ath_resetcalinterval*hz)
56242dc7fcc4SSam Leffler 			sc->sc_resetcal = 1;	/* setup reset next trip */
5625a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_FALSE;
5626bd5a9920SSam Leffler 	}
5627a108ab63SAdrian Chadd 	/* ANI calibration may occur more often than short/long/resetcal */
5628a108ab63SAdrian Chadd 	if (ath_anicalinterval > 0)
5629a108ab63SAdrian Chadd 		nextcal = MIN(nextcal, ath_anicalinterval*hz/1000);
5630bd5a9920SSam Leffler 
56312dc7fcc4SSam Leffler 	if (nextcal != 0) {
56322dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: next +%u (%sisCalDone)\n",
56332dc7fcc4SSam Leffler 		    __func__, nextcal, isCalDone ? "" : "!");
56342dc7fcc4SSam Leffler 		callout_reset(&sc->sc_cal_ch, nextcal, ath_calibrate, sc);
56352dc7fcc4SSam Leffler 	} else {
56362dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: calibration disabled\n",
56372dc7fcc4SSam Leffler 		    __func__);
56382dc7fcc4SSam Leffler 		/* NB: don't rearm timer */
56392dc7fcc4SSam Leffler 	}
5640f5c30c4eSAdrian Chadd 	/*
5641f5c30c4eSAdrian Chadd 	 * Restore power state now that we're done.
5642f5c30c4eSAdrian Chadd 	 */
5643f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
56445591b213SSam Leffler }
56455591b213SSam Leffler 
564668e8e04eSSam Leffler static void
564768e8e04eSSam Leffler ath_scan_start(struct ieee80211com *ic)
564868e8e04eSSam Leffler {
56493797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
565068e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
565168e8e04eSSam Leffler 	u_int32_t rfilt;
565268e8e04eSSam Leffler 
565368e8e04eSSam Leffler 	/* XXX calibration timer? */
56547a79cebfSGleb Smirnoff 	/* XXXGL: is constant ieee80211broadcastaddr a correct choice? */
565568e8e04eSSam Leffler 
5656c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
565768e8e04eSSam Leffler 	sc->sc_scanning = 1;
565868e8e04eSSam Leffler 	sc->sc_syncbeacon = 0;
565968e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5660c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
5661c98cefc5SAdrian Chadd 
5662c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
566368e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
56647a79cebfSGleb Smirnoff 	ath_hal_setassocid(ah, ieee80211broadcastaddr, 0);
5665c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
566668e8e04eSSam Leffler 
566768e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0\n",
56687a79cebfSGleb Smirnoff 		 __func__, rfilt, ether_sprintf(ieee80211broadcastaddr));
566968e8e04eSSam Leffler }
567068e8e04eSSam Leffler 
567168e8e04eSSam Leffler static void
567268e8e04eSSam Leffler ath_scan_end(struct ieee80211com *ic)
567368e8e04eSSam Leffler {
56743797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
567568e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
567668e8e04eSSam Leffler 	u_int32_t rfilt;
567768e8e04eSSam Leffler 
5678c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
567968e8e04eSSam Leffler 	sc->sc_scanning = 0;
568068e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5681c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
5682c98cefc5SAdrian Chadd 
5683c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
568468e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
568568e8e04eSSam Leffler 	ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
568668e8e04eSSam Leffler 
568768e8e04eSSam Leffler 	ath_hal_process_noisefloor(ah);
5688c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
568968e8e04eSSam Leffler 
569068e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
569168e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(sc->sc_curbssid),
569268e8e04eSSam Leffler 		 sc->sc_curaid);
569368e8e04eSSam Leffler }
569468e8e04eSSam Leffler 
5695fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
5696e7200579SAdrian Chadd /*
5697e7200579SAdrian Chadd  * For now, just do a channel change.
5698e7200579SAdrian Chadd  *
5699e7200579SAdrian Chadd  * Later, we'll go through the hard slog of suspending tx/rx, changing rate
5700e7200579SAdrian Chadd  * control state and resetting the hardware without dropping frames out
5701e7200579SAdrian Chadd  * of the queue.
5702e7200579SAdrian Chadd  *
5703e7200579SAdrian Chadd  * The unfortunate trouble here is making absolutely sure that the
5704e7200579SAdrian Chadd  * channel width change has propagated enough so the hardware
5705e7200579SAdrian Chadd  * absolutely isn't handed bogus frames for it's current operating
5706e7200579SAdrian Chadd  * mode. (Eg, 40MHz frames in 20MHz mode.) Since TX and RX can and
5707e7200579SAdrian Chadd  * does occur in parallel, we need to make certain we've blocked
5708e7200579SAdrian Chadd  * any further ongoing TX (and RX, that can cause raw TX)
5709e7200579SAdrian Chadd  * before we do this.
5710e7200579SAdrian Chadd  */
5711e7200579SAdrian Chadd static void
5712e7200579SAdrian Chadd ath_update_chw(struct ieee80211com *ic)
5713e7200579SAdrian Chadd {
57143797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
5715e7200579SAdrian Chadd 
5716e7200579SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: called\n", __func__);
5717e7200579SAdrian Chadd 	ath_set_channel(ic);
5718e7200579SAdrian Chadd }
5719fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
5720e7200579SAdrian Chadd 
572168e8e04eSSam Leffler static void
572268e8e04eSSam Leffler ath_set_channel(struct ieee80211com *ic)
572368e8e04eSSam Leffler {
57243797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
572568e8e04eSSam Leffler 
5726f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
5727f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
5728f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
5729f5c30c4eSAdrian Chadd 
573068e8e04eSSam Leffler 	(void) ath_chan_set(sc, ic->ic_curchan);
573168e8e04eSSam Leffler 	/*
573268e8e04eSSam Leffler 	 * If we are returning to our bss channel then mark state
573368e8e04eSSam Leffler 	 * so the next recv'd beacon's tsf will be used to sync the
573468e8e04eSSam Leffler 	 * beacon timers.  Note that since we only hear beacons in
573568e8e04eSSam Leffler 	 * sta/ibss mode this has no effect in other operating modes.
573668e8e04eSSam Leffler 	 */
5737a887b1e3SAdrian Chadd 	ATH_LOCK(sc);
573868e8e04eSSam Leffler 	if (!sc->sc_scanning && ic->ic_curchan == ic->ic_bsschan)
573968e8e04eSSam Leffler 		sc->sc_syncbeacon = 1;
5740f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
5741a887b1e3SAdrian Chadd 	ATH_UNLOCK(sc);
574268e8e04eSSam Leffler }
574368e8e04eSSam Leffler 
5744b032f27cSSam Leffler /*
5745b032f27cSSam Leffler  * Walk the vap list and check if there any vap's in RUN state.
5746b032f27cSSam Leffler  */
57475591b213SSam Leffler static int
5748b032f27cSSam Leffler ath_isanyrunningvaps(struct ieee80211vap *this)
57495591b213SSam Leffler {
5750b032f27cSSam Leffler 	struct ieee80211com *ic = this->iv_ic;
5751b032f27cSSam Leffler 	struct ieee80211vap *vap;
5752b032f27cSSam Leffler 
5753b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
5754b032f27cSSam Leffler 
5755b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
5756309a3e45SSam Leffler 		if (vap != this && vap->iv_state >= IEEE80211_S_RUN)
5757b032f27cSSam Leffler 			return 1;
5758b032f27cSSam Leffler 	}
5759b032f27cSSam Leffler 	return 0;
5760b032f27cSSam Leffler }
5761b032f27cSSam Leffler 
5762b032f27cSSam Leffler static int
5763b032f27cSSam Leffler ath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
5764b032f27cSSam Leffler {
5765b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
57663797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
5767b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
576845bbf62fSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5769b032f27cSSam Leffler 	struct ieee80211_node *ni = NULL;
577068e8e04eSSam Leffler 	int i, error, stamode;
57715591b213SSam Leffler 	u_int32_t rfilt;
5772f52efb6dSAdrian Chadd 	int csa_run_transition = 0;
5773f5c30c4eSAdrian Chadd 	enum ieee80211_state ostate = vap->iv_state;
5774a74ebfe5SAdrian Chadd 
57755591b213SSam Leffler 	static const HAL_LED_STATE leds[] = {
57765591b213SSam Leffler 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
57775591b213SSam Leffler 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
57785591b213SSam Leffler 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
57795591b213SSam Leffler 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
578077d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CAC */
57815591b213SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
578277d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CSA */
578377d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_SLEEP */
57845591b213SSam Leffler 	};
57855591b213SSam Leffler 
5786c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
5787f5c30c4eSAdrian Chadd 		ieee80211_state_name[ostate],
5788c42a7b7eSSam Leffler 		ieee80211_state_name[nstate]);
57895591b213SSam Leffler 
5790107fdf96SAdrian Chadd 	/*
5791107fdf96SAdrian Chadd 	 * net80211 _should_ have the comlock asserted at this point.
5792107fdf96SAdrian Chadd 	 * There are some comments around the calls to vap->iv_newstate
5793107fdf96SAdrian Chadd 	 * which indicate that it (newstate) may end up dropping the
5794107fdf96SAdrian Chadd 	 * lock.  This and the subsequent lock assert check after newstate
5795107fdf96SAdrian Chadd 	 * are an attempt to catch these and figure out how/why.
5796107fdf96SAdrian Chadd 	 */
5797107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
5798107fdf96SAdrian Chadd 
5799f5c30c4eSAdrian Chadd 	/* Before we touch the hardware - wake it up */
5800f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
58017d567ed6SAdrian Chadd 	/*
58027d567ed6SAdrian Chadd 	 * If the NIC is in anything other than SLEEP state,
58037d567ed6SAdrian Chadd 	 * we need to ensure that self-generated frames are
58047d567ed6SAdrian Chadd 	 * set for PWRMGT=0.  Otherwise we may end up with
58057d567ed6SAdrian Chadd 	 * strange situations.
58067d567ed6SAdrian Chadd 	 *
58077d567ed6SAdrian Chadd 	 * XXX TODO: is this actually the case? :-)
58087d567ed6SAdrian Chadd 	 */
58097d567ed6SAdrian Chadd 	if (nstate != IEEE80211_S_SLEEP)
58107d567ed6SAdrian Chadd 		ath_power_setselfgen(sc, HAL_PM_AWAKE);
58117d567ed6SAdrian Chadd 
58127d567ed6SAdrian Chadd 	/*
58137d567ed6SAdrian Chadd 	 * Now, wake the thing up.
58147d567ed6SAdrian Chadd 	 */
5815f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
58167707f31dSAdrian Chadd 
58177707f31dSAdrian Chadd 	/*
58187707f31dSAdrian Chadd 	 * And stop the calibration callout whilst we have
58197707f31dSAdrian Chadd 	 * ATH_LOCK held.
58207707f31dSAdrian Chadd 	 */
58217707f31dSAdrian Chadd 	callout_stop(&sc->sc_cal_ch);
5822f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
5823f5c30c4eSAdrian Chadd 
5824f5c30c4eSAdrian Chadd 	if (ostate == IEEE80211_S_CSA && nstate == IEEE80211_S_RUN)
5825f52efb6dSAdrian Chadd 		csa_run_transition = 1;
5826f52efb6dSAdrian Chadd 
58275591b213SSam Leffler 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
58285591b213SSam Leffler 
5829b032f27cSSam Leffler 	if (nstate == IEEE80211_S_SCAN) {
583058769f58SSam Leffler 		/*
5831b032f27cSSam Leffler 		 * Scanning: turn off beacon miss and don't beacon.
5832b032f27cSSam Leffler 		 * Mark beacon state so when we reach RUN state we'll
5833b032f27cSSam Leffler 		 * [re]setup beacons.  Unblock the task q thread so
5834b032f27cSSam Leffler 		 * deferred interrupt processing is done.
583558769f58SSam Leffler 		 */
5836f5c30c4eSAdrian Chadd 
5837f5c30c4eSAdrian Chadd 		/* Ensure we stay awake during scan */
5838f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
58397d567ed6SAdrian Chadd 		ath_power_setselfgen(sc, HAL_PM_AWAKE);
5840f5c30c4eSAdrian Chadd 		ath_power_setpower(sc, HAL_PM_AWAKE);
5841f5c30c4eSAdrian Chadd 		ATH_UNLOCK(sc);
5842f5c30c4eSAdrian Chadd 
5843b032f27cSSam Leffler 		ath_hal_intrset(ah,
5844b032f27cSSam Leffler 		    sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
58455591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5846b032f27cSSam Leffler 		sc->sc_beacons = 0;
5847b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
58485591b213SSam Leffler 	}
58495591b213SSam Leffler 
585080767531SAdrian Chadd 	ni = ieee80211_ref_node(vap->iv_bss);
585168e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5852b032f27cSSam Leffler 	stamode = (vap->iv_opmode == IEEE80211_M_STA ||
58537b916f89SSam Leffler 		   vap->iv_opmode == IEEE80211_M_AHDEMO ||
5854b032f27cSSam Leffler 		   vap->iv_opmode == IEEE80211_M_IBSS);
5855f5c30c4eSAdrian Chadd 
5856f5c30c4eSAdrian Chadd 	/*
5857f5c30c4eSAdrian Chadd 	 * XXX Dont need to do this (and others) if we've transitioned
5858f5c30c4eSAdrian Chadd 	 * from SLEEP->RUN.
5859f5c30c4eSAdrian Chadd 	 */
586068e8e04eSSam Leffler 	if (stamode && nstate == IEEE80211_S_RUN) {
586168e8e04eSSam Leffler 		sc->sc_curaid = ni->ni_associd;
586268e8e04eSSam Leffler 		IEEE80211_ADDR_COPY(sc->sc_curbssid, ni->ni_bssid);
5863b032f27cSSam Leffler 		ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
5864b032f27cSSam Leffler 	}
586568e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
5866b032f27cSSam Leffler 	   __func__, rfilt, ether_sprintf(sc->sc_curbssid), sc->sc_curaid);
586768e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
586868e8e04eSSam Leffler 
5869b032f27cSSam Leffler 	/* XXX is this to restore keycache on resume? */
5870b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_STA &&
5871b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY)) {
58725591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
58735591b213SSam Leffler 			if (ath_hal_keyisvalid(ah, i))
587468e8e04eSSam Leffler 				ath_hal_keysetmac(ah, i, ni->ni_bssid);
58755591b213SSam Leffler 	}
5876b032f27cSSam Leffler 
5877b032f27cSSam Leffler 	/*
5878b032f27cSSam Leffler 	 * Invoke the parent method to do net80211 work.
5879b032f27cSSam Leffler 	 */
5880b032f27cSSam Leffler 	error = avp->av_newstate(vap, nstate, arg);
5881b032f27cSSam Leffler 	if (error != 0)
5882b032f27cSSam Leffler 		goto bad;
5883c42a7b7eSSam Leffler 
5884107fdf96SAdrian Chadd 	/*
5885107fdf96SAdrian Chadd 	 * See above: ensure av_newstate() doesn't drop the lock
5886107fdf96SAdrian Chadd 	 * on us.
5887107fdf96SAdrian Chadd 	 */
5888107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
5889107fdf96SAdrian Chadd 
589068e8e04eSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
5891b032f27cSSam Leffler 		/* NB: collect bss node again, it may have changed */
589280767531SAdrian Chadd 		ieee80211_free_node(ni);
589380767531SAdrian Chadd 		ni = ieee80211_ref_node(vap->iv_bss);
58945591b213SSam Leffler 
5895b032f27cSSam Leffler 		DPRINTF(sc, ATH_DEBUG_STATE,
5896b032f27cSSam Leffler 		    "%s(RUN): iv_flags 0x%08x bintvl %d bssid %s "
5897b032f27cSSam Leffler 		    "capinfo 0x%04x chan %d\n", __func__,
5898b032f27cSSam Leffler 		    vap->iv_flags, ni->ni_intval, ether_sprintf(ni->ni_bssid),
5899b032f27cSSam Leffler 		    ni->ni_capinfo, ieee80211_chan2ieee(ic, ic->ic_curchan));
5900b032f27cSSam Leffler 
5901b032f27cSSam Leffler 		switch (vap->iv_opmode) {
5902584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
590310ad9a77SSam Leffler 		case IEEE80211_M_AHDEMO:
590410ad9a77SSam Leffler 			if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
590510ad9a77SSam Leffler 				break;
590610ad9a77SSam Leffler 			/* fall thru... */
590710ad9a77SSam Leffler #endif
5908e8fd88a3SSam Leffler 		case IEEE80211_M_HOSTAP:
5909e8fd88a3SSam Leffler 		case IEEE80211_M_IBSS:
591059aa14a9SRui Paulo 		case IEEE80211_M_MBSS:
59115591b213SSam Leffler 			/*
5912e8fd88a3SSam Leffler 			 * Allocate and setup the beacon frame.
5913e8fd88a3SSam Leffler 			 *
5914f818612bSSam Leffler 			 * Stop any previous beacon DMA.  This may be
5915f818612bSSam Leffler 			 * necessary, for example, when an ibss merge
5916f818612bSSam Leffler 			 * causes reconfiguration; there will be a state
5917f818612bSSam Leffler 			 * transition from RUN->RUN that means we may
5918f818612bSSam Leffler 			 * be called with beacon transmission active.
5919f818612bSSam Leffler 			 */
5920f818612bSSam Leffler 			ath_hal_stoptxdma(ah, sc->sc_bhalq);
5921b032f27cSSam Leffler 
59225591b213SSam Leffler 			error = ath_beacon_alloc(sc, ni);
59235591b213SSam Leffler 			if (error != 0)
59245591b213SSam Leffler 				goto bad;
59257a04dc27SSam Leffler 			/*
592680d939bfSSam Leffler 			 * If joining an adhoc network defer beacon timer
592780d939bfSSam Leffler 			 * configuration to the next beacon frame so we
592880d939bfSSam Leffler 			 * have a current TSF to use.  Otherwise we're
5929b032f27cSSam Leffler 			 * starting an ibss/bss so there's no need to delay;
5930b032f27cSSam Leffler 			 * if this is the first vap moving to RUN state, then
5931b032f27cSSam Leffler 			 * beacon state needs to be [re]configured.
59327a04dc27SSam Leffler 			 */
5933b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_IBSS &&
5934b032f27cSSam Leffler 			    ni->ni_tstamp.tsf != 0) {
593580d939bfSSam Leffler 				sc->sc_syncbeacon = 1;
5936b032f27cSSam Leffler 			} else if (!sc->sc_beacons) {
5937584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
593810ad9a77SSam Leffler 				if (vap->iv_caps & IEEE80211_C_TDMA)
593910ad9a77SSam Leffler 					ath_tdma_config(sc, vap);
594010ad9a77SSam Leffler 				else
594110ad9a77SSam Leffler #endif
5942b032f27cSSam Leffler 					ath_beacon_config(sc, vap);
5943b032f27cSSam Leffler 				sc->sc_beacons = 1;
5944b032f27cSSam Leffler 			}
5945e8fd88a3SSam Leffler 			break;
5946e8fd88a3SSam Leffler 		case IEEE80211_M_STA:
5947e8fd88a3SSam Leffler 			/*
594880d939bfSSam Leffler 			 * Defer beacon timer configuration to the next
594980d939bfSSam Leffler 			 * beacon frame so we have a current TSF to use
595080d939bfSSam Leffler 			 * (any TSF collected when scanning is likely old).
5951f52efb6dSAdrian Chadd 			 * However if it's due to a CSA -> RUN transition,
5952f52efb6dSAdrian Chadd 			 * force a beacon update so we pick up a lack of
5953f52efb6dSAdrian Chadd 			 * beacons from an AP in CAC and thus force a
5954f52efb6dSAdrian Chadd 			 * scan.
5955a74ebfe5SAdrian Chadd 			 *
5956a74ebfe5SAdrian Chadd 			 * And, there's also corner cases here where
5957a74ebfe5SAdrian Chadd 			 * after a scan, the AP may have disappeared.
5958a74ebfe5SAdrian Chadd 			 * In that case, we may not receive an actual
5959a74ebfe5SAdrian Chadd 			 * beacon to update the beacon timer and thus we
5960a74ebfe5SAdrian Chadd 			 * won't get notified of the missing beacons.
59617a04dc27SSam Leffler 			 */
5962f5c30c4eSAdrian Chadd 			if (ostate != IEEE80211_S_RUN &&
5963f5c30c4eSAdrian Chadd 			    ostate != IEEE80211_S_SLEEP) {
5964f5c30c4eSAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_BEACON,
5965f5c30c4eSAdrian Chadd 				    "%s: STA; syncbeacon=1\n", __func__);
596680d939bfSSam Leffler 				sc->sc_syncbeacon = 1;
5967f5c30c4eSAdrian Chadd 
5968f52efb6dSAdrian Chadd 				if (csa_run_transition)
5969f52efb6dSAdrian Chadd 					ath_beacon_config(sc, vap);
5970a74ebfe5SAdrian Chadd 
5971a74ebfe5SAdrian Chadd 			/*
5972a74ebfe5SAdrian Chadd 			 * PR: kern/175227
5973a74ebfe5SAdrian Chadd 			 *
5974a74ebfe5SAdrian Chadd 			 * Reconfigure beacons during reset; as otherwise
5975a74ebfe5SAdrian Chadd 			 * we won't get the beacon timers reprogrammed
5976a74ebfe5SAdrian Chadd 			 * after a reset and thus we won't pick up a
5977a74ebfe5SAdrian Chadd 			 * beacon miss interrupt.
5978a74ebfe5SAdrian Chadd 			 *
5979a74ebfe5SAdrian Chadd 			 * Hopefully we'll see a beacon before the BMISS
5980a74ebfe5SAdrian Chadd 			 * timer fires (too often), leading to a STA
5981a74ebfe5SAdrian Chadd 			 * disassociation.
5982a74ebfe5SAdrian Chadd 			 */
5983a74ebfe5SAdrian Chadd 				sc->sc_beacons = 1;
5984f5c30c4eSAdrian Chadd 			}
5985e8fd88a3SSam Leffler 			break;
5986b032f27cSSam Leffler 		case IEEE80211_M_MONITOR:
5987b032f27cSSam Leffler 			/*
5988b032f27cSSam Leffler 			 * Monitor mode vaps have only INIT->RUN and RUN->RUN
5989b032f27cSSam Leffler 			 * transitions so we must re-enable interrupts here to
5990b032f27cSSam Leffler 			 * handle the case of a single monitor mode vap.
5991b032f27cSSam Leffler 			 */
5992b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask);
5993b032f27cSSam Leffler 			break;
5994b032f27cSSam Leffler 		case IEEE80211_M_WDS:
5995b032f27cSSam Leffler 			break;
5996e8fd88a3SSam Leffler 		default:
5997e8fd88a3SSam Leffler 			break;
59985591b213SSam Leffler 		}
59995591b213SSam Leffler 		/*
60007b0c77ecSSam Leffler 		 * Let the hal process statistics collected during a
60017b0c77ecSSam Leffler 		 * scan so it can provide calibrated noise floor data.
60027b0c77ecSSam Leffler 		 */
60037b0c77ecSSam Leffler 		ath_hal_process_noisefloor(ah);
60047b0c77ecSSam Leffler 		/*
6005ffa2cab6SSam Leffler 		 * Reset rssi stats; maybe not the best place...
6006ffa2cab6SSam Leffler 		 */
6007ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
6008ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
6009ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
6010f5c30c4eSAdrian Chadd 
6011f5c30c4eSAdrian Chadd 		/*
6012f5c30c4eSAdrian Chadd 		 * Force awake for RUN mode.
6013f5c30c4eSAdrian Chadd 		 */
6014f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
60157d567ed6SAdrian Chadd 		ath_power_setselfgen(sc, HAL_PM_AWAKE);
6016f5c30c4eSAdrian Chadd 		ath_power_setpower(sc, HAL_PM_AWAKE);
6017f5c30c4eSAdrian Chadd 
601845bbf62fSSam Leffler 		/*
6019b032f27cSSam Leffler 		 * Finally, start any timers and the task q thread
6020b032f27cSSam Leffler 		 * (in case we didn't go through SCAN state).
602145bbf62fSSam Leffler 		 */
60222dc7fcc4SSam Leffler 		if (ath_longcalinterval != 0) {
6023c42a7b7eSSam Leffler 			/* start periodic recalibration timer */
60242dc7fcc4SSam Leffler 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
60252dc7fcc4SSam Leffler 		} else {
60262dc7fcc4SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
60272dc7fcc4SSam Leffler 			    "%s: calibration disabled\n", __func__);
6028c42a7b7eSSam Leffler 		}
60297707f31dSAdrian Chadd 		ATH_UNLOCK(sc);
6030f5c30c4eSAdrian Chadd 
6031b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
6032b032f27cSSam Leffler 	} else if (nstate == IEEE80211_S_INIT) {
6033b032f27cSSam Leffler 		/*
6034b032f27cSSam Leffler 		 * If there are no vaps left in RUN state then
6035b032f27cSSam Leffler 		 * shutdown host/driver operation:
6036b032f27cSSam Leffler 		 * o disable interrupts
6037b032f27cSSam Leffler 		 * o disable the task queue thread
6038b032f27cSSam Leffler 		 * o mark beacon processing as stopped
6039b032f27cSSam Leffler 		 */
6040b032f27cSSam Leffler 		if (!ath_isanyrunningvaps(vap)) {
6041b032f27cSSam Leffler 			sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
6042b032f27cSSam Leffler 			/* disable interrupts  */
6043b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL);
6044b032f27cSSam Leffler 			taskqueue_block(sc->sc_tq);
6045b032f27cSSam Leffler 			sc->sc_beacons = 0;
6046b032f27cSSam Leffler 		}
6047584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
604810ad9a77SSam Leffler 		ath_hal_setcca(ah, AH_TRUE);
604910ad9a77SSam Leffler #endif
6050f5c30c4eSAdrian Chadd 	} else if (nstate == IEEE80211_S_SLEEP) {
6051f5c30c4eSAdrian Chadd 		/* We're going to sleep, so transition appropriately */
6052f5c30c4eSAdrian Chadd 		/* For now, only do this if we're a single STA vap */
6053f5c30c4eSAdrian Chadd 		if (sc->sc_nvaps == 1 &&
6054f5c30c4eSAdrian Chadd 		    vap->iv_opmode == IEEE80211_M_STA) {
6055f5c30c4eSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_BEACON, "%s: syncbeacon=%d\n", __func__, sc->sc_syncbeacon);
6056f5c30c4eSAdrian Chadd 			ATH_LOCK(sc);
60577d567ed6SAdrian Chadd 			/*
60587d567ed6SAdrian Chadd 			 * Always at least set the self-generated
60597d567ed6SAdrian Chadd 			 * frame config to set PWRMGT=1.
60607d567ed6SAdrian Chadd 			 */
60617d567ed6SAdrian Chadd 			ath_power_setselfgen(sc, HAL_PM_NETWORK_SLEEP);
60627d567ed6SAdrian Chadd 
60637d567ed6SAdrian Chadd 			/*
60647d567ed6SAdrian Chadd 			 * If we're not syncing beacons, transition
60657d567ed6SAdrian Chadd 			 * to NETWORK_SLEEP.
60667d567ed6SAdrian Chadd 			 *
60677d567ed6SAdrian Chadd 			 * We stay awake if syncbeacon > 0 in case
60687d567ed6SAdrian Chadd 			 * we need to listen for some beacons otherwise
60697d567ed6SAdrian Chadd 			 * our beacon timer config may be wrong.
60707d567ed6SAdrian Chadd 			 */
6071f5c30c4eSAdrian Chadd 			if (sc->sc_syncbeacon == 0) {
6072f5c30c4eSAdrian Chadd 				ath_power_setpower(sc, HAL_PM_NETWORK_SLEEP);
6073f5c30c4eSAdrian Chadd 			}
6074f5c30c4eSAdrian Chadd 			ATH_UNLOCK(sc);
6075f5c30c4eSAdrian Chadd 		}
6076b032f27cSSam Leffler 	}
60775591b213SSam Leffler bad:
607880767531SAdrian Chadd 	ieee80211_free_node(ni);
6079f5c30c4eSAdrian Chadd 
6080f5c30c4eSAdrian Chadd 	/*
6081f5c30c4eSAdrian Chadd 	 * Restore the power state - either to what it was, or
6082f5c30c4eSAdrian Chadd 	 * to network_sleep if it's alright.
6083f5c30c4eSAdrian Chadd 	 */
6084f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
6085f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
6086f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
60875591b213SSam Leffler 	return error;
60885591b213SSam Leffler }
60895591b213SSam Leffler 
60905591b213SSam Leffler /*
6091e8fd88a3SSam Leffler  * Allocate a key cache slot to the station so we can
6092e8fd88a3SSam Leffler  * setup a mapping from key index to node. The key cache
6093e8fd88a3SSam Leffler  * slot is needed for managing antenna state and for
6094e8fd88a3SSam Leffler  * compression when stations do not use crypto.  We do
6095e8fd88a3SSam Leffler  * it uniliaterally here; if crypto is employed this slot
6096e8fd88a3SSam Leffler  * will be reassigned.
6097e8fd88a3SSam Leffler  */
6098e8fd88a3SSam Leffler static void
6099e8fd88a3SSam Leffler ath_setup_stationkey(struct ieee80211_node *ni)
6100e8fd88a3SSam Leffler {
6101b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
61023797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
6103c1225b52SSam Leffler 	ieee80211_keyix keyix, rxkeyix;
6104e8fd88a3SSam Leffler 
610580767531SAdrian Chadd 	/* XXX should take a locked ref to vap->iv_bss */
6106b032f27cSSam Leffler 	if (!ath_key_alloc(vap, &ni->ni_ucastkey, &keyix, &rxkeyix)) {
6107e8fd88a3SSam Leffler 		/*
6108e8fd88a3SSam Leffler 		 * Key cache is full; we'll fall back to doing
6109e8fd88a3SSam Leffler 		 * the more expensive lookup in software.  Note
6110e8fd88a3SSam Leffler 		 * this also means no h/w compression.
6111e8fd88a3SSam Leffler 		 */
6112e8fd88a3SSam Leffler 		/* XXX msg+statistic */
6113e8fd88a3SSam Leffler 	} else {
6114c1225b52SSam Leffler 		/* XXX locking? */
6115e8fd88a3SSam Leffler 		ni->ni_ucastkey.wk_keyix = keyix;
6116c1225b52SSam Leffler 		ni->ni_ucastkey.wk_rxkeyix = rxkeyix;
611733052833SSam Leffler 		/* NB: must mark device key to get called back on delete */
611833052833SSam Leffler 		ni->ni_ucastkey.wk_flags |= IEEE80211_KEY_DEVKEY;
6119d3ac945bSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_ucastkey.wk_macaddr, ni->ni_macaddr);
6120e8fd88a3SSam Leffler 		/* NB: this will create a pass-thru key entry */
612155c7b877SAdrian Chadd 		ath_keyset(sc, vap, &ni->ni_ucastkey, vap->iv_bss);
6122e8fd88a3SSam Leffler 	}
6123e8fd88a3SSam Leffler }
6124e8fd88a3SSam Leffler 
6125e8fd88a3SSam Leffler /*
61265591b213SSam Leffler  * Setup driver-specific state for a newly associated node.
61275591b213SSam Leffler  * Note that we're called also on a re-associate, the isnew
61285591b213SSam Leffler  * param tells us if this is the first time or not.
61295591b213SSam Leffler  */
61305591b213SSam Leffler static void
6131e9962332SSam Leffler ath_newassoc(struct ieee80211_node *ni, int isnew)
61325591b213SSam Leffler {
6133b032f27cSSam Leffler 	struct ath_node *an = ATH_NODE(ni);
6134b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
61353797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
6136c62362cbSSam Leffler 	const struct ieee80211_txparam *tp = ni->ni_txparms;
61375591b213SSam Leffler 
6138ab06fdf2SSam Leffler 	an->an_mcastrix = ath_tx_findrix(sc, tp->mcastrate);
6139ab06fdf2SSam Leffler 	an->an_mgmtrix = ath_tx_findrix(sc, tp->mgmtrate);
6140b032f27cSSam Leffler 
6141f5c30c4eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: reassoc; isnew=%d, is_powersave=%d\n",
6142f5c30c4eSAdrian Chadd 	    __func__,
6143f5c30c4eSAdrian Chadd 	    ni->ni_macaddr,
6144f5c30c4eSAdrian Chadd 	    ":",
6145f5c30c4eSAdrian Chadd 	    isnew,
6146f5c30c4eSAdrian Chadd 	    an->an_is_powersave);
6147f5c30c4eSAdrian Chadd 
6148656380e7SAdrian Chadd 	ATH_NODE_LOCK(an);
6149b032f27cSSam Leffler 	ath_rate_newassoc(sc, an, isnew);
6150656380e7SAdrian Chadd 	ATH_NODE_UNLOCK(an);
615132da86a0SAdrian Chadd 
6152e8fd88a3SSam Leffler 	if (isnew &&
6153b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey &&
6154b032f27cSSam Leffler 	    ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE)
6155e8fd88a3SSam Leffler 		ath_setup_stationkey(ni);
61564bed2b67SAdrian Chadd 
61574bed2b67SAdrian Chadd 	/*
61584bed2b67SAdrian Chadd 	 * If we're reassociating, make sure that any paused queues
61594bed2b67SAdrian Chadd 	 * get unpaused.
61604bed2b67SAdrian Chadd 	 *
61614bed2b67SAdrian Chadd 	 * Now, we may hvae frames in the hardware queue for this node.
61624bed2b67SAdrian Chadd 	 * So if we are reassociating and there are frames in the queue,
61634bed2b67SAdrian Chadd 	 * we need to go through the cleanup path to ensure that they're
61644bed2b67SAdrian Chadd 	 * marked as non-aggregate.
61654bed2b67SAdrian Chadd 	 */
61664bed2b67SAdrian Chadd 	if (! isnew) {
616732da86a0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE,
61684bed2b67SAdrian Chadd 		    "%s: %6D: reassoc; is_powersave=%d\n",
61694bed2b67SAdrian Chadd 		    __func__,
61704bed2b67SAdrian Chadd 		    ni->ni_macaddr,
61714bed2b67SAdrian Chadd 		    ":",
61724bed2b67SAdrian Chadd 		    an->an_is_powersave);
61734bed2b67SAdrian Chadd 
61744bed2b67SAdrian Chadd 		/* XXX for now, we can't hold the lock across assoc */
61754bed2b67SAdrian Chadd 		ath_tx_node_reassoc(sc, an);
61764bed2b67SAdrian Chadd 
61774bed2b67SAdrian Chadd 		/* XXX for now, we can't hold the lock across wakeup */
61784bed2b67SAdrian Chadd 		if (an->an_is_powersave)
61794bed2b67SAdrian Chadd 			ath_tx_node_wakeup(sc, an);
61804bed2b67SAdrian Chadd 	}
6181e8fd88a3SSam Leffler }
61825591b213SSam Leffler 
61835591b213SSam Leffler static int
618459efa8b5SSam Leffler ath_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *reg,
6185b032f27cSSam Leffler 	int nchans, struct ieee80211_channel chans[])
6186b032f27cSSam Leffler {
61873797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
6188b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
618959efa8b5SSam Leffler 	HAL_STATUS status;
6190b032f27cSSam Leffler 
6191033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
619259efa8b5SSam Leffler 	    "%s: rd %u cc %u location %c%s\n",
619359efa8b5SSam Leffler 	    __func__, reg->regdomain, reg->country, reg->location,
619459efa8b5SSam Leffler 	    reg->ecm ? " ecm" : "");
6195033022a9SSam Leffler 
619659efa8b5SSam Leffler 	status = ath_hal_set_channels(ah, chans, nchans,
619759efa8b5SSam Leffler 	    reg->country, reg->regdomain);
619859efa8b5SSam Leffler 	if (status != HAL_OK) {
619959efa8b5SSam Leffler 		DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: failed, status %u\n",
620059efa8b5SSam Leffler 		    __func__, status);
620159efa8b5SSam Leffler 		return EINVAL;		/* XXX */
6202b032f27cSSam Leffler 	}
62038db87e40SAdrian Chadd 
6204b032f27cSSam Leffler 	return 0;
6205b032f27cSSam Leffler }
6206b032f27cSSam Leffler 
6207b032f27cSSam Leffler static void
6208b032f27cSSam Leffler ath_getradiocaps(struct ieee80211com *ic,
62095fe9f044SSam Leffler 	int maxchans, int *nchans, struct ieee80211_channel chans[])
6210b032f27cSSam Leffler {
62113797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
6212b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6213b032f27cSSam Leffler 
621459efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: use rd %u cc %d\n",
621559efa8b5SSam Leffler 	    __func__, SKU_DEBUG, CTRY_DEFAULT);
6216033022a9SSam Leffler 
621759efa8b5SSam Leffler 	/* XXX check return */
621859efa8b5SSam Leffler 	(void) ath_hal_getchannels(ah, chans, maxchans, nchans,
621959efa8b5SSam Leffler 	    HAL_MODE_ALL, CTRY_DEFAULT, SKU_DEBUG, AH_TRUE);
6220033022a9SSam Leffler 
6221b032f27cSSam Leffler }
6222b032f27cSSam Leffler 
6223b032f27cSSam Leffler static int
6224b032f27cSSam Leffler ath_getchannels(struct ath_softc *sc)
6225b032f27cSSam Leffler {
62267a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
6227b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
622859efa8b5SSam Leffler 	HAL_STATUS status;
6229b032f27cSSam Leffler 
6230b032f27cSSam Leffler 	/*
623159efa8b5SSam Leffler 	 * Collect channel set based on EEPROM contents.
6232b032f27cSSam Leffler 	 */
623359efa8b5SSam Leffler 	status = ath_hal_init_channels(ah, ic->ic_channels, IEEE80211_CHAN_MAX,
623459efa8b5SSam Leffler 	    &ic->ic_nchans, HAL_MODE_ALL, CTRY_DEFAULT, SKU_NONE, AH_TRUE);
623559efa8b5SSam Leffler 	if (status != HAL_OK) {
623676e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
623776e6fd5dSGleb Smirnoff 		    "%s: unable to collect channel list from hal, status %d\n",
623876e6fd5dSGleb Smirnoff 		    __func__, status);
623959efa8b5SSam Leffler 		return EINVAL;
624059efa8b5SSam Leffler 	}
6241ca876918SSam Leffler 	(void) ath_hal_getregdomain(ah, &sc->sc_eerd);
6242ca876918SSam Leffler 	ath_hal_getcountrycode(ah, &sc->sc_eecc);	/* NB: cannot fail */
624359efa8b5SSam Leffler 	/* XXX map Atheros sku's to net80211 SKU's */
624459efa8b5SSam Leffler 	/* XXX net80211 types too small */
624559efa8b5SSam Leffler 	ic->ic_regdomain.regdomain = (uint16_t) sc->sc_eerd;
624659efa8b5SSam Leffler 	ic->ic_regdomain.country = (uint16_t) sc->sc_eecc;
624759efa8b5SSam Leffler 	ic->ic_regdomain.isocc[0] = ' ';	/* XXX don't know */
624859efa8b5SSam Leffler 	ic->ic_regdomain.isocc[1] = ' ';
624959efa8b5SSam Leffler 
6250b032f27cSSam Leffler 	ic->ic_regdomain.ecm = 1;
6251b032f27cSSam Leffler 	ic->ic_regdomain.location = 'I';
6252033022a9SSam Leffler 
6253033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
625459efa8b5SSam Leffler 	    "%s: eeprom rd %u cc %u (mapped rd %u cc %u) location %c%s\n",
6255033022a9SSam Leffler 	    __func__, sc->sc_eerd, sc->sc_eecc,
6256033022a9SSam Leffler 	    ic->ic_regdomain.regdomain, ic->ic_regdomain.country,
625759efa8b5SSam Leffler 	    ic->ic_regdomain.location, ic->ic_regdomain.ecm ? " ecm" : "");
62585591b213SSam Leffler 	return 0;
62595591b213SSam Leffler }
62605591b213SSam Leffler 
62616c4612b9SSam Leffler static int
62626c4612b9SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode)
62636c4612b9SSam Leffler {
62646c4612b9SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
62656c4612b9SSam Leffler 	const HAL_RATE_TABLE *rt;
62666c4612b9SSam Leffler 
62676c4612b9SSam Leffler 	switch (mode) {
62686c4612b9SSam Leffler 	case IEEE80211_MODE_11A:
62696c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A);
62706c4612b9SSam Leffler 		break;
6271724c193aSSam Leffler 	case IEEE80211_MODE_HALF:
6272aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_HALF_RATE);
6273aaa70f2fSSam Leffler 		break;
6274724c193aSSam Leffler 	case IEEE80211_MODE_QUARTER:
6275aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_QUARTER_RATE);
6276aaa70f2fSSam Leffler 		break;
62776c4612b9SSam Leffler 	case IEEE80211_MODE_11B:
62786c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11B);
62796c4612b9SSam Leffler 		break;
62806c4612b9SSam Leffler 	case IEEE80211_MODE_11G:
62816c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11G);
62826c4612b9SSam Leffler 		break;
62836c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_A:
628468e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108A);
62856c4612b9SSam Leffler 		break;
62866c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_G:
62876c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108G);
62886c4612b9SSam Leffler 		break;
628968e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
629068e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_TURBO);
629168e8e04eSSam Leffler 		break;
629268e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
629368e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NA_HT20);
629468e8e04eSSam Leffler 		break;
629568e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
629668e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NG_HT20);
629768e8e04eSSam Leffler 		break;
62986c4612b9SSam Leffler 	default:
62996c4612b9SSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
63006c4612b9SSam Leffler 			__func__, mode);
63016c4612b9SSam Leffler 		return 0;
63026c4612b9SSam Leffler 	}
63036c4612b9SSam Leffler 	sc->sc_rates[mode] = rt;
6304aaa70f2fSSam Leffler 	return (rt != NULL);
63055591b213SSam Leffler }
63065591b213SSam Leffler 
63075591b213SSam Leffler static void
63085591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
63095591b213SSam Leffler {
63103e50ec2cSSam Leffler 	/* NB: on/off times from the Atheros NDIS driver, w/ permission */
63113e50ec2cSSam Leffler 	static const struct {
63123e50ec2cSSam Leffler 		u_int		rate;		/* tx/rx 802.11 rate */
63133e50ec2cSSam Leffler 		u_int16_t	timeOn;		/* LED on time (ms) */
63143e50ec2cSSam Leffler 		u_int16_t	timeOff;	/* LED off time (ms) */
63153e50ec2cSSam Leffler 	} blinkrates[] = {
63163e50ec2cSSam Leffler 		{ 108,  40,  10 },
63173e50ec2cSSam Leffler 		{  96,  44,  11 },
63183e50ec2cSSam Leffler 		{  72,  50,  13 },
63193e50ec2cSSam Leffler 		{  48,  57,  14 },
63203e50ec2cSSam Leffler 		{  36,  67,  16 },
63213e50ec2cSSam Leffler 		{  24,  80,  20 },
63223e50ec2cSSam Leffler 		{  22, 100,  25 },
63233e50ec2cSSam Leffler 		{  18, 133,  34 },
63243e50ec2cSSam Leffler 		{  12, 160,  40 },
63253e50ec2cSSam Leffler 		{  10, 200,  50 },
63263e50ec2cSSam Leffler 		{   6, 240,  58 },
63273e50ec2cSSam Leffler 		{   4, 267,  66 },
63283e50ec2cSSam Leffler 		{   2, 400, 100 },
63293e50ec2cSSam Leffler 		{   0, 500, 130 },
6330724c193aSSam Leffler 		/* XXX half/quarter rates */
63313e50ec2cSSam Leffler 	};
63325591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
63333e50ec2cSSam Leffler 	int i, j;
63345591b213SSam Leffler 
63355591b213SSam Leffler 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
63365591b213SSam Leffler 	rt = sc->sc_rates[mode];
63375591b213SSam Leffler 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
6338180f268dSSam Leffler 	for (i = 0; i < rt->rateCount; i++) {
6339180f268dSSam Leffler 		uint8_t ieeerate = rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
6340180f268dSSam Leffler 		if (rt->info[i].phy != IEEE80211_T_HT)
6341180f268dSSam Leffler 			sc->sc_rixmap[ieeerate] = i;
6342180f268dSSam Leffler 		else
6343180f268dSSam Leffler 			sc->sc_rixmap[ieeerate | IEEE80211_RATE_MCS] = i;
6344180f268dSSam Leffler 	}
63451b1a8e41SSam Leffler 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
6346d6166defSAdrian Chadd 	for (i = 0; i < nitems(sc->sc_hwmap); i++) {
634746d4d74cSSam Leffler 		if (i >= rt->rateCount) {
63483e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledon = (500 * hz) / 1000;
63493e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledoff = (130 * hz) / 1000;
635016b4851aSSam Leffler 			continue;
63513e50ec2cSSam Leffler 		}
63523e50ec2cSSam Leffler 		sc->sc_hwmap[i].ieeerate =
635346d4d74cSSam Leffler 			rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
635446d4d74cSSam Leffler 		if (rt->info[i].phy == IEEE80211_T_HT)
635526041a14SSam Leffler 			sc->sc_hwmap[i].ieeerate |= IEEE80211_RATE_MCS;
6356d3be6f5bSSam Leffler 		sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
635746d4d74cSSam Leffler 		if (rt->info[i].shortPreamble ||
635846d4d74cSSam Leffler 		    rt->info[i].phy == IEEE80211_T_OFDM)
6359d3be6f5bSSam Leffler 			sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE;
63605463c4a4SSam Leffler 		sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags;
6361d6166defSAdrian Chadd 		for (j = 0; j < nitems(blinkrates)-1; j++)
63623e50ec2cSSam Leffler 			if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate)
63633e50ec2cSSam Leffler 				break;
63643e50ec2cSSam Leffler 		/* NB: this uses the last entry if the rate isn't found */
63653e50ec2cSSam Leffler 		/* XXX beware of overlow */
63663e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000;
63673e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000;
6368c42a7b7eSSam Leffler 	}
63695591b213SSam Leffler 	sc->sc_currates = rt;
63705591b213SSam Leffler 	sc->sc_curmode = mode;
63715591b213SSam Leffler 	/*
6372c42a7b7eSSam Leffler 	 * All protection frames are transmited at 2Mb/s for
6373c42a7b7eSSam Leffler 	 * 11g, otherwise at 1Mb/s.
63745591b213SSam Leffler 	 */
6375913a1ba1SSam Leffler 	if (mode == IEEE80211_MODE_11G)
6376ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*2);
6377913a1ba1SSam Leffler 	else
6378ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*1);
63794fa8d4efSDaniel Eischen 	/* NB: caller is responsible for resetting rate control state */
63805591b213SSam Leffler }
63815591b213SSam Leffler 
6382c42a7b7eSSam Leffler static void
63832e986da5SSam Leffler ath_watchdog(void *arg)
6384c42a7b7eSSam Leffler {
63852e986da5SSam Leffler 	struct ath_softc *sc = arg;
63867a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
6387ef27340cSAdrian Chadd 	int do_reset = 0;
6388c42a7b7eSSam Leffler 
6389adcdc8f2SAdrian Chadd 	ATH_LOCK_ASSERT(sc);
63907707f31dSAdrian Chadd 
63912e986da5SSam Leffler 	if (sc->sc_wd_timer != 0 && --sc->sc_wd_timer == 0) {
6392459bc4f0SSam Leffler 		uint32_t hangs;
6393459bc4f0SSam Leffler 
6394f5c30c4eSAdrian Chadd 		ath_power_set_power_state(sc, HAL_PM_AWAKE);
6395f5c30c4eSAdrian Chadd 
6396459bc4f0SSam Leffler 		if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) &&
6397459bc4f0SSam Leffler 		    hangs != 0) {
639876e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev, "%s hang detected (0x%x)\n",
6399459bc4f0SSam Leffler 			    hangs & 0xff ? "bb" : "mac", hangs);
6400459bc4f0SSam Leffler 		} else
640176e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev, "device timeout\n");
6402ef27340cSAdrian Chadd 		do_reset = 1;
64037a79cebfSGleb Smirnoff 		counter_u64_add(ic->ic_oerrors, 1);
6404c42a7b7eSSam Leffler 		sc->sc_stats.ast_watchdog++;
6405f5c30c4eSAdrian Chadd 
6406f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
6407c42a7b7eSSam Leffler 	}
6408ef27340cSAdrian Chadd 
6409ef27340cSAdrian Chadd 	/*
6410ef27340cSAdrian Chadd 	 * We can't hold the lock across the ath_reset() call.
6411d52f7132SAdrian Chadd 	 *
6412d52f7132SAdrian Chadd 	 * And since this routine can't hold a lock and sleep,
6413d52f7132SAdrian Chadd 	 * do the reset deferred.
6414ef27340cSAdrian Chadd 	 */
6415ef27340cSAdrian Chadd 	if (do_reset) {
6416d52f7132SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
6417ef27340cSAdrian Chadd 	}
6418ef27340cSAdrian Chadd 
64192e986da5SSam Leffler 	callout_schedule(&sc->sc_wd_ch, hz);
6420c42a7b7eSSam Leffler }
6421c42a7b7eSSam Leffler 
6422b8f2a853SAdrian Chadd /*
6423b8f2a853SAdrian Chadd  * Fetch the rate control statistics for the given node.
6424b8f2a853SAdrian Chadd  */
6425b8f2a853SAdrian Chadd static int
6426b8f2a853SAdrian Chadd ath_ioctl_ratestats(struct ath_softc *sc, struct ath_rateioctl *rs)
6427b8f2a853SAdrian Chadd {
6428b8f2a853SAdrian Chadd 	struct ath_node *an;
64297a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
6430b8f2a853SAdrian Chadd 	struct ieee80211_node *ni;
6431b8f2a853SAdrian Chadd 	int error = 0;
6432b8f2a853SAdrian Chadd 
6433b8f2a853SAdrian Chadd 	/* Perform a lookup on the given node */
6434b8f2a853SAdrian Chadd 	ni = ieee80211_find_node(&ic->ic_sta, rs->is_u.macaddr);
6435b8f2a853SAdrian Chadd 	if (ni == NULL) {
6436b8f2a853SAdrian Chadd 		error = EINVAL;
6437b8f2a853SAdrian Chadd 		goto bad;
6438b8f2a853SAdrian Chadd 	}
6439b8f2a853SAdrian Chadd 
6440b8f2a853SAdrian Chadd 	/* Lock the ath_node */
6441b8f2a853SAdrian Chadd 	an = ATH_NODE(ni);
6442b8f2a853SAdrian Chadd 	ATH_NODE_LOCK(an);
6443b8f2a853SAdrian Chadd 
6444b8f2a853SAdrian Chadd 	/* Fetch the rate control stats for this node */
6445b8f2a853SAdrian Chadd 	error = ath_rate_fetch_node_stats(sc, an, rs);
6446b8f2a853SAdrian Chadd 
6447b8f2a853SAdrian Chadd 	/* No matter what happens here, just drop through */
6448b8f2a853SAdrian Chadd 
6449b8f2a853SAdrian Chadd 	/* Unlock the ath_node */
6450b8f2a853SAdrian Chadd 	ATH_NODE_UNLOCK(an);
6451b8f2a853SAdrian Chadd 
6452b8f2a853SAdrian Chadd 	/* Unref the node */
6453b8f2a853SAdrian Chadd 	ieee80211_node_decref(ni);
6454b8f2a853SAdrian Chadd 
6455b8f2a853SAdrian Chadd bad:
6456b8f2a853SAdrian Chadd 	return (error);
6457b8f2a853SAdrian Chadd }
6458b8f2a853SAdrian Chadd 
6459a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
6460c42a7b7eSSam Leffler /*
6461c42a7b7eSSam Leffler  * Diagnostic interface to the HAL.  This is used by various
6462c42a7b7eSSam Leffler  * tools to do things like retrieve register contents for
6463c42a7b7eSSam Leffler  * debugging.  The mechanism is intentionally opaque so that
6464c42a7b7eSSam Leffler  * it can change frequently w/o concern for compatiblity.
6465c42a7b7eSSam Leffler  */
6466c42a7b7eSSam Leffler static int
6467c42a7b7eSSam Leffler ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
6468c42a7b7eSSam Leffler {
6469c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6470c42a7b7eSSam Leffler 	u_int id = ad->ad_id & ATH_DIAG_ID;
6471c42a7b7eSSam Leffler 	void *indata = NULL;
6472c42a7b7eSSam Leffler 	void *outdata = NULL;
6473c42a7b7eSSam Leffler 	u_int32_t insize = ad->ad_in_size;
6474c42a7b7eSSam Leffler 	u_int32_t outsize = ad->ad_out_size;
6475c42a7b7eSSam Leffler 	int error = 0;
6476c42a7b7eSSam Leffler 
6477c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_IN) {
6478c42a7b7eSSam Leffler 		/*
6479c42a7b7eSSam Leffler 		 * Copy in data.
6480c42a7b7eSSam Leffler 		 */
6481c42a7b7eSSam Leffler 		indata = malloc(insize, M_TEMP, M_NOWAIT);
6482c42a7b7eSSam Leffler 		if (indata == NULL) {
6483c42a7b7eSSam Leffler 			error = ENOMEM;
6484c42a7b7eSSam Leffler 			goto bad;
6485c42a7b7eSSam Leffler 		}
6486c42a7b7eSSam Leffler 		error = copyin(ad->ad_in_data, indata, insize);
6487c42a7b7eSSam Leffler 		if (error)
6488c42a7b7eSSam Leffler 			goto bad;
6489c42a7b7eSSam Leffler 	}
6490c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_DYN) {
6491c42a7b7eSSam Leffler 		/*
6492c42a7b7eSSam Leffler 		 * Allocate a buffer for the results (otherwise the HAL
6493c42a7b7eSSam Leffler 		 * returns a pointer to a buffer where we can read the
6494c42a7b7eSSam Leffler 		 * results).  Note that we depend on the HAL leaving this
6495c42a7b7eSSam Leffler 		 * pointer for us to use below in reclaiming the buffer;
6496c42a7b7eSSam Leffler 		 * may want to be more defensive.
6497c42a7b7eSSam Leffler 		 */
6498c42a7b7eSSam Leffler 		outdata = malloc(outsize, M_TEMP, M_NOWAIT);
6499c42a7b7eSSam Leffler 		if (outdata == NULL) {
6500c42a7b7eSSam Leffler 			error = ENOMEM;
6501c42a7b7eSSam Leffler 			goto bad;
6502c42a7b7eSSam Leffler 		}
6503c42a7b7eSSam Leffler 	}
6504f5c30c4eSAdrian Chadd 
6505f5c30c4eSAdrian Chadd 
6506f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
6507f5c30c4eSAdrian Chadd 	if (id != HAL_DIAG_REGS)
6508f5c30c4eSAdrian Chadd 		ath_power_set_power_state(sc, HAL_PM_AWAKE);
6509f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
6510f5c30c4eSAdrian Chadd 
6511c42a7b7eSSam Leffler 	if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) {
6512c42a7b7eSSam Leffler 		if (outsize < ad->ad_out_size)
6513c42a7b7eSSam Leffler 			ad->ad_out_size = outsize;
6514c42a7b7eSSam Leffler 		if (outdata != NULL)
6515c42a7b7eSSam Leffler 			error = copyout(outdata, ad->ad_out_data,
6516c42a7b7eSSam Leffler 					ad->ad_out_size);
6517c42a7b7eSSam Leffler 	} else {
6518c42a7b7eSSam Leffler 		error = EINVAL;
6519c42a7b7eSSam Leffler 	}
6520f5c30c4eSAdrian Chadd 
6521f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
6522f5c30c4eSAdrian Chadd 	if (id != HAL_DIAG_REGS)
6523f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
6524f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
6525f5c30c4eSAdrian Chadd 
6526c42a7b7eSSam Leffler bad:
6527c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
6528c42a7b7eSSam Leffler 		free(indata, M_TEMP);
6529c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
6530c42a7b7eSSam Leffler 		free(outdata, M_TEMP);
6531c42a7b7eSSam Leffler 	return error;
6532c42a7b7eSSam Leffler }
6533a585a9a1SSam Leffler #endif /* ATH_DIAGAPI */
6534c42a7b7eSSam Leffler 
65357a79cebfSGleb Smirnoff static void
65367a79cebfSGleb Smirnoff ath_parent(struct ieee80211com *ic)
6537c42a7b7eSSam Leffler {
65383797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
65397a79cebfSGleb Smirnoff 	int error = EDOOFUS;
6540c42a7b7eSSam Leffler 
65417a79cebfSGleb Smirnoff 	ATH_LOCK(sc);
65427a79cebfSGleb Smirnoff 	if (ic->ic_nrunning > 0) {
6543c42a7b7eSSam Leffler 		/*
6544c42a7b7eSSam Leffler 		 * To avoid rescanning another access point,
6545c42a7b7eSSam Leffler 		 * do not call ath_init() here.  Instead,
6546c42a7b7eSSam Leffler 		 * only reflect promisc mode settings.
6547c42a7b7eSSam Leffler 		 */
65487a79cebfSGleb Smirnoff 		if (sc->sc_running) {
65494b734a1cSAdrian Chadd 			ath_power_set_power_state(sc, HAL_PM_AWAKE);
6550c42a7b7eSSam Leffler 			ath_mode_init(sc);
65514b734a1cSAdrian Chadd 			ath_power_restore_power_state(sc);
65527a79cebfSGleb Smirnoff 		} else if (!sc->sc_invalid) {
6553c42a7b7eSSam Leffler 			/*
6554c42a7b7eSSam Leffler 			 * Beware of being called during attach/detach
6555c42a7b7eSSam Leffler 			 * to reset promiscuous mode.  In that case we
6556c42a7b7eSSam Leffler 			 * will still be marked UP but not RUNNING.
6557c42a7b7eSSam Leffler 			 * However trying to re-init the interface
6558c42a7b7eSSam Leffler 			 * is the wrong thing to do as we've already
6559c42a7b7eSSam Leffler 			 * torn down much of our state.  There's
6560c42a7b7eSSam Leffler 			 * probably a better way to deal with this.
6561c42a7b7eSSam Leffler 			 */
65627a79cebfSGleb Smirnoff 			error = ath_init(sc);
65637a79cebfSGleb Smirnoff 		}
6564d3ac945bSSam Leffler 	} else {
65657a79cebfSGleb Smirnoff 		ath_stop(sc);
6566d3ac945bSSam Leffler 		if (!sc->sc_invalid)
6567f5c30c4eSAdrian Chadd 			ath_power_setpower(sc, HAL_PM_FULL_SLEEP);
6568410302ebSAdrian Chadd 	}
65697a79cebfSGleb Smirnoff 	ATH_UNLOCK(sc);
65707a79cebfSGleb Smirnoff 
65717a79cebfSGleb Smirnoff 	if (error == 0) {
65727a79cebfSGleb Smirnoff #ifdef ATH_TX99_DIAG
65737a79cebfSGleb Smirnoff 		if (sc->sc_tx99 != NULL)
65747a79cebfSGleb Smirnoff 			sc->sc_tx99->start(sc->sc_tx99);
65757a79cebfSGleb Smirnoff 		else
65767a79cebfSGleb Smirnoff #endif
65777a79cebfSGleb Smirnoff 		ieee80211_start_all(ic);
65787a79cebfSGleb Smirnoff 	}
65797a79cebfSGleb Smirnoff }
65807a79cebfSGleb Smirnoff 
65817a79cebfSGleb Smirnoff static int
65827a79cebfSGleb Smirnoff ath_ioctl(struct ieee80211com *ic, u_long cmd, void *data)
65837a79cebfSGleb Smirnoff {
65847a79cebfSGleb Smirnoff 	struct ifreq *ifr = data;
65857a79cebfSGleb Smirnoff 	struct ath_softc *sc = ic->ic_softc;
65867a79cebfSGleb Smirnoff 
65877a79cebfSGleb Smirnoff 	switch (cmd) {
65887a79cebfSGleb Smirnoff 	case SIOCGATHSTATS: {
65897a79cebfSGleb Smirnoff 		struct ieee80211vap *vap;
65907a79cebfSGleb Smirnoff 		struct ifnet *ifp;
65917a79cebfSGleb Smirnoff 		const HAL_RATE_TABLE *rt;
65927a79cebfSGleb Smirnoff 
6593c42a7b7eSSam Leffler 		/* NB: embed these numbers to get a consistent view */
65947a79cebfSGleb Smirnoff 		sc->sc_stats.ast_tx_packets = 0;
65957a79cebfSGleb Smirnoff 		sc->sc_stats.ast_rx_packets = 0;
65967a79cebfSGleb Smirnoff 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
65977a79cebfSGleb Smirnoff 			ifp = vap->iv_ifp;
65987a79cebfSGleb Smirnoff 			sc->sc_stats.ast_tx_packets += ifp->if_get_counter(ifp,
65992127b2e2SGleb Smirnoff 			    IFCOUNTER_OPACKETS);
66007a79cebfSGleb Smirnoff 			sc->sc_stats.ast_rx_packets += ifp->if_get_counter(ifp,
66012127b2e2SGleb Smirnoff 			    IFCOUNTER_IPACKETS);
66027a79cebfSGleb Smirnoff 		}
660384784be1SSam Leffler 		sc->sc_stats.ast_tx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgtxrssi);
660484784be1SSam Leffler 		sc->sc_stats.ast_rx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgrssi);
6605584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
660610ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsfadjp = TDMA_AVG(sc->sc_avgtsfdeltap);
660710ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsfadjm = TDMA_AVG(sc->sc_avgtsfdeltam);
660810ad9a77SSam Leffler #endif
660984784be1SSam Leffler 		rt = sc->sc_currates;
661046d4d74cSSam Leffler 		sc->sc_stats.ast_tx_rate =
661146d4d74cSSam Leffler 		    rt->info[sc->sc_txrix].dot11Rate &~ IEEE80211_RATE_BASIC;
66126aa113fdSAdrian Chadd 		if (rt->info[sc->sc_txrix].phy & IEEE80211_T_HT)
66136aa113fdSAdrian Chadd 			sc->sc_stats.ast_tx_rate |= IEEE80211_RATE_MCS;
6614c42a7b7eSSam Leffler 		return copyout(&sc->sc_stats,
6615c42a7b7eSSam Leffler 		    ifr->ifr_data, sizeof (sc->sc_stats));
66167a79cebfSGleb Smirnoff 	}
661794fe37d2SAdrian Chadd 	case SIOCGATHAGSTATS:
661894fe37d2SAdrian Chadd 		return copyout(&sc->sc_aggr_stats,
661994fe37d2SAdrian Chadd 		    ifr->ifr_data, sizeof (sc->sc_aggr_stats));
66207a79cebfSGleb Smirnoff 	case SIOCZATHSTATS: {
66217a79cebfSGleb Smirnoff 		int error;
66227a79cebfSGleb Smirnoff 
66233fc21fedSSam Leffler 		error = priv_check(curthread, PRIV_DRIVER);
66249467e3f3SAdrian Chadd 		if (error == 0) {
66253fc21fedSSam Leffler 			memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
662641b6b507SAdrian Chadd 			memset(&sc->sc_aggr_stats, 0,
662741b6b507SAdrian Chadd 			    sizeof(sc->sc_aggr_stats));
66289467e3f3SAdrian Chadd 			memset(&sc->sc_intr_stats, 0,
66299467e3f3SAdrian Chadd 			    sizeof(sc->sc_intr_stats));
66309467e3f3SAdrian Chadd 		}
66317a79cebfSGleb Smirnoff 		return (error);
66327a79cebfSGleb Smirnoff 	}
6633a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
6634c42a7b7eSSam Leffler 	case SIOCGATHDIAG:
66357a79cebfSGleb Smirnoff 		return (ath_ioctl_diag(sc, data));
6636f51c84eaSAdrian Chadd 	case SIOCGATHPHYERR:
66377a79cebfSGleb Smirnoff 		return (ath_ioctl_phyerr(sc, data));
6638a585a9a1SSam Leffler #endif
66399af351f9SAdrian Chadd 	case SIOCGATHSPECTRAL:
66407a79cebfSGleb Smirnoff 		return (ath_ioctl_spectral(sc, data));
6641b8f2a853SAdrian Chadd 	case SIOCGATHNODERATESTATS:
66427a79cebfSGleb Smirnoff 		return (ath_ioctl_ratestats(sc, data));
664331a8c1edSAndrew Thompson 	default:
66447a79cebfSGleb Smirnoff 		return (ENOTTY);
6645c42a7b7eSSam Leffler 	}
6646c42a7b7eSSam Leffler }
6647c42a7b7eSSam Leffler 
6648c42a7b7eSSam Leffler /*
6649c42a7b7eSSam Leffler  * Announce various information on device/driver attach.
6650c42a7b7eSSam Leffler  */
6651c42a7b7eSSam Leffler static void
6652c42a7b7eSSam Leffler ath_announce(struct ath_softc *sc)
6653c42a7b7eSSam Leffler {
6654c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6655c42a7b7eSSam Leffler 
665676e6fd5dSGleb Smirnoff 	device_printf(sc->sc_dev, "AR%s mac %d.%d RF%s phy %d.%d\n",
6657498657cfSSam Leffler 		ath_hal_mac_name(ah), ah->ah_macVersion, ah->ah_macRev,
6658498657cfSSam Leffler 		ath_hal_rf_name(ah), ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
665976e6fd5dSGleb Smirnoff 	device_printf(sc->sc_dev, "2GHz radio: 0x%.4x; 5GHz radio: 0x%.4x\n",
666046a924c4SAdrian Chadd 		ah->ah_analog2GhzRev, ah->ah_analog5GhzRev);
6661c42a7b7eSSam Leffler 	if (bootverbose) {
6662c42a7b7eSSam Leffler 		int i;
6663c42a7b7eSSam Leffler 		for (i = 0; i <= WME_AC_VO; i++) {
6664c42a7b7eSSam Leffler 			struct ath_txq *txq = sc->sc_ac2q[i];
666576e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev,
666676e6fd5dSGleb Smirnoff 			    "Use hw queue %u for %s traffic\n",
6667c42a7b7eSSam Leffler 			    txq->axq_qnum, ieee80211_wme_acnames[i]);
6668c42a7b7eSSam Leffler 		}
666976e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "Use hw queue %u for CAB traffic\n",
6670c42a7b7eSSam Leffler 		    sc->sc_cabq->axq_qnum);
667176e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "Use hw queue %u for beacons\n",
667276e6fd5dSGleb Smirnoff 		    sc->sc_bhalq);
6673c42a7b7eSSam Leffler 	}
6674e2d787faSSam Leffler 	if (ath_rxbuf != ATH_RXBUF)
667576e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "using %u rx buffers\n", ath_rxbuf);
6676e2d787faSSam Leffler 	if (ath_txbuf != ATH_TXBUF)
667776e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "using %u tx buffers\n", ath_txbuf);
66789ac01d39SRui Paulo 	if (sc->sc_mcastkey && bootverbose)
667976e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "using multicast key search\n");
6680c42a7b7eSSam Leffler }
668110ad9a77SSam Leffler 
668248237774SAdrian Chadd static void
668348237774SAdrian Chadd ath_dfs_tasklet(void *p, int npending)
668448237774SAdrian Chadd {
668548237774SAdrian Chadd 	struct ath_softc *sc = (struct ath_softc *) p;
66867a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
668748237774SAdrian Chadd 
668848237774SAdrian Chadd 	/*
668948237774SAdrian Chadd 	 * If previous processing has found a radar event,
669048237774SAdrian Chadd 	 * signal this to the net80211 layer to begin DFS
669148237774SAdrian Chadd 	 * processing.
669248237774SAdrian Chadd 	 */
669348237774SAdrian Chadd 	if (ath_dfs_process_radar_event(sc, sc->sc_curchan)) {
669448237774SAdrian Chadd 		/* DFS event found, initiate channel change */
669506fc4a10SAdrian Chadd 		/*
669606fc4a10SAdrian Chadd 		 * XXX doesn't currently tell us whether the event
669706fc4a10SAdrian Chadd 		 * XXX was found in the primary or extension
669806fc4a10SAdrian Chadd 		 * XXX channel!
669906fc4a10SAdrian Chadd 		 */
670006fc4a10SAdrian Chadd 		IEEE80211_LOCK(ic);
670148237774SAdrian Chadd 		ieee80211_dfs_notify_radar(ic, sc->sc_curchan);
670206fc4a10SAdrian Chadd 		IEEE80211_UNLOCK(ic);
670348237774SAdrian Chadd 	}
670448237774SAdrian Chadd }
670548237774SAdrian Chadd 
67060eb81626SAdrian Chadd /*
67070eb81626SAdrian Chadd  * Enable/disable power save.  This must be called with
67080eb81626SAdrian Chadd  * no TX driver locks currently held, so it should only
67090eb81626SAdrian Chadd  * be called from the RX path (which doesn't hold any
67100eb81626SAdrian Chadd  * TX driver locks.)
67110eb81626SAdrian Chadd  */
67120eb81626SAdrian Chadd static void
67130eb81626SAdrian Chadd ath_node_powersave(struct ieee80211_node *ni, int enable)
67140eb81626SAdrian Chadd {
6715bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
67160eb81626SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
67170eb81626SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
67183797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
67190eb81626SAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
67200eb81626SAdrian Chadd 
67210eb81626SAdrian Chadd 	/* XXX and no TXQ locks should be held here */
67220eb81626SAdrian Chadd 
67239b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, "%s: %6D: enable=%d\n",
67249b48fb4bSAdrian Chadd 	    __func__,
67259b48fb4bSAdrian Chadd 	    ni->ni_macaddr,
67269b48fb4bSAdrian Chadd 	    ":",
67279b48fb4bSAdrian Chadd 	    !! enable);
67280eb81626SAdrian Chadd 
67290eb81626SAdrian Chadd 	/* Suspend or resume software queue handling */
67300eb81626SAdrian Chadd 	if (enable)
67310eb81626SAdrian Chadd 		ath_tx_node_sleep(sc, an);
67320eb81626SAdrian Chadd 	else
67330eb81626SAdrian Chadd 		ath_tx_node_wakeup(sc, an);
67340eb81626SAdrian Chadd 
67350eb81626SAdrian Chadd 	/* Update net80211 state */
67360eb81626SAdrian Chadd 	avp->av_node_ps(ni, enable);
6737bdbb6e5bSAdrian Chadd #else
6738bdbb6e5bSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6739bdbb6e5bSAdrian Chadd 
6740bdbb6e5bSAdrian Chadd 	/* Update net80211 state */
6741bdbb6e5bSAdrian Chadd 	avp->av_node_ps(ni, enable);
6742bdbb6e5bSAdrian Chadd #endif/* ATH_SW_PSQ */
67430eb81626SAdrian Chadd }
67440eb81626SAdrian Chadd 
6745548a605dSAdrian Chadd /*
6746548a605dSAdrian Chadd  * Notification from net80211 that the powersave queue state has
6747548a605dSAdrian Chadd  * changed.
6748548a605dSAdrian Chadd  *
6749548a605dSAdrian Chadd  * Since the software queue also may have some frames:
6750548a605dSAdrian Chadd  *
6751548a605dSAdrian Chadd  * + if the node software queue has frames and the TID state
6752548a605dSAdrian Chadd  *   is 0, we set the TIM;
6753548a605dSAdrian Chadd  * + if the node and the stack are both empty, we clear the TIM bit.
6754548a605dSAdrian Chadd  * + If the stack tries to set the bit, always set it.
6755548a605dSAdrian Chadd  * + If the stack tries to clear the bit, only clear it if the
6756548a605dSAdrian Chadd  *   software queue in question is also cleared.
6757548a605dSAdrian Chadd  *
6758548a605dSAdrian Chadd  * TODO: this is called during node teardown; so let's ensure this
6759548a605dSAdrian Chadd  * is all correctly handled and that the TIM bit is cleared.
6760548a605dSAdrian Chadd  * It may be that the node flush is called _AFTER_ the net80211
6761548a605dSAdrian Chadd  * stack clears the TIM.
6762548a605dSAdrian Chadd  *
6763548a605dSAdrian Chadd  * Here is the racy part.  Since it's possible >1 concurrent,
6764548a605dSAdrian Chadd  * overlapping TXes will appear complete with a TX completion in
6765548a605dSAdrian Chadd  * another thread, it's possible that the concurrent TIM calls will
6766548a605dSAdrian Chadd  * clash.  We can't hold the node lock here because setting the
6767548a605dSAdrian Chadd  * TIM grabs the net80211 comlock and this may cause a LOR.
6768548a605dSAdrian Chadd  * The solution is either to totally serialise _everything_ at
6769548a605dSAdrian Chadd  * this point (ie, all TX, completion and any reset/flush go into
6770548a605dSAdrian Chadd  * one taskqueue) or a new "ath TIM lock" needs to be created that
6771548a605dSAdrian Chadd  * just wraps the driver state change and this call to avp->av_set_tim().
6772548a605dSAdrian Chadd  *
6773548a605dSAdrian Chadd  * The same race exists in the net80211 power save queue handling
6774548a605dSAdrian Chadd  * as well.  Since multiple transmitting threads may queue frames
6775548a605dSAdrian Chadd  * into the driver, as well as ps-poll and the driver transmitting
6776548a605dSAdrian Chadd  * frames (and thus clearing the psq), it's quite possible that
6777548a605dSAdrian Chadd  * a packet entering the PSQ and a ps-poll being handled will
6778548a605dSAdrian Chadd  * race, causing the TIM to be cleared and not re-set.
6779548a605dSAdrian Chadd  */
6780548a605dSAdrian Chadd static int
6781548a605dSAdrian Chadd ath_node_set_tim(struct ieee80211_node *ni, int enable)
6782548a605dSAdrian Chadd {
6783bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
6784548a605dSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
67853797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
6786548a605dSAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
6787548a605dSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6788548a605dSAdrian Chadd 	int changed = 0;
6789548a605dSAdrian Chadd 
67904bed2b67SAdrian Chadd 	ATH_TX_LOCK(sc);
6791548a605dSAdrian Chadd 	an->an_stack_psq = enable;
6792548a605dSAdrian Chadd 
6793548a605dSAdrian Chadd 	/*
6794548a605dSAdrian Chadd 	 * This will get called for all operating modes,
6795548a605dSAdrian Chadd 	 * even if avp->av_set_tim is unset.
6796548a605dSAdrian Chadd 	 * It's currently set for hostap/ibss modes; but
6797548a605dSAdrian Chadd 	 * the same infrastructure is used for both STA
6798548a605dSAdrian Chadd 	 * and AP/IBSS node power save.
6799548a605dSAdrian Chadd 	 */
6800548a605dSAdrian Chadd 	if (avp->av_set_tim == NULL) {
68014bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6802548a605dSAdrian Chadd 		return (0);
6803548a605dSAdrian Chadd 	}
6804548a605dSAdrian Chadd 
6805548a605dSAdrian Chadd 	/*
6806548a605dSAdrian Chadd 	 * If setting the bit, always set it here.
6807548a605dSAdrian Chadd 	 * If clearing the bit, only clear it if the
6808548a605dSAdrian Chadd 	 * software queue is also empty.
6809548a605dSAdrian Chadd 	 *
6810548a605dSAdrian Chadd 	 * If the node has left power save, just clear the TIM
6811548a605dSAdrian Chadd 	 * bit regardless of the state of the power save queue.
6812548a605dSAdrian Chadd 	 *
6813548a605dSAdrian Chadd 	 * XXX TODO: although atomics are used, it's quite possible
6814548a605dSAdrian Chadd 	 * that a race will occur between this and setting/clearing
6815548a605dSAdrian Chadd 	 * in another thread.  TX completion will occur always in
6816548a605dSAdrian Chadd 	 * one thread, however setting/clearing the TIM bit can come
6817548a605dSAdrian Chadd 	 * from a variety of different process contexts!
6818548a605dSAdrian Chadd 	 */
6819548a605dSAdrian Chadd 	if (enable && an->an_tim_set == 1) {
6820548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
68219b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, tim_set=1, ignoring\n",
68229b48fb4bSAdrian Chadd 		    __func__,
68239b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
68249b48fb4bSAdrian Chadd 		    ":",
68259b48fb4bSAdrian Chadd 		    enable);
68264bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6827548a605dSAdrian Chadd 	} else if (enable) {
6828548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
68299b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, enabling TIM\n",
68309b48fb4bSAdrian Chadd 		    __func__,
68319b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
68329b48fb4bSAdrian Chadd 		    ":",
68339b48fb4bSAdrian Chadd 		    enable);
6834548a605dSAdrian Chadd 		an->an_tim_set = 1;
68354bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6836548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6837ba83edd4SAdrian Chadd 	} else if (an->an_swq_depth == 0) {
6838548a605dSAdrian Chadd 		/* disable */
6839548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
68409b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_swq_depth == 0, disabling\n",
68419b48fb4bSAdrian Chadd 		    __func__,
68429b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
68439b48fb4bSAdrian Chadd 		    ":",
68449b48fb4bSAdrian Chadd 		    enable);
6845548a605dSAdrian Chadd 		an->an_tim_set = 0;
68464bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6847548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6848548a605dSAdrian Chadd 	} else if (! an->an_is_powersave) {
6849548a605dSAdrian Chadd 		/*
6850548a605dSAdrian Chadd 		 * disable regardless; the node isn't in powersave now
6851548a605dSAdrian Chadd 		 */
6852548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
68539b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_pwrsave=0, disabling\n",
68549b48fb4bSAdrian Chadd 		    __func__,
68559b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
68569b48fb4bSAdrian Chadd 		    ":",
68579b48fb4bSAdrian Chadd 		    enable);
6858548a605dSAdrian Chadd 		an->an_tim_set = 0;
68594bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6860548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6861548a605dSAdrian Chadd 	} else {
6862548a605dSAdrian Chadd 		/*
6863548a605dSAdrian Chadd 		 * psq disable, node is currently in powersave, node
6864548a605dSAdrian Chadd 		 * software queue isn't empty, so don't clear the TIM bit
6865548a605dSAdrian Chadd 		 * for now.
6866548a605dSAdrian Chadd 		 */
68674bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6868548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
68699b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_swq_depth > 0, ignoring\n",
68709b48fb4bSAdrian Chadd 		    __func__,
68719b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
68729b48fb4bSAdrian Chadd 		    ":",
68739b48fb4bSAdrian Chadd 		    enable);
6874548a605dSAdrian Chadd 		changed = 0;
6875548a605dSAdrian Chadd 	}
6876548a605dSAdrian Chadd 
6877548a605dSAdrian Chadd 	return (changed);
6878bdbb6e5bSAdrian Chadd #else
6879bdbb6e5bSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6880bdbb6e5bSAdrian Chadd 
688160328038SAdrian Chadd 	/*
6882661c81c3SBaptiste Daroussin 	 * Some operating modes don't set av_set_tim(), so don't
688360328038SAdrian Chadd 	 * update it here.
688460328038SAdrian Chadd 	 */
688560328038SAdrian Chadd 	if (avp->av_set_tim == NULL)
688660328038SAdrian Chadd 		return (0);
688760328038SAdrian Chadd 
6888bdbb6e5bSAdrian Chadd 	return (avp->av_set_tim(ni, enable));
6889bdbb6e5bSAdrian Chadd #endif /* ATH_SW_PSQ */
6890548a605dSAdrian Chadd }
6891548a605dSAdrian Chadd 
6892548a605dSAdrian Chadd /*
6893548a605dSAdrian Chadd  * Set or update the TIM from the software queue.
6894548a605dSAdrian Chadd  *
6895548a605dSAdrian Chadd  * Check the software queue depth before attempting to do lock
6896548a605dSAdrian Chadd  * anything; that avoids trying to obtain the lock.  Then,
6897548a605dSAdrian Chadd  * re-check afterwards to ensure nothing has changed in the
6898548a605dSAdrian Chadd  * meantime.
6899548a605dSAdrian Chadd  *
6900548a605dSAdrian Chadd  * set:   This is designed to be called from the TX path, after
6901548a605dSAdrian Chadd  *        a frame has been queued; to see if the swq > 0.
6902548a605dSAdrian Chadd  *
6903548a605dSAdrian Chadd  * clear: This is designed to be called from the buffer completion point
6904548a605dSAdrian Chadd  *        (right now it's ath_tx_default_comp()) where the state of
6905548a605dSAdrian Chadd  *        a software queue has changed.
6906548a605dSAdrian Chadd  *
6907548a605dSAdrian Chadd  * It makes sense to place it at buffer free / completion rather
6908548a605dSAdrian Chadd  * than after each software queue operation, as there's no real
6909548a605dSAdrian Chadd  * point in churning the TIM bit as the last frames in the software
6910548a605dSAdrian Chadd  * queue are transmitted.  If they fail and we retry them, we'd
6911548a605dSAdrian Chadd  * just be setting the TIM bit again anyway.
6912548a605dSAdrian Chadd  */
6913548a605dSAdrian Chadd void
6914548a605dSAdrian Chadd ath_tx_update_tim(struct ath_softc *sc, struct ieee80211_node *ni,
6915548a605dSAdrian Chadd      int enable)
6916548a605dSAdrian Chadd {
6917bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
6918548a605dSAdrian Chadd 	struct ath_node *an;
6919548a605dSAdrian Chadd 	struct ath_vap *avp;
6920548a605dSAdrian Chadd 
6921548a605dSAdrian Chadd 	/* Don't do this for broadcast/etc frames */
6922548a605dSAdrian Chadd 	if (ni == NULL)
6923548a605dSAdrian Chadd 		return;
6924548a605dSAdrian Chadd 
6925548a605dSAdrian Chadd 	an = ATH_NODE(ni);
6926548a605dSAdrian Chadd 	avp = ATH_VAP(ni->ni_vap);
6927548a605dSAdrian Chadd 
6928548a605dSAdrian Chadd 	/*
6929548a605dSAdrian Chadd 	 * And for operating modes without the TIM handler set, let's
6930548a605dSAdrian Chadd 	 * just skip those.
6931548a605dSAdrian Chadd 	 */
6932548a605dSAdrian Chadd 	if (avp->av_set_tim == NULL)
6933548a605dSAdrian Chadd 		return;
6934548a605dSAdrian Chadd 
69354bed2b67SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
6936548a605dSAdrian Chadd 
6937548a605dSAdrian Chadd 	if (enable) {
6938548a605dSAdrian Chadd 		if (an->an_is_powersave &&
6939548a605dSAdrian Chadd 		    an->an_tim_set == 0 &&
6940ba83edd4SAdrian Chadd 		    an->an_swq_depth != 0) {
6941548a605dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
69429b48fb4bSAdrian Chadd 			    "%s: %6D: swq_depth>0, tim_set=0, set!\n",
69439b48fb4bSAdrian Chadd 			    __func__,
69449b48fb4bSAdrian Chadd 			    ni->ni_macaddr,
69459b48fb4bSAdrian Chadd 			    ":");
6946548a605dSAdrian Chadd 			an->an_tim_set = 1;
6947548a605dSAdrian Chadd 			(void) avp->av_set_tim(ni, 1);
6948548a605dSAdrian Chadd 		}
6949548a605dSAdrian Chadd 	} else {
6950548a605dSAdrian Chadd 		/*
6951548a605dSAdrian Chadd 		 * Don't bother grabbing the lock unless the queue is empty.
6952548a605dSAdrian Chadd 		 */
69533b48f36eSAdrian Chadd 		if (an->an_swq_depth != 0)
6954548a605dSAdrian Chadd 			return;
6955548a605dSAdrian Chadd 
6956548a605dSAdrian Chadd 		if (an->an_is_powersave &&
6957548a605dSAdrian Chadd 		    an->an_stack_psq == 0 &&
6958548a605dSAdrian Chadd 		    an->an_tim_set == 1 &&
6959ba83edd4SAdrian Chadd 		    an->an_swq_depth == 0) {
6960548a605dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
696122a3aee6SAdrian Chadd 			    "%s: %6D: swq_depth=0, tim_set=1, psq_set=0,"
6962548a605dSAdrian Chadd 			    " clear!\n",
696322a3aee6SAdrian Chadd 			    __func__,
696422a3aee6SAdrian Chadd 			    ni->ni_macaddr,
696522a3aee6SAdrian Chadd 			    ":");
6966548a605dSAdrian Chadd 			an->an_tim_set = 0;
6967548a605dSAdrian Chadd 			(void) avp->av_set_tim(ni, 0);
6968548a605dSAdrian Chadd 		}
6969548a605dSAdrian Chadd 	}
6970bdbb6e5bSAdrian Chadd #else
6971bdbb6e5bSAdrian Chadd 	return;
6972bdbb6e5bSAdrian Chadd #endif	/* ATH_SW_PSQ */
6973548a605dSAdrian Chadd }
69740eb81626SAdrian Chadd 
697522a3aee6SAdrian Chadd /*
697622a3aee6SAdrian Chadd  * Received a ps-poll frame from net80211.
697722a3aee6SAdrian Chadd  *
697822a3aee6SAdrian Chadd  * Here we get a chance to serve out a software-queued frame ourselves
697922a3aee6SAdrian Chadd  * before we punt it to net80211 to transmit us one itself - either
698022a3aee6SAdrian Chadd  * because there's traffic in the net80211 psq, or a NULL frame to
698122a3aee6SAdrian Chadd  * indicate there's nothing else.
698222a3aee6SAdrian Chadd  */
698322a3aee6SAdrian Chadd static void
698422a3aee6SAdrian Chadd ath_node_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m)
698522a3aee6SAdrian Chadd {
698622a3aee6SAdrian Chadd #ifdef	ATH_SW_PSQ
698722a3aee6SAdrian Chadd 	struct ath_node *an;
698822a3aee6SAdrian Chadd 	struct ath_vap *avp;
698922a3aee6SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
69903797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
699122a3aee6SAdrian Chadd 	int tid;
699222a3aee6SAdrian Chadd 
699322a3aee6SAdrian Chadd 	/* Just paranoia */
699422a3aee6SAdrian Chadd 	if (ni == NULL)
699522a3aee6SAdrian Chadd 		return;
699622a3aee6SAdrian Chadd 
699722a3aee6SAdrian Chadd 	/*
699822a3aee6SAdrian Chadd 	 * Unassociated (temporary node) station.
699922a3aee6SAdrian Chadd 	 */
700022a3aee6SAdrian Chadd 	if (ni->ni_associd == 0)
700122a3aee6SAdrian Chadd 		return;
700222a3aee6SAdrian Chadd 
700322a3aee6SAdrian Chadd 	/*
700422a3aee6SAdrian Chadd 	 * We do have an active node, so let's begin looking into it.
700522a3aee6SAdrian Chadd 	 */
700622a3aee6SAdrian Chadd 	an = ATH_NODE(ni);
700722a3aee6SAdrian Chadd 	avp = ATH_VAP(ni->ni_vap);
700822a3aee6SAdrian Chadd 
700922a3aee6SAdrian Chadd 	/*
701022a3aee6SAdrian Chadd 	 * For now, we just call the original ps-poll method.
701122a3aee6SAdrian Chadd 	 * Once we're ready to flip this on:
701222a3aee6SAdrian Chadd 	 *
701322a3aee6SAdrian Chadd 	 * + Set leak to 1, as no matter what we're going to have
701422a3aee6SAdrian Chadd 	 *   to send a frame;
701522a3aee6SAdrian Chadd 	 * + Check the software queue and if there's something in it,
701622a3aee6SAdrian Chadd 	 *   schedule the highest TID thas has traffic from this node.
701722a3aee6SAdrian Chadd 	 *   Then make sure we schedule the software scheduler to
701822a3aee6SAdrian Chadd 	 *   run so it picks up said frame.
701922a3aee6SAdrian Chadd 	 *
702022a3aee6SAdrian Chadd 	 * That way whatever happens, we'll at least send _a_ frame
702122a3aee6SAdrian Chadd 	 * to the given node.
702222a3aee6SAdrian Chadd 	 *
702322a3aee6SAdrian Chadd 	 * Again, yes, it's crappy QoS if the node has multiple
702422a3aee6SAdrian Chadd 	 * TIDs worth of traffic - but let's get it working first
702522a3aee6SAdrian Chadd 	 * before we optimise it.
702622a3aee6SAdrian Chadd 	 *
702722a3aee6SAdrian Chadd 	 * Also yes, there's definitely latency here - we're not
702822a3aee6SAdrian Chadd 	 * direct dispatching to the hardware in this path (and
702922a3aee6SAdrian Chadd 	 * we're likely being called from the packet receive path,
703022a3aee6SAdrian Chadd 	 * so going back into TX may be a little hairy!) but again
703122a3aee6SAdrian Chadd 	 * I'd like to get this working first before optimising
703222a3aee6SAdrian Chadd 	 * turn-around time.
703322a3aee6SAdrian Chadd 	 */
703422a3aee6SAdrian Chadd 
703522a3aee6SAdrian Chadd 	ATH_TX_LOCK(sc);
703622a3aee6SAdrian Chadd 
703722a3aee6SAdrian Chadd 	/*
703822a3aee6SAdrian Chadd 	 * Legacy - we're called and the node isn't asleep.
703922a3aee6SAdrian Chadd 	 * Immediately punt.
704022a3aee6SAdrian Chadd 	 */
704122a3aee6SAdrian Chadd 	if (! an->an_is_powersave) {
704283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
704322a3aee6SAdrian Chadd 		    "%s: %6D: not in powersave?\n",
704422a3aee6SAdrian Chadd 		    __func__,
704522a3aee6SAdrian Chadd 		    ni->ni_macaddr,
704622a3aee6SAdrian Chadd 		    ":");
704722a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
704822a3aee6SAdrian Chadd 		avp->av_recv_pspoll(ni, m);
704922a3aee6SAdrian Chadd 		return;
705022a3aee6SAdrian Chadd 	}
705122a3aee6SAdrian Chadd 
705222a3aee6SAdrian Chadd 	/*
705322a3aee6SAdrian Chadd 	 * We're in powersave.
705422a3aee6SAdrian Chadd 	 *
705522a3aee6SAdrian Chadd 	 * Leak a frame.
705622a3aee6SAdrian Chadd 	 */
705722a3aee6SAdrian Chadd 	an->an_leak_count = 1;
705822a3aee6SAdrian Chadd 
705922a3aee6SAdrian Chadd 	/*
706022a3aee6SAdrian Chadd 	 * Now, if there's no frames in the node, just punt to
706122a3aee6SAdrian Chadd 	 * recv_pspoll.
706222a3aee6SAdrian Chadd 	 *
706322a3aee6SAdrian Chadd 	 * Don't bother checking if the TIM bit is set, we really
706422a3aee6SAdrian Chadd 	 * only care if there are any frames here!
706522a3aee6SAdrian Chadd 	 */
706622a3aee6SAdrian Chadd 	if (an->an_swq_depth == 0) {
706722a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
706822a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
706922a3aee6SAdrian Chadd 		    "%s: %6D: SWQ empty; punting to net80211\n",
707022a3aee6SAdrian Chadd 		    __func__,
707122a3aee6SAdrian Chadd 		    ni->ni_macaddr,
707222a3aee6SAdrian Chadd 		    ":");
707322a3aee6SAdrian Chadd 		avp->av_recv_pspoll(ni, m);
707422a3aee6SAdrian Chadd 		return;
707522a3aee6SAdrian Chadd 	}
707622a3aee6SAdrian Chadd 
707722a3aee6SAdrian Chadd 	/*
707822a3aee6SAdrian Chadd 	 * Ok, let's schedule the highest TID that has traffic
707922a3aee6SAdrian Chadd 	 * and then schedule something.
708022a3aee6SAdrian Chadd 	 */
708122a3aee6SAdrian Chadd 	for (tid = IEEE80211_TID_SIZE - 1; tid >= 0; tid--) {
708222a3aee6SAdrian Chadd 		struct ath_tid *atid = &an->an_tid[tid];
708322a3aee6SAdrian Chadd 		/*
708422a3aee6SAdrian Chadd 		 * No frames? Skip.
708522a3aee6SAdrian Chadd 		 */
708622a3aee6SAdrian Chadd 		if (atid->axq_depth == 0)
708722a3aee6SAdrian Chadd 			continue;
708822a3aee6SAdrian Chadd 		ath_tx_tid_sched(sc, atid);
708922a3aee6SAdrian Chadd 		/*
709022a3aee6SAdrian Chadd 		 * XXX we could do a direct call to the TXQ
709122a3aee6SAdrian Chadd 		 * scheduler code here to optimise latency
709222a3aee6SAdrian Chadd 		 * at the expense of a REALLY deep callstack.
709322a3aee6SAdrian Chadd 		 */
709422a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
709522a3aee6SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_txqtask);
709622a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
709722a3aee6SAdrian Chadd 		    "%s: %6D: leaking frame to TID %d\n",
709822a3aee6SAdrian Chadd 		    __func__,
709922a3aee6SAdrian Chadd 		    ni->ni_macaddr,
710022a3aee6SAdrian Chadd 		    ":",
710122a3aee6SAdrian Chadd 		    tid);
710222a3aee6SAdrian Chadd 		return;
710322a3aee6SAdrian Chadd 	}
710422a3aee6SAdrian Chadd 
710522a3aee6SAdrian Chadd 	ATH_TX_UNLOCK(sc);
710622a3aee6SAdrian Chadd 
710722a3aee6SAdrian Chadd 	/*
710822a3aee6SAdrian Chadd 	 * XXX nothing in the TIDs at this point? Eek.
710922a3aee6SAdrian Chadd 	 */
711083bbd5ebSRui Paulo 	DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
711183bbd5ebSRui Paulo 	    "%s: %6D: TIDs empty, but ath_node showed traffic?!\n",
711222a3aee6SAdrian Chadd 	    __func__,
711322a3aee6SAdrian Chadd 	    ni->ni_macaddr,
711422a3aee6SAdrian Chadd 	    ":");
711522a3aee6SAdrian Chadd 	avp->av_recv_pspoll(ni, m);
711622a3aee6SAdrian Chadd #else
711722a3aee6SAdrian Chadd 	avp->av_recv_pspoll(ni, m);
711822a3aee6SAdrian Chadd #endif	/* ATH_SW_PSQ */
711922a3aee6SAdrian Chadd }
712022a3aee6SAdrian Chadd 
7121dba9c859SAdrian Chadd MODULE_VERSION(if_ath, 1);
7122dba9c859SAdrian Chadd MODULE_DEPEND(if_ath, wlan, 1, 1, 1);          /* 802.11 media layer */
71239389d5a9SAdrian Chadd #if	defined(IEEE80211_ALQ) || defined(AH_DEBUG_ALQ) || defined(ATH_DEBUG_ALQ)
712458816f3fSAdrian Chadd MODULE_DEPEND(if_ath, alq, 1, 1, 1);
712558816f3fSAdrian Chadd #endif
7126