xref: /freebsd/sys/dev/ath/if_ath.c (revision 0149d3d476886786cba3d41ea3e5d6921db7215b)
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>
116bcf5fc49SAdrian Chadd #include <dev/ath/if_ath_btcoex_mci.h>
1179af351f9SAdrian Chadd #include <dev/ath/if_ath_spectral.h>
118216ca234SAdrian Chadd #include <dev/ath/if_ath_lna_div.h>
11948237774SAdrian Chadd #include <dev/ath/if_athdfs.h>
120b45de1ebSAdrian Chadd #include <dev/ath/if_ath_ioctl.h>
121b45de1ebSAdrian Chadd #include <dev/ath/if_ath_descdma.h>
1225bc8125aSAdrian Chadd 
12386e07743SSam Leffler #ifdef ATH_TX99_DIAG
12486e07743SSam Leffler #include <dev/ath/ath_tx99/ath_tx99.h>
12586e07743SSam Leffler #endif
12686e07743SSam Leffler 
12789d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
128bdbb6e5bSAdrian Chadd #include <dev/ath/if_ath_alq.h>
129bdbb6e5bSAdrian Chadd #endif
130bdbb6e5bSAdrian Chadd 
131bdbb6e5bSAdrian Chadd /*
132bdbb6e5bSAdrian Chadd  * Only enable this if you're working on PS-POLL support.
133bdbb6e5bSAdrian Chadd  */
13422a3aee6SAdrian Chadd #define	ATH_SW_PSQ
135bdbb6e5bSAdrian Chadd 
136b032f27cSSam Leffler /*
137b032f27cSSam Leffler  * ATH_BCBUF determines the number of vap's that can transmit
138b032f27cSSam Leffler  * beacons and also (currently) the number of vap's that can
139b032f27cSSam Leffler  * have unique mac addresses/bssid.  When staggering beacons
140b032f27cSSam Leffler  * 4 is probably a good max as otherwise the beacons become
141b032f27cSSam Leffler  * very closely spaced and there is limited time for cab q traffic
142b032f27cSSam Leffler  * to go out.  You can burst beacons instead but that is not good
143b032f27cSSam Leffler  * for stations in power save and at some point you really want
144b032f27cSSam Leffler  * another radio (and channel).
145b032f27cSSam Leffler  *
146b032f27cSSam Leffler  * The limit on the number of mac addresses is tied to our use of
147b032f27cSSam Leffler  * the U/L bit and tracking addresses in a byte; it would be
148b032f27cSSam Leffler  * worthwhile to allow more for applications like proxy sta.
149b032f27cSSam Leffler  */
150b032f27cSSam Leffler CTASSERT(ATH_BCBUF <= 8);
151b032f27cSSam Leffler 
152b032f27cSSam Leffler static struct ieee80211vap *ath_vap_create(struct ieee80211com *,
153fcd9500fSBernhard Schmidt 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
154fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN],
155fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN]);
156b032f27cSSam Leffler static void	ath_vap_delete(struct ieee80211vap *);
1577a79cebfSGleb Smirnoff static int	ath_init(struct ath_softc *);
1587a79cebfSGleb Smirnoff static void	ath_stop(struct ath_softc *);
159b032f27cSSam Leffler static int	ath_reset_vap(struct ieee80211vap *, u_long);
1607a79cebfSGleb Smirnoff static int	ath_transmit(struct ieee80211com *, struct mbuf *);
1615591b213SSam Leffler static int	ath_media_change(struct ifnet *);
1622e986da5SSam Leffler static void	ath_watchdog(void *);
1637a79cebfSGleb Smirnoff static void	ath_parent(struct ieee80211com *);
1645591b213SSam Leffler static void	ath_fatal_proc(void *, int);
165b032f27cSSam Leffler static void	ath_bmiss_vap(struct ieee80211vap *);
1665591b213SSam Leffler static void	ath_bmiss_proc(void *, int);
167b032f27cSSam Leffler static void	ath_key_update_begin(struct ieee80211vap *);
168b032f27cSSam Leffler static void	ath_key_update_end(struct ieee80211vap *);
169e5bd159eSAdrian Chadd static void	ath_update_mcast_hw(struct ath_softc *);
170272f6adeSGleb Smirnoff static void	ath_update_mcast(struct ieee80211com *);
171272f6adeSGleb Smirnoff static void	ath_update_promisc(struct ieee80211com *);
172272f6adeSGleb Smirnoff static void	ath_updateslot(struct ieee80211com *);
173c42a7b7eSSam Leffler static void	ath_bstuck_proc(void *, int);
174d52f7132SAdrian Chadd static void	ath_reset_proc(void *, int);
1755591b213SSam Leffler static int	ath_desc_alloc(struct ath_softc *);
1765591b213SSam Leffler static void	ath_desc_free(struct ath_softc *);
17738c208f8SSam Leffler static struct ieee80211_node *ath_node_alloc(struct ieee80211vap *,
17838c208f8SSam Leffler 			const uint8_t [IEEE80211_ADDR_LEN]);
1794afa805eSAdrian Chadd static void	ath_node_cleanup(struct ieee80211_node *);
180c42a7b7eSSam Leffler static void	ath_node_free(struct ieee80211_node *);
18168e8e04eSSam Leffler static void	ath_node_getsignal(const struct ieee80211_node *,
18268e8e04eSSam Leffler 			int8_t *, int8_t *);
183622b3fd2SSam Leffler static void	ath_txq_init(struct ath_softc *sc, struct ath_txq *, int);
184c42a7b7eSSam Leffler static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype);
185c42a7b7eSSam Leffler static int	ath_tx_setup(struct ath_softc *, int, int);
186c42a7b7eSSam Leffler static void	ath_tx_cleanupq(struct ath_softc *, struct ath_txq *);
187c42a7b7eSSam Leffler static void	ath_tx_cleanup(struct ath_softc *);
188788e6aa9SAdrian Chadd static int	ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq,
189788e6aa9SAdrian Chadd 		    int dosched);
190c42a7b7eSSam Leffler static void	ath_tx_proc_q0(void *, int);
191c42a7b7eSSam Leffler static void	ath_tx_proc_q0123(void *, int);
1925591b213SSam Leffler static void	ath_tx_proc(void *, int);
19303e9308fSAdrian Chadd static void	ath_txq_sched_tasklet(void *, int);
1945591b213SSam Leffler static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
195c42a7b7eSSam Leffler static void	ath_chan_change(struct ath_softc *, struct ieee80211_channel *);
19668e8e04eSSam Leffler static void	ath_scan_start(struct ieee80211com *);
19768e8e04eSSam Leffler static void	ath_scan_end(struct ieee80211com *);
19868e8e04eSSam Leffler static void	ath_set_channel(struct ieee80211com *);
199fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
200e7200579SAdrian Chadd static void	ath_update_chw(struct ieee80211com *);
201fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
2025591b213SSam Leffler static void	ath_calibrate(void *);
203b032f27cSSam Leffler static int	ath_newstate(struct ieee80211vap *, enum ieee80211_state, int);
204e8fd88a3SSam Leffler static void	ath_setup_stationkey(struct ieee80211_node *);
205e9962332SSam Leffler static void	ath_newassoc(struct ieee80211_node *, int);
206b032f27cSSam Leffler static int	ath_setregdomain(struct ieee80211com *,
207b032f27cSSam Leffler 		    struct ieee80211_regdomain *, int,
208b032f27cSSam Leffler 		    struct ieee80211_channel []);
2095fe9f044SSam Leffler static void	ath_getradiocaps(struct ieee80211com *, int, int *,
210b032f27cSSam Leffler 		    struct ieee80211_channel []);
211b032f27cSSam Leffler static int	ath_getchannels(struct ath_softc *);
2125591b213SSam Leffler 
213c42a7b7eSSam Leffler static int	ath_rate_setup(struct ath_softc *, u_int mode);
2145591b213SSam Leffler static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
215c42a7b7eSSam Leffler 
216c42a7b7eSSam Leffler static void	ath_announce(struct ath_softc *);
2175591b213SSam Leffler 
21848237774SAdrian Chadd static void	ath_dfs_tasklet(void *, int);
2190eb81626SAdrian Chadd static void	ath_node_powersave(struct ieee80211_node *, int);
220548a605dSAdrian Chadd static int	ath_node_set_tim(struct ieee80211_node *, int);
22122a3aee6SAdrian Chadd static void	ath_node_recv_pspoll(struct ieee80211_node *, struct mbuf *);
22248237774SAdrian Chadd 
223584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
224a35dae8dSAdrian Chadd #include <dev/ath/if_ath_tdma.h>
225a35dae8dSAdrian Chadd #endif
22610ad9a77SSam Leffler 
2275591b213SSam Leffler SYSCTL_DECL(_hw_ath);
2285591b213SSam Leffler 
2295591b213SSam Leffler /* XXX validate sysctl values */
2302dc7fcc4SSam Leffler static	int ath_longcalinterval = 30;		/* long cals every 30 secs */
2312dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, longcal, CTLFLAG_RW, &ath_longcalinterval,
2322dc7fcc4SSam Leffler 	    0, "long chip calibration interval (secs)");
2332dc7fcc4SSam Leffler static	int ath_shortcalinterval = 100;		/* short cals every 100 ms */
2342dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, shortcal, CTLFLAG_RW, &ath_shortcalinterval,
2352dc7fcc4SSam Leffler 	    0, "short chip calibration interval (msecs)");
2362dc7fcc4SSam Leffler static	int ath_resetcalinterval = 20*60;	/* reset cal state 20 mins */
2372dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, resetcal, CTLFLAG_RW, &ath_resetcalinterval,
2382dc7fcc4SSam Leffler 	    0, "reset chip calibration results (secs)");
239a108ab63SAdrian Chadd static	int ath_anicalinterval = 100;		/* ANI calibration - 100 msec */
240a108ab63SAdrian Chadd SYSCTL_INT(_hw_ath, OID_AUTO, anical, CTLFLAG_RW, &ath_anicalinterval,
241a108ab63SAdrian Chadd 	    0, "ANI calibration (msecs)");
2425591b213SSam Leffler 
2433d184db2SAdrian Chadd int ath_rxbuf = ATH_RXBUF;		/* # rx buffers to allocate */
244af3b2549SHans Petter Selasky SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RWTUN, &ath_rxbuf,
245e2d787faSSam Leffler 	    0, "rx buffers allocated");
2463d184db2SAdrian Chadd int ath_txbuf = ATH_TXBUF;		/* # tx buffers to allocate */
247af3b2549SHans Petter Selasky SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RWTUN, &ath_txbuf,
248e2d787faSSam Leffler 	    0, "tx buffers allocated");
2493d184db2SAdrian Chadd int ath_txbuf_mgmt = ATH_MGMT_TXBUF;	/* # mgmt tx buffers to allocate */
250af3b2549SHans Petter Selasky SYSCTL_INT(_hw_ath, OID_AUTO, txbuf_mgmt, CTLFLAG_RWTUN, &ath_txbuf_mgmt,
251af33d486SAdrian Chadd 	    0, "tx (mgmt) buffers allocated");
252e2d787faSSam Leffler 
253a35dae8dSAdrian Chadd int ath_bstuck_threshold = 4;		/* max missed beacons */
254a32ac9d3SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, bstuck, CTLFLAG_RW, &ath_bstuck_threshold,
255a32ac9d3SSam Leffler 	    0, "max missed beacon xmits before chip reset");
256a32ac9d3SSam Leffler 
2576b349e5aSAdrian Chadd MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
258c42a7b7eSSam Leffler 
259f8418db5SAdrian Chadd void
260f8418db5SAdrian Chadd ath_legacy_attach_comp_func(struct ath_softc *sc)
261f8418db5SAdrian Chadd {
262f8418db5SAdrian Chadd 
263f8418db5SAdrian Chadd 	/*
264f8418db5SAdrian Chadd 	 * Special case certain configurations.  Note the
265f8418db5SAdrian Chadd 	 * CAB queue is handled by these specially so don't
266f8418db5SAdrian Chadd 	 * include them when checking the txq setup mask.
267f8418db5SAdrian Chadd 	 */
268f8418db5SAdrian Chadd 	switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
269f8418db5SAdrian Chadd 	case 0x01:
270f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
271f8418db5SAdrian Chadd 		break;
272f8418db5SAdrian Chadd 	case 0x0f:
273f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
274f8418db5SAdrian Chadd 		break;
275f8418db5SAdrian Chadd 	default:
276f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
277f8418db5SAdrian Chadd 		break;
278f8418db5SAdrian Chadd 	}
279f8418db5SAdrian Chadd }
280f8418db5SAdrian Chadd 
281f5c30c4eSAdrian Chadd /*
282f5c30c4eSAdrian Chadd  * Set the target power mode.
283f5c30c4eSAdrian Chadd  *
284f5c30c4eSAdrian Chadd  * If this is called during a point in time where
285f5c30c4eSAdrian Chadd  * the hardware is being programmed elsewhere, it will
286f5c30c4eSAdrian Chadd  * simply store it away and update it when all current
287f5c30c4eSAdrian Chadd  * uses of the hardware are completed.
2888c03e55dSAdrian Chadd  *
2898c03e55dSAdrian Chadd  * If the chip is going into network sleep or power off, then
2908c03e55dSAdrian Chadd  * we will wait until all uses of the chip are done before
2918c03e55dSAdrian Chadd  * going into network sleep or power off.
2928c03e55dSAdrian Chadd  *
2938c03e55dSAdrian Chadd  * If the chip is being programmed full-awake, then immediately
2948c03e55dSAdrian Chadd  * program it full-awake so we can actually stay awake rather than
2958c03e55dSAdrian Chadd  * the chip potentially going to sleep underneath us.
296f5c30c4eSAdrian Chadd  */
297f5c30c4eSAdrian Chadd void
2988c03e55dSAdrian Chadd _ath_power_setpower(struct ath_softc *sc, int power_state, int selfgen,
2998c03e55dSAdrian Chadd     const char *file, int line)
300f5c30c4eSAdrian Chadd {
301f5c30c4eSAdrian Chadd 	ATH_LOCK_ASSERT(sc);
302f5c30c4eSAdrian Chadd 
3038c03e55dSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) state=%d, refcnt=%d, target=%d, cur=%d\n",
304f5c30c4eSAdrian Chadd 	    __func__,
305f5c30c4eSAdrian Chadd 	    file,
306f5c30c4eSAdrian Chadd 	    line,
307f5c30c4eSAdrian Chadd 	    power_state,
3088c03e55dSAdrian Chadd 	    sc->sc_powersave_refcnt,
3098c03e55dSAdrian Chadd 	    sc->sc_target_powerstate,
3108c03e55dSAdrian Chadd 	    sc->sc_cur_powerstate);
311f5c30c4eSAdrian Chadd 
3128c03e55dSAdrian Chadd 	sc->sc_target_powerstate = power_state;
3138c03e55dSAdrian Chadd 
3148c03e55dSAdrian Chadd 	/*
3158c03e55dSAdrian Chadd 	 * Don't program the chip into network sleep if the chip
3168c03e55dSAdrian Chadd 	 * is being programmed elsewhere.
3178c03e55dSAdrian Chadd 	 *
3188c03e55dSAdrian Chadd 	 * However, if the chip is being programmed /awake/, force
3198c03e55dSAdrian Chadd 	 * the chip awake so we stay awake.
3208c03e55dSAdrian Chadd 	 */
3218c03e55dSAdrian Chadd 	if ((sc->sc_powersave_refcnt == 0 || power_state == HAL_PM_AWAKE) &&
322f5c30c4eSAdrian Chadd 	    power_state != sc->sc_cur_powerstate) {
323f5c30c4eSAdrian Chadd 		sc->sc_cur_powerstate = power_state;
324f5c30c4eSAdrian Chadd 		ath_hal_setpower(sc->sc_ah, power_state);
3257d567ed6SAdrian Chadd 
3267d567ed6SAdrian Chadd 		/*
3277d567ed6SAdrian Chadd 		 * If the NIC is force-awake, then set the
3287d567ed6SAdrian Chadd 		 * self-gen frame state appropriately.
3297d567ed6SAdrian Chadd 		 *
3307d567ed6SAdrian Chadd 		 * If the nic is in network sleep or full-sleep,
3317d567ed6SAdrian Chadd 		 * we let the above call leave the self-gen
3327d567ed6SAdrian Chadd 		 * state as "sleep".
3337d567ed6SAdrian Chadd 		 */
3348c03e55dSAdrian Chadd 		if (selfgen &&
3358c03e55dSAdrian Chadd 		    sc->sc_cur_powerstate == HAL_PM_AWAKE &&
3367d567ed6SAdrian Chadd 		    sc->sc_target_selfgen_state != HAL_PM_AWAKE) {
3377d567ed6SAdrian Chadd 			ath_hal_setselfgenpower(sc->sc_ah,
3387d567ed6SAdrian Chadd 			    sc->sc_target_selfgen_state);
3397d567ed6SAdrian Chadd 		}
3407d567ed6SAdrian Chadd 	}
3417d567ed6SAdrian Chadd }
3427d567ed6SAdrian Chadd 
3437d567ed6SAdrian Chadd /*
3447d567ed6SAdrian Chadd  * Set the current self-generated frames state.
3457d567ed6SAdrian Chadd  *
3467d567ed6SAdrian Chadd  * This is separate from the target power mode.  The chip may be
3477d567ed6SAdrian Chadd  * awake but the desired state is "sleep", so frames sent to the
3487d567ed6SAdrian Chadd  * destination has PWRMGT=1 in the 802.11 header.  The NIC also
3497d567ed6SAdrian Chadd  * needs to know to set PWRMGT=1 in self-generated frames.
3507d567ed6SAdrian Chadd  */
3517d567ed6SAdrian Chadd void
3527d567ed6SAdrian Chadd _ath_power_set_selfgen(struct ath_softc *sc, int power_state, const char *file, int line)
3537d567ed6SAdrian Chadd {
3547d567ed6SAdrian Chadd 
3557d567ed6SAdrian Chadd 	ATH_LOCK_ASSERT(sc);
3567d567ed6SAdrian Chadd 
3577d567ed6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) state=%d, refcnt=%d\n",
3587d567ed6SAdrian Chadd 	    __func__,
3597d567ed6SAdrian Chadd 	    file,
3607d567ed6SAdrian Chadd 	    line,
3617d567ed6SAdrian Chadd 	    power_state,
3627d567ed6SAdrian Chadd 	    sc->sc_target_selfgen_state);
3637d567ed6SAdrian Chadd 
3647d567ed6SAdrian Chadd 	sc->sc_target_selfgen_state = power_state;
3657d567ed6SAdrian Chadd 
3667d567ed6SAdrian Chadd 	/*
3677d567ed6SAdrian Chadd 	 * If the NIC is force-awake, then set the power state.
3687d567ed6SAdrian Chadd 	 * Network-state and full-sleep will already transition it to
3697d567ed6SAdrian Chadd 	 * mark self-gen frames as sleeping - and we can't
3707d567ed6SAdrian Chadd 	 * guarantee the NIC is awake to program the self-gen frame
3717d567ed6SAdrian Chadd 	 * setting anyway.
3727d567ed6SAdrian Chadd 	 */
3737d567ed6SAdrian Chadd 	if (sc->sc_cur_powerstate == HAL_PM_AWAKE) {
3747d567ed6SAdrian Chadd 		ath_hal_setselfgenpower(sc->sc_ah, power_state);
375f5c30c4eSAdrian Chadd 	}
376f5c30c4eSAdrian Chadd }
377f5c30c4eSAdrian Chadd 
378f5c30c4eSAdrian Chadd /*
379f5c30c4eSAdrian Chadd  * Set the hardware power mode and take a reference.
380f5c30c4eSAdrian Chadd  *
381f5c30c4eSAdrian Chadd  * This doesn't update the target power mode in the driver;
382f5c30c4eSAdrian Chadd  * it just updates the hardware power state.
383f5c30c4eSAdrian Chadd  *
384f5c30c4eSAdrian Chadd  * XXX it should only ever force the hardware awake; it should
385f5c30c4eSAdrian Chadd  * never be called to set it asleep.
386f5c30c4eSAdrian Chadd  */
387f5c30c4eSAdrian Chadd void
388f5c30c4eSAdrian Chadd _ath_power_set_power_state(struct ath_softc *sc, int power_state, const char *file, int line)
389f5c30c4eSAdrian Chadd {
390f5c30c4eSAdrian Chadd 	ATH_LOCK_ASSERT(sc);
391f5c30c4eSAdrian Chadd 
392f5c30c4eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) state=%d, refcnt=%d\n",
393f5c30c4eSAdrian Chadd 	    __func__,
394f5c30c4eSAdrian Chadd 	    file,
395f5c30c4eSAdrian Chadd 	    line,
396f5c30c4eSAdrian Chadd 	    power_state,
397f5c30c4eSAdrian Chadd 	    sc->sc_powersave_refcnt);
398f5c30c4eSAdrian Chadd 
399f5c30c4eSAdrian Chadd 	sc->sc_powersave_refcnt++;
400f5c30c4eSAdrian Chadd 
4018c03e55dSAdrian Chadd 	/*
4028c03e55dSAdrian Chadd 	 * Only do the power state change if we're not programming
4038c03e55dSAdrian Chadd 	 * it elsewhere.
4048c03e55dSAdrian Chadd 	 */
405f5c30c4eSAdrian Chadd 	if (power_state != sc->sc_cur_powerstate) {
406f5c30c4eSAdrian Chadd 		ath_hal_setpower(sc->sc_ah, power_state);
407f5c30c4eSAdrian Chadd 		sc->sc_cur_powerstate = power_state;
4087d567ed6SAdrian Chadd 		/*
4097d567ed6SAdrian Chadd 		 * Adjust the self-gen powerstate if appropriate.
4107d567ed6SAdrian Chadd 		 */
4117d567ed6SAdrian Chadd 		if (sc->sc_cur_powerstate == HAL_PM_AWAKE &&
4127d567ed6SAdrian Chadd 		    sc->sc_target_selfgen_state != HAL_PM_AWAKE) {
4137d567ed6SAdrian Chadd 			ath_hal_setselfgenpower(sc->sc_ah,
4147d567ed6SAdrian Chadd 			    sc->sc_target_selfgen_state);
4157d567ed6SAdrian Chadd 		}
416f5c30c4eSAdrian Chadd 	}
417f5c30c4eSAdrian Chadd }
418f5c30c4eSAdrian Chadd 
419f5c30c4eSAdrian Chadd /*
420f5c30c4eSAdrian Chadd  * Restore the power save mode to what it once was.
421f5c30c4eSAdrian Chadd  *
422f5c30c4eSAdrian Chadd  * This will decrement the reference counter and once it hits
423f5c30c4eSAdrian Chadd  * zero, it'll restore the powersave state.
424f5c30c4eSAdrian Chadd  */
425f5c30c4eSAdrian Chadd void
426f5c30c4eSAdrian Chadd _ath_power_restore_power_state(struct ath_softc *sc, const char *file, int line)
427f5c30c4eSAdrian Chadd {
428f5c30c4eSAdrian Chadd 
429f5c30c4eSAdrian Chadd 	ATH_LOCK_ASSERT(sc);
430f5c30c4eSAdrian Chadd 
431f5c30c4eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) refcnt=%d, target state=%d\n",
432f5c30c4eSAdrian Chadd 	    __func__,
433f5c30c4eSAdrian Chadd 	    file,
434f5c30c4eSAdrian Chadd 	    line,
435f5c30c4eSAdrian Chadd 	    sc->sc_powersave_refcnt,
436f5c30c4eSAdrian Chadd 	    sc->sc_target_powerstate);
437f5c30c4eSAdrian Chadd 
438f5c30c4eSAdrian Chadd 	if (sc->sc_powersave_refcnt == 0)
439f5c30c4eSAdrian Chadd 		device_printf(sc->sc_dev, "%s: refcnt=0?\n", __func__);
440f5c30c4eSAdrian Chadd 	else
441f5c30c4eSAdrian Chadd 		sc->sc_powersave_refcnt--;
442f5c30c4eSAdrian Chadd 
443f5c30c4eSAdrian Chadd 	if (sc->sc_powersave_refcnt == 0 &&
444f5c30c4eSAdrian Chadd 	    sc->sc_target_powerstate != sc->sc_cur_powerstate) {
445f5c30c4eSAdrian Chadd 		sc->sc_cur_powerstate = sc->sc_target_powerstate;
446f5c30c4eSAdrian Chadd 		ath_hal_setpower(sc->sc_ah, sc->sc_target_powerstate);
447f5c30c4eSAdrian Chadd 	}
4487d567ed6SAdrian Chadd 
4497d567ed6SAdrian Chadd 	/*
4507d567ed6SAdrian Chadd 	 * Adjust the self-gen powerstate if appropriate.
4517d567ed6SAdrian Chadd 	 */
4527d567ed6SAdrian Chadd 	if (sc->sc_cur_powerstate == HAL_PM_AWAKE &&
4537d567ed6SAdrian Chadd 	    sc->sc_target_selfgen_state != HAL_PM_AWAKE) {
4547d567ed6SAdrian Chadd 		ath_hal_setselfgenpower(sc->sc_ah,
4557d567ed6SAdrian Chadd 		    sc->sc_target_selfgen_state);
4567d567ed6SAdrian Chadd 	}
4577d567ed6SAdrian Chadd 
458f5c30c4eSAdrian Chadd }
459f5c30c4eSAdrian Chadd 
4609389d5a9SAdrian Chadd /*
4619389d5a9SAdrian Chadd  * Configure the initial HAL configuration values based on bus
4629389d5a9SAdrian Chadd  * specific parameters.
4639389d5a9SAdrian Chadd  *
4649389d5a9SAdrian Chadd  * Some PCI IDs and other information may need tweaking.
4659389d5a9SAdrian Chadd  *
4669389d5a9SAdrian Chadd  * XXX TODO: ath9k and the Atheros HAL only program comm2g_switch_enable
4679389d5a9SAdrian Chadd  * if BT antenna diversity isn't enabled.
4689389d5a9SAdrian Chadd  *
4699389d5a9SAdrian Chadd  * So, let's also figure out how to enable BT diversity for AR9485.
4709389d5a9SAdrian Chadd  */
4719389d5a9SAdrian Chadd static void
4729389d5a9SAdrian Chadd ath_setup_hal_config(struct ath_softc *sc, HAL_OPS_CONFIG *ah_config)
4739389d5a9SAdrian Chadd {
4749389d5a9SAdrian Chadd 	/* XXX TODO: only for PCI devices? */
4759389d5a9SAdrian Chadd 
4769389d5a9SAdrian Chadd 	if (sc->sc_pci_devinfo & (ATH_PCI_CUS198 | ATH_PCI_CUS230)) {
4779389d5a9SAdrian Chadd 		ah_config->ath_hal_ext_lna_ctl_gpio = 0x200; /* bit 9 */
4789389d5a9SAdrian Chadd 		ah_config->ath_hal_ext_atten_margin_cfg = AH_TRUE;
4799389d5a9SAdrian Chadd 		ah_config->ath_hal_min_gainidx = AH_TRUE;
4809389d5a9SAdrian Chadd 		ah_config->ath_hal_ant_ctrl_comm2g_switch_enable = 0x000bbb88;
4819389d5a9SAdrian Chadd 		/* XXX low_rssi_thresh */
4829389d5a9SAdrian Chadd 		/* XXX fast_div_bias */
4839389d5a9SAdrian Chadd 		device_printf(sc->sc_dev, "configuring for %s\n",
4849389d5a9SAdrian Chadd 		    (sc->sc_pci_devinfo & ATH_PCI_CUS198) ?
4859389d5a9SAdrian Chadd 		    "CUS198" : "CUS230");
4869389d5a9SAdrian Chadd 	}
4879389d5a9SAdrian Chadd 
4889389d5a9SAdrian Chadd 	if (sc->sc_pci_devinfo & ATH_PCI_CUS217)
4899389d5a9SAdrian Chadd 		device_printf(sc->sc_dev, "CUS217 card detected\n");
4909389d5a9SAdrian Chadd 
4919389d5a9SAdrian Chadd 	if (sc->sc_pci_devinfo & ATH_PCI_CUS252)
4929389d5a9SAdrian Chadd 		device_printf(sc->sc_dev, "CUS252 card detected\n");
4939389d5a9SAdrian Chadd 
4949389d5a9SAdrian Chadd 	if (sc->sc_pci_devinfo & ATH_PCI_AR9565_1ANT)
4959389d5a9SAdrian Chadd 		device_printf(sc->sc_dev, "WB335 1-ANT card detected\n");
4969389d5a9SAdrian Chadd 
4979389d5a9SAdrian Chadd 	if (sc->sc_pci_devinfo & ATH_PCI_AR9565_2ANT)
4989389d5a9SAdrian Chadd 		device_printf(sc->sc_dev, "WB335 2-ANT card detected\n");
4999389d5a9SAdrian Chadd 
500bcf5fc49SAdrian Chadd 	if (sc->sc_pci_devinfo & ATH_PCI_BT_ANT_DIV)
501bcf5fc49SAdrian Chadd 		device_printf(sc->sc_dev,
502bcf5fc49SAdrian Chadd 		    "Bluetooth Antenna Diversity card detected\n");
503bcf5fc49SAdrian Chadd 
5049389d5a9SAdrian Chadd 	if (sc->sc_pci_devinfo & ATH_PCI_KILLER)
5059389d5a9SAdrian Chadd 		device_printf(sc->sc_dev, "Killer Wireless card detected\n");
5069389d5a9SAdrian Chadd 
5079389d5a9SAdrian Chadd #if 0
5089389d5a9SAdrian Chadd         /*
5099389d5a9SAdrian Chadd          * Some WB335 cards do not support antenna diversity. Since
5109389d5a9SAdrian Chadd          * we use a hardcoded value for AR9565 instead of using the
5119389d5a9SAdrian Chadd          * EEPROM/OTP data, remove the combining feature from
5129389d5a9SAdrian Chadd          * the HW capabilities bitmap.
5139389d5a9SAdrian Chadd          */
5149389d5a9SAdrian Chadd         if (sc->sc_pci_devinfo & (ATH9K_PCI_AR9565_1ANT | ATH9K_PCI_AR9565_2ANT)) {
5159389d5a9SAdrian Chadd                 if (!(sc->sc_pci_devinfo & ATH9K_PCI_BT_ANT_DIV))
5169389d5a9SAdrian Chadd                         pCap->hw_caps &= ~ATH9K_HW_CAP_ANT_DIV_COMB;
5179389d5a9SAdrian Chadd         }
5189389d5a9SAdrian Chadd 
5199389d5a9SAdrian Chadd         if (sc->sc_pci_devinfo & ATH9K_PCI_BT_ANT_DIV) {
5209389d5a9SAdrian Chadd                 pCap->hw_caps |= ATH9K_HW_CAP_BT_ANT_DIV;
5219389d5a9SAdrian Chadd                 device_printf(sc->sc_dev, "Set BT/WLAN RX diversity capability\n");
5229389d5a9SAdrian Chadd         }
5239389d5a9SAdrian Chadd #endif
5249389d5a9SAdrian Chadd 
5259389d5a9SAdrian Chadd         if (sc->sc_pci_devinfo & ATH_PCI_D3_L1_WAR) {
5269389d5a9SAdrian Chadd                 ah_config->ath_hal_pcie_waen = 0x0040473b;
5279389d5a9SAdrian Chadd                 device_printf(sc->sc_dev, "Enable WAR for ASPM D3/L1\n");
5289389d5a9SAdrian Chadd         }
5299389d5a9SAdrian Chadd 
5309389d5a9SAdrian Chadd #if 0
5319389d5a9SAdrian Chadd         if (sc->sc_pci_devinfo & ATH9K_PCI_NO_PLL_PWRSAVE) {
5329389d5a9SAdrian Chadd                 ah->config.no_pll_pwrsave = true;
5339389d5a9SAdrian Chadd                 device_printf(sc->sc_dev, "Disable PLL PowerSave\n");
5349389d5a9SAdrian Chadd         }
5359389d5a9SAdrian Chadd #endif
5369389d5a9SAdrian Chadd 
5379389d5a9SAdrian Chadd }
5389389d5a9SAdrian Chadd 
539240b1f1dSAdrian Chadd /*
540240b1f1dSAdrian Chadd  * Attempt to fetch the MAC address from the kernel environment.
541240b1f1dSAdrian Chadd  *
542240b1f1dSAdrian Chadd  * Returns 0, macaddr in macaddr if successful; -1 otherwise.
543240b1f1dSAdrian Chadd  */
544240b1f1dSAdrian Chadd static int
545240b1f1dSAdrian Chadd ath_fetch_mac_kenv(struct ath_softc *sc, uint8_t *macaddr)
546240b1f1dSAdrian Chadd {
547240b1f1dSAdrian Chadd 	char devid_str[32];
548240b1f1dSAdrian Chadd 	int local_mac = 0;
549240b1f1dSAdrian Chadd 	char *local_macstr;
550240b1f1dSAdrian Chadd 
551240b1f1dSAdrian Chadd 	/*
552240b1f1dSAdrian Chadd 	 * Fetch from the kenv rather than using hints.
553240b1f1dSAdrian Chadd 	 *
554240b1f1dSAdrian Chadd 	 * Hints would be nice but the transition to dynamic
555240b1f1dSAdrian Chadd 	 * hints/kenv doesn't happen early enough for this
556240b1f1dSAdrian Chadd 	 * to work reliably (eg on anything embedded.)
557240b1f1dSAdrian Chadd 	 */
558240b1f1dSAdrian Chadd 	snprintf(devid_str, 32, "hint.%s.%d.macaddr",
559240b1f1dSAdrian Chadd 	    device_get_name(sc->sc_dev),
560240b1f1dSAdrian Chadd 	    device_get_unit(sc->sc_dev));
561240b1f1dSAdrian Chadd 
562240b1f1dSAdrian Chadd 	if ((local_macstr = kern_getenv(devid_str)) != NULL) {
563240b1f1dSAdrian Chadd 		uint32_t tmpmac[ETHER_ADDR_LEN];
564240b1f1dSAdrian Chadd 		int count;
565240b1f1dSAdrian Chadd 		int i;
566240b1f1dSAdrian Chadd 
567240b1f1dSAdrian Chadd 		/* Have a MAC address; should use it */
568240b1f1dSAdrian Chadd 		device_printf(sc->sc_dev,
569240b1f1dSAdrian Chadd 		    "Overriding MAC address from environment: '%s'\n",
570240b1f1dSAdrian Chadd 		    local_macstr);
571240b1f1dSAdrian Chadd 
572240b1f1dSAdrian Chadd 		/* Extract out the MAC address */
573240b1f1dSAdrian Chadd 		count = sscanf(local_macstr, "%x%*c%x%*c%x%*c%x%*c%x%*c%x",
574240b1f1dSAdrian Chadd 		    &tmpmac[0], &tmpmac[1],
575240b1f1dSAdrian Chadd 		    &tmpmac[2], &tmpmac[3],
576240b1f1dSAdrian Chadd 		    &tmpmac[4], &tmpmac[5]);
577240b1f1dSAdrian Chadd 		if (count == 6) {
578240b1f1dSAdrian Chadd 			/* Valid! */
579240b1f1dSAdrian Chadd 			local_mac = 1;
580240b1f1dSAdrian Chadd 			for (i = 0; i < ETHER_ADDR_LEN; i++)
581240b1f1dSAdrian Chadd 				macaddr[i] = tmpmac[i];
582240b1f1dSAdrian Chadd 		}
583240b1f1dSAdrian Chadd 		/* Done! */
584240b1f1dSAdrian Chadd 		freeenv(local_macstr);
585240b1f1dSAdrian Chadd 		local_macstr = NULL;
586240b1f1dSAdrian Chadd 	}
587240b1f1dSAdrian Chadd 
588240b1f1dSAdrian Chadd 	if (local_mac)
589240b1f1dSAdrian Chadd 		return (0);
590240b1f1dSAdrian Chadd 	return (-1);
591240b1f1dSAdrian Chadd }
592240b1f1dSAdrian Chadd 
59367397d39SAdrian Chadd #define	HAL_MODE_HT20 (HAL_MODE_11NG_HT20 | HAL_MODE_11NA_HT20)
59467397d39SAdrian Chadd #define	HAL_MODE_HT40 \
59567397d39SAdrian Chadd 	(HAL_MODE_11NG_HT40PLUS | HAL_MODE_11NG_HT40MINUS | \
59667397d39SAdrian Chadd 	HAL_MODE_11NA_HT40PLUS | HAL_MODE_11NA_HT40MINUS)
5975591b213SSam Leffler int
5985591b213SSam Leffler ath_attach(u_int16_t devid, struct ath_softc *sc)
5995591b213SSam Leffler {
6007a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
601fc74a9f9SBrooks Davis 	struct ath_hal *ah = NULL;
6025591b213SSam Leffler 	HAL_STATUS status;
603c42a7b7eSSam Leffler 	int error = 0, i;
604411373ebSSam Leffler 	u_int wmodes;
605a865860dSAdrian Chadd 	int rx_chainmask, tx_chainmask;
6069389d5a9SAdrian Chadd 	HAL_OPS_CONFIG ah_config;
6075591b213SSam Leffler 
608c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
6095591b213SSam Leffler 
61059686fe9SGleb Smirnoff 	ic->ic_softc = sc;
611c8550c02SGleb Smirnoff 	ic->ic_name = device_get_nameunit(sc->sc_dev);
612fc74a9f9SBrooks Davis 
6139389d5a9SAdrian Chadd 	/*
6149389d5a9SAdrian Chadd 	 * Configure the initial configuration data.
6159389d5a9SAdrian Chadd 	 *
6169389d5a9SAdrian Chadd 	 * This is stuff that may be needed early during attach
6179389d5a9SAdrian Chadd 	 * rather than done via configuration calls later.
6189389d5a9SAdrian Chadd 	 */
6199389d5a9SAdrian Chadd 	bzero(&ah_config, sizeof(ah_config));
6209389d5a9SAdrian Chadd 	ath_setup_hal_config(sc, &ah_config);
6219389d5a9SAdrian Chadd 
6227e97436bSAdrian Chadd 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh,
6239389d5a9SAdrian Chadd 	    sc->sc_eepromdata, &ah_config, &status);
6245591b213SSam Leffler 	if (ah == NULL) {
62576e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
62676e6fd5dSGleb Smirnoff 		    "unable to attach hardware; HAL status %u\n", status);
6275591b213SSam Leffler 		error = ENXIO;
6285591b213SSam Leffler 		goto bad;
6295591b213SSam Leffler 	}
6305591b213SSam Leffler 	sc->sc_ah = ah;
631b58b3803SSam Leffler 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
6323297be13SSam Leffler #ifdef	ATH_DEBUG
6333297be13SSam Leffler 	sc->sc_debug = ath_debug;
6343297be13SSam Leffler #endif
6355591b213SSam Leffler 
6365591b213SSam Leffler 	/*
637f8cc9b09SAdrian Chadd 	 * Setup the DMA/EDMA functions based on the current
638f8cc9b09SAdrian Chadd 	 * hardware support.
639f8cc9b09SAdrian Chadd 	 *
640f8cc9b09SAdrian Chadd 	 * This is required before the descriptors are allocated.
641f8cc9b09SAdrian Chadd 	 */
6423d184db2SAdrian Chadd 	if (ath_hal_hasedma(sc->sc_ah)) {
6433d184db2SAdrian Chadd 		sc->sc_isedma = 1;
644f8cc9b09SAdrian Chadd 		ath_recv_setup_edma(sc);
6453fdfc330SAdrian Chadd 		ath_xmit_setup_edma(sc);
6463fdfc330SAdrian Chadd 	} else {
647f8cc9b09SAdrian Chadd 		ath_recv_setup_legacy(sc);
6483fdfc330SAdrian Chadd 		ath_xmit_setup_legacy(sc);
6493fdfc330SAdrian Chadd 	}
650f8cc9b09SAdrian Chadd 
651f5c30c4eSAdrian Chadd 	if (ath_hal_hasmybeacon(sc->sc_ah)) {
652f5c30c4eSAdrian Chadd 		sc->sc_do_mybeacon = 1;
653f5c30c4eSAdrian Chadd 	}
654f5c30c4eSAdrian Chadd 
655f8cc9b09SAdrian Chadd 	/*
656c42a7b7eSSam Leffler 	 * Check if the MAC has multi-rate retry support.
657c42a7b7eSSam Leffler 	 * We do this by trying to setup a fake extended
658c42a7b7eSSam Leffler 	 * descriptor.  MAC's that don't have support will
659c42a7b7eSSam Leffler 	 * return false w/o doing anything.  MAC's that do
660c42a7b7eSSam Leffler 	 * support it will return true w/o doing anything.
661c42a7b7eSSam Leffler 	 */
662c42a7b7eSSam Leffler 	sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
663c42a7b7eSSam Leffler 
664c42a7b7eSSam Leffler 	/*
665c42a7b7eSSam Leffler 	 * Check if the device has hardware counters for PHY
666c42a7b7eSSam Leffler 	 * errors.  If so we need to enable the MIB interrupt
667c42a7b7eSSam Leffler 	 * so we can act on stat triggers.
668c42a7b7eSSam Leffler 	 */
669c42a7b7eSSam Leffler 	if (ath_hal_hwphycounters(ah))
670c42a7b7eSSam Leffler 		sc->sc_needmib = 1;
671c42a7b7eSSam Leffler 
672c42a7b7eSSam Leffler 	/*
673c42a7b7eSSam Leffler 	 * Get the hardware key cache size.
674c42a7b7eSSam Leffler 	 */
675c42a7b7eSSam Leffler 	sc->sc_keymax = ath_hal_keycachesize(ah);
676e8fd88a3SSam Leffler 	if (sc->sc_keymax > ATH_KEYMAX) {
67776e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
67876e6fd5dSGleb Smirnoff 		    "Warning, using only %u of %u key cache slots\n",
679e8fd88a3SSam Leffler 		    ATH_KEYMAX, sc->sc_keymax);
680e8fd88a3SSam Leffler 		sc->sc_keymax = ATH_KEYMAX;
681c42a7b7eSSam Leffler 	}
682c42a7b7eSSam Leffler 	/*
683c42a7b7eSSam Leffler 	 * Reset the key cache since some parts do not
684c42a7b7eSSam Leffler 	 * reset the contents on initial power up.
685c42a7b7eSSam Leffler 	 */
686c42a7b7eSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
687c42a7b7eSSam Leffler 		ath_hal_keyreset(ah, i);
688c42a7b7eSSam Leffler 
689c42a7b7eSSam Leffler 	/*
690b032f27cSSam Leffler 	 * Collect the default channel list.
6915591b213SSam Leffler 	 */
692b032f27cSSam Leffler 	error = ath_getchannels(sc);
6935591b213SSam Leffler 	if (error != 0)
6945591b213SSam Leffler 		goto bad;
6955591b213SSam Leffler 
6965591b213SSam Leffler 	/*
6975591b213SSam Leffler 	 * Setup rate tables for all potential media types.
6985591b213SSam Leffler 	 */
6995591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11A);
7005591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11B);
7015591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11G);
702c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
703c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
70468e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_STURBO_A);
70568e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NA);
70668e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NG);
707724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_HALF);
708724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_QUARTER);
709aaa70f2fSSam Leffler 
710c42a7b7eSSam Leffler 	/* NB: setup here so ath_rate_update is happy */
711c42a7b7eSSam Leffler 	ath_setcurmode(sc, IEEE80211_MODE_11A);
7125591b213SSam Leffler 
713c42a7b7eSSam Leffler 	/*
7143fdfc330SAdrian Chadd 	 * Allocate TX descriptors and populate the lists.
715c42a7b7eSSam Leffler 	 */
7165591b213SSam Leffler 	error = ath_desc_alloc(sc);
7175591b213SSam Leffler 	if (error != 0) {
71876e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
71976e6fd5dSGleb Smirnoff 		    "failed to allocate TX descriptors: %d\n", error);
7203fdfc330SAdrian Chadd 		goto bad;
7213fdfc330SAdrian Chadd 	}
7223fdfc330SAdrian Chadd 	error = ath_txdma_setup(sc);
7233fdfc330SAdrian Chadd 	if (error != 0) {
72476e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
72576e6fd5dSGleb Smirnoff 		    "failed to allocate TX descriptors: %d\n", error);
7265591b213SSam Leffler 		goto bad;
7275591b213SSam Leffler 	}
7283d184db2SAdrian Chadd 
7293fdfc330SAdrian Chadd 	/*
7303fdfc330SAdrian Chadd 	 * Allocate RX descriptors and populate the lists.
7313fdfc330SAdrian Chadd 	 */
7323d184db2SAdrian Chadd 	error = ath_rxdma_setup(sc);
7333d184db2SAdrian Chadd 	if (error != 0) {
73476e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
73576e6fd5dSGleb Smirnoff 		     "failed to allocate RX descriptors: %d\n", error);
7363d184db2SAdrian Chadd 		goto bad;
7373d184db2SAdrian Chadd 	}
7383d184db2SAdrian Chadd 
739adcdc8f2SAdrian Chadd 	callout_init_mtx(&sc->sc_cal_ch, &sc->sc_mtx, 0);
740adcdc8f2SAdrian Chadd 	callout_init_mtx(&sc->sc_wd_ch, &sc->sc_mtx, 0);
7415591b213SSam Leffler 
742f0b2a0beSSam Leffler 	ATH_TXBUF_LOCK_INIT(sc);
7435591b213SSam Leffler 
7440bbf5441SSam Leffler 	sc->sc_tq = taskqueue_create("ath_taskq", M_NOWAIT,
7450bbf5441SSam Leffler 		taskqueue_thread_enqueue, &sc->sc_tq);
7467a79cebfSGleb Smirnoff 	taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, "%s taskq",
7477a79cebfSGleb Smirnoff 	    device_get_nameunit(sc->sc_dev));
7480bbf5441SSam Leffler 
749f8cc9b09SAdrian Chadd 	TASK_INIT(&sc->sc_rxtask, 0, sc->sc_rx.recv_tasklet, sc);
7505591b213SSam Leffler 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
751c42a7b7eSSam Leffler 	TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc);
752d52f7132SAdrian Chadd 	TASK_INIT(&sc->sc_resettask,0, ath_reset_proc, sc);
75303e9308fSAdrian Chadd 	TASK_INIT(&sc->sc_txqtask, 0, ath_txq_sched_tasklet, sc);
754f846cf42SAdrian Chadd 	TASK_INIT(&sc->sc_fataltask, 0, ath_fatal_proc, sc);
7555591b213SSam Leffler 
7565591b213SSam Leffler 	/*
757c42a7b7eSSam Leffler 	 * Allocate hardware transmit queues: one queue for
758c42a7b7eSSam Leffler 	 * beacon frames and one data queue for each QoS
7594fa8d4efSDaniel Eischen 	 * priority.  Note that the hal handles resetting
760c42a7b7eSSam Leffler 	 * these queues at the needed time.
761c42a7b7eSSam Leffler 	 *
762c42a7b7eSSam Leffler 	 * XXX PS-Poll
7635591b213SSam Leffler 	 */
764e1252ce1SAdrian Chadd 	sc->sc_bhalq = ath_beaconq_setup(sc);
7655591b213SSam Leffler 	if (sc->sc_bhalq == (u_int) -1) {
76676e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
76776e6fd5dSGleb Smirnoff 		    "unable to setup a beacon xmit queue!\n");
768c42a7b7eSSam Leffler 		error = EIO;
769b28b4653SSam Leffler 		goto bad2;
7705591b213SSam Leffler 	}
771c42a7b7eSSam Leffler 	sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
772c42a7b7eSSam Leffler 	if (sc->sc_cabq == NULL) {
77376e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "unable to setup CAB xmit queue!\n");
774c42a7b7eSSam Leffler 		error = EIO;
775c42a7b7eSSam Leffler 		goto bad2;
776c42a7b7eSSam Leffler 	}
777c42a7b7eSSam Leffler 	/* NB: insure BK queue is the lowest priority h/w queue */
778c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
77976e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
78076e6fd5dSGleb Smirnoff 		    "unable to setup xmit queue for %s traffic!\n",
781c42a7b7eSSam Leffler 		    ieee80211_wme_acnames[WME_AC_BK]);
782c42a7b7eSSam Leffler 		error = EIO;
783c42a7b7eSSam Leffler 		goto bad2;
784c42a7b7eSSam Leffler 	}
785c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
786c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
787c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
788c42a7b7eSSam Leffler 		/*
789c42a7b7eSSam Leffler 		 * Not enough hardware tx queues to properly do WME;
790c42a7b7eSSam Leffler 		 * just punt and assign them all to the same h/w queue.
791c42a7b7eSSam Leffler 		 * We could do a better job of this if, for example,
792c42a7b7eSSam Leffler 		 * we allocate queues when we switch from station to
793c42a7b7eSSam Leffler 		 * AP mode.
794c42a7b7eSSam Leffler 		 */
795c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_VI] != NULL)
796c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
797c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_BE] != NULL)
798c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
799c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
800c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
801c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
802c42a7b7eSSam Leffler 	}
803c42a7b7eSSam Leffler 
804c42a7b7eSSam Leffler 	/*
805f8418db5SAdrian Chadd 	 * Attach the TX completion function.
806f8418db5SAdrian Chadd 	 *
807f8418db5SAdrian Chadd 	 * The non-EDMA chips may have some special case optimisations;
808f8418db5SAdrian Chadd 	 * this method gives everyone a chance to attach cleanly.
809c42a7b7eSSam Leffler 	 */
810f8418db5SAdrian Chadd 	sc->sc_tx.xmit_attach_comp_func(sc);
811c42a7b7eSSam Leffler 
812c42a7b7eSSam Leffler 	/*
813c42a7b7eSSam Leffler 	 * Setup rate control.  Some rate control modules
814c42a7b7eSSam Leffler 	 * call back to change the anntena state so expose
815c42a7b7eSSam Leffler 	 * the necessary entry points.
816c42a7b7eSSam Leffler 	 * XXX maybe belongs in struct ath_ratectrl?
817c42a7b7eSSam Leffler 	 */
818c42a7b7eSSam Leffler 	sc->sc_setdefantenna = ath_setdefantenna;
819c42a7b7eSSam Leffler 	sc->sc_rc = ath_rate_attach(sc);
820c42a7b7eSSam Leffler 	if (sc->sc_rc == NULL) {
821c42a7b7eSSam Leffler 		error = EIO;
822c42a7b7eSSam Leffler 		goto bad2;
823c42a7b7eSSam Leffler 	}
824c42a7b7eSSam Leffler 
82548237774SAdrian Chadd 	/* Attach DFS module */
82648237774SAdrian Chadd 	if (! ath_dfs_attach(sc)) {
8277e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
8287e97436bSAdrian Chadd 		    "%s: unable to attach DFS\n", __func__);
82948237774SAdrian Chadd 		error = EIO;
83048237774SAdrian Chadd 		goto bad2;
83148237774SAdrian Chadd 	}
83248237774SAdrian Chadd 
8339af351f9SAdrian Chadd 	/* Attach spectral module */
8349af351f9SAdrian Chadd 	if (ath_spectral_attach(sc) < 0) {
8359af351f9SAdrian Chadd 		device_printf(sc->sc_dev,
8369af351f9SAdrian Chadd 		    "%s: unable to attach spectral\n", __func__);
8379af351f9SAdrian Chadd 		error = EIO;
8389af351f9SAdrian Chadd 		goto bad2;
8399af351f9SAdrian Chadd 	}
8409af351f9SAdrian Chadd 
841b70f530bSAdrian Chadd 	/* Attach bluetooth coexistence module */
842b70f530bSAdrian Chadd 	if (ath_btcoex_attach(sc) < 0) {
843b70f530bSAdrian Chadd 		device_printf(sc->sc_dev,
844b70f530bSAdrian Chadd 		    "%s: unable to attach bluetooth coexistence\n", __func__);
845b70f530bSAdrian Chadd 		error = EIO;
846b70f530bSAdrian Chadd 		goto bad2;
847b70f530bSAdrian Chadd 	}
848b70f530bSAdrian Chadd 
849216ca234SAdrian Chadd 	/* Attach LNA diversity module */
850216ca234SAdrian Chadd 	if (ath_lna_div_attach(sc) < 0) {
851216ca234SAdrian Chadd 		device_printf(sc->sc_dev,
852216ca234SAdrian Chadd 		    "%s: unable to attach LNA diversity\n", __func__);
853216ca234SAdrian Chadd 		error = EIO;
854216ca234SAdrian Chadd 		goto bad2;
855216ca234SAdrian Chadd 	}
856216ca234SAdrian Chadd 
85748237774SAdrian Chadd 	/* Start DFS processing tasklet */
85848237774SAdrian Chadd 	TASK_INIT(&sc->sc_dfstask, 0, ath_dfs_tasklet, sc);
85948237774SAdrian Chadd 
8603440495aSAdrian Chadd 	/* Configure LED state */
8613e50ec2cSSam Leffler 	sc->sc_blinking = 0;
862c42a7b7eSSam Leffler 	sc->sc_ledstate = 1;
8633e50ec2cSSam Leffler 	sc->sc_ledon = 0;			/* low true */
8643e50ec2cSSam Leffler 	sc->sc_ledidle = (2700*hz)/1000;	/* 2.7sec */
865fd90e2edSJung-uk Kim 	callout_init(&sc->sc_ledtimer, 1);
8663440495aSAdrian Chadd 
8673440495aSAdrian Chadd 	/*
8683440495aSAdrian Chadd 	 * Don't setup hardware-based blinking.
8693440495aSAdrian Chadd 	 *
8703440495aSAdrian Chadd 	 * Although some NICs may have this configured in the
8713440495aSAdrian Chadd 	 * default reset register values, the user may wish
8723440495aSAdrian Chadd 	 * to alter which pins have which function.
8733440495aSAdrian Chadd 	 *
8743440495aSAdrian Chadd 	 * The reference driver attaches the MAC network LED to GPIO1 and
8753440495aSAdrian Chadd 	 * the MAC power LED to GPIO2.  However, the DWA-552 cardbus
8763440495aSAdrian Chadd 	 * NIC has these reversed.
8773440495aSAdrian Chadd 	 */
8783440495aSAdrian Chadd 	sc->sc_hardled = (1 == 0);
8793440495aSAdrian Chadd 	sc->sc_led_net_pin = -1;
8803440495aSAdrian Chadd 	sc->sc_led_pwr_pin = -1;
881c42a7b7eSSam Leffler 	/*
882c42a7b7eSSam Leffler 	 * Auto-enable soft led processing for IBM cards and for
883c42a7b7eSSam Leffler 	 * 5211 minipci cards.  Users can also manually enable/disable
884c42a7b7eSSam Leffler 	 * support with a sysctl.
885c42a7b7eSSam Leffler 	 */
886c42a7b7eSSam Leffler 	sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
8876558ffd9SAdrian Chadd 	ath_led_config(sc);
888a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
8895591b213SSam Leffler 
8905591b213SSam Leffler 	/* XXX not right but it's not used anywhere important */
8915591b213SSam Leffler 	ic->ic_phytype = IEEE80211_T_OFDM;
8925591b213SSam Leffler 	ic->ic_opmode = IEEE80211_M_STA;
893c42a7b7eSSam Leffler 	ic->ic_caps =
894c43feedeSSam Leffler 		  IEEE80211_C_STA		/* station mode */
895c43feedeSSam Leffler 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
896fe32c3efSSam Leffler 		| IEEE80211_C_HOSTAP		/* hostap mode */
897fe32c3efSSam Leffler 		| IEEE80211_C_MONITOR		/* monitor mode */
8987a04dc27SSam Leffler 		| IEEE80211_C_AHDEMO		/* adhoc demo mode */
899b032f27cSSam Leffler 		| IEEE80211_C_WDS		/* 4-address traffic works */
90059aa14a9SRui Paulo 		| IEEE80211_C_MBSS		/* mesh point link mode */
901fe32c3efSSam Leffler 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
902c42a7b7eSSam Leffler 		| IEEE80211_C_SHSLOT		/* short slot time supported */
903c42a7b7eSSam Leffler 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
9043b324f57SAdrian Chadd #ifndef	ATH_ENABLE_11N
90568e8e04eSSam Leffler 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
9063b324f57SAdrian Chadd #endif
90768e8e04eSSam Leffler 		| IEEE80211_C_TXFRAG		/* handle tx frags */
90810dc8de4SAdrian Chadd #ifdef	ATH_ENABLE_DFS
9097e97436bSAdrian Chadd 		| IEEE80211_C_DFS		/* Enable radar detection */
91010dc8de4SAdrian Chadd #endif
911f5c30c4eSAdrian Chadd 		| IEEE80211_C_PMGT		/* Station side power mgmt */
912f5c30c4eSAdrian Chadd 		| IEEE80211_C_SWSLEEP
91301e7e035SSam Leffler 		;
914c42a7b7eSSam Leffler 	/*
915c42a7b7eSSam Leffler 	 * Query the hal to figure out h/w crypto support.
916c42a7b7eSSam Leffler 	 */
917c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
918b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
919c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
920b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_OCB;
921c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
922b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_CCM;
923c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
924b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_CKIP;
925c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
926b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIP;
927c42a7b7eSSam Leffler 		/*
928c42a7b7eSSam Leffler 		 * Check if h/w does the MIC and/or whether the
929c42a7b7eSSam Leffler 		 * separate key cache entries are required to
930c42a7b7eSSam Leffler 		 * handle both tx+rx MIC keys.
931c42a7b7eSSam Leffler 		 */
932c42a7b7eSSam Leffler 		if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
933b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
9345901d2d3SSam Leffler 		/*
9355901d2d3SSam Leffler 		 * If the h/w supports storing tx+rx MIC keys
9365901d2d3SSam Leffler 		 * in one cache slot automatically enable use.
9375901d2d3SSam Leffler 		 */
9385901d2d3SSam Leffler 		if (ath_hal_hastkipsplit(ah) ||
9395901d2d3SSam Leffler 		    !ath_hal_settkipsplit(ah, AH_FALSE))
940c42a7b7eSSam Leffler 			sc->sc_splitmic = 1;
941b032f27cSSam Leffler 		/*
942b032f27cSSam Leffler 		 * If the h/w can do TKIP MIC together with WME then
943b032f27cSSam Leffler 		 * we use it; otherwise we force the MIC to be done
944b032f27cSSam Leffler 		 * in software by the net80211 layer.
945b032f27cSSam Leffler 		 */
946b032f27cSSam Leffler 		if (ath_hal_haswmetkipmic(ah))
947b032f27cSSam Leffler 			sc->sc_wmetkipmic = 1;
948c42a7b7eSSam Leffler 	}
949e8fd88a3SSam Leffler 	sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR);
9509ac01d39SRui Paulo 	/*
9511ac5dac2SRui Paulo 	 * Check for multicast key search support.
9529ac01d39SRui Paulo 	 */
9539ac01d39SRui Paulo 	if (ath_hal_hasmcastkeysearch(sc->sc_ah) &&
9549ac01d39SRui Paulo 	    !ath_hal_getmcastkeysearch(sc->sc_ah)) {
9559ac01d39SRui Paulo 		ath_hal_setmcastkeysearch(sc->sc_ah, 1);
9569ac01d39SRui Paulo 	}
957e8fd88a3SSam Leffler 	sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah);
958c42a7b7eSSam Leffler 	/*
9595901d2d3SSam Leffler 	 * Mark key cache slots associated with global keys
9605901d2d3SSam Leffler 	 * as in use.  If we knew TKIP was not to be used we
9615901d2d3SSam Leffler 	 * could leave the +32, +64, and +32+64 slots free.
9625901d2d3SSam Leffler 	 */
9635901d2d3SSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
9645901d2d3SSam Leffler 		setbit(sc->sc_keymap, i);
9655901d2d3SSam Leffler 		setbit(sc->sc_keymap, i+64);
9665901d2d3SSam Leffler 		if (sc->sc_splitmic) {
9675901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32);
9685901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32+64);
9695901d2d3SSam Leffler 		}
9705901d2d3SSam Leffler 	}
9715901d2d3SSam Leffler 	/*
972c42a7b7eSSam Leffler 	 * TPC support can be done either with a global cap or
973c42a7b7eSSam Leffler 	 * per-packet support.  The latter is not available on
974c42a7b7eSSam Leffler 	 * all parts.  We're a bit pedantic here as all parts
975c42a7b7eSSam Leffler 	 * support a global cap.
976c42a7b7eSSam Leffler 	 */
977c59005e9SSam Leffler 	if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah))
978c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TXPMGT;
979c42a7b7eSSam Leffler 
980c42a7b7eSSam Leffler 	/*
981c42a7b7eSSam Leffler 	 * Mark WME capability only if we have sufficient
982c42a7b7eSSam Leffler 	 * hardware queues to do proper priority scheduling.
983c42a7b7eSSam Leffler 	 */
984c42a7b7eSSam Leffler 	if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
985c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_WME;
986c42a7b7eSSam Leffler 	/*
987e8fd88a3SSam Leffler 	 * Check for misc other capabilities.
988c42a7b7eSSam Leffler 	 */
989c42a7b7eSSam Leffler 	if (ath_hal_hasbursting(ah))
990c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_BURST;
991b032f27cSSam Leffler 	sc->sc_hasbmask = ath_hal_hasbssidmask(ah);
99259aa14a9SRui Paulo 	sc->sc_hasbmatch = ath_hal_hasbssidmatch(ah);
993b032f27cSSam Leffler 	sc->sc_hastsfadd = ath_hal_hastsfadjust(ah);
9948a2a6beeSAdrian Chadd 	sc->sc_rxslink = ath_hal_self_linked_final_rxdesc(ah);
99551558243SAdrian Chadd 
99651558243SAdrian Chadd 	/* XXX TODO: just make this a "store tx/rx timestamp length" operation */
99751558243SAdrian Chadd 	if (ath_hal_get_rx_tsf_prec(ah, &i)) {
99851558243SAdrian Chadd 		if (i == 32) {
99951558243SAdrian Chadd 			sc->sc_rxtsf32 = 1;
100051558243SAdrian Chadd 		}
100151558243SAdrian Chadd 		if (bootverbose)
100251558243SAdrian Chadd 			device_printf(sc->sc_dev, "RX timestamp: %d bits\n", i);
100351558243SAdrian Chadd 	}
100451558243SAdrian Chadd 	if (ath_hal_get_tx_tsf_prec(ah, &i)) {
100551558243SAdrian Chadd 		if (bootverbose)
100651558243SAdrian Chadd 			device_printf(sc->sc_dev, "TX timestamp: %d bits\n", i);
100751558243SAdrian Chadd 	}
100851558243SAdrian Chadd 
1009dd6a574eSAdrian Chadd 	sc->sc_hasenforcetxop = ath_hal_hasenforcetxop(ah);
10103df7a8abSAdrian Chadd 	sc->sc_rx_lnamixer = ath_hal_hasrxlnamixer(ah);
1011216ca234SAdrian Chadd 	sc->sc_hasdivcomb = ath_hal_hasdivantcomb(ah);
1012216ca234SAdrian Chadd 
101368e8e04eSSam Leffler 	if (ath_hal_hasfastframes(ah))
101468e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_FF;
101559efa8b5SSam Leffler 	wmodes = ath_hal_getwirelessmodes(ah);
1016411373ebSSam Leffler 	if (wmodes & (HAL_MODE_108G|HAL_MODE_TURBO))
101768e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TURBOP;
1018584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
101910ad9a77SSam Leffler 	if (ath_hal_macversion(ah) > 0x78) {
102010ad9a77SSam Leffler 		ic->ic_caps |= IEEE80211_C_TDMA; /* capable of TDMA */
102110ad9a77SSam Leffler 		ic->ic_tdma_update = ath_tdma_update;
102210ad9a77SSam Leffler 	}
102310ad9a77SSam Leffler #endif
102467397d39SAdrian Chadd 
102567397d39SAdrian Chadd 	/*
10269c85ff91SAdrian Chadd 	 * TODO: enforce that at least this many frames are available
10279c85ff91SAdrian Chadd 	 * in the txbuf list before allowing data frames (raw or
10289c85ff91SAdrian Chadd 	 * otherwise) to be transmitted.
10299c85ff91SAdrian Chadd 	 */
10309c85ff91SAdrian Chadd 	sc->sc_txq_data_minfree = 10;
10319c85ff91SAdrian Chadd 	/*
10329c85ff91SAdrian Chadd 	 * Leave this as default to maintain legacy behaviour.
10339c85ff91SAdrian Chadd 	 * Shortening the cabq/mcastq may end up causing some
10349c85ff91SAdrian Chadd 	 * undesirable behaviour.
10359c85ff91SAdrian Chadd 	 */
10369c85ff91SAdrian Chadd 	sc->sc_txq_mcastq_maxdepth = ath_txbuf;
10379c85ff91SAdrian Chadd 
10387dcb2beaSAdrian Chadd 	/*
103922a3aee6SAdrian Chadd 	 * How deep can the node software TX queue get whilst it's asleep.
104022a3aee6SAdrian Chadd 	 */
104122a3aee6SAdrian Chadd 	sc->sc_txq_node_psq_maxdepth = 16;
104222a3aee6SAdrian Chadd 
104322a3aee6SAdrian Chadd 	/*
10447dcb2beaSAdrian Chadd 	 * Default the maximum queue depth for a given node
10457dcb2beaSAdrian Chadd 	 * to 1/4'th the TX buffers, or 64, whichever
10467dcb2beaSAdrian Chadd 	 * is larger.
10477dcb2beaSAdrian Chadd 	 */
10487dcb2beaSAdrian Chadd 	sc->sc_txq_node_maxdepth = MAX(64, ath_txbuf / 4);
10497dcb2beaSAdrian Chadd 
1050b837332dSAdrian Chadd 	/* Enable CABQ by default */
1051b837332dSAdrian Chadd 	sc->sc_cabq_enable = 1;
1052b837332dSAdrian Chadd 
10539c85ff91SAdrian Chadd 	/*
1054a865860dSAdrian Chadd 	 * Allow the TX and RX chainmasks to be overridden by
1055a865860dSAdrian Chadd 	 * environment variables and/or device.hints.
1056a865860dSAdrian Chadd 	 *
1057a865860dSAdrian Chadd 	 * This must be done early - before the hardware is
1058a865860dSAdrian Chadd 	 * calibrated or before the 802.11n stream calculation
1059a865860dSAdrian Chadd 	 * is done.
1060a865860dSAdrian Chadd 	 */
1061a865860dSAdrian Chadd 	if (resource_int_value(device_get_name(sc->sc_dev),
1062a865860dSAdrian Chadd 	    device_get_unit(sc->sc_dev), "rx_chainmask",
1063a865860dSAdrian Chadd 	    &rx_chainmask) == 0) {
1064a865860dSAdrian Chadd 		device_printf(sc->sc_dev, "Setting RX chainmask to 0x%x\n",
1065a865860dSAdrian Chadd 		    rx_chainmask);
1066a865860dSAdrian Chadd 		(void) ath_hal_setrxchainmask(sc->sc_ah, rx_chainmask);
1067a865860dSAdrian Chadd 	}
1068a865860dSAdrian Chadd 	if (resource_int_value(device_get_name(sc->sc_dev),
1069a865860dSAdrian Chadd 	    device_get_unit(sc->sc_dev), "tx_chainmask",
1070a865860dSAdrian Chadd 	    &tx_chainmask) == 0) {
1071a865860dSAdrian Chadd 		device_printf(sc->sc_dev, "Setting TX chainmask to 0x%x\n",
1072a865860dSAdrian Chadd 		    tx_chainmask);
1073dc8552d5SAdrian Chadd 		(void) ath_hal_settxchainmask(sc->sc_ah, tx_chainmask);
1074a865860dSAdrian Chadd 	}
1075a865860dSAdrian Chadd 
1076af017101SAdrian Chadd 	/*
1077ff5b5634SAdrian Chadd 	 * Query the TX/RX chainmask configuration.
1078ff5b5634SAdrian Chadd 	 *
1079ff5b5634SAdrian Chadd 	 * This is only relevant for 11n devices.
1080ff5b5634SAdrian Chadd 	 */
1081ff5b5634SAdrian Chadd 	ath_hal_getrxchainmask(ah, &sc->sc_rxchainmask);
1082ff5b5634SAdrian Chadd 	ath_hal_gettxchainmask(ah, &sc->sc_txchainmask);
1083ff5b5634SAdrian Chadd 
1084ff5b5634SAdrian Chadd 	/*
1085af017101SAdrian Chadd 	 * Disable MRR with protected frames by default.
1086af017101SAdrian Chadd 	 * Only 802.11n series NICs can handle this.
1087af017101SAdrian Chadd 	 */
1088af017101SAdrian Chadd 	sc->sc_mrrprot = 0;	/* XXX should be a capability */
1089af017101SAdrian Chadd 
10905540369bSAdrian Chadd 	/*
10915540369bSAdrian Chadd 	 * Query the enterprise mode information the HAL.
10925540369bSAdrian Chadd 	 */
10935540369bSAdrian Chadd 	if (ath_hal_getcapability(ah, HAL_CAP_ENTERPRISE_MODE, 0,
10945540369bSAdrian Chadd 	    &sc->sc_ent_cfg) == HAL_OK)
10955540369bSAdrian Chadd 		sc->sc_use_ent = 1;
10965540369bSAdrian Chadd 
10978fd67f92SAdrian Chadd #ifdef	ATH_ENABLE_11N
109867397d39SAdrian Chadd 	/*
109967397d39SAdrian Chadd 	 * Query HT capabilities
110067397d39SAdrian Chadd 	 */
110167397d39SAdrian Chadd 	if (ath_hal_getcapability(ah, HAL_CAP_HT, 0, NULL) == HAL_OK &&
110267397d39SAdrian Chadd 	    (wmodes & (HAL_MODE_HT20 | HAL_MODE_HT40))) {
11036f4fb2d8SAdrian Chadd 		uint32_t rxs, txs;
11049f3a9150SAdrian Chadd 		uint32_t ldpc;
110567397d39SAdrian Chadd 
110667397d39SAdrian Chadd 		device_printf(sc->sc_dev, "[HT] enabling HT modes\n");
1107af017101SAdrian Chadd 
1108af017101SAdrian Chadd 		sc->sc_mrrprot = 1;	/* XXX should be a capability */
1109af017101SAdrian Chadd 
111067397d39SAdrian Chadd 		ic->ic_htcaps = IEEE80211_HTC_HT	/* HT operation */
111167397d39SAdrian Chadd 			    | IEEE80211_HTC_AMPDU	/* A-MPDU tx/rx */
111267397d39SAdrian Chadd 			    | IEEE80211_HTC_AMSDU	/* A-MSDU tx/rx */
11137e97436bSAdrian Chadd 			    | IEEE80211_HTCAP_MAXAMSDU_3839
11147e97436bSAdrian Chadd 			    				/* max A-MSDU length */
111567397d39SAdrian Chadd 			    | IEEE80211_HTCAP_SMPS_OFF;	/* SM power save off */
111667397d39SAdrian Chadd 
111776355edbSAdrian Chadd 		/*
111876355edbSAdrian Chadd 		 * Enable short-GI for HT20 only if the hardware
111976355edbSAdrian Chadd 		 * advertises support.
112076355edbSAdrian Chadd 		 * Notably, anything earlier than the AR9287 doesn't.
112176355edbSAdrian Chadd 		 */
112276355edbSAdrian Chadd 		if ((ath_hal_getcapability(ah,
112376355edbSAdrian Chadd 		    HAL_CAP_HT20_SGI, 0, NULL) == HAL_OK) &&
112476355edbSAdrian Chadd 		    (wmodes & HAL_MODE_HT20)) {
112576355edbSAdrian Chadd 			device_printf(sc->sc_dev,
112676355edbSAdrian Chadd 			    "[HT] enabling short-GI in 20MHz mode\n");
112776355edbSAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
112876355edbSAdrian Chadd 		}
112976355edbSAdrian Chadd 
113067397d39SAdrian Chadd 		if (wmodes & HAL_MODE_HT40)
113167397d39SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40
113267397d39SAdrian Chadd 			    |  IEEE80211_HTCAP_SHORTGI40;
113367397d39SAdrian Chadd 
113467397d39SAdrian Chadd 		/*
11357e97436bSAdrian Chadd 		 * TX/RX streams need to be taken into account when
11367e97436bSAdrian Chadd 		 * negotiating which MCS rates it'll receive and
113767397d39SAdrian Chadd 		 * what MCS rates are available for TX.
113867397d39SAdrian Chadd 		 */
113954517070SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 0, &txs);
114054517070SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 1, &rxs);
114167397d39SAdrian Chadd 		ic->ic_txstream = txs;
114267397d39SAdrian Chadd 		ic->ic_rxstream = rxs;
114367397d39SAdrian Chadd 
11446606ba81SAdrian Chadd 		/*
11456606ba81SAdrian Chadd 		 * Setup TX and RX STBC based on what the HAL allows and
11466606ba81SAdrian Chadd 		 * the currently configured chainmask set.
11476606ba81SAdrian Chadd 		 * Ie - don't enable STBC TX if only one chain is enabled.
11486606ba81SAdrian Chadd 		 * STBC RX is fine on a single RX chain; it just won't
11496606ba81SAdrian Chadd 		 * provide any real benefit.
11506606ba81SAdrian Chadd 		 */
11516606ba81SAdrian Chadd 		if (ath_hal_getcapability(ah, HAL_CAP_RX_STBC, 0,
11526606ba81SAdrian Chadd 		    NULL) == HAL_OK) {
11536606ba81SAdrian Chadd 			sc->sc_rx_stbc = 1;
11546606ba81SAdrian Chadd 			device_printf(sc->sc_dev,
11556606ba81SAdrian Chadd 			    "[HT] 1 stream STBC receive enabled\n");
11566606ba81SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_1STREAM;
11576606ba81SAdrian Chadd 		}
11586606ba81SAdrian Chadd 		if (txs > 1 && ath_hal_getcapability(ah, HAL_CAP_TX_STBC, 0,
11596606ba81SAdrian Chadd 		    NULL) == HAL_OK) {
11606606ba81SAdrian Chadd 			sc->sc_tx_stbc = 1;
11616606ba81SAdrian Chadd 			device_printf(sc->sc_dev,
11626606ba81SAdrian Chadd 			    "[HT] 1 stream STBC transmit enabled\n");
11636606ba81SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
11646606ba81SAdrian Chadd 		}
11656606ba81SAdrian Chadd 
1166ce656facSAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_RTS_AGGR_LIMIT, 1,
1167ce656facSAdrian Chadd 		    &sc->sc_rts_aggr_limit);
1168ce656facSAdrian Chadd 		if (sc->sc_rts_aggr_limit != (64 * 1024))
1169ce656facSAdrian Chadd 			device_printf(sc->sc_dev,
1170ce656facSAdrian Chadd 			    "[HT] RTS aggregates limited to %d KiB\n",
1171ce656facSAdrian Chadd 			    sc->sc_rts_aggr_limit / 1024);
1172ce656facSAdrian Chadd 
11739f3a9150SAdrian Chadd 		/*
11749f3a9150SAdrian Chadd 		 * LDPC
11759f3a9150SAdrian Chadd 		 */
11769f3a9150SAdrian Chadd 		if ((ath_hal_getcapability(ah, HAL_CAP_LDPC, 0, &ldpc))
11779f3a9150SAdrian Chadd 		    == HAL_OK && (ldpc == 1)) {
11789f3a9150SAdrian Chadd 			sc->sc_has_ldpc = 1;
11799f3a9150SAdrian Chadd 			device_printf(sc->sc_dev,
11809f3a9150SAdrian Chadd 			    "[HT] LDPC transmit/receive enabled\n");
11819f3a9150SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_LDPC;
11829f3a9150SAdrian Chadd 		}
11839f3a9150SAdrian Chadd 
11849f3a9150SAdrian Chadd 
11857e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
11867e97436bSAdrian Chadd 		    "[HT] %d RX streams; %d TX streams\n", rxs, txs);
118767397d39SAdrian Chadd 	}
118867397d39SAdrian Chadd #endif
118967397d39SAdrian Chadd 
1190c42a7b7eSSam Leffler 	/*
1191f8aa9fd5SAdrian Chadd 	 * Initial aggregation settings.
1192f8aa9fd5SAdrian Chadd 	 */
119372910f03SAdrian Chadd 	sc->sc_hwq_limit_aggr = ATH_AGGR_MIN_QDEPTH;
119472910f03SAdrian Chadd 	sc->sc_hwq_limit_nonaggr = ATH_NONAGGR_MIN_QDEPTH;
1195f8aa9fd5SAdrian Chadd 	sc->sc_tid_hwq_lo = ATH_AGGR_SCHED_LOW;
1196f8aa9fd5SAdrian Chadd 	sc->sc_tid_hwq_hi = ATH_AGGR_SCHED_HIGH;
11974a502c33SAdrian Chadd 	sc->sc_aggr_limit = ATH_AGGR_MAXSIZE;
1198a54ecf78SAdrian Chadd 	sc->sc_delim_min_pad = 0;
1199f8aa9fd5SAdrian Chadd 
1200f8aa9fd5SAdrian Chadd 	/*
1201ddbe3036SAdrian Chadd 	 * Check if the hardware requires PCI register serialisation.
1202ddbe3036SAdrian Chadd 	 * Some of the Owl based MACs require this.
1203ddbe3036SAdrian Chadd 	 */
1204ddbe3036SAdrian Chadd 	if (mp_ncpus > 1 &&
1205ddbe3036SAdrian Chadd 	    ath_hal_getcapability(ah, HAL_CAP_SERIALISE_WAR,
1206ddbe3036SAdrian Chadd 	     0, NULL) == HAL_OK) {
1207ddbe3036SAdrian Chadd 		sc->sc_ah->ah_config.ah_serialise_reg_war = 1;
12087e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
12097e97436bSAdrian Chadd 		    "Enabling register serialisation\n");
1210ddbe3036SAdrian Chadd 	}
1211ddbe3036SAdrian Chadd 
1212ddbe3036SAdrian Chadd 	/*
1213f0db652cSAdrian Chadd 	 * Initialise the deferred completed RX buffer list.
1214f0db652cSAdrian Chadd 	 */
12155d4dedadSAdrian Chadd 	TAILQ_INIT(&sc->sc_rx_rxlist[HAL_RX_QUEUE_HP]);
12165d4dedadSAdrian Chadd 	TAILQ_INIT(&sc->sc_rx_rxlist[HAL_RX_QUEUE_LP]);
1217f0db652cSAdrian Chadd 
1218f0db652cSAdrian Chadd 	/*
1219c42a7b7eSSam Leffler 	 * Indicate we need the 802.11 header padded to a
1220c42a7b7eSSam Leffler 	 * 32-bit boundary for 4-address and QoS frames.
1221c42a7b7eSSam Leffler 	 */
1222c42a7b7eSSam Leffler 	ic->ic_flags |= IEEE80211_F_DATAPAD;
1223c42a7b7eSSam Leffler 
1224c42a7b7eSSam Leffler 	/*
1225c42a7b7eSSam Leffler 	 * Query the hal about antenna support.
1226c42a7b7eSSam Leffler 	 */
1227c42a7b7eSSam Leffler 	sc->sc_defant = ath_hal_getdefantenna(ah);
1228c42a7b7eSSam Leffler 
1229c42a7b7eSSam Leffler 	/*
1230c42a7b7eSSam Leffler 	 * Not all chips have the VEOL support we want to
1231c42a7b7eSSam Leffler 	 * use with IBSS beacons; check here for it.
1232c42a7b7eSSam Leffler 	 */
1233c42a7b7eSSam Leffler 	sc->sc_hasveol = ath_hal_hasveol(ah);
12345591b213SSam Leffler 
1235240b1f1dSAdrian Chadd 	/* get mac address from kenv first, then hardware */
12367a79cebfSGleb Smirnoff 	if (ath_fetch_mac_kenv(sc, ic->ic_macaddr) == 0) {
12379cecaef7SAdrian Chadd 		/* Tell the HAL now about the new MAC */
12387a79cebfSGleb Smirnoff 		ath_hal_setmac(ah, ic->ic_macaddr);
12399cecaef7SAdrian Chadd 	} else {
12407a79cebfSGleb Smirnoff 		ath_hal_getmac(ah, ic->ic_macaddr);
12419cecaef7SAdrian Chadd 	}
1242240b1f1dSAdrian Chadd 
1243b032f27cSSam Leffler 	if (sc->sc_hasbmask)
1244b032f27cSSam Leffler 		ath_hal_getbssidmask(ah, sc->sc_hwbssidmask);
12455591b213SSam Leffler 
1246b032f27cSSam Leffler 	/* NB: used to size node table key mapping array */
1247b032f27cSSam Leffler 	ic->ic_max_keyix = sc->sc_keymax;
12485591b213SSam Leffler 	/* call MI attach routine. */
12497a79cebfSGleb Smirnoff 	ieee80211_ifattach(ic);
1250b032f27cSSam Leffler 	ic->ic_setregdomain = ath_setregdomain;
1251b032f27cSSam Leffler 	ic->ic_getradiocaps = ath_getradiocaps;
1252b032f27cSSam Leffler 	sc->sc_opmode = HAL_M_STA;
1253b032f27cSSam Leffler 
12545591b213SSam Leffler 	/* override default methods */
12557a79cebfSGleb Smirnoff 	ic->ic_ioctl = ath_ioctl;
12567a79cebfSGleb Smirnoff 	ic->ic_parent = ath_parent;
12577a79cebfSGleb Smirnoff 	ic->ic_transmit = ath_transmit;
1258b032f27cSSam Leffler 	ic->ic_newassoc = ath_newassoc;
1259b032f27cSSam Leffler 	ic->ic_updateslot = ath_updateslot;
1260b032f27cSSam Leffler 	ic->ic_wme.wme_update = ath_wme_update;
1261b032f27cSSam Leffler 	ic->ic_vap_create = ath_vap_create;
1262b032f27cSSam Leffler 	ic->ic_vap_delete = ath_vap_delete;
1263b032f27cSSam Leffler 	ic->ic_raw_xmit = ath_raw_xmit;
1264b032f27cSSam Leffler 	ic->ic_update_mcast = ath_update_mcast;
1265b032f27cSSam Leffler 	ic->ic_update_promisc = ath_update_promisc;
12665591b213SSam Leffler 	ic->ic_node_alloc = ath_node_alloc;
12671e774079SSam Leffler 	sc->sc_node_free = ic->ic_node_free;
12685591b213SSam Leffler 	ic->ic_node_free = ath_node_free;
12694afa805eSAdrian Chadd 	sc->sc_node_cleanup = ic->ic_node_cleanup;
12704afa805eSAdrian Chadd 	ic->ic_node_cleanup = ath_node_cleanup;
127168e8e04eSSam Leffler 	ic->ic_node_getsignal = ath_node_getsignal;
127268e8e04eSSam Leffler 	ic->ic_scan_start = ath_scan_start;
127368e8e04eSSam Leffler 	ic->ic_scan_end = ath_scan_end;
127468e8e04eSSam Leffler 	ic->ic_set_channel = ath_set_channel;
1275fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
1276eb6f0de0SAdrian Chadd 	/* 802.11n specific - but just override anyway */
1277eb6f0de0SAdrian Chadd 	sc->sc_addba_request = ic->ic_addba_request;
1278eb6f0de0SAdrian Chadd 	sc->sc_addba_response = ic->ic_addba_response;
1279eb6f0de0SAdrian Chadd 	sc->sc_addba_stop = ic->ic_addba_stop;
1280eb6f0de0SAdrian Chadd 	sc->sc_bar_response = ic->ic_bar_response;
1281eb6f0de0SAdrian Chadd 	sc->sc_addba_response_timeout = ic->ic_addba_response_timeout;
1282eb6f0de0SAdrian Chadd 
1283eb6f0de0SAdrian Chadd 	ic->ic_addba_request = ath_addba_request;
1284eb6f0de0SAdrian Chadd 	ic->ic_addba_response = ath_addba_response;
1285eb6f0de0SAdrian Chadd 	ic->ic_addba_response_timeout = ath_addba_response_timeout;
1286eb6f0de0SAdrian Chadd 	ic->ic_addba_stop = ath_addba_stop;
1287eb6f0de0SAdrian Chadd 	ic->ic_bar_response = ath_bar_response;
1288eb6f0de0SAdrian Chadd 
1289fdd72b4aSAdrian Chadd 	ic->ic_update_chw = ath_update_chw;
1290fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
1291fdd72b4aSAdrian Chadd 
1292e1b5ab97SAdrian Chadd #ifdef	ATH_ENABLE_RADIOTAP_VENDOR_EXT
1293e1b5ab97SAdrian Chadd 	/*
1294e1b5ab97SAdrian Chadd 	 * There's one vendor bitmap entry in the RX radiotap
1295e1b5ab97SAdrian Chadd 	 * header; make sure that's taken into account.
1296e1b5ab97SAdrian Chadd 	 */
1297e1b5ab97SAdrian Chadd 	ieee80211_radiotap_attachv(ic,
1298e1b5ab97SAdrian Chadd 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th), 0,
1299e1b5ab97SAdrian Chadd 		ATH_TX_RADIOTAP_PRESENT,
1300e1b5ab97SAdrian Chadd 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th), 1,
1301e1b5ab97SAdrian Chadd 		ATH_RX_RADIOTAP_PRESENT);
1302e1b5ab97SAdrian Chadd #else
1303e1b5ab97SAdrian Chadd 	/*
1304e1b5ab97SAdrian Chadd 	 * No vendor bitmap/extensions are present.
1305e1b5ab97SAdrian Chadd 	 */
13065463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
13075463c4a4SSam Leffler 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
13085463c4a4SSam Leffler 		ATH_TX_RADIOTAP_PRESENT,
13095463c4a4SSam Leffler 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
13105463c4a4SSam Leffler 		ATH_RX_RADIOTAP_PRESENT);
1311e1b5ab97SAdrian Chadd #endif	/* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
13125463c4a4SSam Leffler 
13134866e6c2SSam Leffler 	/*
1314bdbb6e5bSAdrian Chadd 	 * Setup the ALQ logging if required
1315bdbb6e5bSAdrian Chadd 	 */
131689d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
1317bdbb6e5bSAdrian Chadd 	if_ath_alq_init(&sc->sc_alq, device_get_nameunit(sc->sc_dev));
1318bb327d28SAdrian Chadd 	if_ath_alq_setcfg(&sc->sc_alq,
1319bb327d28SAdrian Chadd 	    sc->sc_ah->ah_macVersion,
1320bb327d28SAdrian Chadd 	    sc->sc_ah->ah_macRev,
1321bb327d28SAdrian Chadd 	    sc->sc_ah->ah_phyRev,
1322bb327d28SAdrian Chadd 	    sc->sc_ah->ah_magic);
1323bdbb6e5bSAdrian Chadd #endif
1324bdbb6e5bSAdrian Chadd 
1325bdbb6e5bSAdrian Chadd 	/*
13264866e6c2SSam Leffler 	 * Setup dynamic sysctl's now that country code and
13274866e6c2SSam Leffler 	 * regdomain are available from the hal.
13284866e6c2SSam Leffler 	 */
13294866e6c2SSam Leffler 	ath_sysctlattach(sc);
1330e8dabfbeSAdrian Chadd 	ath_sysctl_stats_attach(sc);
133137931a35SAdrian Chadd 	ath_sysctl_hal_attach(sc);
133273454c73SSam Leffler 
1333c42a7b7eSSam Leffler 	if (bootverbose)
1334c42a7b7eSSam Leffler 		ieee80211_announce(ic);
1335c42a7b7eSSam Leffler 	ath_announce(sc);
1336f5c30c4eSAdrian Chadd 
1337f5c30c4eSAdrian Chadd 	/*
1338f5c30c4eSAdrian Chadd 	 * Put it to sleep for now.
1339f5c30c4eSAdrian Chadd 	 */
1340f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
13418c03e55dSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_FULL_SLEEP, 1);
1342f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1343f5c30c4eSAdrian Chadd 
13445591b213SSam Leffler 	return 0;
1345b28b4653SSam Leffler bad2:
1346c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
1347b28b4653SSam Leffler 	ath_desc_free(sc);
13483fdfc330SAdrian Chadd 	ath_txdma_teardown(sc);
13493d184db2SAdrian Chadd 	ath_rxdma_teardown(sc);
13505591b213SSam Leffler bad:
13515591b213SSam Leffler 	if (ah)
13525591b213SSam Leffler 		ath_hal_detach(ah);
13535591b213SSam Leffler 	sc->sc_invalid = 1;
13545591b213SSam Leffler 	return error;
13555591b213SSam Leffler }
13565591b213SSam Leffler 
13575591b213SSam Leffler int
13585591b213SSam Leffler ath_detach(struct ath_softc *sc)
13595591b213SSam Leffler {
13605591b213SSam Leffler 
1361c42a7b7eSSam Leffler 	/*
1362c42a7b7eSSam Leffler 	 * NB: the order of these is important:
136371b85077SSam Leffler 	 * o stop the chip so no more interrupts will fire
1364c42a7b7eSSam Leffler 	 * o call the 802.11 layer before detaching the hal to
1365c42a7b7eSSam Leffler 	 *   insure callbacks into the driver to delete global
1366c42a7b7eSSam Leffler 	 *   key cache entries can be handled
136771b85077SSam Leffler 	 * o free the taskqueue which drains any pending tasks
1368c42a7b7eSSam Leffler 	 * o reclaim the tx queue data structures after calling
1369c42a7b7eSSam Leffler 	 *   the 802.11 layer as we'll get called back to reclaim
1370c42a7b7eSSam Leffler 	 *   node state and potentially want to use them
1371c42a7b7eSSam Leffler 	 * o to cleanup the tx queues the hal is called, so detach
1372c42a7b7eSSam Leffler 	 *   it last
1373c42a7b7eSSam Leffler 	 * Other than that, it's straightforward...
1374c42a7b7eSSam Leffler 	 */
1375f5c30c4eSAdrian Chadd 
1376f5c30c4eSAdrian Chadd 	/*
1377f5c30c4eSAdrian Chadd 	 * XXX Wake the hardware up first.  ath_stop() will still
1378f5c30c4eSAdrian Chadd 	 * wake it up first, but I'd rather do it here just to
1379f5c30c4eSAdrian Chadd 	 * ensure it's awake.
1380f5c30c4eSAdrian Chadd 	 */
1381f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1382f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
13838c03e55dSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_AWAKE, 1);
1384f5c30c4eSAdrian Chadd 
1385f5c30c4eSAdrian Chadd 	/*
1386f5c30c4eSAdrian Chadd 	 * Stop things cleanly.
1387f5c30c4eSAdrian Chadd 	 */
13887a79cebfSGleb Smirnoff 	ath_stop(sc);
13897a79cebfSGleb Smirnoff 	ATH_UNLOCK(sc);
1390f5c30c4eSAdrian Chadd 
13917a79cebfSGleb Smirnoff 	ieee80211_ifdetach(&sc->sc_ic);
139271b85077SSam Leffler 	taskqueue_free(sc->sc_tq);
139386e07743SSam Leffler #ifdef ATH_TX99_DIAG
139486e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
139586e07743SSam Leffler 		sc->sc_tx99->detach(sc->sc_tx99);
139686e07743SSam Leffler #endif
1397c42a7b7eSSam Leffler 	ath_rate_detach(sc->sc_rc);
139889d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
1399bdbb6e5bSAdrian Chadd 	if_ath_alq_tidyup(&sc->sc_alq);
1400bdbb6e5bSAdrian Chadd #endif
1401216ca234SAdrian Chadd 	ath_lna_div_detach(sc);
1402b70f530bSAdrian Chadd 	ath_btcoex_detach(sc);
14039af351f9SAdrian Chadd 	ath_spectral_detach(sc);
140448237774SAdrian Chadd 	ath_dfs_detach(sc);
14055591b213SSam Leffler 	ath_desc_free(sc);
14064bf404eaSAdrian Chadd 	ath_txdma_teardown(sc);
14073d184db2SAdrian Chadd 	ath_rxdma_teardown(sc);
1408c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
140971b85077SSam Leffler 	ath_hal_detach(sc->sc_ah);	/* NB: sets chip in full sleep */
1410a93c5097SAdrian Chadd 
14115591b213SSam Leffler 	return 0;
14125591b213SSam Leffler }
14135591b213SSam Leffler 
1414b032f27cSSam Leffler /*
1415b032f27cSSam Leffler  * MAC address handling for multiple BSS on the same radio.
1416b032f27cSSam Leffler  * The first vap uses the MAC address from the EEPROM.  For
1417b032f27cSSam Leffler  * subsequent vap's we set the U/L bit (bit 1) in the MAC
1418b032f27cSSam Leffler  * address and use the next six bits as an index.
1419b032f27cSSam Leffler  */
1420b032f27cSSam Leffler static void
1421b032f27cSSam Leffler assign_address(struct ath_softc *sc, uint8_t mac[IEEE80211_ADDR_LEN], int clone)
1422b032f27cSSam Leffler {
1423b032f27cSSam Leffler 	int i;
1424b032f27cSSam Leffler 
1425b032f27cSSam Leffler 	if (clone && sc->sc_hasbmask) {
1426b032f27cSSam Leffler 		/* NB: we only do this if h/w supports multiple bssid */
1427b032f27cSSam Leffler 		for (i = 0; i < 8; i++)
1428b032f27cSSam Leffler 			if ((sc->sc_bssidmask & (1<<i)) == 0)
1429b032f27cSSam Leffler 				break;
1430b032f27cSSam Leffler 		if (i != 0)
1431b032f27cSSam Leffler 			mac[0] |= (i << 2)|0x2;
1432b032f27cSSam Leffler 	} else
1433b032f27cSSam Leffler 		i = 0;
1434b032f27cSSam Leffler 	sc->sc_bssidmask |= 1<<i;
1435b032f27cSSam Leffler 	sc->sc_hwbssidmask[0] &= ~mac[0];
1436b032f27cSSam Leffler 	if (i == 0)
1437b032f27cSSam Leffler 		sc->sc_nbssid0++;
1438b032f27cSSam Leffler }
1439b032f27cSSam Leffler 
1440b032f27cSSam Leffler static void
1441b032f27cSSam Leffler reclaim_address(struct ath_softc *sc, const uint8_t mac[IEEE80211_ADDR_LEN])
1442b032f27cSSam Leffler {
1443b032f27cSSam Leffler 	int i = mac[0] >> 2;
1444b032f27cSSam Leffler 	uint8_t mask;
1445b032f27cSSam Leffler 
1446b032f27cSSam Leffler 	if (i != 0 || --sc->sc_nbssid0 == 0) {
1447b032f27cSSam Leffler 		sc->sc_bssidmask &= ~(1<<i);
1448b032f27cSSam Leffler 		/* recalculate bssid mask from remaining addresses */
1449b032f27cSSam Leffler 		mask = 0xff;
1450b032f27cSSam Leffler 		for (i = 1; i < 8; i++)
1451b032f27cSSam Leffler 			if (sc->sc_bssidmask & (1<<i))
1452b032f27cSSam Leffler 				mask &= ~((i<<2)|0x2);
1453b032f27cSSam Leffler 		sc->sc_hwbssidmask[0] |= mask;
1454b032f27cSSam Leffler 	}
1455b032f27cSSam Leffler }
1456b032f27cSSam Leffler 
1457b032f27cSSam Leffler /*
1458b032f27cSSam Leffler  * Assign a beacon xmit slot.  We try to space out
1459b032f27cSSam Leffler  * assignments so when beacons are staggered the
1460b032f27cSSam Leffler  * traffic coming out of the cab q has maximal time
1461b032f27cSSam Leffler  * to go out before the next beacon is scheduled.
1462b032f27cSSam Leffler  */
1463b032f27cSSam Leffler static int
1464b032f27cSSam Leffler assign_bslot(struct ath_softc *sc)
1465b032f27cSSam Leffler {
1466b032f27cSSam Leffler 	u_int slot, free;
1467b032f27cSSam Leffler 
1468b032f27cSSam Leffler 	free = 0;
1469b032f27cSSam Leffler 	for (slot = 0; slot < ATH_BCBUF; slot++)
1470b032f27cSSam Leffler 		if (sc->sc_bslot[slot] == NULL) {
1471b032f27cSSam Leffler 			if (sc->sc_bslot[(slot+1)%ATH_BCBUF] == NULL &&
1472b032f27cSSam Leffler 			    sc->sc_bslot[(slot-1)%ATH_BCBUF] == NULL)
1473b032f27cSSam Leffler 				return slot;
1474b032f27cSSam Leffler 			free = slot;
1475b032f27cSSam Leffler 			/* NB: keep looking for a double slot */
1476b032f27cSSam Leffler 		}
1477b032f27cSSam Leffler 	return free;
1478b032f27cSSam Leffler }
1479b032f27cSSam Leffler 
1480b032f27cSSam Leffler static struct ieee80211vap *
1481fcd9500fSBernhard Schmidt ath_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
1482fcd9500fSBernhard Schmidt     enum ieee80211_opmode opmode, int flags,
1483b032f27cSSam Leffler     const uint8_t bssid[IEEE80211_ADDR_LEN],
1484b032f27cSSam Leffler     const uint8_t mac0[IEEE80211_ADDR_LEN])
1485b032f27cSSam Leffler {
14863797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
1487b032f27cSSam Leffler 	struct ath_vap *avp;
1488b032f27cSSam Leffler 	struct ieee80211vap *vap;
1489b032f27cSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN];
1490fcd9500fSBernhard Schmidt 	int needbeacon, error;
1491fcd9500fSBernhard Schmidt 	enum ieee80211_opmode ic_opmode;
1492b032f27cSSam Leffler 
14938aabf601SKevin Lo 	avp = malloc(sizeof(struct ath_vap), M_80211_VAP, M_WAITOK | M_ZERO);
1494b032f27cSSam Leffler 	needbeacon = 0;
1495b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(mac, mac0);
1496b032f27cSSam Leffler 
1497b032f27cSSam Leffler 	ATH_LOCK(sc);
1498a8962181SSam Leffler 	ic_opmode = opmode;		/* default to opmode of new vap */
1499b032f27cSSam Leffler 	switch (opmode) {
1500b032f27cSSam Leffler 	case IEEE80211_M_STA:
1501a8962181SSam Leffler 		if (sc->sc_nstavaps != 0) {	/* XXX only 1 for now */
1502b032f27cSSam Leffler 			device_printf(sc->sc_dev, "only 1 sta vap supported\n");
1503b032f27cSSam Leffler 			goto bad;
1504b032f27cSSam Leffler 		}
1505b032f27cSSam Leffler 		if (sc->sc_nvaps) {
1506b032f27cSSam Leffler 			/*
1507a8962181SSam Leffler 			 * With multiple vaps we must fall back
1508a8962181SSam Leffler 			 * to s/w beacon miss handling.
1509b032f27cSSam Leffler 			 */
1510b032f27cSSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
1511b032f27cSSam Leffler 		}
1512a8962181SSam Leffler 		if (flags & IEEE80211_CLONE_NOBEACONS) {
1513a8962181SSam Leffler 			/*
1514a8962181SSam Leffler 			 * Station mode w/o beacons are implemented w/ AP mode.
1515a8962181SSam Leffler 			 */
1516b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
1517a8962181SSam Leffler 		}
1518b032f27cSSam Leffler 		break;
1519b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1520b032f27cSSam Leffler 		if (sc->sc_nvaps != 0) {	/* XXX only 1 for now */
1521b032f27cSSam Leffler 			device_printf(sc->sc_dev,
1522b032f27cSSam Leffler 			    "only 1 ibss vap supported\n");
1523b032f27cSSam Leffler 			goto bad;
1524b032f27cSSam Leffler 		}
1525b032f27cSSam Leffler 		needbeacon = 1;
1526b032f27cSSam Leffler 		break;
1527b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1528584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
152910ad9a77SSam Leffler 		if (flags & IEEE80211_CLONE_TDMA) {
1530a8962181SSam Leffler 			if (sc->sc_nvaps != 0) {
1531a8962181SSam Leffler 				device_printf(sc->sc_dev,
1532a8962181SSam Leffler 				    "only 1 tdma vap supported\n");
1533a8962181SSam Leffler 				goto bad;
1534a8962181SSam Leffler 			}
153510ad9a77SSam Leffler 			needbeacon = 1;
153610ad9a77SSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
153710ad9a77SSam Leffler 		}
1538b032f27cSSam Leffler 		/* fall thru... */
153910ad9a77SSam Leffler #endif
1540b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1541b032f27cSSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode != opmode) {
1542a8962181SSam Leffler 			/*
1543a8962181SSam Leffler 			 * Adopt existing mode.  Adding a monitor or ahdemo
1544a8962181SSam Leffler 			 * vap to an existing configuration is of dubious
1545a8962181SSam Leffler 			 * value but should be ok.
1546a8962181SSam Leffler 			 */
1547b032f27cSSam Leffler 			/* XXX not right for monitor mode */
1548b032f27cSSam Leffler 			ic_opmode = ic->ic_opmode;
1549a8962181SSam Leffler 		}
1550b032f27cSSam Leffler 		break;
1551b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
155259aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1553b032f27cSSam Leffler 		needbeacon = 1;
1554a8962181SSam Leffler 		break;
1555b032f27cSSam Leffler 	case IEEE80211_M_WDS:
1556a8962181SSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode == IEEE80211_M_STA) {
1557b032f27cSSam Leffler 			device_printf(sc->sc_dev,
1558b032f27cSSam Leffler 			    "wds not supported in sta mode\n");
1559b032f27cSSam Leffler 			goto bad;
1560b032f27cSSam Leffler 		}
1561b032f27cSSam Leffler 		/*
1562b032f27cSSam Leffler 		 * Silently remove any request for a unique
1563b032f27cSSam Leffler 		 * bssid; WDS vap's always share the local
1564b032f27cSSam Leffler 		 * mac address.
1565b032f27cSSam Leffler 		 */
1566b032f27cSSam Leffler 		flags &= ~IEEE80211_CLONE_BSSID;
1567a8962181SSam Leffler 		if (sc->sc_nvaps == 0)
1568b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
1569a8962181SSam Leffler 		else
1570a8962181SSam Leffler 			ic_opmode = ic->ic_opmode;
15717d261891SRui Paulo 		break;
1572b032f27cSSam Leffler 	default:
1573b032f27cSSam Leffler 		device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
1574b032f27cSSam Leffler 		goto bad;
1575b032f27cSSam Leffler 	}
1576b032f27cSSam Leffler 	/*
1577b032f27cSSam Leffler 	 * Check that a beacon buffer is available; the code below assumes it.
1578b032f27cSSam Leffler 	 */
15796b349e5aSAdrian Chadd 	if (needbeacon & TAILQ_EMPTY(&sc->sc_bbuf)) {
1580b032f27cSSam Leffler 		device_printf(sc->sc_dev, "no beacon buffer available\n");
1581b032f27cSSam Leffler 		goto bad;
1582b032f27cSSam Leffler 	}
1583b032f27cSSam Leffler 
1584b032f27cSSam Leffler 	/* STA, AHDEMO? */
158559aa14a9SRui Paulo 	if (opmode == IEEE80211_M_HOSTAP || opmode == IEEE80211_M_MBSS) {
1586b032f27cSSam Leffler 		assign_address(sc, mac, flags & IEEE80211_CLONE_BSSID);
1587b032f27cSSam Leffler 		ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1588b032f27cSSam Leffler 	}
1589b032f27cSSam Leffler 
1590b032f27cSSam Leffler 	vap = &avp->av_vap;
1591b032f27cSSam Leffler 	/* XXX can't hold mutex across if_alloc */
1592b032f27cSSam Leffler 	ATH_UNLOCK(sc);
15937a79cebfSGleb Smirnoff 	error = ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
1594b032f27cSSam Leffler 	ATH_LOCK(sc);
1595b032f27cSSam Leffler 	if (error != 0) {
1596b032f27cSSam Leffler 		device_printf(sc->sc_dev, "%s: error %d creating vap\n",
1597b032f27cSSam Leffler 		    __func__, error);
1598b032f27cSSam Leffler 		goto bad2;
1599b032f27cSSam Leffler 	}
1600b032f27cSSam Leffler 
1601b032f27cSSam Leffler 	/* h/w crypto support */
1602b032f27cSSam Leffler 	vap->iv_key_alloc = ath_key_alloc;
1603b032f27cSSam Leffler 	vap->iv_key_delete = ath_key_delete;
1604b032f27cSSam Leffler 	vap->iv_key_set = ath_key_set;
1605b032f27cSSam Leffler 	vap->iv_key_update_begin = ath_key_update_begin;
1606b032f27cSSam Leffler 	vap->iv_key_update_end = ath_key_update_end;
1607b032f27cSSam Leffler 
1608b032f27cSSam Leffler 	/* override various methods */
1609b032f27cSSam Leffler 	avp->av_recv_mgmt = vap->iv_recv_mgmt;
1610b032f27cSSam Leffler 	vap->iv_recv_mgmt = ath_recv_mgmt;
1611b032f27cSSam Leffler 	vap->iv_reset = ath_reset_vap;
1612b032f27cSSam Leffler 	vap->iv_update_beacon = ath_beacon_update;
1613b032f27cSSam Leffler 	avp->av_newstate = vap->iv_newstate;
1614b032f27cSSam Leffler 	vap->iv_newstate = ath_newstate;
1615b032f27cSSam Leffler 	avp->av_bmiss = vap->iv_bmiss;
1616b032f27cSSam Leffler 	vap->iv_bmiss = ath_bmiss_vap;
1617b032f27cSSam Leffler 
16180eb81626SAdrian Chadd 	avp->av_node_ps = vap->iv_node_ps;
16190eb81626SAdrian Chadd 	vap->iv_node_ps = ath_node_powersave;
16200eb81626SAdrian Chadd 
1621548a605dSAdrian Chadd 	avp->av_set_tim = vap->iv_set_tim;
1622548a605dSAdrian Chadd 	vap->iv_set_tim = ath_node_set_tim;
1623548a605dSAdrian Chadd 
162422a3aee6SAdrian Chadd 	avp->av_recv_pspoll = vap->iv_recv_pspoll;
162522a3aee6SAdrian Chadd 	vap->iv_recv_pspoll = ath_node_recv_pspoll;
162622a3aee6SAdrian Chadd 
16279be25f4aSAdrian Chadd 	/* Set default parameters */
16289be25f4aSAdrian Chadd 
16299be25f4aSAdrian Chadd 	/*
16309be25f4aSAdrian Chadd 	 * Anything earlier than some AR9300 series MACs don't
16319be25f4aSAdrian Chadd 	 * support a smaller MPDU density.
16329be25f4aSAdrian Chadd 	 */
16339be25f4aSAdrian Chadd 	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_8;
16349be25f4aSAdrian Chadd 	/*
16359be25f4aSAdrian Chadd 	 * All NICs can handle the maximum size, however
16369be25f4aSAdrian Chadd 	 * AR5416 based MACs can only TX aggregates w/ RTS
16379be25f4aSAdrian Chadd 	 * protection when the total aggregate size is <= 8k.
16389be25f4aSAdrian Chadd 	 * However, for now that's enforced by the TX path.
16399be25f4aSAdrian Chadd 	 */
16409be25f4aSAdrian Chadd 	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K;
1641*0149d3d4SAdrian Chadd 	vap->iv_ampdu_limit = IEEE80211_HTCAP_MAXRXAMPDU_64K;
16429be25f4aSAdrian Chadd 
1643b032f27cSSam Leffler 	avp->av_bslot = -1;
1644b032f27cSSam Leffler 	if (needbeacon) {
1645b032f27cSSam Leffler 		/*
1646b032f27cSSam Leffler 		 * Allocate beacon state and setup the q for buffered
1647b032f27cSSam Leffler 		 * multicast frames.  We know a beacon buffer is
1648b032f27cSSam Leffler 		 * available because we checked above.
1649b032f27cSSam Leffler 		 */
16506b349e5aSAdrian Chadd 		avp->av_bcbuf = TAILQ_FIRST(&sc->sc_bbuf);
16516b349e5aSAdrian Chadd 		TAILQ_REMOVE(&sc->sc_bbuf, avp->av_bcbuf, bf_list);
1652b032f27cSSam Leffler 		if (opmode != IEEE80211_M_IBSS || !sc->sc_hasveol) {
1653b032f27cSSam Leffler 			/*
1654b032f27cSSam Leffler 			 * Assign the vap to a beacon xmit slot.  As above
1655b032f27cSSam Leffler 			 * this cannot fail to find a free one.
1656b032f27cSSam Leffler 			 */
1657b032f27cSSam Leffler 			avp->av_bslot = assign_bslot(sc);
1658b032f27cSSam Leffler 			KASSERT(sc->sc_bslot[avp->av_bslot] == NULL,
1659b032f27cSSam Leffler 			    ("beacon slot %u not empty", avp->av_bslot));
1660b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = vap;
1661b032f27cSSam Leffler 			sc->sc_nbcnvaps++;
1662b032f27cSSam Leffler 		}
1663b032f27cSSam Leffler 		if (sc->sc_hastsfadd && sc->sc_nbcnvaps > 0) {
1664b032f27cSSam Leffler 			/*
1665b032f27cSSam Leffler 			 * Multple vaps are to transmit beacons and we
1666b032f27cSSam Leffler 			 * have h/w support for TSF adjusting; enable
1667b032f27cSSam Leffler 			 * use of staggered beacons.
1668b032f27cSSam Leffler 			 */
1669b032f27cSSam Leffler 			sc->sc_stagbeacons = 1;
1670b032f27cSSam Leffler 		}
1671b032f27cSSam Leffler 		ath_txq_init(sc, &avp->av_mcastq, ATH_TXQ_SWQ);
1672b032f27cSSam Leffler 	}
1673b032f27cSSam Leffler 
1674b032f27cSSam Leffler 	ic->ic_opmode = ic_opmode;
1675b032f27cSSam Leffler 	if (opmode != IEEE80211_M_WDS) {
1676b032f27cSSam Leffler 		sc->sc_nvaps++;
1677b032f27cSSam Leffler 		if (opmode == IEEE80211_M_STA)
1678b032f27cSSam Leffler 			sc->sc_nstavaps++;
1679fe0dd789SSam Leffler 		if (opmode == IEEE80211_M_MBSS)
1680fe0dd789SSam Leffler 			sc->sc_nmeshvaps++;
1681b032f27cSSam Leffler 	}
1682b032f27cSSam Leffler 	switch (ic_opmode) {
1683b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1684b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_IBSS;
1685b032f27cSSam Leffler 		break;
1686b032f27cSSam Leffler 	case IEEE80211_M_STA:
1687b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_STA;
1688b032f27cSSam Leffler 		break;
1689b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1690584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
169110ad9a77SSam Leffler 		if (vap->iv_caps & IEEE80211_C_TDMA) {
169210ad9a77SSam Leffler 			sc->sc_tdma = 1;
169310ad9a77SSam Leffler 			/* NB: disable tsf adjust */
169410ad9a77SSam Leffler 			sc->sc_stagbeacons = 0;
169510ad9a77SSam Leffler 		}
169610ad9a77SSam Leffler 		/*
169710ad9a77SSam Leffler 		 * NB: adhoc demo mode is a pseudo mode; to the hal it's
169810ad9a77SSam Leffler 		 * just ap mode.
169910ad9a77SSam Leffler 		 */
170010ad9a77SSam Leffler 		/* fall thru... */
170110ad9a77SSam Leffler #endif
1702b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
170359aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1704b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_HOSTAP;
1705b032f27cSSam Leffler 		break;
1706b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1707b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_MONITOR;
1708b032f27cSSam Leffler 		break;
1709b032f27cSSam Leffler 	default:
1710b032f27cSSam Leffler 		/* XXX should not happen */
1711b032f27cSSam Leffler 		break;
1712b032f27cSSam Leffler 	}
1713b032f27cSSam Leffler 	if (sc->sc_hastsfadd) {
1714b032f27cSSam Leffler 		/*
1715b032f27cSSam Leffler 		 * Configure whether or not TSF adjust should be done.
1716b032f27cSSam Leffler 		 */
1717b032f27cSSam Leffler 		ath_hal_settsfadjust(sc->sc_ah, sc->sc_stagbeacons);
1718b032f27cSSam Leffler 	}
171910ad9a77SSam Leffler 	if (flags & IEEE80211_CLONE_NOBEACONS) {
172010ad9a77SSam Leffler 		/*
172110ad9a77SSam Leffler 		 * Enable s/w beacon miss handling.
172210ad9a77SSam Leffler 		 */
172310ad9a77SSam Leffler 		sc->sc_swbmiss = 1;
172410ad9a77SSam Leffler 	}
1725b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1726b032f27cSSam Leffler 
1727b032f27cSSam Leffler 	/* complete setup */
17287a79cebfSGleb Smirnoff 	ieee80211_vap_attach(vap, ath_media_change, ieee80211_media_status,
17297a79cebfSGleb Smirnoff 	    mac);
1730b032f27cSSam Leffler 	return vap;
1731b032f27cSSam Leffler bad2:
1732b032f27cSSam Leffler 	reclaim_address(sc, mac);
1733b032f27cSSam Leffler 	ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1734b032f27cSSam Leffler bad:
1735b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1736b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1737b032f27cSSam Leffler 	return NULL;
1738b032f27cSSam Leffler }
1739b032f27cSSam Leffler 
1740b032f27cSSam Leffler static void
1741b032f27cSSam Leffler ath_vap_delete(struct ieee80211vap *vap)
1742b032f27cSSam Leffler {
1743b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
17443797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
1745b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1746b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
1747b032f27cSSam Leffler 
1748f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1749f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1750f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1751f5c30c4eSAdrian Chadd 
1752f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
17537a79cebfSGleb Smirnoff 	if (sc->sc_running) {
1754b032f27cSSam Leffler 		/*
1755b032f27cSSam Leffler 		 * Quiesce the hardware while we remove the vap.  In
1756b032f27cSSam Leffler 		 * particular we need to reclaim all references to
1757b032f27cSSam Leffler 		 * the vap state by any frames pending on the tx queues.
1758b032f27cSSam Leffler 		 */
1759b032f27cSSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
1760517526efSAdrian Chadd 		/* XXX Do all frames from all vaps/nodes need draining here? */
17619a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* stop recv side */
1762062cf7d9SAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);		/* stop hw xmit side */
1763b032f27cSSam Leffler 	}
1764b032f27cSSam Leffler 
1765f5c30c4eSAdrian Chadd 	/* .. leave the hardware awake for now. */
1766f5c30c4eSAdrian Chadd 
1767b032f27cSSam Leffler 	ieee80211_vap_detach(vap);
176816d4de92SAdrian Chadd 
176916d4de92SAdrian Chadd 	/*
177016d4de92SAdrian Chadd 	 * XXX Danger Will Robinson! Danger!
177116d4de92SAdrian Chadd 	 *
177216d4de92SAdrian Chadd 	 * Because ieee80211_vap_detach() can queue a frame (the station
177316d4de92SAdrian Chadd 	 * diassociate message?) after we've drained the TXQ and
177416d4de92SAdrian Chadd 	 * flushed the software TXQ, we will end up with a frame queued
177516d4de92SAdrian Chadd 	 * to a node whose vap is about to be freed.
177616d4de92SAdrian Chadd 	 *
177716d4de92SAdrian Chadd 	 * To work around this, flush the hardware/software again.
177816d4de92SAdrian Chadd 	 * This may be racy - the ath task may be running and the packet
177916d4de92SAdrian Chadd 	 * may be being scheduled between sw->hw txq. Tsk.
178016d4de92SAdrian Chadd 	 *
178116d4de92SAdrian Chadd 	 * TODO: figure out why a new node gets allocated somewhere around
17827a79cebfSGleb Smirnoff 	 * here (after the ath_tx_swq() call; and after an ath_stop()
178316d4de92SAdrian Chadd 	 * call!)
178416d4de92SAdrian Chadd 	 */
178516d4de92SAdrian Chadd 
178616d4de92SAdrian Chadd 	ath_draintxq(sc, ATH_RESET_DEFAULT);
178716d4de92SAdrian Chadd 
1788b032f27cSSam Leffler 	ATH_LOCK(sc);
1789b032f27cSSam Leffler 	/*
1790b032f27cSSam Leffler 	 * Reclaim beacon state.  Note this must be done before
1791b032f27cSSam Leffler 	 * the vap instance is reclaimed as we may have a reference
1792b032f27cSSam Leffler 	 * to it in the buffer for the beacon frame.
1793b032f27cSSam Leffler 	 */
1794b032f27cSSam Leffler 	if (avp->av_bcbuf != NULL) {
1795b032f27cSSam Leffler 		if (avp->av_bslot != -1) {
1796b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = NULL;
1797b032f27cSSam Leffler 			sc->sc_nbcnvaps--;
1798b032f27cSSam Leffler 		}
1799b032f27cSSam Leffler 		ath_beacon_return(sc, avp->av_bcbuf);
1800b032f27cSSam Leffler 		avp->av_bcbuf = NULL;
1801b032f27cSSam Leffler 		if (sc->sc_nbcnvaps == 0) {
1802b032f27cSSam Leffler 			sc->sc_stagbeacons = 0;
1803b032f27cSSam Leffler 			if (sc->sc_hastsfadd)
1804b032f27cSSam Leffler 				ath_hal_settsfadjust(sc->sc_ah, 0);
1805b032f27cSSam Leffler 		}
1806b032f27cSSam Leffler 		/*
1807b032f27cSSam Leffler 		 * Reclaim any pending mcast frames for the vap.
1808b032f27cSSam Leffler 		 */
1809b032f27cSSam Leffler 		ath_tx_draintxq(sc, &avp->av_mcastq);
1810b032f27cSSam Leffler 	}
1811b032f27cSSam Leffler 	/*
1812b032f27cSSam Leffler 	 * Update bookkeeping.
1813b032f27cSSam Leffler 	 */
1814b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
1815b032f27cSSam Leffler 		sc->sc_nstavaps--;
1816b032f27cSSam Leffler 		if (sc->sc_nstavaps == 0 && sc->sc_swbmiss)
1817b032f27cSSam Leffler 			sc->sc_swbmiss = 0;
181859aa14a9SRui Paulo 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
181959aa14a9SRui Paulo 	    vap->iv_opmode == IEEE80211_M_MBSS) {
1820b032f27cSSam Leffler 		reclaim_address(sc, vap->iv_myaddr);
1821b032f27cSSam Leffler 		ath_hal_setbssidmask(ah, sc->sc_hwbssidmask);
1822fe0dd789SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_MBSS)
1823fe0dd789SSam Leffler 			sc->sc_nmeshvaps--;
1824b032f27cSSam Leffler 	}
1825b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_WDS)
1826b032f27cSSam Leffler 		sc->sc_nvaps--;
1827584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
182810ad9a77SSam Leffler 	/* TDMA operation ceases when the last vap is destroyed */
182910ad9a77SSam Leffler 	if (sc->sc_tdma && sc->sc_nvaps == 0) {
183010ad9a77SSam Leffler 		sc->sc_tdma = 0;
183110ad9a77SSam Leffler 		sc->sc_swbmiss = 0;
183210ad9a77SSam Leffler 	}
183310ad9a77SSam Leffler #endif
1834b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1835b032f27cSSam Leffler 
18367a79cebfSGleb Smirnoff 	if (sc->sc_running) {
1837b032f27cSSam Leffler 		/*
1838b032f27cSSam Leffler 		 * Restart rx+tx machines if still running (RUNNING will
1839b032f27cSSam Leffler 		 * be reset if we just destroyed the last vap).
1840b032f27cSSam Leffler 		 */
1841b032f27cSSam Leffler 		if (ath_startrecv(sc) != 0)
184276e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev,
184376e6fd5dSGleb Smirnoff 			    "%s: unable to restart recv logic\n", __func__);
1844c89b957aSSam Leffler 		if (sc->sc_beacons) {		/* restart beacons */
1845c89b957aSSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
1846c89b957aSSam Leffler 			if (sc->sc_tdma)
1847c89b957aSSam Leffler 				ath_tdma_config(sc, NULL);
1848c89b957aSSam Leffler 			else
1849c89b957aSSam Leffler #endif
1850b032f27cSSam Leffler 				ath_beacon_config(sc, NULL);
1851c89b957aSSam Leffler 		}
1852b032f27cSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
1853b032f27cSSam Leffler 	}
1854f5c30c4eSAdrian Chadd 
1855f5c30c4eSAdrian Chadd 	/* Ok, let the hardware asleep. */
1856f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
185716d4de92SAdrian Chadd 	ATH_UNLOCK(sc);
1858b032f27cSSam Leffler }
1859b032f27cSSam Leffler 
18605591b213SSam Leffler void
18615591b213SSam Leffler ath_suspend(struct ath_softc *sc)
18625591b213SSam Leffler {
18637a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
18645591b213SSam Leffler 
18657a79cebfSGleb Smirnoff 	sc->sc_resume_up = ic->ic_nrunning != 0;
1866d1328898SAdrian Chadd 
1867d3ac945bSSam Leffler 	ieee80211_suspend_all(ic);
1868d3ac945bSSam Leffler 	/*
1869d3ac945bSSam Leffler 	 * NB: don't worry about putting the chip in low power
1870d3ac945bSSam Leffler 	 * mode; pci will power off our socket on suspend and
1871f29b8b7fSWarner Losh 	 * CardBus detaches the device.
187217bb5fd1SAdrian Chadd 	 *
187317bb5fd1SAdrian Chadd 	 * XXX TODO: well, that's great, except for non-cardbus
187417bb5fd1SAdrian Chadd 	 * devices!
1875d3ac945bSSam Leffler 	 */
1876d73df6d5SAdrian Chadd 
1877ae2a0aa4SAdrian Chadd 	/*
187817bb5fd1SAdrian Chadd 	 * XXX This doesn't wait until all pending taskqueue
187917bb5fd1SAdrian Chadd 	 * items and parallel transmit/receive/other threads
188017bb5fd1SAdrian Chadd 	 * are running!
188117bb5fd1SAdrian Chadd 	 */
188217bb5fd1SAdrian Chadd 	ath_hal_intrset(sc->sc_ah, 0);
188317bb5fd1SAdrian Chadd 	taskqueue_block(sc->sc_tq);
18847707f31dSAdrian Chadd 
18857707f31dSAdrian Chadd 	ATH_LOCK(sc);
18867707f31dSAdrian Chadd 	callout_stop(&sc->sc_cal_ch);
18877707f31dSAdrian Chadd 	ATH_UNLOCK(sc);
188817bb5fd1SAdrian Chadd 
188917bb5fd1SAdrian Chadd 	/*
1890ae2a0aa4SAdrian Chadd 	 * XXX ensure sc_invalid is 1
1891ae2a0aa4SAdrian Chadd 	 */
1892ae2a0aa4SAdrian Chadd 
1893ae2a0aa4SAdrian Chadd 	/* Disable the PCIe PHY, complete with workarounds */
1894ae2a0aa4SAdrian Chadd 	ath_hal_enablepcie(sc->sc_ah, 1, 1);
1895d3ac945bSSam Leffler }
1896d3ac945bSSam Leffler 
1897d3ac945bSSam Leffler /*
1898d3ac945bSSam Leffler  * Reset the key cache since some parts do not reset the
1899d3ac945bSSam Leffler  * contents on resume.  First we clear all entries, then
1900d3ac945bSSam Leffler  * re-load keys that the 802.11 layer assumes are setup
1901d3ac945bSSam Leffler  * in h/w.
1902d3ac945bSSam Leffler  */
1903d3ac945bSSam Leffler static void
1904d3ac945bSSam Leffler ath_reset_keycache(struct ath_softc *sc)
1905d3ac945bSSam Leffler {
19067a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
1907d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1908d3ac945bSSam Leffler 	int i;
1909d3ac945bSSam Leffler 
1910f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1911f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1912d3ac945bSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
1913d3ac945bSSam Leffler 		ath_hal_keyreset(ah, i);
1914f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
1915f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1916d3ac945bSSam Leffler 	ieee80211_crypto_reload_keys(ic);
19175591b213SSam Leffler }
19185591b213SSam Leffler 
19196322256bSAdrian Chadd /*
19206322256bSAdrian Chadd  * Fetch the current chainmask configuration based on the current
19216322256bSAdrian Chadd  * operating channel and options.
19226322256bSAdrian Chadd  */
19236322256bSAdrian Chadd static void
19246322256bSAdrian Chadd ath_update_chainmasks(struct ath_softc *sc, struct ieee80211_channel *chan)
19256322256bSAdrian Chadd {
19266322256bSAdrian Chadd 
19276322256bSAdrian Chadd 	/*
19286322256bSAdrian Chadd 	 * Set TX chainmask to the currently configured chainmask;
19296322256bSAdrian Chadd 	 * the TX chainmask depends upon the current operating mode.
19306322256bSAdrian Chadd 	 */
19316322256bSAdrian Chadd 	sc->sc_cur_rxchainmask = sc->sc_rxchainmask;
19326322256bSAdrian Chadd 	if (IEEE80211_IS_CHAN_HT(chan)) {
19336322256bSAdrian Chadd 		sc->sc_cur_txchainmask = sc->sc_txchainmask;
19346322256bSAdrian Chadd 	} else {
19356322256bSAdrian Chadd 		sc->sc_cur_txchainmask = 1;
19366322256bSAdrian Chadd 	}
19377904f516SAdrian Chadd 
19387904f516SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
19397904f516SAdrian Chadd 	    "%s: TX chainmask is now 0x%x, RX is now 0x%x\n",
19407904f516SAdrian Chadd 	    __func__,
19417904f516SAdrian Chadd 	    sc->sc_cur_txchainmask,
19427904f516SAdrian Chadd 	    sc->sc_cur_rxchainmask);
19436322256bSAdrian Chadd }
19446322256bSAdrian Chadd 
19455591b213SSam Leffler void
19465591b213SSam Leffler ath_resume(struct ath_softc *sc)
19475591b213SSam Leffler {
19487a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
1949d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1950d3ac945bSSam Leffler 	HAL_STATUS status;
19515591b213SSam Leffler 
1952ae2a0aa4SAdrian Chadd 	ath_hal_enablepcie(ah, 0, 0);
1953d73df6d5SAdrian Chadd 
1954d3ac945bSSam Leffler 	/*
1955d3ac945bSSam Leffler 	 * Must reset the chip before we reload the
1956d3ac945bSSam Leffler 	 * keycache as we were powered down on suspend.
1957d3ac945bSSam Leffler 	 */
19586322256bSAdrian Chadd 	ath_update_chainmasks(sc,
19596322256bSAdrian Chadd 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan);
19606322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
19616322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
1962f5c30c4eSAdrian Chadd 
1963f5c30c4eSAdrian Chadd 	/* Ensure we set the current power state to on */
1964f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
19657d567ed6SAdrian Chadd 	ath_power_setselfgen(sc, HAL_PM_AWAKE);
1966f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
19678c03e55dSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_AWAKE, 1);
1968f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1969f5c30c4eSAdrian Chadd 
1970054d7b69SSam Leffler 	ath_hal_reset(ah, sc->sc_opmode,
1971054d7b69SSam Leffler 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan,
1972f50e4ebfSAdrian Chadd 	    AH_FALSE, HAL_RESET_NORMAL, &status);
1973d3ac945bSSam Leffler 	ath_reset_keycache(sc);
19747e5eb44dSAdrian Chadd 
197517bb5fd1SAdrian Chadd 	ATH_RX_LOCK(sc);
197617bb5fd1SAdrian Chadd 	sc->sc_rx_stopped = 1;
197717bb5fd1SAdrian Chadd 	sc->sc_rx_resetted = 1;
197817bb5fd1SAdrian Chadd 	ATH_RX_UNLOCK(sc);
197917bb5fd1SAdrian Chadd 
19807e5eb44dSAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
19817e5eb44dSAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
19827e5eb44dSAdrian Chadd 
19839af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
19849af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
19859af351f9SAdrian Chadd 
1986dd6a574eSAdrian Chadd 	/*
1987b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
1988b70f530bSAdrian Chadd 	 */
1989b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
1990b70f530bSAdrian Chadd 
1991b70f530bSAdrian Chadd 	/*
1992dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
1993dd6a574eSAdrian Chadd 	 * support it.
1994dd6a574eSAdrian Chadd 	 */
1995dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
1996dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
1997dd6a574eSAdrian Chadd 	else
1998dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
1999dd6a574eSAdrian Chadd 
2000a497cd88SAdrian Chadd 	/* Restore the LED configuration */
2001a497cd88SAdrian Chadd 	ath_led_config(sc);
2002a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
2003a497cd88SAdrian Chadd 
2004d1328898SAdrian Chadd 	if (sc->sc_resume_up)
2005021a0db5SAdrian Chadd 		ieee80211_resume_all(ic);
20062fd9aabbSAdrian Chadd 
2007f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2008f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2009f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2010f5c30c4eSAdrian Chadd 
20112fd9aabbSAdrian Chadd 	/* XXX beacons ? */
20126b59f5e3SSam Leffler }
20135591b213SSam Leffler 
20145591b213SSam Leffler void
20155591b213SSam Leffler ath_shutdown(struct ath_softc *sc)
20165591b213SSam Leffler {
20175591b213SSam Leffler 
20187a79cebfSGleb Smirnoff 	ATH_LOCK(sc);
20197a79cebfSGleb Smirnoff 	ath_stop(sc);
20207a79cebfSGleb Smirnoff 	ATH_UNLOCK(sc);
2021d3ac945bSSam Leffler 	/* NB: no point powering down chip as we're about to reboot */
20225591b213SSam Leffler }
20235591b213SSam Leffler 
2024c42a7b7eSSam Leffler /*
2025c42a7b7eSSam Leffler  * Interrupt handler.  Most of the actual processing is deferred.
2026c42a7b7eSSam Leffler  */
20275591b213SSam Leffler void
20285591b213SSam Leffler ath_intr(void *arg)
20295591b213SSam Leffler {
20305591b213SSam Leffler 	struct ath_softc *sc = arg;
20315591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
20326f5fe81eSAdrian Chadd 	HAL_INT status = 0;
20338f939e79SAdrian Chadd 	uint32_t txqs;
20345591b213SSam Leffler 
2035ef27340cSAdrian Chadd 	/*
2036ef27340cSAdrian Chadd 	 * If we're inside a reset path, just print a warning and
2037ef27340cSAdrian Chadd 	 * clear the ISR. The reset routine will finish it for us.
2038ef27340cSAdrian Chadd 	 */
2039ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2040ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt) {
2041ef27340cSAdrian Chadd 		HAL_INT status;
2042ef27340cSAdrian Chadd 		ath_hal_getisr(ah, &status);	/* clear ISR */
2043ef27340cSAdrian Chadd 		ath_hal_intrset(ah, 0);		/* disable further intr's */
2044ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_ANY,
2045ef27340cSAdrian Chadd 		    "%s: in reset, ignoring: status=0x%x\n",
2046ef27340cSAdrian Chadd 		    __func__, status);
2047ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2048ef27340cSAdrian Chadd 		return;
2049ef27340cSAdrian Chadd 	}
2050ef27340cSAdrian Chadd 
20515591b213SSam Leffler 	if (sc->sc_invalid) {
20525591b213SSam Leffler 		/*
2053b58b3803SSam Leffler 		 * The hardware is not ready/present, don't touch anything.
2054b58b3803SSam Leffler 		 * Note this can happen early on if the IRQ is shared.
20555591b213SSam Leffler 		 */
2056c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
2057ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
20585591b213SSam Leffler 		return;
20595591b213SSam Leffler 	}
2060ef27340cSAdrian Chadd 	if (!ath_hal_intrpend(ah)) {		/* shared irq, not for us */
2061ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2062fdd758d4SSam Leffler 		return;
2063ef27340cSAdrian Chadd 	}
2064ef27340cSAdrian Chadd 
2065f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2066f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2067f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2068f5c30c4eSAdrian Chadd 
20697a79cebfSGleb Smirnoff 	if (sc->sc_ic.ic_nrunning == 0 && sc->sc_running == 0) {
207068e8e04eSSam Leffler 		HAL_INT status;
207168e8e04eSSam Leffler 
20727a79cebfSGleb Smirnoff 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: ic_nrunning %d sc_running %d\n",
20737a79cebfSGleb Smirnoff 		    __func__, sc->sc_ic.ic_nrunning, sc->sc_running);
20745591b213SSam Leffler 		ath_hal_getisr(ah, &status);	/* clear ISR */
20755591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable further intr's */
2076ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2077f5c30c4eSAdrian Chadd 
2078f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
2079f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
2080f5c30c4eSAdrian Chadd 		ATH_UNLOCK(sc);
20815591b213SSam Leffler 		return;
20825591b213SSam Leffler 	}
2083ef27340cSAdrian Chadd 
2084c42a7b7eSSam Leffler 	/*
2085c42a7b7eSSam Leffler 	 * Figure out the reason(s) for the interrupt.  Note
2086c42a7b7eSSam Leffler 	 * that the hal returns a pseudo-ISR that may include
2087c42a7b7eSSam Leffler 	 * bits we haven't explicitly enabled so we mask the
2088c42a7b7eSSam Leffler 	 * value to insure we only process bits we requested.
2089c42a7b7eSSam Leffler 	 */
20905591b213SSam Leffler 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
2091c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
209203682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 1, "ath_intr: mask=0x%.8x", status);
2093a26f3327SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
2094a26f3327SAdrian Chadd 	if_ath_alq_post_intr(&sc->sc_alq, status, ah->ah_intrstate,
2095a26f3327SAdrian Chadd 	    ah->ah_syncstate);
2096a26f3327SAdrian Chadd #endif	/* ATH_DEBUG_ALQ */
209731fdf3d6SAdrian Chadd #ifdef	ATH_KTR_INTR_DEBUG
209803682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 5,
2099f52d3452SAdrian Chadd 	    "ath_intr: ISR=0x%.8x, ISR_S0=0x%.8x, ISR_S1=0x%.8x, ISR_S2=0x%.8x, ISR_S5=0x%.8x",
2100f52d3452SAdrian Chadd 	    ah->ah_intrstate[0],
2101f52d3452SAdrian Chadd 	    ah->ah_intrstate[1],
2102f52d3452SAdrian Chadd 	    ah->ah_intrstate[2],
2103f52d3452SAdrian Chadd 	    ah->ah_intrstate[3],
2104f52d3452SAdrian Chadd 	    ah->ah_intrstate[6]);
210531fdf3d6SAdrian Chadd #endif
21069467e3f3SAdrian Chadd 
21079467e3f3SAdrian Chadd 	/* Squirrel away SYNC interrupt debugging */
21089467e3f3SAdrian Chadd 	if (ah->ah_syncstate != 0) {
21099467e3f3SAdrian Chadd 		int i;
21109467e3f3SAdrian Chadd 		for (i = 0; i < 32; i++)
21119467e3f3SAdrian Chadd 			if (ah->ah_syncstate & (i << i))
21129467e3f3SAdrian Chadd 				sc->sc_intr_stats.sync_intr[i]++;
21139467e3f3SAdrian Chadd 	}
21149467e3f3SAdrian Chadd 
2115ecddff40SSam Leffler 	status &= sc->sc_imask;			/* discard unasked for bits */
21166f5fe81eSAdrian Chadd 
21176f5fe81eSAdrian Chadd 	/* Short-circuit un-handled interrupts */
2118ef27340cSAdrian Chadd 	if (status == 0x0) {
2119ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2120f5c30c4eSAdrian Chadd 
2121f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
2122f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
2123f5c30c4eSAdrian Chadd 		ATH_UNLOCK(sc);
2124f5c30c4eSAdrian Chadd 
21256f5fe81eSAdrian Chadd 		return;
2126ef27340cSAdrian Chadd 	}
21276f5fe81eSAdrian Chadd 
2128ef27340cSAdrian Chadd 	/*
2129ef27340cSAdrian Chadd 	 * Take a note that we're inside the interrupt handler, so
2130ef27340cSAdrian Chadd 	 * the reset routines know to wait.
2131ef27340cSAdrian Chadd 	 */
2132ef27340cSAdrian Chadd 	sc->sc_intr_cnt++;
2133ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2134ef27340cSAdrian Chadd 
2135ef27340cSAdrian Chadd 	/*
2136ef27340cSAdrian Chadd 	 * Handle the interrupt. We won't run concurrent with the reset
2137ef27340cSAdrian Chadd 	 * or channel change routines as they'll wait for sc_intr_cnt
2138ef27340cSAdrian Chadd 	 * to be 0 before continuing.
2139ef27340cSAdrian Chadd 	 */
21405591b213SSam Leffler 	if (status & HAL_INT_FATAL) {
21415591b213SSam Leffler 		sc->sc_stats.ast_hardware++;
21425591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
2143f846cf42SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_fataltask);
21445591b213SSam Leffler 	} else {
2145c42a7b7eSSam Leffler 		if (status & HAL_INT_SWBA) {
2146c42a7b7eSSam Leffler 			/*
2147c42a7b7eSSam Leffler 			 * Software beacon alert--time to send a beacon.
2148c42a7b7eSSam Leffler 			 * Handle beacon transmission directly; deferring
2149c42a7b7eSSam Leffler 			 * this is too slow to meet timing constraints
2150c42a7b7eSSam Leffler 			 * under load.
2151c42a7b7eSSam Leffler 			 */
2152584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
215310ad9a77SSam Leffler 			if (sc->sc_tdma) {
215410ad9a77SSam Leffler 				if (sc->sc_tdmaswba == 0) {
21557a79cebfSGleb Smirnoff 					struct ieee80211com *ic = &sc->sc_ic;
215610ad9a77SSam Leffler 					struct ieee80211vap *vap =
215710ad9a77SSam Leffler 					    TAILQ_FIRST(&ic->ic_vaps);
215810ad9a77SSam Leffler 					ath_tdma_beacon_send(sc, vap);
215910ad9a77SSam Leffler 					sc->sc_tdmaswba =
216010ad9a77SSam Leffler 					    vap->iv_tdma->tdma_bintval;
216110ad9a77SSam Leffler 				} else
216210ad9a77SSam Leffler 					sc->sc_tdmaswba--;
216310ad9a77SSam Leffler 			} else
216410ad9a77SSam Leffler #endif
2165339ccfb3SSam Leffler 			{
2166c42a7b7eSSam Leffler 				ath_beacon_proc(sc, 0);
2167339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
2168339ccfb3SSam Leffler 				/*
2169339ccfb3SSam Leffler 				 * Schedule the rx taskq in case there's no
2170339ccfb3SSam Leffler 				 * traffic so any frames held on the staging
2171339ccfb3SSam Leffler 				 * queue are aged and potentially flushed.
2172339ccfb3SSam Leffler 				 */
2173f0db652cSAdrian Chadd 				sc->sc_rx.recv_sched(sc, 1);
2174339ccfb3SSam Leffler #endif
2175339ccfb3SSam Leffler 			}
2176c42a7b7eSSam Leffler 		}
21775591b213SSam Leffler 		if (status & HAL_INT_RXEOL) {
21788f939e79SAdrian Chadd 			int imask;
217903682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXEOL");
218017bb5fd1SAdrian Chadd 			if (! sc->sc_isedma) {
2181ef27340cSAdrian Chadd 				ATH_PCU_LOCK(sc);
21825591b213SSam Leffler 				/*
21835591b213SSam Leffler 				 * NB: the hardware should re-read the link when
21845591b213SSam Leffler 				 *     RXE bit is written, but it doesn't work at
21855591b213SSam Leffler 				 *     least on older hardware revs.
21865591b213SSam Leffler 				 */
21875591b213SSam Leffler 				sc->sc_stats.ast_rxeol++;
218873f895fcSAdrian Chadd 				/*
218973f895fcSAdrian Chadd 				 * Disable RXEOL/RXORN - prevent an interrupt
219073f895fcSAdrian Chadd 				 * storm until the PCU logic can be reset.
21911fdadc0fSAdrian Chadd 				 * In case the interface is reset some other
21921fdadc0fSAdrian Chadd 				 * way before "sc_kickpcu" is called, don't
21931fdadc0fSAdrian Chadd 				 * modify sc_imask - that way if it is reset
21941fdadc0fSAdrian Chadd 				 * by a call to ath_reset() somehow, the
21951fdadc0fSAdrian Chadd 				 * interrupt mask will be correctly reprogrammed.
219673f895fcSAdrian Chadd 				 */
21978f939e79SAdrian Chadd 				imask = sc->sc_imask;
21981fdadc0fSAdrian Chadd 				imask &= ~(HAL_INT_RXEOL | HAL_INT_RXORN);
21991fdadc0fSAdrian Chadd 				ath_hal_intrset(ah, imask);
22001fdadc0fSAdrian Chadd 				/*
22018f939e79SAdrian Chadd 				 * Only blank sc_rxlink if we've not yet kicked
22028f939e79SAdrian Chadd 				 * the PCU.
22038f939e79SAdrian Chadd 				 *
22048f939e79SAdrian Chadd 				 * This isn't entirely correct - the correct solution
22058f939e79SAdrian Chadd 				 * would be to have a PCU lock and engage that for
22068f939e79SAdrian Chadd 				 * the duration of the PCU fiddling; which would include
22078f939e79SAdrian Chadd 				 * running the RX process. Otherwise we could end up
22088f939e79SAdrian Chadd 				 * messing up the RX descriptor chain and making the
22098f939e79SAdrian Chadd 				 * RX desc list much shorter.
22108f939e79SAdrian Chadd 				 */
22118f939e79SAdrian Chadd 				if (! sc->sc_kickpcu)
22128f939e79SAdrian Chadd 					sc->sc_rxlink = NULL;
22138f939e79SAdrian Chadd 				sc->sc_kickpcu = 1;
2214f0db652cSAdrian Chadd 				ATH_PCU_UNLOCK(sc);
221517bb5fd1SAdrian Chadd 			}
22168f939e79SAdrian Chadd 			/*
221717bb5fd1SAdrian Chadd 			 * Enqueue an RX proc to handle whatever
22181fdadc0fSAdrian Chadd 			 * is in the RX queue.
221917bb5fd1SAdrian Chadd 			 * This will then kick the PCU if required.
22201fdadc0fSAdrian Chadd 			 */
2221f0db652cSAdrian Chadd 			sc->sc_rx.recv_sched(sc, 1);
22225591b213SSam Leffler 		}
22235591b213SSam Leffler 		if (status & HAL_INT_TXURN) {
22245591b213SSam Leffler 			sc->sc_stats.ast_txurn++;
22255591b213SSam Leffler 			/* bump tx trigger level */
22265591b213SSam Leffler 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
22275591b213SSam Leffler 		}
2228bcbb08ceSAdrian Chadd 		/*
2229bcbb08ceSAdrian Chadd 		 * Handle both the legacy and RX EDMA interrupt bits.
2230bcbb08ceSAdrian Chadd 		 * Note that HAL_INT_RXLP is also HAL_INT_RXDESC.
2231bcbb08ceSAdrian Chadd 		 */
2232bcbb08ceSAdrian Chadd 		if (status & (HAL_INT_RX | HAL_INT_RXHP | HAL_INT_RXLP)) {
22338f939e79SAdrian Chadd 			sc->sc_stats.ast_rx_intr++;
2234f0db652cSAdrian Chadd 			sc->sc_rx.recv_sched(sc, 1);
22358f939e79SAdrian Chadd 		}
22368f939e79SAdrian Chadd 		if (status & HAL_INT_TX) {
22378f939e79SAdrian Chadd 			sc->sc_stats.ast_tx_intr++;
22388f939e79SAdrian Chadd 			/*
22398f939e79SAdrian Chadd 			 * Grab all the currently set bits in the HAL txq bitmap
22408f939e79SAdrian Chadd 			 * and blank them. This is the only place we should be
22418f939e79SAdrian Chadd 			 * doing this.
22428f939e79SAdrian Chadd 			 */
2243bad98824SAdrian Chadd 			if (! sc->sc_isedma) {
2244ef27340cSAdrian Chadd 				ATH_PCU_LOCK(sc);
22458f939e79SAdrian Chadd 				txqs = 0xffffffff;
22468f939e79SAdrian Chadd 				ath_hal_gettxintrtxqs(sc->sc_ah, &txqs);
224703682514SAdrian Chadd 				ATH_KTR(sc, ATH_KTR_INTERRUPTS, 3,
224803682514SAdrian Chadd 				    "ath_intr: TX; txqs=0x%08x, txq_active was 0x%08x, now 0x%08x",
224903682514SAdrian Chadd 				    txqs,
225003682514SAdrian Chadd 				    sc->sc_txq_active,
225103682514SAdrian Chadd 				    sc->sc_txq_active | txqs);
22528f939e79SAdrian Chadd 				sc->sc_txq_active |= txqs;
2253ef27340cSAdrian Chadd 				ATH_PCU_UNLOCK(sc);
22548f939e79SAdrian Chadd 			}
2255bad98824SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask);
2256bad98824SAdrian Chadd 		}
22575591b213SSam Leffler 		if (status & HAL_INT_BMISS) {
22585591b213SSam Leffler 			sc->sc_stats.ast_bmiss++;
22590bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask);
22605591b213SSam Leffler 		}
22616ad02dbaSAdrian Chadd 		if (status & HAL_INT_GTT)
22626ad02dbaSAdrian Chadd 			sc->sc_stats.ast_tx_timeout++;
22635594f5c0SAdrian Chadd 		if (status & HAL_INT_CST)
22645594f5c0SAdrian Chadd 			sc->sc_stats.ast_tx_cst++;
2265c42a7b7eSSam Leffler 		if (status & HAL_INT_MIB) {
2266c42a7b7eSSam Leffler 			sc->sc_stats.ast_mib++;
2267ef27340cSAdrian Chadd 			ATH_PCU_LOCK(sc);
2268c42a7b7eSSam Leffler 			/*
2269c42a7b7eSSam Leffler 			 * Disable interrupts until we service the MIB
2270c42a7b7eSSam Leffler 			 * interrupt; otherwise it will continue to fire.
2271c42a7b7eSSam Leffler 			 */
2272c42a7b7eSSam Leffler 			ath_hal_intrset(ah, 0);
2273c42a7b7eSSam Leffler 			/*
2274c42a7b7eSSam Leffler 			 * Let the hal handle the event.  We assume it will
2275c42a7b7eSSam Leffler 			 * clear whatever condition caused the interrupt.
2276c42a7b7eSSam Leffler 			 */
2277ffa2cab6SSam Leffler 			ath_hal_mibevent(ah, &sc->sc_halstats);
22788f939e79SAdrian Chadd 			/*
22798f939e79SAdrian Chadd 			 * Don't reset the interrupt if we've just
22808f939e79SAdrian Chadd 			 * kicked the PCU, or we may get a nested
22818f939e79SAdrian Chadd 			 * RXEOL before the rxproc has had a chance
22828f939e79SAdrian Chadd 			 * to run.
22838f939e79SAdrian Chadd 			 */
22848f939e79SAdrian Chadd 			if (sc->sc_kickpcu == 0)
2285c42a7b7eSSam Leffler 				ath_hal_intrset(ah, sc->sc_imask);
2286ef27340cSAdrian Chadd 			ATH_PCU_UNLOCK(sc);
2287c42a7b7eSSam Leffler 		}
22889c4fc1e8SSam Leffler 		if (status & HAL_INT_RXORN) {
22899c4fc1e8SSam Leffler 			/* NB: hal marks HAL_INT_FATAL when RXORN is fatal */
229003682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXORN");
22919c4fc1e8SSam Leffler 			sc->sc_stats.ast_rxorn++;
22929c4fc1e8SSam Leffler 		}
2293f5c30c4eSAdrian Chadd 		if (status & HAL_INT_TSFOOR) {
22948c03e55dSAdrian Chadd 			/* out of range beacon - wake the chip up,
22958c03e55dSAdrian Chadd 			 * but don't modify self-gen frame config */
2296f5c30c4eSAdrian Chadd 			device_printf(sc->sc_dev, "%s: TSFOOR\n", __func__);
2297f5c30c4eSAdrian Chadd 			sc->sc_syncbeacon = 1;
22988c03e55dSAdrian Chadd 			ATH_LOCK(sc);
22998c03e55dSAdrian Chadd 			ath_power_setpower(sc, HAL_PM_AWAKE, 0);
23008c03e55dSAdrian Chadd 			ATH_UNLOCK(sc);
2301f5c30c4eSAdrian Chadd 		}
2302bcf5fc49SAdrian Chadd 		if (status & HAL_INT_MCI) {
2303bcf5fc49SAdrian Chadd 			ath_btcoex_mci_intr(sc);
2304bcf5fc49SAdrian Chadd 		}
23055591b213SSam Leffler 	}
2306ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2307ef27340cSAdrian Chadd 	sc->sc_intr_cnt--;
2308ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2309f5c30c4eSAdrian Chadd 
2310f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2311f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2312f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
23135591b213SSam Leffler }
23145591b213SSam Leffler 
23155591b213SSam Leffler static void
23165591b213SSam Leffler ath_fatal_proc(void *arg, int pending)
23175591b213SSam Leffler {
23185591b213SSam Leffler 	struct ath_softc *sc = arg;
231916c8acaaSSam Leffler 	u_int32_t *state;
232016c8acaaSSam Leffler 	u_int32_t len;
232168e8e04eSSam Leffler 	void *sp;
23225591b213SSam Leffler 
232370c81b20SAdrian Chadd 	if (sc->sc_invalid)
232470c81b20SAdrian Chadd 		return;
232570c81b20SAdrian Chadd 
232676e6fd5dSGleb Smirnoff 	device_printf(sc->sc_dev, "hardware error; resetting\n");
232716c8acaaSSam Leffler 	/*
232816c8acaaSSam Leffler 	 * Fatal errors are unrecoverable.  Typically these
232916c8acaaSSam Leffler 	 * are caused by DMA errors.  Collect h/w state from
233016c8acaaSSam Leffler 	 * the hal so we can diagnose what's going on.
233116c8acaaSSam Leffler 	 */
233268e8e04eSSam Leffler 	if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) {
233316c8acaaSSam Leffler 		KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len));
233468e8e04eSSam Leffler 		state = sp;
233576e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
233676e6fd5dSGleb Smirnoff 		    "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n", state[0],
233776e6fd5dSGleb Smirnoff 		    state[1] , state[2], state[3], state[4], state[5]);
233816c8acaaSSam Leffler 	}
23397a79cebfSGleb Smirnoff 	ath_reset(sc, ATH_RESET_NOLOSS);
23405591b213SSam Leffler }
23415591b213SSam Leffler 
23425591b213SSam Leffler static void
2343b032f27cSSam Leffler ath_bmiss_vap(struct ieee80211vap *vap)
23445591b213SSam Leffler {
23453797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
2346f5c30c4eSAdrian Chadd 
234759fbb257SSam Leffler 	/*
234859fbb257SSam Leffler 	 * Workaround phantom bmiss interrupts by sanity-checking
234959fbb257SSam Leffler 	 * the time of our last rx'd frame.  If it is within the
235059fbb257SSam Leffler 	 * beacon miss interval then ignore the interrupt.  If it's
235159fbb257SSam Leffler 	 * truly a bmiss we'll get another interrupt soon and that'll
235259fbb257SSam Leffler 	 * be dispatched up for processing.  Note this applies only
235359fbb257SSam Leffler 	 * for h/w beacon miss events.
235459fbb257SSam Leffler 	 */
2355f5c30c4eSAdrian Chadd 
2356f5c30c4eSAdrian Chadd 	/*
2357f5c30c4eSAdrian Chadd 	 * XXX TODO: Just read the TSF during the interrupt path;
2358f5c30c4eSAdrian Chadd 	 * that way we don't have to wake up again just to read it
2359f5c30c4eSAdrian Chadd 	 * again.
2360f5c30c4eSAdrian Chadd 	 */
2361f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2362f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2363f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2364f5c30c4eSAdrian Chadd 
236559fbb257SSam Leffler 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
2366d7736e13SSam Leffler 		u_int64_t lastrx = sc->sc_lastrx;
2367d7736e13SSam Leffler 		u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah);
236880767531SAdrian Chadd 		/* XXX should take a locked ref to iv_bss */
2369d7736e13SSam Leffler 		u_int bmisstimeout =
2370b032f27cSSam Leffler 			vap->iv_bmissthreshold * vap->iv_bss->ni_intval * 1024;
2371d7736e13SSam Leffler 
2372d7736e13SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2373d7736e13SSam Leffler 		    "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n",
2374d7736e13SSam Leffler 		    __func__, (unsigned long long) tsf,
2375d7736e13SSam Leffler 		    (unsigned long long)(tsf - lastrx),
2376d7736e13SSam Leffler 		    (unsigned long long) lastrx, bmisstimeout);
237759fbb257SSam Leffler 
237859fbb257SSam Leffler 		if (tsf - lastrx <= bmisstimeout) {
2379d7736e13SSam Leffler 			sc->sc_stats.ast_bmiss_phantom++;
2380f5c30c4eSAdrian Chadd 
2381f5c30c4eSAdrian Chadd 			ATH_LOCK(sc);
2382f5c30c4eSAdrian Chadd 			ath_power_restore_power_state(sc);
2383f5c30c4eSAdrian Chadd 			ATH_UNLOCK(sc);
2384f5c30c4eSAdrian Chadd 
238559fbb257SSam Leffler 			return;
238659fbb257SSam Leffler 		}
238759fbb257SSam Leffler 	}
2388f5c30c4eSAdrian Chadd 
2389f5c30c4eSAdrian Chadd 	/*
23908c03e55dSAdrian Chadd 	 * Keep the hardware awake if it's asleep (and leave self-gen
23918c03e55dSAdrian Chadd 	 * frame config alone) until the next beacon, so we can resync
23928c03e55dSAdrian Chadd 	 * against the next beacon.
23938c03e55dSAdrian Chadd 	 *
23948c03e55dSAdrian Chadd 	 * This handles three common beacon miss cases in STA powersave mode -
23958c03e55dSAdrian Chadd 	 * (a) the beacon TBTT isnt a multiple of bintval;
23968c03e55dSAdrian Chadd 	 * (b) the beacon was missed; and
23978c03e55dSAdrian Chadd 	 * (c) the beacons are being delayed because the AP is busy and
23988c03e55dSAdrian Chadd 	 *     isn't reliably able to meet its TBTT.
2399f5c30c4eSAdrian Chadd 	 */
2400f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
24018c03e55dSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_AWAKE, 0);
2402f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2403f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
24048c03e55dSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_BEACON,
24058c03e55dSAdrian Chadd 	    "%s: forced awake; force syncbeacon=1\n", __func__);
2406f5c30c4eSAdrian Chadd 
2407f5c30c4eSAdrian Chadd 	/*
2408f5c30c4eSAdrian Chadd 	 * Attempt to force a beacon resync.
2409f5c30c4eSAdrian Chadd 	 */
2410f5c30c4eSAdrian Chadd 	sc->sc_syncbeacon = 1;
2411f5c30c4eSAdrian Chadd 
241259fbb257SSam Leffler 	ATH_VAP(vap)->av_bmiss(vap);
2413e585d188SSam Leffler }
2414b032f27cSSam Leffler 
2415f5c30c4eSAdrian Chadd /* XXX this needs a force wakeup! */
2416b837332dSAdrian Chadd int
2417459bc4f0SSam Leffler ath_hal_gethangstate(struct ath_hal *ah, uint32_t mask, uint32_t *hangs)
2418459bc4f0SSam Leffler {
2419459bc4f0SSam Leffler 	uint32_t rsize;
2420459bc4f0SSam Leffler 	void *sp;
2421459bc4f0SSam Leffler 
242225c96056SAdrian Chadd 	if (!ath_hal_getdiagstate(ah, HAL_DIAG_CHECK_HANGS, &mask, sizeof(mask), &sp, &rsize))
2423459bc4f0SSam Leffler 		return 0;
2424459bc4f0SSam Leffler 	KASSERT(rsize == sizeof(uint32_t), ("resultsize %u", rsize));
2425459bc4f0SSam Leffler 	*hangs = *(uint32_t *)sp;
2426459bc4f0SSam Leffler 	return 1;
2427459bc4f0SSam Leffler }
2428459bc4f0SSam Leffler 
2429b032f27cSSam Leffler static void
2430b032f27cSSam Leffler ath_bmiss_proc(void *arg, int pending)
2431b032f27cSSam Leffler {
2432b032f27cSSam Leffler 	struct ath_softc *sc = arg;
2433459bc4f0SSam Leffler 	uint32_t hangs;
2434b032f27cSSam Leffler 
2435b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
2436459bc4f0SSam Leffler 
2437f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2438f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2439f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2440f5c30c4eSAdrian Chadd 
2441f5c30c4eSAdrian Chadd 	ath_beacon_miss(sc);
2442f5c30c4eSAdrian Chadd 
2443a74ebfe5SAdrian Chadd 	/*
2444a74ebfe5SAdrian Chadd 	 * Do a reset upon any becaon miss event.
2445a74ebfe5SAdrian Chadd 	 *
2446a74ebfe5SAdrian Chadd 	 * It may be a non-recognised RX clear hang which needs a reset
2447a74ebfe5SAdrian Chadd 	 * to clear.
2448a74ebfe5SAdrian Chadd 	 */
2449459bc4f0SSam Leffler 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) {
24507a79cebfSGleb Smirnoff 		ath_reset(sc, ATH_RESET_NOLOSS);
245176e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
245276e6fd5dSGleb Smirnoff 		    "bb hang detected (0x%x), resetting\n", hangs);
2453a74ebfe5SAdrian Chadd 	} else {
24547a79cebfSGleb Smirnoff 		ath_reset(sc, ATH_RESET_NOLOSS);
24557a79cebfSGleb Smirnoff 		ieee80211_beacon_miss(&sc->sc_ic);
24565591b213SSam Leffler 	}
2457f5c30c4eSAdrian Chadd 
2458f5c30c4eSAdrian Chadd 	/* Force a beacon resync, in case they've drifted */
2459f5c30c4eSAdrian Chadd 	sc->sc_syncbeacon = 1;
2460f5c30c4eSAdrian Chadd 
2461f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2462f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2463f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2464a74ebfe5SAdrian Chadd }
24655591b213SSam Leffler 
2466724c193aSSam Leffler /*
2467b032f27cSSam Leffler  * Handle TKIP MIC setup to deal hardware that doesn't do MIC
2468b032f27cSSam Leffler  * calcs together with WME.  If necessary disable the crypto
2469b032f27cSSam Leffler  * hardware and mark the 802.11 state so keys will be setup
2470b032f27cSSam Leffler  * with the MIC work done in software.
2471b032f27cSSam Leffler  */
2472b032f27cSSam Leffler static void
2473b032f27cSSam Leffler ath_settkipmic(struct ath_softc *sc)
2474b032f27cSSam Leffler {
24757a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
2476b032f27cSSam Leffler 
2477b032f27cSSam Leffler 	if ((ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIP) && !sc->sc_wmetkipmic) {
2478b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WME) {
2479b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_FALSE);
2480b032f27cSSam Leffler 			ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC;
2481b032f27cSSam Leffler 		} else {
2482b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_TRUE);
2483b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
2484b032f27cSSam Leffler 		}
2485b032f27cSSam Leffler 	}
2486b032f27cSSam Leffler }
2487b032f27cSSam Leffler 
24887a79cebfSGleb Smirnoff static int
24897a79cebfSGleb Smirnoff ath_init(struct ath_softc *sc)
24905591b213SSam Leffler {
24917a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
24925591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
24935591b213SSam Leffler 	HAL_STATUS status;
24945591b213SSam Leffler 
24957a79cebfSGleb Smirnoff 	ATH_LOCK_ASSERT(sc);
24965591b213SSam Leffler 
24975591b213SSam Leffler 	/*
2498f5c30c4eSAdrian Chadd 	 * Force the sleep state awake.
2499f5c30c4eSAdrian Chadd 	 */
25007d567ed6SAdrian Chadd 	ath_power_setselfgen(sc, HAL_PM_AWAKE);
2501f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
25028c03e55dSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_AWAKE, 1);
2503f5c30c4eSAdrian Chadd 
2504f5c30c4eSAdrian Chadd 	/*
25055591b213SSam Leffler 	 * Stop anything previously setup.  This is safe
25065591b213SSam Leffler 	 * whether this is the first time through or not.
25075591b213SSam Leffler 	 */
25087a79cebfSGleb Smirnoff 	ath_stop(sc);
25095591b213SSam Leffler 
25105591b213SSam Leffler 	/*
25115591b213SSam Leffler 	 * The basic interface to setting the hardware in a good
25125591b213SSam Leffler 	 * state is ``reset''.  On return the hardware is known to
25135591b213SSam Leffler 	 * be powered up and with interrupts disabled.  This must
25145591b213SSam Leffler 	 * be followed by initialization of the appropriate bits
25155591b213SSam Leffler 	 * and then setup of the interrupt mask.
25165591b213SSam Leffler 	 */
2517b032f27cSSam Leffler 	ath_settkipmic(sc);
25186322256bSAdrian Chadd 	ath_update_chainmasks(sc, ic->ic_curchan);
25196322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
25206322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
2521f5c30c4eSAdrian Chadd 
252276e6fd5dSGleb Smirnoff 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_FALSE,
2523f50e4ebfSAdrian Chadd 	    HAL_RESET_NORMAL, &status)) {
252476e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
252576e6fd5dSGleb Smirnoff 		    "unable to reset hardware; hal status %u\n", status);
25267a79cebfSGleb Smirnoff 		return (ENODEV);
25275591b213SSam Leffler 	}
252817bb5fd1SAdrian Chadd 
252917bb5fd1SAdrian Chadd 	ATH_RX_LOCK(sc);
253017bb5fd1SAdrian Chadd 	sc->sc_rx_stopped = 1;
253117bb5fd1SAdrian Chadd 	sc->sc_rx_resetted = 1;
253217bb5fd1SAdrian Chadd 	ATH_RX_UNLOCK(sc);
253317bb5fd1SAdrian Chadd 
2534b032f27cSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
25355591b213SSam Leffler 
253648237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
253748237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
253848237774SAdrian Chadd 
25399af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
25409af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
25419af351f9SAdrian Chadd 
25425591b213SSam Leffler 	/*
2543b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
2544b70f530bSAdrian Chadd 	 */
2545b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
2546b70f530bSAdrian Chadd 
2547b70f530bSAdrian Chadd 	/*
2548dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
2549dd6a574eSAdrian Chadd 	 * support it.
2550dd6a574eSAdrian Chadd 	 */
2551dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
2552dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
2553dd6a574eSAdrian Chadd 	else
2554dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
2555dd6a574eSAdrian Chadd 
2556dd6a574eSAdrian Chadd 	/*
2557c59005e9SSam Leffler 	 * Likewise this is set during reset so update
2558c59005e9SSam Leffler 	 * state cached in the driver.
2559c59005e9SSam Leffler 	 */
2560c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
25619bbfde1eSAdrian Chadd 	sc->sc_lastlongcal = ticks;
25622dc7fcc4SSam Leffler 	sc->sc_resetcal = 1;
25632dc7fcc4SSam Leffler 	sc->sc_lastcalreset = 0;
25649bbfde1eSAdrian Chadd 	sc->sc_lastani = ticks;
25659bbfde1eSAdrian Chadd 	sc->sc_lastshortcal = ticks;
2566a108ab63SAdrian Chadd 	sc->sc_doresetcal = AH_FALSE;
25672fd9aabbSAdrian Chadd 	/*
25682fd9aabbSAdrian Chadd 	 * Beacon timers were cleared here; give ath_newstate()
25692fd9aabbSAdrian Chadd 	 * a hint that the beacon timers should be poked when
25702fd9aabbSAdrian Chadd 	 * things transition to the RUN state.
25712fd9aabbSAdrian Chadd 	 */
25722fd9aabbSAdrian Chadd 	sc->sc_beacons = 0;
2573c42a7b7eSSam Leffler 
2574c42a7b7eSSam Leffler 	/*
25755591b213SSam Leffler 	 * Setup the hardware after reset: the key cache
25765591b213SSam Leffler 	 * is filled as needed and the receive engine is
25775591b213SSam Leffler 	 * set going.  Frame transmit is handled entirely
25785591b213SSam Leffler 	 * in the frame output path; there's nothing to do
25795591b213SSam Leffler 	 * here except setup the interrupt mask.
25805591b213SSam Leffler 	 */
25815591b213SSam Leffler 	if (ath_startrecv(sc) != 0) {
258276e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "unable to start recv logic\n");
2583f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
25847a79cebfSGleb Smirnoff 		return (ENODEV);
25855591b213SSam Leffler 	}
25865591b213SSam Leffler 
25875591b213SSam Leffler 	/*
25885591b213SSam Leffler 	 * Enable interrupts.
25895591b213SSam Leffler 	 */
25905591b213SSam Leffler 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
259117bb5fd1SAdrian Chadd 		  | HAL_INT_RXORN | HAL_INT_TXURN
25925591b213SSam Leffler 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
2593bcbb08ceSAdrian Chadd 
2594bcbb08ceSAdrian Chadd 	/*
2595bcbb08ceSAdrian Chadd 	 * Enable RX EDMA bits.  Note these overlap with
2596bcbb08ceSAdrian Chadd 	 * HAL_INT_RX and HAL_INT_RXDESC respectively.
2597bcbb08ceSAdrian Chadd 	 */
2598bcbb08ceSAdrian Chadd 	if (sc->sc_isedma)
2599bcbb08ceSAdrian Chadd 		sc->sc_imask |= (HAL_INT_RXHP | HAL_INT_RXLP);
2600bcbb08ceSAdrian Chadd 
2601c42a7b7eSSam Leffler 	/*
260217bb5fd1SAdrian Chadd 	 * If we're an EDMA NIC, we don't care about RXEOL.
260317bb5fd1SAdrian Chadd 	 * Writing a new descriptor in will simply restart
260417bb5fd1SAdrian Chadd 	 * RX DMA.
260517bb5fd1SAdrian Chadd 	 */
260617bb5fd1SAdrian Chadd 	if (! sc->sc_isedma)
260717bb5fd1SAdrian Chadd 		sc->sc_imask |= HAL_INT_RXEOL;
260817bb5fd1SAdrian Chadd 
260917bb5fd1SAdrian Chadd 	/*
2610bcf5fc49SAdrian Chadd 	 * Enable MCI interrupt for MCI devices.
2611bcf5fc49SAdrian Chadd 	 */
2612bcf5fc49SAdrian Chadd 	if (sc->sc_btcoex_mci)
2613bcf5fc49SAdrian Chadd 		sc->sc_imask |= HAL_INT_MCI;
2614bcf5fc49SAdrian Chadd 
2615bcf5fc49SAdrian Chadd 	/*
2616c42a7b7eSSam Leffler 	 * Enable MIB interrupts when there are hardware phy counters.
2617c42a7b7eSSam Leffler 	 * Note we only do this (at the moment) for station mode.
2618c42a7b7eSSam Leffler 	 */
2619c42a7b7eSSam Leffler 	if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
2620c42a7b7eSSam Leffler 		sc->sc_imask |= HAL_INT_MIB;
26215591b213SSam Leffler 
2622f5c30c4eSAdrian Chadd 	/*
2623f5c30c4eSAdrian Chadd 	 * XXX add capability for this.
2624f5c30c4eSAdrian Chadd 	 *
2625f5c30c4eSAdrian Chadd 	 * If we're in STA mode (and maybe IBSS?) then register for
2626f5c30c4eSAdrian Chadd 	 * TSFOOR interrupts.
2627f5c30c4eSAdrian Chadd 	 */
2628f5c30c4eSAdrian Chadd 	if (ic->ic_opmode == IEEE80211_M_STA)
2629f5c30c4eSAdrian Chadd 		sc->sc_imask |= HAL_INT_TSFOOR;
2630f5c30c4eSAdrian Chadd 
26315594f5c0SAdrian Chadd 	/* Enable global TX timeout and carrier sense timeout if available */
26326ad02dbaSAdrian Chadd 	if (ath_hal_gtxto_supported(ah))
26333788ebedSAdrian Chadd 		sc->sc_imask |= HAL_INT_GTT;
2634d0a0ebc6SAdrian Chadd 
2635d0a0ebc6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: imask=0x%x\n",
2636d0a0ebc6SAdrian Chadd 		__func__, sc->sc_imask);
26376ad02dbaSAdrian Chadd 
26387a79cebfSGleb Smirnoff 	sc->sc_running = 1;
26392e986da5SSam Leffler 	callout_reset(&sc->sc_wd_ch, hz, ath_watchdog, sc);
2640b032f27cSSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
26415591b213SSam Leffler 
2642f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2643b032f27cSSam Leffler 
26447a79cebfSGleb Smirnoff 	return (0);
26455591b213SSam Leffler }
26465591b213SSam Leffler 
26475591b213SSam Leffler static void
26487a79cebfSGleb Smirnoff ath_stop(struct ath_softc *sc)
26495591b213SSam Leffler {
26505591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
26515591b213SSam Leffler 
2652c42a7b7eSSam Leffler 	ATH_LOCK_ASSERT(sc);
2653f5c30c4eSAdrian Chadd 
2654f5c30c4eSAdrian Chadd 	/*
2655f5c30c4eSAdrian Chadd 	 * Wake the hardware up before fiddling with it.
2656f5c30c4eSAdrian Chadd 	 */
2657f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2658f5c30c4eSAdrian Chadd 
26597a79cebfSGleb Smirnoff 	if (sc->sc_running) {
26605591b213SSam Leffler 		/*
26615591b213SSam Leffler 		 * Shutdown the hardware and driver:
2662c42a7b7eSSam Leffler 		 *    reset 802.11 state machine
26635591b213SSam Leffler 		 *    turn off timers
2664c42a7b7eSSam Leffler 		 *    disable interrupts
2665c42a7b7eSSam Leffler 		 *    turn off the radio
26665591b213SSam Leffler 		 *    clear transmit machinery
26675591b213SSam Leffler 		 *    clear receive machinery
26685591b213SSam Leffler 		 *    drain and release tx queues
26695591b213SSam Leffler 		 *    reclaim beacon resources
26705591b213SSam Leffler 		 *    power down hardware
26715591b213SSam Leffler 		 *
26725591b213SSam Leffler 		 * Note that some of this work is not possible if the
26735591b213SSam Leffler 		 * hardware is gone (invalid).
26745591b213SSam Leffler 		 */
267586e07743SSam Leffler #ifdef ATH_TX99_DIAG
267686e07743SSam Leffler 		if (sc->sc_tx99 != NULL)
267786e07743SSam Leffler 			sc->sc_tx99->stop(sc->sc_tx99);
267886e07743SSam Leffler #endif
26792e986da5SSam Leffler 		callout_stop(&sc->sc_wd_ch);
26802e986da5SSam Leffler 		sc->sc_wd_timer = 0;
26817a79cebfSGleb Smirnoff 		sc->sc_running = 0;
2682c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
26833e50ec2cSSam Leffler 			if (sc->sc_softled) {
26843e50ec2cSSam Leffler 				callout_stop(&sc->sc_ledtimer);
26853e50ec2cSSam Leffler 				ath_hal_gpioset(ah, sc->sc_ledpin,
26863e50ec2cSSam Leffler 					!sc->sc_ledon);
26873e50ec2cSSam Leffler 				sc->sc_blinking = 0;
26883e50ec2cSSam Leffler 			}
26895591b213SSam Leffler 			ath_hal_intrset(ah, 0);
2690c42a7b7eSSam Leffler 		}
2691062cf7d9SAdrian Chadd 		/* XXX we should stop RX regardless of whether it's valid */
2692c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
26939a842e8bSAdrian Chadd 			ath_stoprecv(sc, 1);
2694c42a7b7eSSam Leffler 			ath_hal_phydisable(ah);
2695c42a7b7eSSam Leffler 		} else
26965591b213SSam Leffler 			sc->sc_rxlink = NULL;
2697062cf7d9SAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);
2698b032f27cSSam Leffler 		ath_beacon_free(sc);	/* XXX not needed */
2699c42a7b7eSSam Leffler 	}
2700f5c30c4eSAdrian Chadd 
2701f5c30c4eSAdrian Chadd 	/* And now, restore the current power state */
2702f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2703c42a7b7eSSam Leffler }
2704c42a7b7eSSam Leffler 
2705f5c30c4eSAdrian Chadd /*
2706f5c30c4eSAdrian Chadd  * Wait until all pending TX/RX has completed.
2707f5c30c4eSAdrian Chadd  *
2708f5c30c4eSAdrian Chadd  * This waits until all existing transmit, receive and interrupts
2709f5c30c4eSAdrian Chadd  * have completed.  It's assumed that the caller has first
2710f5c30c4eSAdrian Chadd  * grabbed the reset lock so it doesn't try to do overlapping
2711f5c30c4eSAdrian Chadd  * chip resets.
2712f5c30c4eSAdrian Chadd  */
2713f5c30c4eSAdrian Chadd #define	MAX_TXRX_ITERATIONS	100
2714ef27340cSAdrian Chadd static void
271521008bf1SAdrian Chadd ath_txrx_stop_locked(struct ath_softc *sc)
2716ef27340cSAdrian Chadd {
2717ef27340cSAdrian Chadd 	int i = MAX_TXRX_ITERATIONS;
2718ef27340cSAdrian Chadd 
2719ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
272021008bf1SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
272121008bf1SAdrian Chadd 
2722ef27340cSAdrian Chadd 	/*
2723ef27340cSAdrian Chadd 	 * Sleep until all the pending operations have completed.
2724ef27340cSAdrian Chadd 	 *
2725ef27340cSAdrian Chadd 	 * The caller must ensure that reset has been incremented
2726ef27340cSAdrian Chadd 	 * or the pending operations may continue being queued.
2727ef27340cSAdrian Chadd 	 */
2728ef27340cSAdrian Chadd 	while (sc->sc_rxproc_cnt || sc->sc_txproc_cnt ||
2729ef27340cSAdrian Chadd 	    sc->sc_txstart_cnt || sc->sc_intr_cnt) {
2730ef27340cSAdrian Chadd 		if (i <= 0)
2731ef27340cSAdrian Chadd 			break;
2732f5c30c4eSAdrian Chadd 		msleep(sc, &sc->sc_pcu_mtx, 0, "ath_txrx_stop",
2733f5c30c4eSAdrian Chadd 		    msecs_to_ticks(10));
2734ef27340cSAdrian Chadd 		i--;
2735ef27340cSAdrian Chadd 	}
2736ef27340cSAdrian Chadd 
2737ef27340cSAdrian Chadd 	if (i <= 0)
2738ef27340cSAdrian Chadd 		device_printf(sc->sc_dev,
2739ef27340cSAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2740ef27340cSAdrian Chadd 		    __func__, MAX_TXRX_ITERATIONS);
2741ef27340cSAdrian Chadd }
2742ef27340cSAdrian Chadd #undef	MAX_TXRX_ITERATIONS
2743ef27340cSAdrian Chadd 
2744e78719adSAdrian Chadd #if 0
2745ef27340cSAdrian Chadd static void
274621008bf1SAdrian Chadd ath_txrx_stop(struct ath_softc *sc)
274721008bf1SAdrian Chadd {
274821008bf1SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
274921008bf1SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
275021008bf1SAdrian Chadd 
275121008bf1SAdrian Chadd 	ATH_PCU_LOCK(sc);
275221008bf1SAdrian Chadd 	ath_txrx_stop_locked(sc);
275321008bf1SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
275421008bf1SAdrian Chadd }
2755e78719adSAdrian Chadd #endif
275621008bf1SAdrian Chadd 
275721008bf1SAdrian Chadd static void
2758ef27340cSAdrian Chadd ath_txrx_start(struct ath_softc *sc)
2759ef27340cSAdrian Chadd {
2760ef27340cSAdrian Chadd 
2761ef27340cSAdrian Chadd 	taskqueue_unblock(sc->sc_tq);
2762ef27340cSAdrian Chadd }
2763ef27340cSAdrian Chadd 
2764ee321975SAdrian Chadd /*
2765ee321975SAdrian Chadd  * Grab the reset lock, and wait around until no one else
2766ee321975SAdrian Chadd  * is trying to do anything with it.
2767ee321975SAdrian Chadd  *
2768ee321975SAdrian Chadd  * This is totally horrible but we can't hold this lock for
2769ee321975SAdrian Chadd  * long enough to do TX/RX or we end up with net80211/ip stack
2770ee321975SAdrian Chadd  * LORs and eventual deadlock.
2771ee321975SAdrian Chadd  *
2772ee321975SAdrian Chadd  * "dowait" signals whether to spin, waiting for the reset
2773ee321975SAdrian Chadd  * lock count to reach 0. This should (for now) only be used
2774ee321975SAdrian Chadd  * during the reset path, as the rest of the code may not
2775ee321975SAdrian Chadd  * be locking-reentrant enough to behave correctly.
2776ee321975SAdrian Chadd  *
2777ee321975SAdrian Chadd  * Another, cleaner way should be found to serialise all of
2778ee321975SAdrian Chadd  * these operations.
2779ee321975SAdrian Chadd  */
2780f5c30c4eSAdrian Chadd #define	MAX_RESET_ITERATIONS	25
2781ee321975SAdrian Chadd static int
2782ee321975SAdrian Chadd ath_reset_grablock(struct ath_softc *sc, int dowait)
2783ee321975SAdrian Chadd {
2784ee321975SAdrian Chadd 	int w = 0;
2785ee321975SAdrian Chadd 	int i = MAX_RESET_ITERATIONS;
2786ee321975SAdrian Chadd 
2787ee321975SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
2788ee321975SAdrian Chadd 	do {
2789ee321975SAdrian Chadd 		if (sc->sc_inreset_cnt == 0) {
2790ee321975SAdrian Chadd 			w = 1;
2791ee321975SAdrian Chadd 			break;
2792ee321975SAdrian Chadd 		}
2793ee321975SAdrian Chadd 		if (dowait == 0) {
2794ee321975SAdrian Chadd 			w = 0;
2795ee321975SAdrian Chadd 			break;
2796ee321975SAdrian Chadd 		}
2797ee321975SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2798f5c30c4eSAdrian Chadd 		/*
2799f5c30c4eSAdrian Chadd 		 * 1 tick is likely not enough time for long calibrations
2800f5c30c4eSAdrian Chadd 		 * to complete.  So we should wait quite a while.
2801f5c30c4eSAdrian Chadd 		 */
2802f5c30c4eSAdrian Chadd 		pause("ath_reset_grablock", msecs_to_ticks(100));
2803ee321975SAdrian Chadd 		i--;
2804ee321975SAdrian Chadd 		ATH_PCU_LOCK(sc);
2805ee321975SAdrian Chadd 	} while (i > 0);
2806ee321975SAdrian Chadd 
2807ee321975SAdrian Chadd 	/*
2808ee321975SAdrian Chadd 	 * We always increment the refcounter, regardless
2809ee321975SAdrian Chadd 	 * of whether we succeeded to get it in an exclusive
2810ee321975SAdrian Chadd 	 * way.
2811ee321975SAdrian Chadd 	 */
2812ee321975SAdrian Chadd 	sc->sc_inreset_cnt++;
2813ee321975SAdrian Chadd 
2814ee321975SAdrian Chadd 	if (i <= 0)
2815ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2816ee321975SAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2817ee321975SAdrian Chadd 		    __func__, MAX_RESET_ITERATIONS);
2818ee321975SAdrian Chadd 
2819ee321975SAdrian Chadd 	if (w == 0)
2820ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2821ee321975SAdrian Chadd 		    "%s: warning, recursive reset path!\n",
2822ee321975SAdrian Chadd 		    __func__);
2823ee321975SAdrian Chadd 
2824ee321975SAdrian Chadd 	return w;
2825ee321975SAdrian Chadd }
2826ee321975SAdrian Chadd #undef MAX_RESET_ITERATIONS
2827ee321975SAdrian Chadd 
2828ee321975SAdrian Chadd /*
28295591b213SSam Leffler  * Reset the hardware w/o losing operational state.  This is
28305591b213SSam Leffler  * basically a more efficient way of doing ath_stop, ath_init,
28315591b213SSam Leffler  * followed by state transitions to the current 802.11
2832c42a7b7eSSam Leffler  * operational state.  Used to recover from various errors and
2833c42a7b7eSSam Leffler  * to reset or reload hardware state.
28345591b213SSam Leffler  */
28356079fdbeSAdrian Chadd int
28367a79cebfSGleb Smirnoff ath_reset(struct ath_softc *sc, ATH_RESET_TYPE reset_type)
28375591b213SSam Leffler {
28387a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
28395591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
28405591b213SSam Leffler 	HAL_STATUS status;
2841ef27340cSAdrian Chadd 	int i;
28425591b213SSam Leffler 
2843f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
284416d4de92SAdrian Chadd 
2845ee321975SAdrian Chadd 	/* Ensure ATH_LOCK isn't held; ath_rx_proc can't be locked */
2846ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
2847ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
2848ef27340cSAdrian Chadd 
2849f6b6084bSPedro F. Giffuni 	/* Try to (stop any further TX/RX from occurring */
2850d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
2851d52f7132SAdrian Chadd 
2852f5c30c4eSAdrian Chadd 	/*
2853f5c30c4eSAdrian Chadd 	 * Wake the hardware up.
2854f5c30c4eSAdrian Chadd 	 */
2855f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2856f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2857f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2858f5c30c4eSAdrian Chadd 
2859ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2860904e385eSAdrian Chadd 
2861904e385eSAdrian Chadd 	/*
2862904e385eSAdrian Chadd 	 * Grab the reset lock before TX/RX is stopped.
2863904e385eSAdrian Chadd 	 *
2864904e385eSAdrian Chadd 	 * This is needed to ensure that when the TX/RX actually does finish,
2865904e385eSAdrian Chadd 	 * no further TX/RX/reset runs in parallel with this.
2866904e385eSAdrian Chadd 	 */
2867ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
2868ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
2869ef27340cSAdrian Chadd 		    __func__);
2870ef27340cSAdrian Chadd 	}
2871904e385eSAdrian Chadd 
2872904e385eSAdrian Chadd 	/* disable interrupts */
2873904e385eSAdrian Chadd 	ath_hal_intrset(ah, 0);
2874904e385eSAdrian Chadd 
2875904e385eSAdrian Chadd 	/*
2876904e385eSAdrian Chadd 	 * Now, ensure that any in progress TX/RX completes before we
2877904e385eSAdrian Chadd 	 * continue.
2878904e385eSAdrian Chadd 	 */
2879904e385eSAdrian Chadd 	ath_txrx_stop_locked(sc);
2880904e385eSAdrian Chadd 
2881ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2882ef27340cSAdrian Chadd 
2883f52d3452SAdrian Chadd 	/*
2884ef27340cSAdrian Chadd 	 * Regardless of whether we're doing a no-loss flush or
2885ef27340cSAdrian Chadd 	 * not, stop the PCU and handle what's in the RX queue.
2886ef27340cSAdrian Chadd 	 * That way frames aren't dropped which shouldn't be.
2887ef27340cSAdrian Chadd 	 */
28889a842e8bSAdrian Chadd 	ath_stoprecv(sc, (reset_type != ATH_RESET_NOLOSS));
2889f8cc9b09SAdrian Chadd 	ath_rx_flush(sc);
2890ef27340cSAdrian Chadd 
2891062cf7d9SAdrian Chadd 	/*
2892062cf7d9SAdrian Chadd 	 * Should now wait for pending TX/RX to complete
2893f6b6084bSPedro F. Giffuni 	 * and block future ones from occurring. This needs to be
2894062cf7d9SAdrian Chadd 	 * done before the TX queue is drained.
2895062cf7d9SAdrian Chadd 	 */
2896062cf7d9SAdrian Chadd 	ath_draintxq(sc, reset_type);	/* stop xmit side */
2897062cf7d9SAdrian Chadd 
2898b032f27cSSam Leffler 	ath_settkipmic(sc);		/* configure TKIP MIC handling */
28995591b213SSam Leffler 	/* NB: indicate channel change so we do a full reset */
29006322256bSAdrian Chadd 	ath_update_chainmasks(sc, ic->ic_curchan);
29016322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
29026322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
2903f50e4ebfSAdrian Chadd 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_TRUE,
2904f50e4ebfSAdrian Chadd 	    HAL_RESET_NORMAL, &status))
290576e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
290676e6fd5dSGleb Smirnoff 		    "%s: unable to reset hardware; hal status %u\n",
29075591b213SSam Leffler 		    __func__, status);
2908c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
290948237774SAdrian Chadd 
291017bb5fd1SAdrian Chadd 	ATH_RX_LOCK(sc);
291117bb5fd1SAdrian Chadd 	sc->sc_rx_stopped = 1;
291217bb5fd1SAdrian Chadd 	sc->sc_rx_resetted = 1;
291317bb5fd1SAdrian Chadd 	ATH_RX_UNLOCK(sc);
291417bb5fd1SAdrian Chadd 
291548237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
291648237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
291748237774SAdrian Chadd 
29189af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
29199af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
29209af351f9SAdrian Chadd 
2921dd6a574eSAdrian Chadd 	/*
2922b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
2923b70f530bSAdrian Chadd 	 */
2924b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
2925b70f530bSAdrian Chadd 
2926b70f530bSAdrian Chadd 	/*
2927dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
2928dd6a574eSAdrian Chadd 	 * support it.
2929dd6a574eSAdrian Chadd 	 */
2930dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
2931dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
2932dd6a574eSAdrian Chadd 	else
2933dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
2934dd6a574eSAdrian Chadd 
293568e8e04eSSam Leffler 	if (ath_startrecv(sc) != 0)	/* restart recv */
293676e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
293776e6fd5dSGleb Smirnoff 		    "%s: unable to start recv logic\n", __func__);
2938c42a7b7eSSam Leffler 	/*
2939c42a7b7eSSam Leffler 	 * We may be doing a reset in response to an ioctl
2940c42a7b7eSSam Leffler 	 * that changes the channel so update any state that
2941c42a7b7eSSam Leffler 	 * might change as a result.
2942c42a7b7eSSam Leffler 	 */
2943724c193aSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
2944c89b957aSSam Leffler 	if (sc->sc_beacons) {		/* restart beacons */
2945584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
294610ad9a77SSam Leffler 		if (sc->sc_tdma)
294710ad9a77SSam Leffler 			ath_tdma_config(sc, NULL);
294810ad9a77SSam Leffler 		else
294910ad9a77SSam Leffler #endif
2950c89b957aSSam Leffler 			ath_beacon_config(sc, NULL);
295110ad9a77SSam Leffler 	}
2952c42a7b7eSSam Leffler 
2953ef27340cSAdrian Chadd 	/*
2954ef27340cSAdrian Chadd 	 * Release the reset lock and re-enable interrupts here.
2955ef27340cSAdrian Chadd 	 * If an interrupt was being processed in ath_intr(),
2956ef27340cSAdrian Chadd 	 * it would disable interrupts at this point. So we have
2957ef27340cSAdrian Chadd 	 * to atomically enable interrupts and decrement the
2958ef27340cSAdrian Chadd 	 * reset counter - this way ath_intr() doesn't end up
2959ef27340cSAdrian Chadd 	 * disabling interrupts without a corresponding enable
2960ef27340cSAdrian Chadd 	 * in the rest or channel change path.
2961f5c30c4eSAdrian Chadd 	 *
2962f5c30c4eSAdrian Chadd 	 * Grab the TX reference in case we need to transmit.
2963f5c30c4eSAdrian Chadd 	 * That way a parallel transmit doesn't.
2964ef27340cSAdrian Chadd 	 */
2965ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2966ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
2967f5c30c4eSAdrian Chadd 	sc->sc_txstart_cnt++;
2968ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
2969ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
2970ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2971ef27340cSAdrian Chadd 
2972ef27340cSAdrian Chadd 	/*
2973ef27340cSAdrian Chadd 	 * TX and RX can be started here. If it were started with
2974ef27340cSAdrian Chadd 	 * sc_inreset_cnt > 0, the TX and RX path would abort.
2975ef27340cSAdrian Chadd 	 * Thus if this is a nested call through the reset or
2976ef27340cSAdrian Chadd 	 * channel change code, TX completion will occur but
2977ef27340cSAdrian Chadd 	 * RX completion and ath_start / ath_tx_start will not
2978ef27340cSAdrian Chadd 	 * run.
2979ef27340cSAdrian Chadd 	 */
2980ef27340cSAdrian Chadd 
2981ef27340cSAdrian Chadd 	/* Restart TX/RX as needed */
2982ef27340cSAdrian Chadd 	ath_txrx_start(sc);
2983ef27340cSAdrian Chadd 
2984f5c30c4eSAdrian Chadd 	/* XXX TODO: we need to hold the tx refcount here! */
2985f5c30c4eSAdrian Chadd 
2986375307d4SAdrian Chadd 	/* Restart TX completion and pending TX */
2987ef27340cSAdrian Chadd 	if (reset_type == ATH_RESET_NOLOSS) {
2988ef27340cSAdrian Chadd 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
2989ef27340cSAdrian Chadd 			if (ATH_TXQ_SETUP(sc, i)) {
2990b837332dSAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
2991ef27340cSAdrian Chadd 				ath_txq_restart_dma(sc, &sc->sc_txq[i]);
2992b837332dSAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
2993b837332dSAdrian Chadd 
2994b837332dSAdrian Chadd 				ATH_TX_LOCK(sc);
2995ef27340cSAdrian Chadd 				ath_txq_sched(sc, &sc->sc_txq[i]);
2996375307d4SAdrian Chadd 				ATH_TX_UNLOCK(sc);
2997ef27340cSAdrian Chadd 			}
2998b837332dSAdrian Chadd 		}
2999b837332dSAdrian Chadd 	}
3000ef27340cSAdrian Chadd 
3001f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3002f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3003f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3004f5c30c4eSAdrian Chadd 
3005f5c30c4eSAdrian Chadd 	ATH_PCU_LOCK(sc);
3006f5c30c4eSAdrian Chadd 	sc->sc_txstart_cnt--;
3007f5c30c4eSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3008f5c30c4eSAdrian Chadd 
3009ef27340cSAdrian Chadd 	/* Handle any frames in the TX queue */
3010ef27340cSAdrian Chadd 	/*
3011ef27340cSAdrian Chadd 	 * XXX should this be done by the caller, rather than
3012ef27340cSAdrian Chadd 	 * ath_reset() ?
3013ef27340cSAdrian Chadd 	 */
30148e739394SAdrian Chadd 	ath_tx_kick(sc);		/* restart xmit */
3015c42a7b7eSSam Leffler 	return 0;
30165591b213SSam Leffler }
30175591b213SSam Leffler 
301868e8e04eSSam Leffler static int
3019b032f27cSSam Leffler ath_reset_vap(struct ieee80211vap *vap, u_long cmd)
3020b032f27cSSam Leffler {
30214b54a231SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
30223797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
30234b54a231SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
30244b54a231SSam Leffler 
30254b54a231SSam Leffler 	switch (cmd) {
30264b54a231SSam Leffler 	case IEEE80211_IOC_TXPOWER:
30274b54a231SSam Leffler 		/*
30284b54a231SSam Leffler 		 * If per-packet TPC is enabled, then we have nothing
30294b54a231SSam Leffler 		 * to do; otherwise we need to force the global limit.
30304b54a231SSam Leffler 		 * All this can happen directly; no need to reset.
30314b54a231SSam Leffler 		 */
30324b54a231SSam Leffler 		if (!ath_hal_gettpc(ah))
30334b54a231SSam Leffler 			ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
30344b54a231SSam Leffler 		return 0;
30354b54a231SSam Leffler 	}
3036517526efSAdrian Chadd 	/* XXX? Full or NOLOSS? */
30377a79cebfSGleb Smirnoff 	return ath_reset(sc, ATH_RESET_FULL);
3038b032f27cSSam Leffler }
3039b032f27cSSam Leffler 
3040b8e788a5SAdrian Chadd struct ath_buf *
3041af33d486SAdrian Chadd _ath_getbuf_locked(struct ath_softc *sc, ath_buf_type_t btype)
304210ad9a77SSam Leffler {
304310ad9a77SSam Leffler 	struct ath_buf *bf;
304410ad9a77SSam Leffler 
304510ad9a77SSam Leffler 	ATH_TXBUF_LOCK_ASSERT(sc);
304610ad9a77SSam Leffler 
3047af33d486SAdrian Chadd 	if (btype == ATH_BUFTYPE_MGMT)
3048af33d486SAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_txbuf_mgmt);
3049af33d486SAdrian Chadd 	else
30506b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_txbuf);
3051af33d486SAdrian Chadd 
3052e346b073SAdrian Chadd 	if (bf == NULL) {
3053e346b073SAdrian Chadd 		sc->sc_stats.ast_tx_getnobuf++;
3054e346b073SAdrian Chadd 	} else {
3055e346b073SAdrian Chadd 		if (bf->bf_flags & ATH_BUF_BUSY) {
3056e346b073SAdrian Chadd 			sc->sc_stats.ast_tx_getbusybuf++;
3057e346b073SAdrian Chadd 			bf = NULL;
3058e346b073SAdrian Chadd 		}
3059e346b073SAdrian Chadd 	}
3060e346b073SAdrian Chadd 
3061af33d486SAdrian Chadd 	if (bf != NULL && (bf->bf_flags & ATH_BUF_BUSY) == 0) {
3062af33d486SAdrian Chadd 		if (btype == ATH_BUFTYPE_MGMT)
3063af33d486SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_txbuf_mgmt, bf, bf_list);
306423ced6c1SAdrian Chadd 		else {
3065af33d486SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
306623ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt--;
306723ced6c1SAdrian Chadd 
306823ced6c1SAdrian Chadd 			/*
306923ced6c1SAdrian Chadd 			 * This shuldn't happen; however just to be
307023ced6c1SAdrian Chadd 			 * safe print a warning and fudge the txbuf
307123ced6c1SAdrian Chadd 			 * count.
307223ced6c1SAdrian Chadd 			 */
307323ced6c1SAdrian Chadd 			if (sc->sc_txbuf_cnt < 0) {
307423ced6c1SAdrian Chadd 				device_printf(sc->sc_dev,
307523ced6c1SAdrian Chadd 				    "%s: sc_txbuf_cnt < 0?\n",
307623ced6c1SAdrian Chadd 				    __func__);
307723ced6c1SAdrian Chadd 				sc->sc_txbuf_cnt = 0;
307823ced6c1SAdrian Chadd 			}
307923ced6c1SAdrian Chadd 		}
3080af33d486SAdrian Chadd 	} else
308110ad9a77SSam Leffler 		bf = NULL;
3082e346b073SAdrian Chadd 
308310ad9a77SSam Leffler 	if (bf == NULL) {
3084af33d486SAdrian Chadd 		/* XXX should check which list, mgmt or otherwise */
308510ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: %s\n", __func__,
30866b349e5aSAdrian Chadd 		    TAILQ_FIRST(&sc->sc_txbuf) == NULL ?
308710ad9a77SSam Leffler 			"out of xmit buffers" : "xmit buffer busy");
3088e346b073SAdrian Chadd 		return NULL;
308910ad9a77SSam Leffler 	}
3090e346b073SAdrian Chadd 
3091af33d486SAdrian Chadd 	/* XXX TODO: should do this at buffer list initialisation */
3092af33d486SAdrian Chadd 	/* XXX (then, ensure the buffer has the right flag set) */
30933feffbd7SAdrian Chadd 	bf->bf_flags = 0;
3094af33d486SAdrian Chadd 	if (btype == ATH_BUFTYPE_MGMT)
3095af33d486SAdrian Chadd 		bf->bf_flags |= ATH_BUF_MGMT;
3096af33d486SAdrian Chadd 	else
3097af33d486SAdrian Chadd 		bf->bf_flags &= (~ATH_BUF_MGMT);
3098af33d486SAdrian Chadd 
3099e346b073SAdrian Chadd 	/* Valid bf here; clear some basic fields */
3100e346b073SAdrian Chadd 	bf->bf_next = NULL;	/* XXX just to be sure */
3101e346b073SAdrian Chadd 	bf->bf_last = NULL;	/* XXX again, just to be sure */
3102e346b073SAdrian Chadd 	bf->bf_comp = NULL;	/* XXX again, just to be sure */
3103e346b073SAdrian Chadd 	bzero(&bf->bf_state, sizeof(bf->bf_state));
3104e346b073SAdrian Chadd 
310585bf9bc3SAdrian Chadd 	/*
310685bf9bc3SAdrian Chadd 	 * Track the descriptor ID only if doing EDMA
310785bf9bc3SAdrian Chadd 	 */
310885bf9bc3SAdrian Chadd 	if (sc->sc_isedma) {
310985bf9bc3SAdrian Chadd 		bf->bf_descid = sc->sc_txbuf_descid;
311085bf9bc3SAdrian Chadd 		sc->sc_txbuf_descid++;
311185bf9bc3SAdrian Chadd 	}
311285bf9bc3SAdrian Chadd 
311310ad9a77SSam Leffler 	return bf;
311410ad9a77SSam Leffler }
311510ad9a77SSam Leffler 
3116e346b073SAdrian Chadd /*
3117e346b073SAdrian Chadd  * When retrying a software frame, buffers marked ATH_BUF_BUSY
3118e346b073SAdrian Chadd  * can't be thrown back on the queue as they could still be
3119e346b073SAdrian Chadd  * in use by the hardware.
3120e346b073SAdrian Chadd  *
3121e346b073SAdrian Chadd  * This duplicates the buffer, or returns NULL.
3122e346b073SAdrian Chadd  *
3123e346b073SAdrian Chadd  * The descriptor is also copied but the link pointers and
3124e346b073SAdrian Chadd  * the DMA segments aren't copied; this frame should thus
3125e346b073SAdrian Chadd  * be again passed through the descriptor setup/chain routines
3126e346b073SAdrian Chadd  * so the link is correct.
3127e346b073SAdrian Chadd  *
3128e346b073SAdrian Chadd  * The caller must free the buffer using ath_freebuf().
3129e346b073SAdrian Chadd  */
3130e346b073SAdrian Chadd struct ath_buf *
31313f3a5dbdSAdrian Chadd ath_buf_clone(struct ath_softc *sc, struct ath_buf *bf)
3132e346b073SAdrian Chadd {
3133e346b073SAdrian Chadd 	struct ath_buf *tbf;
3134e346b073SAdrian Chadd 
3135af33d486SAdrian Chadd 	tbf = ath_getbuf(sc,
3136af33d486SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_MGMT) ?
3137af33d486SAdrian Chadd 	     ATH_BUFTYPE_MGMT : ATH_BUFTYPE_NORMAL);
3138e346b073SAdrian Chadd 	if (tbf == NULL)
3139e346b073SAdrian Chadd 		return NULL;	/* XXX failure? Why? */
3140e346b073SAdrian Chadd 
3141e346b073SAdrian Chadd 	/* Copy basics */
3142e346b073SAdrian Chadd 	tbf->bf_next = NULL;
3143e346b073SAdrian Chadd 	tbf->bf_nseg = bf->bf_nseg;
31443feffbd7SAdrian Chadd 	tbf->bf_flags = bf->bf_flags & ATH_BUF_FLAGS_CLONE;
3145e346b073SAdrian Chadd 	tbf->bf_status = bf->bf_status;
3146e346b073SAdrian Chadd 	tbf->bf_m = bf->bf_m;
3147e346b073SAdrian Chadd 	tbf->bf_node = bf->bf_node;
3148f5c30c4eSAdrian Chadd 	KASSERT((bf->bf_node != NULL), ("%s: bf_node=NULL!", __func__));
3149e346b073SAdrian Chadd 	/* will be setup by the chain/setup function */
3150e346b073SAdrian Chadd 	tbf->bf_lastds = NULL;
3151e346b073SAdrian Chadd 	/* for now, last == self */
3152e346b073SAdrian Chadd 	tbf->bf_last = tbf;
3153e346b073SAdrian Chadd 	tbf->bf_comp = bf->bf_comp;
3154e346b073SAdrian Chadd 
3155e346b073SAdrian Chadd 	/* NOTE: DMA segments will be setup by the setup/chain functions */
3156e346b073SAdrian Chadd 
3157e346b073SAdrian Chadd 	/* The caller has to re-init the descriptor + links */
3158e346b073SAdrian Chadd 
31593f3a5dbdSAdrian Chadd 	/*
31603f3a5dbdSAdrian Chadd 	 * Free the DMA mapping here, before we NULL the mbuf.
31613f3a5dbdSAdrian Chadd 	 * We must only call bus_dmamap_unload() once per mbuf chain
31623f3a5dbdSAdrian Chadd 	 * or behaviour is undefined.
31633f3a5dbdSAdrian Chadd 	 */
31643f3a5dbdSAdrian Chadd 	if (bf->bf_m != NULL) {
31653f3a5dbdSAdrian Chadd 		/*
31663f3a5dbdSAdrian Chadd 		 * XXX is this POSTWRITE call required?
31673f3a5dbdSAdrian Chadd 		 */
31683f3a5dbdSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
31693f3a5dbdSAdrian Chadd 		    BUS_DMASYNC_POSTWRITE);
31703f3a5dbdSAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
31713f3a5dbdSAdrian Chadd 	}
31723f3a5dbdSAdrian Chadd 
31733f3a5dbdSAdrian Chadd 	bf->bf_m = NULL;
31743f3a5dbdSAdrian Chadd 	bf->bf_node = NULL;
31753f3a5dbdSAdrian Chadd 
3176e346b073SAdrian Chadd 	/* Copy state */
3177e346b073SAdrian Chadd 	memcpy(&tbf->bf_state, &bf->bf_state, sizeof(bf->bf_state));
3178e346b073SAdrian Chadd 
3179e346b073SAdrian Chadd 	return tbf;
3180e346b073SAdrian Chadd }
3181e346b073SAdrian Chadd 
3182b8e788a5SAdrian Chadd struct ath_buf *
3183af33d486SAdrian Chadd ath_getbuf(struct ath_softc *sc, ath_buf_type_t btype)
318410ad9a77SSam Leffler {
318510ad9a77SSam Leffler 	struct ath_buf *bf;
318610ad9a77SSam Leffler 
318710ad9a77SSam Leffler 	ATH_TXBUF_LOCK(sc);
3188af33d486SAdrian Chadd 	bf = _ath_getbuf_locked(sc, btype);
3189af33d486SAdrian Chadd 	/*
3190af33d486SAdrian Chadd 	 * If a mgmt buffer was requested but we're out of those,
3191af33d486SAdrian Chadd 	 * try requesting a normal one.
3192af33d486SAdrian Chadd 	 */
3193af33d486SAdrian Chadd 	if (bf == NULL && btype == ATH_BUFTYPE_MGMT)
3194af33d486SAdrian Chadd 		bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL);
3195e4e7938aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
319610ad9a77SSam Leffler 	if (bf == NULL) {
319710ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: stop queue\n", __func__);
319810ad9a77SSam Leffler 		sc->sc_stats.ast_tx_qstop++;
319910ad9a77SSam Leffler 	}
320010ad9a77SSam Leffler 	return bf;
320110ad9a77SSam Leffler }
320210ad9a77SSam Leffler 
32037dcb2beaSAdrian Chadd /*
3204cd7dffd0SAdrian Chadd  * Transmit a single frame.
3205cd7dffd0SAdrian Chadd  *
3206cd7dffd0SAdrian Chadd  * net80211 will free the node reference if the transmit
3207cd7dffd0SAdrian Chadd  * fails, so don't free the node reference here.
32087dcb2beaSAdrian Chadd  */
3209cd7dffd0SAdrian Chadd static int
32107a79cebfSGleb Smirnoff ath_transmit(struct ieee80211com *ic, struct mbuf *m)
3211cd7dffd0SAdrian Chadd {
32123797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
3213cd7dffd0SAdrian Chadd 	struct ieee80211_node *ni;
3214cd7dffd0SAdrian Chadd 	struct mbuf *next;
3215cd7dffd0SAdrian Chadd 	struct ath_buf *bf;
3216cd7dffd0SAdrian Chadd 	ath_bufhead frags;
3217cd7dffd0SAdrian Chadd 	int retval = 0;
3218cd7dffd0SAdrian Chadd 
3219cd7dffd0SAdrian Chadd 	/*
3220cd7dffd0SAdrian Chadd 	 * Tell the reset path that we're currently transmitting.
3221cd7dffd0SAdrian Chadd 	 */
3222cd7dffd0SAdrian Chadd 	ATH_PCU_LOCK(sc);
3223cd7dffd0SAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
322483bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
3225cd7dffd0SAdrian Chadd 		    "%s: sc_inreset_cnt > 0; bailing\n", __func__);
3226cd7dffd0SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
3227cd7dffd0SAdrian Chadd 		sc->sc_stats.ast_tx_qstop++;
3228cd7dffd0SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TX, 0, "ath_start_task: OACTIVE, finish");
3229cd7dffd0SAdrian Chadd 		return (ENOBUFS);	/* XXX should be EINVAL or? */
3230cd7dffd0SAdrian Chadd 	}
3231cd7dffd0SAdrian Chadd 	sc->sc_txstart_cnt++;
3232cd7dffd0SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3233cd7dffd0SAdrian Chadd 
3234f5c30c4eSAdrian Chadd 	/* Wake the hardware up already */
3235f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3236f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3237f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3238f5c30c4eSAdrian Chadd 
3239cd7dffd0SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_transmit: start");
3240cd7dffd0SAdrian Chadd 	/*
3241cd7dffd0SAdrian Chadd 	 * Grab the TX lock - it's ok to do this here; we haven't
3242cd7dffd0SAdrian Chadd 	 * yet started transmitting.
3243cd7dffd0SAdrian Chadd 	 */
3244cd7dffd0SAdrian Chadd 	ATH_TX_LOCK(sc);
3245cd7dffd0SAdrian Chadd 
3246cd7dffd0SAdrian Chadd 	/*
3247cd7dffd0SAdrian Chadd 	 * Node reference, if there's one.
3248cd7dffd0SAdrian Chadd 	 */
32497dcb2beaSAdrian Chadd 	ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
32507dcb2beaSAdrian Chadd 
32517dcb2beaSAdrian Chadd 	/*
32527dcb2beaSAdrian Chadd 	 * Enforce how deep a node queue can get.
32537dcb2beaSAdrian Chadd 	 *
32547dcb2beaSAdrian Chadd 	 * XXX it would be nicer if we kept an mbuf queue per
32557dcb2beaSAdrian Chadd 	 * node and only whacked them into ath_bufs when we
32567dcb2beaSAdrian Chadd 	 * are ready to schedule some traffic from them.
32577dcb2beaSAdrian Chadd 	 * .. that may come later.
32587dcb2beaSAdrian Chadd 	 *
32597dcb2beaSAdrian Chadd 	 * XXX we should also track the per-node hardware queue
32607dcb2beaSAdrian Chadd 	 * depth so it is easy to limit the _SUM_ of the swq and
32617dcb2beaSAdrian Chadd 	 * hwq frames.  Since we only schedule two HWQ frames
32627dcb2beaSAdrian Chadd 	 * at a time, this should be OK for now.
32637dcb2beaSAdrian Chadd 	 */
32647dcb2beaSAdrian Chadd 	if ((!(m->m_flags & M_EAPOL)) &&
32657dcb2beaSAdrian Chadd 	    (ATH_NODE(ni)->an_swq_depth > sc->sc_txq_node_maxdepth)) {
32667dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nodeq_overflow++;
3267cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
3268cd7dffd0SAdrian Chadd 		goto finish;
32697dcb2beaSAdrian Chadd 	}
32707dcb2beaSAdrian Chadd 
32717dcb2beaSAdrian Chadd 	/*
32727dcb2beaSAdrian Chadd 	 * Check how many TX buffers are available.
32737dcb2beaSAdrian Chadd 	 *
32747dcb2beaSAdrian Chadd 	 * If this is for non-EAPOL traffic, just leave some
32757dcb2beaSAdrian Chadd 	 * space free in order for buffer cloning and raw
32767dcb2beaSAdrian Chadd 	 * frame transmission to occur.
32777dcb2beaSAdrian Chadd 	 *
32787dcb2beaSAdrian Chadd 	 * If it's for EAPOL traffic, ignore this for now.
32797dcb2beaSAdrian Chadd 	 * Management traffic will be sent via the raw transmit
32807dcb2beaSAdrian Chadd 	 * method which bypasses this check.
32817dcb2beaSAdrian Chadd 	 *
32827dcb2beaSAdrian Chadd 	 * This is needed to ensure that EAPOL frames during
32837dcb2beaSAdrian Chadd 	 * (re) keying have a chance to go out.
32847dcb2beaSAdrian Chadd 	 *
32857dcb2beaSAdrian Chadd 	 * See kern/138379 for more information.
32867dcb2beaSAdrian Chadd 	 */
32877dcb2beaSAdrian Chadd 	if ((!(m->m_flags & M_EAPOL)) &&
32887dcb2beaSAdrian Chadd 	    (sc->sc_txbuf_cnt <= sc->sc_txq_data_minfree)) {
32897dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
3290cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
3291cd7dffd0SAdrian Chadd 		goto finish;
329223ced6c1SAdrian Chadd 	}
329323ced6c1SAdrian Chadd 
32945591b213SSam Leffler 	/*
32955591b213SSam Leffler 	 * Grab a TX buffer and associated resources.
32967dcb2beaSAdrian Chadd 	 *
32977dcb2beaSAdrian Chadd 	 * If it's an EAPOL frame, allocate a MGMT ath_buf.
32987dcb2beaSAdrian Chadd 	 * That way even with temporary buffer exhaustion due to
32997dcb2beaSAdrian Chadd 	 * the data path doesn't leave us without the ability
33007dcb2beaSAdrian Chadd 	 * to transmit management frames.
33017dcb2beaSAdrian Chadd 	 *
33027dcb2beaSAdrian Chadd 	 * Otherwise allocate a normal buffer.
33035591b213SSam Leffler 	 */
33047dcb2beaSAdrian Chadd 	if (m->m_flags & M_EAPOL)
33057dcb2beaSAdrian Chadd 		bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT);
33067dcb2beaSAdrian Chadd 	else
3307af33d486SAdrian Chadd 		bf = ath_getbuf(sc, ATH_BUFTYPE_NORMAL);
33081a85141aSAdrian Chadd 
33097dcb2beaSAdrian Chadd 	if (bf == NULL) {
33107dcb2beaSAdrian Chadd 		/*
3311cd7dffd0SAdrian Chadd 		 * If we failed to allocate a buffer, fail.
33127dcb2beaSAdrian Chadd 		 *
33137dcb2beaSAdrian Chadd 		 * We shouldn't fail normally, due to the check
33147dcb2beaSAdrian Chadd 		 * above.
33157dcb2beaSAdrian Chadd 		 */
33167dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
3317cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
3318cd7dffd0SAdrian Chadd 		goto finish;
3319b032f27cSSam Leffler 	}
33207dcb2beaSAdrian Chadd 
3321cd7dffd0SAdrian Chadd 	/*
3322cd7dffd0SAdrian Chadd 	 * At this point we have a buffer; so we need to free it
3323cd7dffd0SAdrian Chadd 	 * if we hit any error conditions.
3324cd7dffd0SAdrian Chadd 	 */
33257dcb2beaSAdrian Chadd 
332668e8e04eSSam Leffler 	/*
332768e8e04eSSam Leffler 	 * Check for fragmentation.  If this frame
332868e8e04eSSam Leffler 	 * has been broken up verify we have enough
332968e8e04eSSam Leffler 	 * buffers to send all the fragments so all
333068e8e04eSSam Leffler 	 * go out or none...
333168e8e04eSSam Leffler 	 */
33326b349e5aSAdrian Chadd 	TAILQ_INIT(&frags);
33331a85141aSAdrian Chadd 	if ((m->m_flags & M_FRAG) &&
33341a85141aSAdrian Chadd 	    !ath_txfrag_setup(sc, &frags, m, ni)) {
333568e8e04eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT,
333668e8e04eSSam Leffler 		    "%s: out of txfrag buffers\n", __func__);
333736c6be9aSSam Leffler 		sc->sc_stats.ast_tx_nofrag++;
33387a79cebfSGleb Smirnoff 		if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
33397a79cebfSGleb Smirnoff 		/*
33407a79cebfSGleb Smirnoff 		 * XXXGL: is mbuf valid after ath_txfrag_setup? If yes,
33417a79cebfSGleb Smirnoff 		 * we shouldn't free it but return back.
33427a79cebfSGleb Smirnoff 		 */
3343d07be335SAdrian Chadd 		ieee80211_free_mbuf(m);
33447a79cebfSGleb Smirnoff 		m = NULL;
334568e8e04eSSam Leffler 		goto bad;
334668e8e04eSSam Leffler 	}
3347cd7dffd0SAdrian Chadd 
3348cd7dffd0SAdrian Chadd 	/*
3349cd7dffd0SAdrian Chadd 	 * At this point if we have any TX fragments, then we will
3350cd7dffd0SAdrian Chadd 	 * have bumped the node reference once for each of those.
3351cd7dffd0SAdrian Chadd 	 */
3352cd7dffd0SAdrian Chadd 
3353cd7dffd0SAdrian Chadd 	/*
3354cd7dffd0SAdrian Chadd 	 * XXX Is there anything actually _enforcing_ that the
3355cd7dffd0SAdrian Chadd 	 * fragments are being transmitted in one hit, rather than
3356cd7dffd0SAdrian Chadd 	 * being interleaved with other transmissions on that
3357cd7dffd0SAdrian Chadd 	 * hardware queue?
3358cd7dffd0SAdrian Chadd 	 *
3359cd7dffd0SAdrian Chadd 	 * The ATH TX output lock is the only thing serialising this
3360cd7dffd0SAdrian Chadd 	 * right now.
3361cd7dffd0SAdrian Chadd 	 */
3362cd7dffd0SAdrian Chadd 
3363cd7dffd0SAdrian Chadd 	/*
3364cd7dffd0SAdrian Chadd 	 * Calculate the "next fragment" length field in ath_buf
3365cd7dffd0SAdrian Chadd 	 * in order to let the transmit path know enough about
3366cd7dffd0SAdrian Chadd 	 * what to next write to the hardware.
3367cd7dffd0SAdrian Chadd 	 */
3368cd7dffd0SAdrian Chadd 	if (m->m_flags & M_FRAG) {
3369cd7dffd0SAdrian Chadd 		struct ath_buf *fbf = bf;
3370cd7dffd0SAdrian Chadd 		struct ath_buf *n_fbf = NULL;
3371cd7dffd0SAdrian Chadd 		struct mbuf *fm = m->m_nextpkt;
3372cd7dffd0SAdrian Chadd 
3373cd7dffd0SAdrian Chadd 		/*
3374cd7dffd0SAdrian Chadd 		 * We need to walk the list of fragments and set
3375cd7dffd0SAdrian Chadd 		 * the next size to the following buffer.
3376cd7dffd0SAdrian Chadd 		 * However, the first buffer isn't in the frag
3377cd7dffd0SAdrian Chadd 		 * list, so we have to do some gymnastics here.
3378cd7dffd0SAdrian Chadd 		 */
3379cd7dffd0SAdrian Chadd 		TAILQ_FOREACH(n_fbf, &frags, bf_list) {
3380cd7dffd0SAdrian Chadd 			fbf->bf_nextfraglen = fm->m_pkthdr.len;
3381cd7dffd0SAdrian Chadd 			fbf = n_fbf;
3382cd7dffd0SAdrian Chadd 			fm = fm->m_nextpkt;
3383cd7dffd0SAdrian Chadd 		}
3384cd7dffd0SAdrian Chadd 	}
3385cd7dffd0SAdrian Chadd 
33861a85141aSAdrian Chadd nextfrag:
338768e8e04eSSam Leffler 	/*
33881a85141aSAdrian Chadd 	 * Pass the frame to the h/w for transmission.
33891a85141aSAdrian Chadd 	 * Fragmented frames have each frag chained together
33901a85141aSAdrian Chadd 	 * with m_nextpkt.  We know there are sufficient ath_buf's
33911a85141aSAdrian Chadd 	 * to send all the frags because of work done by
33921a85141aSAdrian Chadd 	 * ath_txfrag_setup.  We leave m_nextpkt set while
33931a85141aSAdrian Chadd 	 * calling ath_tx_start so it can use it to extend the
33941a85141aSAdrian Chadd 	 * the tx duration to cover the subsequent frag and
33951a85141aSAdrian Chadd 	 * so it can reclaim all the mbufs in case of an error;
33961a85141aSAdrian Chadd 	 * ath_tx_start clears m_nextpkt once it commits to
33971a85141aSAdrian Chadd 	 * handing the frame to the hardware.
3398cd7dffd0SAdrian Chadd 	 *
3399cd7dffd0SAdrian Chadd 	 * Note: if this fails, then the mbufs are freed but
3400cd7dffd0SAdrian Chadd 	 * not the node reference.
3401da4552abSAdrian Chadd 	 *
3402da4552abSAdrian Chadd 	 * So, we now have to free the node reference ourselves here
3403da4552abSAdrian Chadd 	 * and return OK up to the stack.
340468e8e04eSSam Leffler 	 */
34051a85141aSAdrian Chadd 	next = m->m_nextpkt;
34061a85141aSAdrian Chadd 	if (ath_tx_start(sc, ni, bf, m)) {
34075591b213SSam Leffler bad:
34087a79cebfSGleb Smirnoff 		if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
34091a85141aSAdrian Chadd reclaim:
341068e8e04eSSam Leffler 		bf->bf_m = NULL;
341168e8e04eSSam Leffler 		bf->bf_node = NULL;
3412c42a7b7eSSam Leffler 		ATH_TXBUF_LOCK(sc);
3413e1a50456SAdrian Chadd 		ath_returnbuf_head(sc, bf);
3414cd7dffd0SAdrian Chadd 		/*
3415cd7dffd0SAdrian Chadd 		 * Free the rest of the node references and
3416cd7dffd0SAdrian Chadd 		 * buffers for the fragment list.
3417cd7dffd0SAdrian Chadd 		 */
341868e8e04eSSam Leffler 		ath_txfrag_cleanup(sc, &frags, ni);
3419c42a7b7eSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
3420da4552abSAdrian Chadd 
3421da4552abSAdrian Chadd 		/*
3422da4552abSAdrian Chadd 		 * XXX: And free the node/return OK; ath_tx_start() may have
3423da4552abSAdrian Chadd 		 *      modified the buffer.  We currently have no way to
3424da4552abSAdrian Chadd 		 *      signify that the mbuf was freed but there was an error.
3425da4552abSAdrian Chadd 		 */
3426da4552abSAdrian Chadd 		ieee80211_free_node(ni);
3427da4552abSAdrian Chadd 		retval = 0;
3428cd7dffd0SAdrian Chadd 		goto finish;
34291a85141aSAdrian Chadd 	}
34301a85141aSAdrian Chadd 
3431548a605dSAdrian Chadd 	/*
3432548a605dSAdrian Chadd 	 * Check here if the node is in power save state.
3433548a605dSAdrian Chadd 	 */
3434548a605dSAdrian Chadd 	ath_tx_update_tim(sc, ni, 1);
3435548a605dSAdrian Chadd 
34361a85141aSAdrian Chadd 	if (next != NULL) {
343768e8e04eSSam Leffler 		/*
34381a85141aSAdrian Chadd 		 * Beware of state changing between frags.
34391a85141aSAdrian Chadd 		 * XXX check sta power-save state?
344068e8e04eSSam Leffler 		 */
34411a85141aSAdrian Chadd 		if (ni->ni_vap->iv_state != IEEE80211_S_RUN) {
3442c5239edbSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
34431a85141aSAdrian Chadd 			    "%s: flush fragmented packet, state %s\n",
34441a85141aSAdrian Chadd 			    __func__,
34451a85141aSAdrian Chadd 			    ieee80211_state_name[ni->ni_vap->iv_state]);
3446a91ab3c0SAdrian Chadd 			/* XXX dmamap */
3447d07be335SAdrian Chadd 			ieee80211_free_mbuf(next);
34481a85141aSAdrian Chadd 			goto reclaim;
3449c5239edbSAdrian Chadd 		}
34501a85141aSAdrian Chadd 		m = next;
34511a85141aSAdrian Chadd 		bf = TAILQ_FIRST(&frags);
34521a85141aSAdrian Chadd 		KASSERT(bf != NULL, ("no buf for txfrag"));
34531a85141aSAdrian Chadd 		TAILQ_REMOVE(&frags, bf, bf_list);
34541a85141aSAdrian Chadd 		goto nextfrag;
3455c5239edbSAdrian Chadd 	}
3456c5239edbSAdrian Chadd 
3457cd7dffd0SAdrian Chadd 	/*
3458cd7dffd0SAdrian Chadd 	 * Bump watchdog timer.
3459cd7dffd0SAdrian Chadd 	 */
34601a85141aSAdrian Chadd 	sc->sc_wd_timer = 5;
3461cd7dffd0SAdrian Chadd 
3462cd7dffd0SAdrian Chadd finish:
3463cd7dffd0SAdrian Chadd 	ATH_TX_UNLOCK(sc);
3464cd7dffd0SAdrian Chadd 
3465cd7dffd0SAdrian Chadd 	/*
3466cd7dffd0SAdrian Chadd 	 * Finished transmitting!
3467cd7dffd0SAdrian Chadd 	 */
3468cd7dffd0SAdrian Chadd 	ATH_PCU_LOCK(sc);
3469cd7dffd0SAdrian Chadd 	sc->sc_txstart_cnt--;
3470cd7dffd0SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3471cd7dffd0SAdrian Chadd 
3472f5c30c4eSAdrian Chadd 	/* Sleep the hardware if required */
3473f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3474f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3475f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3476f5c30c4eSAdrian Chadd 
3477cd7dffd0SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_transmit: finished");
3478cd7dffd0SAdrian Chadd 
3479cd7dffd0SAdrian Chadd 	return (retval);
34805591b213SSam Leffler }
3481cd7dffd0SAdrian Chadd 
34825591b213SSam Leffler static int
34835591b213SSam Leffler ath_media_change(struct ifnet *ifp)
34845591b213SSam Leffler {
3485b032f27cSSam Leffler 	int error = ieee80211_media_change(ifp);
3486b032f27cSSam Leffler 	/* NB: only the fixed rate can change and that doesn't need a reset */
3487b032f27cSSam Leffler 	return (error == ENETRESET ? 0 : error);
34885591b213SSam Leffler }
34895591b213SSam Leffler 
3490c42a7b7eSSam Leffler /*
3491c42a7b7eSSam Leffler  * Block/unblock tx+rx processing while a key change is done.
3492c42a7b7eSSam Leffler  * We assume the caller serializes key management operations
3493c42a7b7eSSam Leffler  * so we only need to worry about synchronization with other
3494c42a7b7eSSam Leffler  * uses that originate in the driver.
3495c42a7b7eSSam Leffler  */
3496c42a7b7eSSam Leffler static void
3497b032f27cSSam Leffler ath_key_update_begin(struct ieee80211vap *vap)
3498c42a7b7eSSam Leffler {
34993797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
3500c42a7b7eSSam Leffler 
3501c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
3502b032f27cSSam Leffler 	taskqueue_block(sc->sc_tq);
3503c42a7b7eSSam Leffler }
3504c42a7b7eSSam Leffler 
3505c42a7b7eSSam Leffler static void
3506b032f27cSSam Leffler ath_key_update_end(struct ieee80211vap *vap)
3507c42a7b7eSSam Leffler {
35083797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
3509c42a7b7eSSam Leffler 
3510c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
3511b032f27cSSam Leffler 	taskqueue_unblock(sc->sc_tq);
3512c42a7b7eSSam Leffler }
35135591b213SSam Leffler 
3514b032f27cSSam Leffler static void
3515272f6adeSGleb Smirnoff ath_update_promisc(struct ieee80211com *ic)
3516b032f27cSSam Leffler {
3517272f6adeSGleb Smirnoff 	struct ath_softc *sc = ic->ic_softc;
3518b032f27cSSam Leffler 	u_int32_t rfilt;
3519b032f27cSSam Leffler 
3520b032f27cSSam Leffler 	/* configure rx filter */
3521f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3522f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3523b032f27cSSam Leffler 	rfilt = ath_calcrxfilter(sc);
3524b032f27cSSam Leffler 	ath_hal_setrxfilter(sc->sc_ah, rfilt);
3525f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3526f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3527b032f27cSSam Leffler 
3528b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x\n", __func__, rfilt);
3529b032f27cSSam Leffler }
3530b032f27cSSam Leffler 
3531e5bd159eSAdrian Chadd /*
3532e5bd159eSAdrian Chadd  * Driver-internal mcast update call.
3533e5bd159eSAdrian Chadd  *
3534e5bd159eSAdrian Chadd  * Assumes the hardware is already awake.
3535e5bd159eSAdrian Chadd  */
3536b032f27cSSam Leffler static void
3537e5bd159eSAdrian Chadd ath_update_mcast_hw(struct ath_softc *sc)
3538b032f27cSSam Leffler {
35397a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
3540b032f27cSSam Leffler 	u_int32_t mfilt[2];
3541b032f27cSSam Leffler 
3542b032f27cSSam Leffler 	/* calculate and install multicast filter */
35437a79cebfSGleb Smirnoff 	if (ic->ic_allmulti == 0) {
35447a79cebfSGleb Smirnoff 		struct ieee80211vap *vap;
35457a79cebfSGleb Smirnoff 		struct ifnet *ifp;
3546b032f27cSSam Leffler 		struct ifmultiaddr *ifma;
35477a79cebfSGleb Smirnoff 
3548b032f27cSSam Leffler 		/*
3549b032f27cSSam Leffler 		 * Merge multicast addresses to form the hardware filter.
3550b032f27cSSam Leffler 		 */
3551b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = 0;
35527a79cebfSGleb Smirnoff 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
35537a79cebfSGleb Smirnoff 			ifp = vap->iv_ifp;
35547a79cebfSGleb Smirnoff 			if_maddr_rlock(ifp);
3555b032f27cSSam Leffler 			TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3556b032f27cSSam Leffler 				caddr_t dl;
35577a79cebfSGleb Smirnoff 				uint32_t val;
35587a79cebfSGleb Smirnoff 				uint8_t pos;
3559b032f27cSSam Leffler 
3560b032f27cSSam Leffler 				/* calculate XOR of eight 6bit values */
35617a79cebfSGleb Smirnoff 				dl = LLADDR((struct sockaddr_dl *)
35627a79cebfSGleb Smirnoff 				    ifma->ifma_addr);
356331021a2bSAndriy Voskoboinyk 				val = le32dec(dl + 0);
35647a79cebfSGleb Smirnoff 				pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^
35657a79cebfSGleb Smirnoff 				    val;
356631021a2bSAndriy Voskoboinyk 				val = le32dec(dl + 3);
35677a79cebfSGleb Smirnoff 				pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^
35687a79cebfSGleb Smirnoff 				    val;
3569b032f27cSSam Leffler 				pos &= 0x3f;
3570b032f27cSSam Leffler 				mfilt[pos / 32] |= (1 << (pos % 32));
3571b032f27cSSam Leffler 			}
3572eb956cd0SRobert Watson 			if_maddr_runlock(ifp);
35737a79cebfSGleb Smirnoff 		}
3574b032f27cSSam Leffler 	} else
3575b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = ~0;
3576e5bd159eSAdrian Chadd 
3577b032f27cSSam Leffler 	ath_hal_setmcastfilter(sc->sc_ah, mfilt[0], mfilt[1]);
3578e5bd159eSAdrian Chadd 
3579b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: MC filter %08x:%08x\n",
3580b032f27cSSam Leffler 		__func__, mfilt[0], mfilt[1]);
35814bc0e754SSam Leffler }
35824bc0e754SSam Leffler 
3583e5bd159eSAdrian Chadd /*
3584e5bd159eSAdrian Chadd  * Called from the net80211 layer - force the hardware
3585e5bd159eSAdrian Chadd  * awake before operating.
3586e5bd159eSAdrian Chadd  */
3587e5bd159eSAdrian Chadd static void
3588272f6adeSGleb Smirnoff ath_update_mcast(struct ieee80211com *ic)
3589e5bd159eSAdrian Chadd {
3590272f6adeSGleb Smirnoff 	struct ath_softc *sc = ic->ic_softc;
3591e5bd159eSAdrian Chadd 
3592e5bd159eSAdrian Chadd 	ATH_LOCK(sc);
3593e5bd159eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3594e5bd159eSAdrian Chadd 	ATH_UNLOCK(sc);
3595e5bd159eSAdrian Chadd 
3596e5bd159eSAdrian Chadd 	ath_update_mcast_hw(sc);
3597e5bd159eSAdrian Chadd 
3598e5bd159eSAdrian Chadd 	ATH_LOCK(sc);
3599e5bd159eSAdrian Chadd 	ath_power_restore_power_state(sc);
3600e5bd159eSAdrian Chadd 	ATH_UNLOCK(sc);
3601e5bd159eSAdrian Chadd }
3602e5bd159eSAdrian Chadd 
3603e60c4fc2SAdrian Chadd void
36045591b213SSam Leffler ath_mode_init(struct ath_softc *sc)
36055591b213SSam Leffler {
36067a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
3607b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3608b032f27cSSam Leffler 	u_int32_t rfilt;
36095591b213SSam Leffler 
36104bc0e754SSam Leffler 	/* configure rx filter */
361168e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
36124bc0e754SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
36134bc0e754SSam Leffler 
36145591b213SSam Leffler 	/* configure operational mode */
3615c42a7b7eSSam Leffler 	ath_hal_setopmode(ah);
3616c42a7b7eSSam Leffler 
361729aca940SSam Leffler 	/* handle any link-level address change */
36187a79cebfSGleb Smirnoff 	ath_hal_setmac(ah, ic->ic_macaddr);
36195591b213SSam Leffler 
36205591b213SSam Leffler 	/* calculate and install multicast filter */
3621e5bd159eSAdrian Chadd 	ath_update_mcast_hw(sc);
36225591b213SSam Leffler }
36235591b213SSam Leffler 
3624c42a7b7eSSam Leffler /*
3625c42a7b7eSSam Leffler  * Set the slot time based on the current setting.
3626c42a7b7eSSam Leffler  */
3627ba5c15d9SAdrian Chadd void
3628c42a7b7eSSam Leffler ath_setslottime(struct ath_softc *sc)
3629c42a7b7eSSam Leffler {
36307a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
3631c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3632aaa70f2fSSam Leffler 	u_int usec;
3633c42a7b7eSSam Leffler 
3634aaa70f2fSSam Leffler 	if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan))
3635aaa70f2fSSam Leffler 		usec = 13;
3636aaa70f2fSSam Leffler 	else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan))
3637aaa70f2fSSam Leffler 		usec = 21;
3638724c193aSSam Leffler 	else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) {
3639724c193aSSam Leffler 		/* honor short/long slot time only in 11g */
3640724c193aSSam Leffler 		/* XXX shouldn't honor on pure g or turbo g channel */
3641724c193aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
3642aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_9;
3643aaa70f2fSSam Leffler 		else
3644aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_20;
3645724c193aSSam Leffler 	} else
3646724c193aSSam Leffler 		usec = HAL_SLOT_TIME_9;
3647aaa70f2fSSam Leffler 
3648aaa70f2fSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
3649aaa70f2fSSam Leffler 	    "%s: chan %u MHz flags 0x%x %s slot, %u usec\n",
3650aaa70f2fSSam Leffler 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
3651aaa70f2fSSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec);
3652aaa70f2fSSam Leffler 
3653f5c30c4eSAdrian Chadd 	/* Wake up the hardware first before updating the slot time */
3654f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3655f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3656aaa70f2fSSam Leffler 	ath_hal_setslottime(ah, usec);
3657f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3658c42a7b7eSSam Leffler 	sc->sc_updateslot = OK;
3659f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3660c42a7b7eSSam Leffler }
3661c42a7b7eSSam Leffler 
3662c42a7b7eSSam Leffler /*
3663c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update the
3664c42a7b7eSSam Leffler  * slot time based on the current setting.
3665c42a7b7eSSam Leffler  */
3666c42a7b7eSSam Leffler static void
3667272f6adeSGleb Smirnoff ath_updateslot(struct ieee80211com *ic)
3668c42a7b7eSSam Leffler {
3669272f6adeSGleb Smirnoff 	struct ath_softc *sc = ic->ic_softc;
3670c42a7b7eSSam Leffler 
3671c42a7b7eSSam Leffler 	/*
3672c42a7b7eSSam Leffler 	 * When not coordinating the BSS, change the hardware
3673c42a7b7eSSam Leffler 	 * immediately.  For other operation we defer the change
3674c42a7b7eSSam Leffler 	 * until beacon updates have propagated to the stations.
3675f5c30c4eSAdrian Chadd 	 *
3676f5c30c4eSAdrian Chadd 	 * XXX sc_updateslot isn't changed behind a lock?
3677c42a7b7eSSam Leffler 	 */
367859aa14a9SRui Paulo 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
367959aa14a9SRui Paulo 	    ic->ic_opmode == IEEE80211_M_MBSS)
3680c42a7b7eSSam Leffler 		sc->sc_updateslot = UPDATE;
3681c42a7b7eSSam Leffler 	else
3682c42a7b7eSSam Leffler 		ath_setslottime(sc);
3683c42a7b7eSSam Leffler }
3684c42a7b7eSSam Leffler 
3685c42a7b7eSSam Leffler /*
3686622b3fd2SSam Leffler  * Append the contents of src to dst; both queues
3687622b3fd2SSam Leffler  * are assumed to be locked.
3688622b3fd2SSam Leffler  */
3689ba5c15d9SAdrian Chadd void
3690622b3fd2SSam Leffler ath_txqmove(struct ath_txq *dst, struct ath_txq *src)
3691622b3fd2SSam Leffler {
3692e86fd7a7SAdrian Chadd 
3693b837332dSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(src);
3694b837332dSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(dst);
3695b837332dSAdrian Chadd 
36966b349e5aSAdrian Chadd 	TAILQ_CONCAT(&dst->axq_q, &src->axq_q, bf_list);
3697622b3fd2SSam Leffler 	dst->axq_link = src->axq_link;
3698622b3fd2SSam Leffler 	src->axq_link = NULL;
3699622b3fd2SSam Leffler 	dst->axq_depth += src->axq_depth;
37006edf1dc7SAdrian Chadd 	dst->axq_aggr_depth += src->axq_aggr_depth;
3701622b3fd2SSam Leffler 	src->axq_depth = 0;
37026edf1dc7SAdrian Chadd 	src->axq_aggr_depth = 0;
3703622b3fd2SSam Leffler }
3704622b3fd2SSam Leffler 
3705622b3fd2SSam Leffler /*
3706d52f7132SAdrian Chadd  * Reset the hardware, with no loss.
3707d52f7132SAdrian Chadd  *
3708d52f7132SAdrian Chadd  * This can't be used for a general case reset.
3709d52f7132SAdrian Chadd  */
3710d52f7132SAdrian Chadd static void
3711d52f7132SAdrian Chadd ath_reset_proc(void *arg, int pending)
3712d52f7132SAdrian Chadd {
3713d52f7132SAdrian Chadd 	struct ath_softc *sc = arg;
3714d52f7132SAdrian Chadd 
3715d52f7132SAdrian Chadd #if 0
371676e6fd5dSGleb Smirnoff 	device_printf(sc->sc_dev, "%s: resetting\n", __func__);
3717d52f7132SAdrian Chadd #endif
37187a79cebfSGleb Smirnoff 	ath_reset(sc, ATH_RESET_NOLOSS);
3719d52f7132SAdrian Chadd }
3720d52f7132SAdrian Chadd 
3721d52f7132SAdrian Chadd /*
3722c42a7b7eSSam Leffler  * Reset the hardware after detecting beacons have stopped.
3723c42a7b7eSSam Leffler  */
3724c42a7b7eSSam Leffler static void
3725c42a7b7eSSam Leffler ath_bstuck_proc(void *arg, int pending)
3726c42a7b7eSSam Leffler {
3727c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
372816d4de92SAdrian Chadd 	uint32_t hangs = 0;
372916d4de92SAdrian Chadd 
373016d4de92SAdrian Chadd 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0)
373176e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "bb hang detected (0x%x)\n", hangs);
3732c42a7b7eSSam Leffler 
3733370f81faSAdrian Chadd #ifdef	ATH_DEBUG_ALQ
3734370f81faSAdrian Chadd 	if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_STUCK_BEACON))
3735370f81faSAdrian Chadd 		if_ath_alq_post(&sc->sc_alq, ATH_ALQ_STUCK_BEACON, 0, NULL);
3736370f81faSAdrian Chadd #endif
3737370f81faSAdrian Chadd 
373876e6fd5dSGleb Smirnoff 	device_printf(sc->sc_dev, "stuck beacon; resetting (bmiss count %u)\n",
3739c42a7b7eSSam Leffler 	    sc->sc_bmisscount);
3740c2e34459SSam Leffler 	sc->sc_stats.ast_bstuck++;
374116d4de92SAdrian Chadd 	/*
374216d4de92SAdrian Chadd 	 * This assumes that there's no simultaneous channel mode change
3743f6b6084bSPedro F. Giffuni 	 * occurring.
374416d4de92SAdrian Chadd 	 */
37457a79cebfSGleb Smirnoff 	ath_reset(sc, ATH_RESET_NOLOSS);
3746c42a7b7eSSam Leffler }
3747c42a7b7eSSam Leffler 
3748c42a7b7eSSam Leffler static int
37495591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc)
37505591b213SSam Leffler {
3751c42a7b7eSSam Leffler 	int error;
37525591b213SSam Leffler 
3753c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
375409067b6eSAdrian Chadd 		    "tx", sc->sc_tx_desclen, ath_txbuf, ATH_MAX_SCATTER);
3755c42a7b7eSSam Leffler 	if (error != 0) {
37565591b213SSam Leffler 		return error;
3757c42a7b7eSSam Leffler 	}
375823ced6c1SAdrian Chadd 	sc->sc_txbuf_cnt = ath_txbuf;
3759c42a7b7eSSam Leffler 
3760af33d486SAdrian Chadd 	error = ath_descdma_setup(sc, &sc->sc_txdma_mgmt, &sc->sc_txbuf_mgmt,
37611006fc0cSAdrian Chadd 		    "tx_mgmt", sc->sc_tx_desclen, ath_txbuf_mgmt,
37621006fc0cSAdrian Chadd 		    ATH_TXDESC);
3763af33d486SAdrian Chadd 	if (error != 0) {
3764af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3765af33d486SAdrian Chadd 		return error;
3766af33d486SAdrian Chadd 	}
3767af33d486SAdrian Chadd 
3768af33d486SAdrian Chadd 	/*
3769af33d486SAdrian Chadd 	 * XXX mark txbuf_mgmt frames with ATH_BUF_MGMT, so the
3770af33d486SAdrian Chadd 	 * flag doesn't have to be set in ath_getbuf_locked().
3771af33d486SAdrian Chadd 	 */
3772af33d486SAdrian Chadd 
3773c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
37741006fc0cSAdrian Chadd 			"beacon", sc->sc_tx_desclen, ATH_BCBUF, 1);
3775c42a7b7eSSam Leffler 	if (error != 0) {
3776af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3777af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
3778af33d486SAdrian Chadd 		    &sc->sc_txbuf_mgmt);
3779c42a7b7eSSam Leffler 		return error;
3780c42a7b7eSSam Leffler 	}
37815591b213SSam Leffler 	return 0;
37825591b213SSam Leffler }
37835591b213SSam Leffler 
37845591b213SSam Leffler static void
37855591b213SSam Leffler ath_desc_free(struct ath_softc *sc)
37865591b213SSam Leffler {
37875591b213SSam Leffler 
3788c42a7b7eSSam Leffler 	if (sc->sc_bdma.dd_desc_len != 0)
3789c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
3790c42a7b7eSSam Leffler 	if (sc->sc_txdma.dd_desc_len != 0)
3791c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3792af33d486SAdrian Chadd 	if (sc->sc_txdma_mgmt.dd_desc_len != 0)
3793af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
3794af33d486SAdrian Chadd 		    &sc->sc_txbuf_mgmt);
37955591b213SSam Leffler }
37965591b213SSam Leffler 
37975591b213SSam Leffler static struct ieee80211_node *
379838c208f8SSam Leffler ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
37995591b213SSam Leffler {
380038c208f8SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
38013797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
3802c42a7b7eSSam Leffler 	const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
3803c42a7b7eSSam Leffler 	struct ath_node *an;
3804c42a7b7eSSam Leffler 
3805c42a7b7eSSam Leffler 	an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
3806c42a7b7eSSam Leffler 	if (an == NULL) {
3807c42a7b7eSSam Leffler 		/* XXX stat+msg */
3808de5af704SSam Leffler 		return NULL;
38095591b213SSam Leffler 	}
3810c42a7b7eSSam Leffler 	ath_rate_node_init(sc, an);
38115591b213SSam Leffler 
38123dd85b26SAdrian Chadd 	/* Setup the mutex - there's no associd yet so set the name to NULL */
38133dd85b26SAdrian Chadd 	snprintf(an->an_name, sizeof(an->an_name), "%s: node %p",
38143dd85b26SAdrian Chadd 	    device_get_nameunit(sc->sc_dev), an);
38153dd85b26SAdrian Chadd 	mtx_init(&an->an_mtx, an->an_name, NULL, MTX_DEF);
38163dd85b26SAdrian Chadd 
3817eb6f0de0SAdrian Chadd 	/* XXX setup ath_tid */
3818eb6f0de0SAdrian Chadd 	ath_tx_tid_init(sc, an);
3819eb6f0de0SAdrian Chadd 
38209b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__, mac, ":", an);
3821c42a7b7eSSam Leffler 	return &an->an_node;
3822c42a7b7eSSam Leffler }
3823c42a7b7eSSam Leffler 
38245591b213SSam Leffler static void
38254afa805eSAdrian Chadd ath_node_cleanup(struct ieee80211_node *ni)
38264afa805eSAdrian Chadd {
38274afa805eSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
38283797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
38294afa805eSAdrian Chadd 
38309b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__,
38319b48fb4bSAdrian Chadd 	    ni->ni_macaddr, ":", ATH_NODE(ni));
38329b48fb4bSAdrian Chadd 
38334afa805eSAdrian Chadd 	/* Cleanup ath_tid, free unused bufs, unlink bufs in TXQ */
3834eb6f0de0SAdrian Chadd 	ath_tx_node_flush(sc, ATH_NODE(ni));
38354afa805eSAdrian Chadd 	ath_rate_node_cleanup(sc, ATH_NODE(ni));
38364afa805eSAdrian Chadd 	sc->sc_node_cleanup(ni);
38374afa805eSAdrian Chadd }
38384afa805eSAdrian Chadd 
38394afa805eSAdrian Chadd static void
3840c42a7b7eSSam Leffler ath_node_free(struct ieee80211_node *ni)
38415591b213SSam Leffler {
3842c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
38433797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
38441e774079SSam Leffler 
38459b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__,
38469b48fb4bSAdrian Chadd 	    ni->ni_macaddr, ":", ATH_NODE(ni));
38473dd85b26SAdrian Chadd 	mtx_destroy(&ATH_NODE(ni)->an_mtx);
3848c42a7b7eSSam Leffler 	sc->sc_node_free(ni);
38495591b213SSam Leffler }
38505591b213SSam Leffler 
385168e8e04eSSam Leffler static void
385268e8e04eSSam Leffler ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
385368e8e04eSSam Leffler {
385468e8e04eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
38553797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
385668e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
385768e8e04eSSam Leffler 
3858b032f27cSSam Leffler 	*rssi = ic->ic_node_getrssi(ni);
385959efa8b5SSam Leffler 	if (ni->ni_chan != IEEE80211_CHAN_ANYC)
386059efa8b5SSam Leffler 		*noise = ath_hal_getchannoise(ah, ni->ni_chan);
386159efa8b5SSam Leffler 	else
386268e8e04eSSam Leffler 		*noise = -95;		/* nominally correct */
386368e8e04eSSam Leffler }
386468e8e04eSSam Leffler 
3865c42a7b7eSSam Leffler /*
3866c42a7b7eSSam Leffler  * Set the default antenna.
3867c42a7b7eSSam Leffler  */
3868e60c4fc2SAdrian Chadd void
3869c42a7b7eSSam Leffler ath_setdefantenna(struct ath_softc *sc, u_int antenna)
3870c42a7b7eSSam Leffler {
3871c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3872c42a7b7eSSam Leffler 
3873c42a7b7eSSam Leffler 	/* XXX block beacon interrupts */
3874c42a7b7eSSam Leffler 	ath_hal_setdefantenna(ah, antenna);
3875c42a7b7eSSam Leffler 	if (sc->sc_defant != antenna)
3876c42a7b7eSSam Leffler 		sc->sc_stats.ast_ant_defswitch++;
3877c42a7b7eSSam Leffler 	sc->sc_defant = antenna;
3878c42a7b7eSSam Leffler 	sc->sc_rxotherant = 0;
3879c42a7b7eSSam Leffler }
3880c42a7b7eSSam Leffler 
38815463c4a4SSam Leffler static void
3882622b3fd2SSam Leffler ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum)
3883622b3fd2SSam Leffler {
3884622b3fd2SSam Leffler 	txq->axq_qnum = qnum;
3885339ccfb3SSam Leffler 	txq->axq_ac = 0;
3886622b3fd2SSam Leffler 	txq->axq_depth = 0;
388716d4de92SAdrian Chadd 	txq->axq_aggr_depth = 0;
3888622b3fd2SSam Leffler 	txq->axq_intrcnt = 0;
3889622b3fd2SSam Leffler 	txq->axq_link = NULL;
38906b349e5aSAdrian Chadd 	txq->axq_softc = sc;
38916b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_q);
38926b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_tidq);
38933feffbd7SAdrian Chadd 	TAILQ_INIT(&txq->fifo.axq_q);
3894b837332dSAdrian Chadd 	ATH_TXQ_LOCK_INIT(sc, txq);
3895622b3fd2SSam Leffler }
3896622b3fd2SSam Leffler 
38975591b213SSam Leffler /*
3898c42a7b7eSSam Leffler  * Setup a h/w transmit queue.
38995591b213SSam Leffler  */
3900c42a7b7eSSam Leffler static struct ath_txq *
3901c42a7b7eSSam Leffler ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
3902c42a7b7eSSam Leffler {
3903c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3904c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
3905c42a7b7eSSam Leffler 	int qnum;
3906c42a7b7eSSam Leffler 
3907c42a7b7eSSam Leffler 	memset(&qi, 0, sizeof(qi));
3908c42a7b7eSSam Leffler 	qi.tqi_subtype = subtype;
3909c42a7b7eSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
3910c42a7b7eSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
3911c42a7b7eSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
3912c42a7b7eSSam Leffler 	/*
3913c42a7b7eSSam Leffler 	 * Enable interrupts only for EOL and DESC conditions.
3914c42a7b7eSSam Leffler 	 * We mark tx descriptors to receive a DESC interrupt
3915c42a7b7eSSam Leffler 	 * when a tx queue gets deep; otherwise waiting for the
3916c42a7b7eSSam Leffler 	 * EOL to reap descriptors.  Note that this is done to
3917c42a7b7eSSam Leffler 	 * reduce interrupt load and this only defers reaping
3918c42a7b7eSSam Leffler 	 * descriptors, never transmitting frames.  Aside from
3919c42a7b7eSSam Leffler 	 * reducing interrupts this also permits more concurrency.
3920c42a7b7eSSam Leffler 	 * The only potential downside is if the tx queue backs
3921c42a7b7eSSam Leffler 	 * up in which case the top half of the kernel may backup
3922c42a7b7eSSam Leffler 	 * due to a lack of tx descriptors.
3923c42a7b7eSSam Leffler 	 */
39246961e9edSAdrian Chadd 	if (sc->sc_isedma)
39256961e9edSAdrian Chadd 		qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE |
39266961e9edSAdrian Chadd 		    HAL_TXQ_TXOKINT_ENABLE;
39276961e9edSAdrian Chadd 	else
39286961e9edSAdrian Chadd 		qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE |
39296961e9edSAdrian Chadd 		    HAL_TXQ_TXDESCINT_ENABLE;
39306961e9edSAdrian Chadd 
3931c42a7b7eSSam Leffler 	qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
3932c42a7b7eSSam Leffler 	if (qnum == -1) {
3933c42a7b7eSSam Leffler 		/*
3934c42a7b7eSSam Leffler 		 * NB: don't print a message, this happens
3935a614e076SSam Leffler 		 * normally on parts with too few tx queues
3936c42a7b7eSSam Leffler 		 */
3937c42a7b7eSSam Leffler 		return NULL;
3938c42a7b7eSSam Leffler 	}
3939d6166defSAdrian Chadd 	if (qnum >= nitems(sc->sc_txq)) {
39406891c875SPeter Wemm 		device_printf(sc->sc_dev,
39416891c875SPeter Wemm 			"hal qnum %u out of range, max %zu!\n",
3942d6166defSAdrian Chadd 			qnum, nitems(sc->sc_txq));
3943c42a7b7eSSam Leffler 		ath_hal_releasetxqueue(ah, qnum);
3944c42a7b7eSSam Leffler 		return NULL;
3945c42a7b7eSSam Leffler 	}
3946c42a7b7eSSam Leffler 	if (!ATH_TXQ_SETUP(sc, qnum)) {
3947622b3fd2SSam Leffler 		ath_txq_init(sc, &sc->sc_txq[qnum], qnum);
3948c42a7b7eSSam Leffler 		sc->sc_txqsetup |= 1<<qnum;
3949c42a7b7eSSam Leffler 	}
3950c42a7b7eSSam Leffler 	return &sc->sc_txq[qnum];
3951c42a7b7eSSam Leffler }
3952c42a7b7eSSam Leffler 
3953c42a7b7eSSam Leffler /*
3954c42a7b7eSSam Leffler  * Setup a hardware data transmit queue for the specified
3955c42a7b7eSSam Leffler  * access control.  The hal may not support all requested
3956c42a7b7eSSam Leffler  * queues in which case it will return a reference to a
3957c42a7b7eSSam Leffler  * previously setup queue.  We record the mapping from ac's
3958c42a7b7eSSam Leffler  * to h/w queues for use by ath_tx_start and also track
3959c42a7b7eSSam Leffler  * the set of h/w queues being used to optimize work in the
3960c42a7b7eSSam Leffler  * transmit interrupt handler and related routines.
3961c42a7b7eSSam Leffler  */
3962c42a7b7eSSam Leffler static int
3963c42a7b7eSSam Leffler ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
3964c42a7b7eSSam Leffler {
3965c42a7b7eSSam Leffler 	struct ath_txq *txq;
3966c42a7b7eSSam Leffler 
3967d6166defSAdrian Chadd 	if (ac >= nitems(sc->sc_ac2q)) {
39686891c875SPeter Wemm 		device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
3969d6166defSAdrian Chadd 			ac, nitems(sc->sc_ac2q));
3970c42a7b7eSSam Leffler 		return 0;
3971c42a7b7eSSam Leffler 	}
3972c42a7b7eSSam Leffler 	txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
3973c42a7b7eSSam Leffler 	if (txq != NULL) {
3974339ccfb3SSam Leffler 		txq->axq_ac = ac;
3975c42a7b7eSSam Leffler 		sc->sc_ac2q[ac] = txq;
3976c42a7b7eSSam Leffler 		return 1;
3977c42a7b7eSSam Leffler 	} else
3978c42a7b7eSSam Leffler 		return 0;
3979c42a7b7eSSam Leffler }
3980c42a7b7eSSam Leffler 
3981c42a7b7eSSam Leffler /*
3982c42a7b7eSSam Leffler  * Update WME parameters for a transmit queue.
3983c42a7b7eSSam Leffler  */
3984c42a7b7eSSam Leffler static int
3985c42a7b7eSSam Leffler ath_txq_update(struct ath_softc *sc, int ac)
3986c42a7b7eSSam Leffler {
3987c42a7b7eSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<v)-1)
39887a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
3989c42a7b7eSSam Leffler 	struct ath_txq *txq = sc->sc_ac2q[ac];
3990c42a7b7eSSam Leffler 	struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
3991c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3992c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
3993c42a7b7eSSam Leffler 
3994c42a7b7eSSam Leffler 	ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
3995584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
399610ad9a77SSam Leffler 	if (sc->sc_tdma) {
399710ad9a77SSam Leffler 		/*
399810ad9a77SSam Leffler 		 * AIFS is zero so there's no pre-transmit wait.  The
399910ad9a77SSam Leffler 		 * burst time defines the slot duration and is configured
400009be6601SSam Leffler 		 * through net80211.  The QCU is setup to not do post-xmit
400110ad9a77SSam Leffler 		 * back off, lockout all lower-priority QCU's, and fire
400210ad9a77SSam Leffler 		 * off the DMA beacon alert timer which is setup based
400310ad9a77SSam Leffler 		 * on the slot configuration.
400410ad9a77SSam Leffler 		 */
400510ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
400610ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
400710ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
400810ad9a77SSam Leffler 			      | HAL_TXQ_TXEOLINT_ENABLE
400910ad9a77SSam Leffler 			      | HAL_TXQ_DBA_GATED
401010ad9a77SSam Leffler 			      | HAL_TXQ_BACKOFF_DISABLE
401110ad9a77SSam Leffler 			      | HAL_TXQ_ARB_LOCKOUT_GLOBAL
401210ad9a77SSam Leffler 			      ;
401310ad9a77SSam Leffler 		qi.tqi_aifs = 0;
401410ad9a77SSam Leffler 		/* XXX +dbaprep? */
401510ad9a77SSam Leffler 		qi.tqi_readyTime = sc->sc_tdmaslotlen;
401610ad9a77SSam Leffler 		qi.tqi_burstTime = qi.tqi_readyTime;
401710ad9a77SSam Leffler 	} else {
401810ad9a77SSam Leffler #endif
401916d4de92SAdrian Chadd 		/*
402016d4de92SAdrian Chadd 		 * XXX shouldn't this just use the default flags
402116d4de92SAdrian Chadd 		 * used in the previous queue setup?
402216d4de92SAdrian Chadd 		 */
402310ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
402410ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
402510ad9a77SSam Leffler 			      | HAL_TXQ_TXDESCINT_ENABLE
402610ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
40271f25c0f7SAdrian Chadd 			      | HAL_TXQ_TXEOLINT_ENABLE
402810ad9a77SSam Leffler 			      ;
4029c42a7b7eSSam Leffler 		qi.tqi_aifs = wmep->wmep_aifsn;
4030c42a7b7eSSam Leffler 		qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
4031c42a7b7eSSam Leffler 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
403210ad9a77SSam Leffler 		qi.tqi_readyTime = 0;
4033d6166defSAdrian Chadd 		qi.tqi_burstTime = IEEE80211_TXOP_TO_US(wmep->wmep_txopLimit);
4034584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
403510ad9a77SSam Leffler 	}
403610ad9a77SSam Leffler #endif
403710ad9a77SSam Leffler 
403810ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
403910ad9a77SSam Leffler 	    "%s: Q%u qflags 0x%x aifs %u cwmin %u cwmax %u burstTime %u\n",
404010ad9a77SSam Leffler 	    __func__, txq->axq_qnum, qi.tqi_qflags,
404110ad9a77SSam Leffler 	    qi.tqi_aifs, qi.tqi_cwmin, qi.tqi_cwmax, qi.tqi_burstTime);
4042c42a7b7eSSam Leffler 
4043c42a7b7eSSam Leffler 	if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
404476e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "unable to update hardware queue "
404576e6fd5dSGleb Smirnoff 		    "parameters for %s traffic!\n", ieee80211_wme_acnames[ac]);
4046c42a7b7eSSam Leffler 		return 0;
4047c42a7b7eSSam Leffler 	} else {
4048c42a7b7eSSam Leffler 		ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
4049c42a7b7eSSam Leffler 		return 1;
4050c42a7b7eSSam Leffler 	}
4051c42a7b7eSSam Leffler #undef ATH_EXPONENT_TO_VALUE
4052c42a7b7eSSam Leffler }
4053c42a7b7eSSam Leffler 
4054c42a7b7eSSam Leffler /*
4055c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update WME parameters.
4056c42a7b7eSSam Leffler  */
4057a35dae8dSAdrian Chadd int
4058c42a7b7eSSam Leffler ath_wme_update(struct ieee80211com *ic)
4059c42a7b7eSSam Leffler {
40603797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
4061c42a7b7eSSam Leffler 
4062c42a7b7eSSam Leffler 	return !ath_txq_update(sc, WME_AC_BE) ||
4063c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_BK) ||
4064c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VI) ||
4065c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
4066c42a7b7eSSam Leffler }
4067c42a7b7eSSam Leffler 
4068c42a7b7eSSam Leffler /*
4069c42a7b7eSSam Leffler  * Reclaim resources for a setup queue.
4070c42a7b7eSSam Leffler  */
4071c42a7b7eSSam Leffler static void
4072c42a7b7eSSam Leffler ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
4073c42a7b7eSSam Leffler {
4074c42a7b7eSSam Leffler 
4075c42a7b7eSSam Leffler 	ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
4076c42a7b7eSSam Leffler 	sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
4077b837332dSAdrian Chadd 	ATH_TXQ_LOCK_DESTROY(txq);
4078c42a7b7eSSam Leffler }
4079c42a7b7eSSam Leffler 
4080c42a7b7eSSam Leffler /*
4081c42a7b7eSSam Leffler  * Reclaim all tx queue resources.
4082c42a7b7eSSam Leffler  */
4083c42a7b7eSSam Leffler static void
4084c42a7b7eSSam Leffler ath_tx_cleanup(struct ath_softc *sc)
4085c42a7b7eSSam Leffler {
4086c42a7b7eSSam Leffler 	int i;
4087c42a7b7eSSam Leffler 
4088c42a7b7eSSam Leffler 	ATH_TXBUF_LOCK_DESTROY(sc);
4089c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4090c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
4091c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, &sc->sc_txq[i]);
4092c42a7b7eSSam Leffler }
40935591b213SSam Leffler 
409499d258fdSSam Leffler /*
4095ab06fdf2SSam Leffler  * Return h/w rate index for an IEEE rate (w/o basic rate bit)
4096ab06fdf2SSam Leffler  * using the current rates in sc_rixmap.
40978b5341deSSam Leffler  */
4098b8e788a5SAdrian Chadd int
4099ab06fdf2SSam Leffler ath_tx_findrix(const struct ath_softc *sc, uint8_t rate)
41008b5341deSSam Leffler {
4101ab06fdf2SSam Leffler 	int rix = sc->sc_rixmap[rate];
4102ab06fdf2SSam Leffler 	/* NB: return lowest rix for invalid rate */
4103ab06fdf2SSam Leffler 	return (rix == 0xff ? 0 : rix);
41048b5341deSSam Leffler }
41058b5341deSSam Leffler 
41069352fb7aSAdrian Chadd static void
41079352fb7aSAdrian Chadd ath_tx_update_stats(struct ath_softc *sc, struct ath_tx_status *ts,
41089352fb7aSAdrian Chadd     struct ath_buf *bf)
41099352fb7aSAdrian Chadd {
41109352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
41117a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
41129352fb7aSAdrian Chadd 	int sr, lr, pri;
41139352fb7aSAdrian Chadd 
41149352fb7aSAdrian Chadd 	if (ts->ts_status == 0) {
41159352fb7aSAdrian Chadd 		u_int8_t txant = ts->ts_antenna;
41169352fb7aSAdrian Chadd 		sc->sc_stats.ast_ant_tx[txant]++;
41179352fb7aSAdrian Chadd 		sc->sc_ant_tx[txant]++;
41189352fb7aSAdrian Chadd 		if (ts->ts_finaltsi != 0)
41199352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_altrate++;
41209352fb7aSAdrian Chadd 		pri = M_WME_GETAC(bf->bf_m);
41219352fb7aSAdrian Chadd 		if (pri >= WME_AC_VO)
41229352fb7aSAdrian Chadd 			ic->ic_wme.wme_hipri_traffic++;
4123875a9451SAdrian Chadd 		if ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)
41249352fb7aSAdrian Chadd 			ni->ni_inact = ni->ni_inact_reload;
41259352fb7aSAdrian Chadd 	} else {
41269352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XRETRY)
41279352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xretries++;
41289352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FIFO)
41299352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_fifoerr++;
41309352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FILT)
41319352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_filtered++;
41329352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XTXOP)
41339352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xtxop++;
41349352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_TIMER_EXPIRED)
41359352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_timerexpired++;
41369352fb7aSAdrian Chadd 
41379352fb7aSAdrian Chadd 		if (bf->bf_m->m_flags & M_FF)
41389352fb7aSAdrian Chadd 			sc->sc_stats.ast_ff_txerr++;
41399352fb7aSAdrian Chadd 	}
41409352fb7aSAdrian Chadd 	/* XXX when is this valid? */
4141158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DESC_CFG_ERR)
41429352fb7aSAdrian Chadd 		sc->sc_stats.ast_tx_desccfgerr++;
4143158cb431SAdrian Chadd 	/*
4144158cb431SAdrian Chadd 	 * This can be valid for successful frame transmission!
4145158cb431SAdrian Chadd 	 * If there's a TX FIFO underrun during aggregate transmission,
4146158cb431SAdrian Chadd 	 * the MAC will pad the rest of the aggregate with delimiters.
4147158cb431SAdrian Chadd 	 * If a BA is returned, the frame is marked as "OK" and it's up
4148158cb431SAdrian Chadd 	 * to the TX completion code to notice which frames weren't
4149158cb431SAdrian Chadd 	 * successfully transmitted.
4150158cb431SAdrian Chadd 	 */
4151158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DATA_UNDERRUN)
4152158cb431SAdrian Chadd 		sc->sc_stats.ast_tx_data_underrun++;
4153158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DELIM_UNDERRUN)
4154158cb431SAdrian Chadd 		sc->sc_stats.ast_tx_delim_underrun++;
41559352fb7aSAdrian Chadd 
41569352fb7aSAdrian Chadd 	sr = ts->ts_shortretry;
41579352fb7aSAdrian Chadd 	lr = ts->ts_longretry;
41589352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_shortretry += sr;
41599352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_longretry += lr;
41609352fb7aSAdrian Chadd 
41619352fb7aSAdrian Chadd }
41629352fb7aSAdrian Chadd 
41639352fb7aSAdrian Chadd /*
41649352fb7aSAdrian Chadd  * The default completion. If fail is 1, this means
41659352fb7aSAdrian Chadd  * "please don't retry the frame, and just return -1 status
41669352fb7aSAdrian Chadd  * to the net80211 stack.
41679352fb7aSAdrian Chadd  */
41689352fb7aSAdrian Chadd void
41699352fb7aSAdrian Chadd ath_tx_default_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
41709352fb7aSAdrian Chadd {
41719352fb7aSAdrian Chadd 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
41729352fb7aSAdrian Chadd 	int st;
41739352fb7aSAdrian Chadd 
41749352fb7aSAdrian Chadd 	if (fail == 1)
41759352fb7aSAdrian Chadd 		st = -1;
41769352fb7aSAdrian Chadd 	else
4177875a9451SAdrian Chadd 		st = ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) ?
41789352fb7aSAdrian Chadd 		    ts->ts_status : HAL_TXERR_XRETRY;
41799352fb7aSAdrian Chadd 
4180ce597531SAdrian Chadd #if 0
41819352fb7aSAdrian Chadd 	if (bf->bf_state.bfs_dobaw)
41829352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
4183a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: dobaw should've been cleared!\n",
4184a66d5089SAdrian Chadd 		    __func__,
4185a66d5089SAdrian Chadd 		    bf,
4186a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
4187ce597531SAdrian Chadd #endif
41889352fb7aSAdrian Chadd 	if (bf->bf_next != NULL)
41899352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
4190a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: bf_next not NULL!\n",
4191a66d5089SAdrian Chadd 		    __func__,
4192a66d5089SAdrian Chadd 		    bf,
4193a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
41949352fb7aSAdrian Chadd 
41959352fb7aSAdrian Chadd 	/*
4196548a605dSAdrian Chadd 	 * Check if the node software queue is empty; if so
4197548a605dSAdrian Chadd 	 * then clear the TIM.
4198548a605dSAdrian Chadd 	 *
4199548a605dSAdrian Chadd 	 * This needs to be done before the buffer is freed as
4200548a605dSAdrian Chadd 	 * otherwise the node reference will have been released
4201548a605dSAdrian Chadd 	 * and the node may not actually exist any longer.
4202548a605dSAdrian Chadd 	 *
4203548a605dSAdrian Chadd 	 * XXX I don't like this belonging here, but it's cleaner
4204548a605dSAdrian Chadd 	 * to do it here right now then all the other places
4205548a605dSAdrian Chadd 	 * where ath_tx_default_comp() is called.
4206548a605dSAdrian Chadd 	 *
4207548a605dSAdrian Chadd 	 * XXX TODO: during drain, ensure that the callback is
4208548a605dSAdrian Chadd 	 * being called so we get a chance to update the TIM.
4209548a605dSAdrian Chadd 	 */
42104bed2b67SAdrian Chadd 	if (bf->bf_node) {
42114bed2b67SAdrian Chadd 		ATH_TX_LOCK(sc);
4212548a605dSAdrian Chadd 		ath_tx_update_tim(sc, bf->bf_node, 0);
42134bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
42144bed2b67SAdrian Chadd 	}
4215548a605dSAdrian Chadd 
4216548a605dSAdrian Chadd 	/*
42179352fb7aSAdrian Chadd 	 * Do any tx complete callback.  Note this must
42189352fb7aSAdrian Chadd 	 * be done before releasing the node reference.
42199352fb7aSAdrian Chadd 	 * This will free the mbuf, release the net80211
42209352fb7aSAdrian Chadd 	 * node and recycle the ath_buf.
42219352fb7aSAdrian Chadd 	 */
42229352fb7aSAdrian Chadd 	ath_tx_freebuf(sc, bf, st);
42239352fb7aSAdrian Chadd }
42249352fb7aSAdrian Chadd 
42259352fb7aSAdrian Chadd /*
4226eb6f0de0SAdrian Chadd  * Update rate control with the given completion status.
4227eb6f0de0SAdrian Chadd  */
4228eb6f0de0SAdrian Chadd void
4229eb6f0de0SAdrian Chadd ath_tx_update_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
4230eb6f0de0SAdrian Chadd     struct ath_rc_series *rc, struct ath_tx_status *ts, int frmlen,
4231eb6f0de0SAdrian Chadd     int nframes, int nbad)
4232eb6f0de0SAdrian Chadd {
4233eb6f0de0SAdrian Chadd 	struct ath_node *an;
4234eb6f0de0SAdrian Chadd 
4235eb6f0de0SAdrian Chadd 	/* Only for unicast frames */
4236eb6f0de0SAdrian Chadd 	if (ni == NULL)
4237eb6f0de0SAdrian Chadd 		return;
4238eb6f0de0SAdrian Chadd 
4239eb6f0de0SAdrian Chadd 	an = ATH_NODE(ni);
4240548a605dSAdrian Chadd 	ATH_NODE_UNLOCK_ASSERT(an);
4241eb6f0de0SAdrian Chadd 
4242eb6f0de0SAdrian Chadd 	if ((ts->ts_status & HAL_TXERR_FILT) == 0) {
4243eb6f0de0SAdrian Chadd 		ATH_NODE_LOCK(an);
4244eb6f0de0SAdrian Chadd 		ath_rate_tx_complete(sc, an, rc, ts, frmlen, nframes, nbad);
4245eb6f0de0SAdrian Chadd 		ATH_NODE_UNLOCK(an);
4246eb6f0de0SAdrian Chadd 	}
4247eb6f0de0SAdrian Chadd }
4248eb6f0de0SAdrian Chadd 
4249eb6f0de0SAdrian Chadd /*
4250bad98824SAdrian Chadd  * Process the completion of the given buffer.
4251bad98824SAdrian Chadd  *
4252bad98824SAdrian Chadd  * This calls the rate control update and then the buffer completion.
4253bad98824SAdrian Chadd  * This will either free the buffer or requeue it.  In any case, the
4254bad98824SAdrian Chadd  * bf pointer should be treated as invalid after this function is called.
4255bad98824SAdrian Chadd  */
4256bad98824SAdrian Chadd void
4257bad98824SAdrian Chadd ath_tx_process_buf_completion(struct ath_softc *sc, struct ath_txq *txq,
4258bad98824SAdrian Chadd     struct ath_tx_status *ts, struct ath_buf *bf)
4259bad98824SAdrian Chadd {
4260bad98824SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
4261bad98824SAdrian Chadd 
4262375307d4SAdrian Chadd 	ATH_TX_UNLOCK_ASSERT(sc);
42635e018508SAdrian Chadd 	ATH_TXQ_UNLOCK_ASSERT(txq);
4264bad98824SAdrian Chadd 
4265bad98824SAdrian Chadd 	/* If unicast frame, update general statistics */
4266bad98824SAdrian Chadd 	if (ni != NULL) {
4267bad98824SAdrian Chadd 		/* update statistics */
4268bad98824SAdrian Chadd 		ath_tx_update_stats(sc, ts, bf);
4269bad98824SAdrian Chadd 	}
4270bad98824SAdrian Chadd 
4271bad98824SAdrian Chadd 	/*
4272bad98824SAdrian Chadd 	 * Call the completion handler.
4273bad98824SAdrian Chadd 	 * The completion handler is responsible for
4274bad98824SAdrian Chadd 	 * calling the rate control code.
4275bad98824SAdrian Chadd 	 *
4276bad98824SAdrian Chadd 	 * Frames with no completion handler get the
4277bad98824SAdrian Chadd 	 * rate control code called here.
4278bad98824SAdrian Chadd 	 */
4279bad98824SAdrian Chadd 	if (bf->bf_comp == NULL) {
4280bad98824SAdrian Chadd 		if ((ts->ts_status & HAL_TXERR_FILT) == 0 &&
4281bad98824SAdrian Chadd 		    (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) {
4282bad98824SAdrian Chadd 			/*
4283bad98824SAdrian Chadd 			 * XXX assume this isn't an aggregate
4284bad98824SAdrian Chadd 			 * frame.
4285bad98824SAdrian Chadd 			 */
4286bad98824SAdrian Chadd 			ath_tx_update_ratectrl(sc, ni,
4287bad98824SAdrian Chadd 			     bf->bf_state.bfs_rc, ts,
4288bad98824SAdrian Chadd 			    bf->bf_state.bfs_pktlen, 1,
4289bad98824SAdrian Chadd 			    (ts->ts_status == 0 ? 0 : 1));
4290bad98824SAdrian Chadd 		}
4291bad98824SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4292bad98824SAdrian Chadd 	} else
4293bad98824SAdrian Chadd 		bf->bf_comp(sc, bf, 0);
4294bad98824SAdrian Chadd }
4295bad98824SAdrian Chadd 
4296bad98824SAdrian Chadd 
4297bad98824SAdrian Chadd 
4298bad98824SAdrian Chadd /*
4299c42a7b7eSSam Leffler  * Process completed xmit descriptors from the specified queue.
4300eb6f0de0SAdrian Chadd  * Kick the packet scheduler if needed. This can occur from this
4301eb6f0de0SAdrian Chadd  * particular task.
4302c42a7b7eSSam Leffler  */
4303788e6aa9SAdrian Chadd static int
4304788e6aa9SAdrian Chadd ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq, int dosched)
43055591b213SSam Leffler {
43065591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
43079352fb7aSAdrian Chadd 	struct ath_buf *bf;
43086edf1dc7SAdrian Chadd 	struct ath_desc *ds;
430965f9edeeSSam Leffler 	struct ath_tx_status *ts;
43105591b213SSam Leffler 	struct ieee80211_node *ni;
431153e98d5aSAdrian Chadd #ifdef	IEEE80211_SUPPORT_SUPERG
43127a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
431353e98d5aSAdrian Chadd #endif	/* IEEE80211_SUPPORT_SUPERG */
43149352fb7aSAdrian Chadd 	int nacked;
43155591b213SSam Leffler 	HAL_STATUS status;
43165591b213SSam Leffler 
4317c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
4318c42a7b7eSSam Leffler 		__func__, txq->axq_qnum,
4319c42a7b7eSSam Leffler 		(caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
4320c42a7b7eSSam Leffler 		txq->axq_link);
432103682514SAdrian Chadd 
432203682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 4,
432303682514SAdrian Chadd 	    "ath_tx_processq: txq=%u head %p link %p depth %p",
432403682514SAdrian Chadd 	    txq->axq_qnum,
432503682514SAdrian Chadd 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
432603682514SAdrian Chadd 	    txq->axq_link,
432703682514SAdrian Chadd 	    txq->axq_depth);
432803682514SAdrian Chadd 
4329d7736e13SSam Leffler 	nacked = 0;
43305591b213SSam Leffler 	for (;;) {
4331b837332dSAdrian Chadd 		ATH_TXQ_LOCK(txq);
4332c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;	/* reset periodic desc intr count */
43336b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&txq->axq_q);
43345591b213SSam Leffler 		if (bf == NULL) {
4335b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
43365591b213SSam Leffler 			break;
43375591b213SSam Leffler 		}
43386edf1dc7SAdrian Chadd 		ds = bf->bf_lastds;	/* XXX must be setup correctly! */
433965f9edeeSSam Leffler 		ts = &bf->bf_status.ds_txstat;
434003682514SAdrian Chadd 
434165f9edeeSSam Leffler 		status = ath_hal_txprocdesc(ah, ds, ts);
4342a585a9a1SSam Leffler #ifdef ATH_DEBUG
4343c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
43446902009eSSam Leffler 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
43456902009eSSam Leffler 			    status == HAL_OK);
434603682514SAdrian Chadd 		else if ((sc->sc_debug & ATH_DEBUG_RESET) && (dosched == 0))
4347d6b20023SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
4348d6b20023SAdrian Chadd 			    status == HAL_OK);
43495591b213SSam Leffler #endif
4350bb327d28SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
4351bb327d28SAdrian Chadd 		if (if_ath_alq_checkdebug(&sc->sc_alq,
4352bb327d28SAdrian Chadd 		    ATH_ALQ_EDMA_TXSTATUS)) {
4353bb327d28SAdrian Chadd 			if_ath_alq_post(&sc->sc_alq, ATH_ALQ_EDMA_TXSTATUS,
4354bb327d28SAdrian Chadd 			sc->sc_tx_statuslen,
4355bb327d28SAdrian Chadd 			(char *) ds);
4356bb327d28SAdrian Chadd 		}
4357bb327d28SAdrian Chadd #endif
435803682514SAdrian Chadd 
43595591b213SSam Leffler 		if (status == HAL_EINPROGRESS) {
436003682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_TXCOMP, 3,
436103682514SAdrian Chadd 			    "ath_tx_processq: txq=%u, bf=%p ds=%p, HAL_EINPROGRESS",
436203682514SAdrian Chadd 			    txq->axq_qnum, bf, ds);
4363b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
43645591b213SSam Leffler 			break;
43655591b213SSam Leffler 		}
43666b349e5aSAdrian Chadd 		ATH_TXQ_REMOVE(txq, bf, bf_list);
43675e018508SAdrian Chadd 
43685e018508SAdrian Chadd 		/*
43695e018508SAdrian Chadd 		 * Sanity check.
43705e018508SAdrian Chadd 		 */
43715e018508SAdrian Chadd 		if (txq->axq_qnum != bf->bf_state.bfs_tx_queue) {
43725e018508SAdrian Chadd 			device_printf(sc->sc_dev,
43735e018508SAdrian Chadd 			    "%s: TXQ=%d: bf=%p, bfs_tx_queue=%d\n",
43745e018508SAdrian Chadd 			    __func__,
43755e018508SAdrian Chadd 			    txq->axq_qnum,
43765e018508SAdrian Chadd 			    bf,
43775e018508SAdrian Chadd 			    bf->bf_state.bfs_tx_queue);
43785e018508SAdrian Chadd 		}
43795e018508SAdrian Chadd 		if (txq->axq_qnum != bf->bf_last->bf_state.bfs_tx_queue) {
43805e018508SAdrian Chadd 			device_printf(sc->sc_dev,
43815e018508SAdrian Chadd 			    "%s: TXQ=%d: bf_last=%p, bfs_tx_queue=%d\n",
43825e018508SAdrian Chadd 			    __func__,
43835e018508SAdrian Chadd 			    txq->axq_qnum,
43845e018508SAdrian Chadd 			    bf->bf_last,
43855e018508SAdrian Chadd 			    bf->bf_last->bf_state.bfs_tx_queue);
43865e018508SAdrian Chadd 		}
43875e018508SAdrian Chadd 
43885e018508SAdrian Chadd #if 0
4389d3731e4bSAdrian Chadd 		if (txq->axq_depth > 0) {
439010ad9a77SSam Leffler 			/*
4391d3731e4bSAdrian Chadd 			 * More frames follow.  Mark the buffer busy
4392d3731e4bSAdrian Chadd 			 * so it's not re-used while the hardware may
4393d3731e4bSAdrian Chadd 			 * still re-read the link field in the descriptor.
43946edf1dc7SAdrian Chadd 			 *
4395d3731e4bSAdrian Chadd 			 * Use the last buffer in an aggregate as that
4396d3731e4bSAdrian Chadd 			 * is where the hardware may be - intermediate
4397d3731e4bSAdrian Chadd 			 * descriptors won't be "busy".
439810ad9a77SSam Leffler 			 */
43996edf1dc7SAdrian Chadd 			bf->bf_last->bf_flags |= ATH_BUF_BUSY;
4400d3731e4bSAdrian Chadd 		} else
4401d3731e4bSAdrian Chadd 			txq->axq_link = NULL;
44025e018508SAdrian Chadd #else
44035e018508SAdrian Chadd 		bf->bf_last->bf_flags |= ATH_BUF_BUSY;
44045e018508SAdrian Chadd #endif
44056edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
44066edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
44075591b213SSam Leffler 
44085591b213SSam Leffler 		ni = bf->bf_node;
440903682514SAdrian Chadd 
441003682514SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TXCOMP, 5,
441103682514SAdrian Chadd 		    "ath_tx_processq: txq=%u, bf=%p, ds=%p, ni=%p, ts_status=0x%08x",
441203682514SAdrian Chadd 		    txq->axq_qnum, bf, ds, ni, ts->ts_status);
4413c42a7b7eSSam Leffler 		/*
44149352fb7aSAdrian Chadd 		 * If unicast frame was ack'd update RSSI,
441584784be1SSam Leffler 		 * including the last rx time used to
441684784be1SSam Leffler 		 * workaround phantom bmiss interrupts.
4417d7736e13SSam Leffler 		 */
44189352fb7aSAdrian Chadd 		if (ni != NULL && ts->ts_status == 0 &&
4419875a9451SAdrian Chadd 		    ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)) {
4420d7736e13SSam Leffler 			nacked++;
442184784be1SSam Leffler 			sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
442284784be1SSam Leffler 			ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi,
442384784be1SSam Leffler 				ts->ts_rssi);
442484784be1SSam Leffler 		}
4425b837332dSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
44269352fb7aSAdrian Chadd 
4427bad98824SAdrian Chadd 		/*
4428bad98824SAdrian Chadd 		 * Update statistics and call completion
4429bad98824SAdrian Chadd 		 */
4430bad98824SAdrian Chadd 		ath_tx_process_buf_completion(sc, txq, ts, bf);
4431548a605dSAdrian Chadd 
4432548a605dSAdrian Chadd 		/* XXX at this point, bf and ni may be totally invalid */
44335591b213SSam Leffler 	}
4434339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
443568e8e04eSSam Leffler 	/*
443668e8e04eSSam Leffler 	 * Flush fast-frame staging queue when traffic slows.
443768e8e04eSSam Leffler 	 */
443868e8e04eSSam Leffler 	if (txq->axq_depth <= 1)
443904f19fd6SSam Leffler 		ieee80211_ff_flush(ic, txq->axq_ac);
4440339ccfb3SSam Leffler #endif
4441eb6f0de0SAdrian Chadd 
444221bca442SAdrian Chadd 	/* Kick the software TXQ scheduler */
4443eb6f0de0SAdrian Chadd 	if (dosched) {
4444a40880adSAdrian Chadd 		ATH_TX_LOCK(sc);
4445a40880adSAdrian Chadd 		ath_txq_sched(sc, txq);
4446a40880adSAdrian Chadd 		ATH_TX_UNLOCK(sc);
4447eb6f0de0SAdrian Chadd 	}
4448eb6f0de0SAdrian Chadd 
444903682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
445003682514SAdrian Chadd 	    "ath_tx_processq: txq=%u: done",
445103682514SAdrian Chadd 	    txq->axq_qnum);
445203682514SAdrian Chadd 
4453d7736e13SSam Leffler 	return nacked;
4454d7736e13SSam Leffler }
4455d7736e13SSam Leffler 
44568f939e79SAdrian Chadd #define	TXQACTIVE(t, q)		( (t) & (1 << (q)))
4457c42a7b7eSSam Leffler 
4458c42a7b7eSSam Leffler /*
4459c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4460c42a7b7eSSam Leffler  * for a single hardware transmit queue (e.g. 5210 and 5211).
4461c42a7b7eSSam Leffler  */
4462c42a7b7eSSam Leffler static void
4463c42a7b7eSSam Leffler ath_tx_proc_q0(void *arg, int npending)
4464c42a7b7eSSam Leffler {
4465c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
44668f939e79SAdrian Chadd 	uint32_t txqs;
4467c42a7b7eSSam Leffler 
4468ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4469ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
44708f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
44718f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4472ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
44738f939e79SAdrian Chadd 
4474f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4475f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4476f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4477f5c30c4eSAdrian Chadd 
447803682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
447903682514SAdrian Chadd 	    "ath_tx_proc_q0: txqs=0x%08x", txqs);
448003682514SAdrian Chadd 
448196ff485dSAdrian Chadd 	if (TXQACTIVE(txqs, 0) && ath_tx_processq(sc, &sc->sc_txq[0], 1))
44828f939e79SAdrian Chadd 		/* XXX why is lastrx updated in tx code? */
4483d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
44848f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
448596ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
44862e986da5SSam Leffler 	sc->sc_wd_timer = 0;
44875591b213SSam Leffler 
44883e50ec2cSSam Leffler 	if (sc->sc_softled)
448946d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
44903e50ec2cSSam Leffler 
4491ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4492ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4493ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
44941a85141aSAdrian Chadd 
4495f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4496f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4497f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4498f5c30c4eSAdrian Chadd 
44991a85141aSAdrian Chadd 	ath_tx_kick(sc);
45005591b213SSam Leffler }
45015591b213SSam Leffler 
45025591b213SSam Leffler /*
4503c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4504c42a7b7eSSam Leffler  * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
45055591b213SSam Leffler  */
45065591b213SSam Leffler static void
4507c42a7b7eSSam Leffler ath_tx_proc_q0123(void *arg, int npending)
4508c42a7b7eSSam Leffler {
4509c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4510d7736e13SSam Leffler 	int nacked;
45118f939e79SAdrian Chadd 	uint32_t txqs;
45128f939e79SAdrian Chadd 
4513ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4514ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
45158f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
45168f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4517ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4518c42a7b7eSSam Leffler 
4519f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4520f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4521f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4522f5c30c4eSAdrian Chadd 
452303682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
452403682514SAdrian Chadd 	    "ath_tx_proc_q0123: txqs=0x%08x", txqs);
452503682514SAdrian Chadd 
4526c42a7b7eSSam Leffler 	/*
4527c42a7b7eSSam Leffler 	 * Process each active queue.
4528c42a7b7eSSam Leffler 	 */
4529d7736e13SSam Leffler 	nacked = 0;
45308f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 0))
453196ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[0], 1);
45328f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 1))
453396ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[1], 1);
45348f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 2))
453596ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[2], 1);
45368f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 3))
453796ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[3], 1);
45388f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
453996ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
4540d7736e13SSam Leffler 	if (nacked)
4541d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4542c42a7b7eSSam Leffler 
45432e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4544c42a7b7eSSam Leffler 
45453e50ec2cSSam Leffler 	if (sc->sc_softled)
454646d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
45473e50ec2cSSam Leffler 
4548ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4549ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4550ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
45511a85141aSAdrian Chadd 
4552f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4553f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4554f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4555f5c30c4eSAdrian Chadd 
45561a85141aSAdrian Chadd 	ath_tx_kick(sc);
4557c42a7b7eSSam Leffler }
4558c42a7b7eSSam Leffler 
4559c42a7b7eSSam Leffler /*
4560c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt.
4561c42a7b7eSSam Leffler  */
4562c42a7b7eSSam Leffler static void
4563c42a7b7eSSam Leffler ath_tx_proc(void *arg, int npending)
4564c42a7b7eSSam Leffler {
4565c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4566d7736e13SSam Leffler 	int i, nacked;
45678f939e79SAdrian Chadd 	uint32_t txqs;
45688f939e79SAdrian Chadd 
4569ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4570ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
45718f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
45728f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4573ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4574c42a7b7eSSam Leffler 
4575f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4576f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4577f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4578f5c30c4eSAdrian Chadd 
457903682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1, "ath_tx_proc: txqs=0x%08x", txqs);
458003682514SAdrian Chadd 
4581c42a7b7eSSam Leffler 	/*
4582c42a7b7eSSam Leffler 	 * Process each active queue.
4583c42a7b7eSSam Leffler 	 */
4584d7736e13SSam Leffler 	nacked = 0;
4585c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
45868f939e79SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i) && TXQACTIVE(txqs, i))
458796ff485dSAdrian Chadd 			nacked += ath_tx_processq(sc, &sc->sc_txq[i], 1);
4588d7736e13SSam Leffler 	if (nacked)
4589d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4590c42a7b7eSSam Leffler 
45912e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4592c42a7b7eSSam Leffler 
45933e50ec2cSSam Leffler 	if (sc->sc_softled)
459446d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
45953e50ec2cSSam Leffler 
4596ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4597ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4598ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
45991a85141aSAdrian Chadd 
4600f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4601f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4602f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4603f5c30c4eSAdrian Chadd 
46041a85141aSAdrian Chadd 	ath_tx_kick(sc);
4605c42a7b7eSSam Leffler }
460616d4de92SAdrian Chadd #undef	TXQACTIVE
4607c42a7b7eSSam Leffler 
46089352fb7aSAdrian Chadd /*
460903e9308fSAdrian Chadd  * Deferred processing of TXQ rescheduling.
461003e9308fSAdrian Chadd  */
461103e9308fSAdrian Chadd static void
461203e9308fSAdrian Chadd ath_txq_sched_tasklet(void *arg, int npending)
461303e9308fSAdrian Chadd {
461403e9308fSAdrian Chadd 	struct ath_softc *sc = arg;
461503e9308fSAdrian Chadd 	int i;
461603e9308fSAdrian Chadd 
461703e9308fSAdrian Chadd 	/* XXX is skipping ok? */
461803e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
461903e9308fSAdrian Chadd #if 0
462003e9308fSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
462103e9308fSAdrian Chadd 		device_printf(sc->sc_dev,
462203e9308fSAdrian Chadd 		    "%s: sc_inreset_cnt > 0; skipping\n", __func__);
462303e9308fSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
462403e9308fSAdrian Chadd 		return;
462503e9308fSAdrian Chadd 	}
462603e9308fSAdrian Chadd #endif
462703e9308fSAdrian Chadd 	sc->sc_txproc_cnt++;
462803e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
462903e9308fSAdrian Chadd 
4630f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4631f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4632f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4633f5c30c4eSAdrian Chadd 
4634375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
463503e9308fSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
4636b5a9dfd5SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
463703e9308fSAdrian Chadd 			ath_txq_sched(sc, &sc->sc_txq[i]);
4638b5a9dfd5SAdrian Chadd 		}
463903e9308fSAdrian Chadd 	}
4640375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
464103e9308fSAdrian Chadd 
4642f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4643f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4644f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4645f5c30c4eSAdrian Chadd 
464603e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
464703e9308fSAdrian Chadd 	sc->sc_txproc_cnt--;
464803e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
464903e9308fSAdrian Chadd }
465003e9308fSAdrian Chadd 
4651e1a50456SAdrian Chadd void
4652e1a50456SAdrian Chadd ath_returnbuf_tail(struct ath_softc *sc, struct ath_buf *bf)
4653e1a50456SAdrian Chadd {
4654e1a50456SAdrian Chadd 
4655e1a50456SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
4656e1a50456SAdrian Chadd 
4657af33d486SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_MGMT)
4658af33d486SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf_mgmt, bf, bf_list);
465923ced6c1SAdrian Chadd 	else {
4660e1a50456SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
466123ced6c1SAdrian Chadd 		sc->sc_txbuf_cnt++;
466223ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt > ath_txbuf) {
466323ced6c1SAdrian Chadd 			device_printf(sc->sc_dev,
466423ced6c1SAdrian Chadd 			    "%s: sc_txbuf_cnt > %d?\n",
466523ced6c1SAdrian Chadd 			    __func__,
466623ced6c1SAdrian Chadd 			    ath_txbuf);
466723ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt = ath_txbuf;
466823ced6c1SAdrian Chadd 		}
466923ced6c1SAdrian Chadd 	}
4670e1a50456SAdrian Chadd }
4671e1a50456SAdrian Chadd 
4672e1a50456SAdrian Chadd void
4673e1a50456SAdrian Chadd ath_returnbuf_head(struct ath_softc *sc, struct ath_buf *bf)
4674e1a50456SAdrian Chadd {
4675e1a50456SAdrian Chadd 
4676e1a50456SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
4677e1a50456SAdrian Chadd 
4678af33d486SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_MGMT)
4679af33d486SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf_mgmt, bf, bf_list);
468023ced6c1SAdrian Chadd 	else {
4681e1a50456SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
468223ced6c1SAdrian Chadd 		sc->sc_txbuf_cnt++;
468323ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt > ATH_TXBUF) {
468423ced6c1SAdrian Chadd 			device_printf(sc->sc_dev,
468523ced6c1SAdrian Chadd 			    "%s: sc_txbuf_cnt > %d?\n",
468623ced6c1SAdrian Chadd 			    __func__,
468723ced6c1SAdrian Chadd 			    ATH_TXBUF);
468823ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt = ATH_TXBUF;
468923ced6c1SAdrian Chadd 		}
469023ced6c1SAdrian Chadd 	}
4691e1a50456SAdrian Chadd }
4692e1a50456SAdrian Chadd 
469303e9308fSAdrian Chadd /*
4694629ce218SAdrian Chadd  * Free the holding buffer if it exists
4695629ce218SAdrian Chadd  */
46963feffbd7SAdrian Chadd void
4697629ce218SAdrian Chadd ath_txq_freeholdingbuf(struct ath_softc *sc, struct ath_txq *txq)
4698629ce218SAdrian Chadd {
46995e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK_ASSERT(sc);
47005e018508SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
4701629ce218SAdrian Chadd 
4702629ce218SAdrian Chadd 	if (txq->axq_holdingbf == NULL)
4703629ce218SAdrian Chadd 		return;
4704629ce218SAdrian Chadd 
4705629ce218SAdrian Chadd 	txq->axq_holdingbf->bf_flags &= ~ATH_BUF_BUSY;
47065e018508SAdrian Chadd 
47075e018508SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
4708629ce218SAdrian Chadd 	ath_returnbuf_tail(sc, txq->axq_holdingbf);
47095e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
47105e018508SAdrian Chadd 
4711629ce218SAdrian Chadd 	txq->axq_holdingbf = NULL;
4712629ce218SAdrian Chadd }
4713629ce218SAdrian Chadd 
4714629ce218SAdrian Chadd /*
4715629ce218SAdrian Chadd  * Add this buffer to the holding queue, freeing the previous
4716629ce218SAdrian Chadd  * one if it exists.
4717629ce218SAdrian Chadd  */
4718629ce218SAdrian Chadd static void
4719629ce218SAdrian Chadd ath_txq_addholdingbuf(struct ath_softc *sc, struct ath_buf *bf)
4720629ce218SAdrian Chadd {
4721629ce218SAdrian Chadd 	struct ath_txq *txq;
4722629ce218SAdrian Chadd 
47235e018508SAdrian Chadd 	txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue];
47245e018508SAdrian Chadd 
47255e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK_ASSERT(sc);
47265e018508SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
47275f2f0e61SAdrian Chadd 
4728629ce218SAdrian Chadd 	/* XXX assert ATH_BUF_BUSY is set */
4729629ce218SAdrian Chadd 
4730629ce218SAdrian Chadd 	/* XXX assert the tx queue is under the max number */
4731629ce218SAdrian Chadd 	if (bf->bf_state.bfs_tx_queue > HAL_NUM_TX_QUEUES) {
4732629ce218SAdrian Chadd 		device_printf(sc->sc_dev, "%s: bf=%p: invalid tx queue (%d)\n",
4733629ce218SAdrian Chadd 		    __func__,
4734629ce218SAdrian Chadd 		    bf,
4735629ce218SAdrian Chadd 		    bf->bf_state.bfs_tx_queue);
4736629ce218SAdrian Chadd 		bf->bf_flags &= ~ATH_BUF_BUSY;
4737629ce218SAdrian Chadd 		ath_returnbuf_tail(sc, bf);
4738629ce218SAdrian Chadd 		return;
4739629ce218SAdrian Chadd 	}
4740629ce218SAdrian Chadd 	ath_txq_freeholdingbuf(sc, txq);
4741629ce218SAdrian Chadd 	txq->axq_holdingbf = bf;
4742629ce218SAdrian Chadd }
4743629ce218SAdrian Chadd 
4744629ce218SAdrian Chadd /*
47459352fb7aSAdrian Chadd  * Return a buffer to the pool and update the 'busy' flag on the
47469352fb7aSAdrian Chadd  * previous 'tail' entry.
47479352fb7aSAdrian Chadd  *
47489352fb7aSAdrian Chadd  * This _must_ only be called when the buffer is involved in a completed
47499352fb7aSAdrian Chadd  * TX. The logic is that if it was part of an active TX, the previous
47509352fb7aSAdrian Chadd  * buffer on the list is now not involved in a halted TX DMA queue, waiting
47519352fb7aSAdrian Chadd  * for restart (eg for TDMA.)
47529352fb7aSAdrian Chadd  *
47539352fb7aSAdrian Chadd  * The caller must free the mbuf and recycle the node reference.
47545e018508SAdrian Chadd  *
47555e018508SAdrian Chadd  * XXX This method of handling busy / holding buffers is insanely stupid.
47565e018508SAdrian Chadd  * It requires bf_state.bfs_tx_queue to be correctly assigned.  It would
47575e018508SAdrian Chadd  * be much nicer if buffers in the processq() methods would instead be
47585e018508SAdrian Chadd  * always completed there (pushed onto a txq or ath_bufhead) so we knew
47595e018508SAdrian Chadd  * exactly what hardware queue they came from in the first place.
47609352fb7aSAdrian Chadd  */
47619352fb7aSAdrian Chadd void
47629352fb7aSAdrian Chadd ath_freebuf(struct ath_softc *sc, struct ath_buf *bf)
47639352fb7aSAdrian Chadd {
47645e018508SAdrian Chadd 	struct ath_txq *txq;
47655e018508SAdrian Chadd 
47665e018508SAdrian Chadd 	txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue];
47675e018508SAdrian Chadd 
47689352fb7aSAdrian Chadd 	KASSERT((bf->bf_node == NULL), ("%s: bf->bf_node != NULL\n", __func__));
47699352fb7aSAdrian Chadd 	KASSERT((bf->bf_m == NULL), ("%s: bf->bf_m != NULL\n", __func__));
47709352fb7aSAdrian Chadd 
4771629ce218SAdrian Chadd 	/*
47725e018508SAdrian Chadd 	 * If this buffer is busy, push it onto the holding queue.
4773629ce218SAdrian Chadd 	 */
4774629ce218SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_BUSY) {
47755e018508SAdrian Chadd 		ATH_TXQ_LOCK(txq);
4776629ce218SAdrian Chadd 		ath_txq_addholdingbuf(sc, bf);
47775e018508SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
4778629ce218SAdrian Chadd 		return;
4779629ce218SAdrian Chadd 	}
4780629ce218SAdrian Chadd 
4781629ce218SAdrian Chadd 	/*
4782629ce218SAdrian Chadd 	 * Not a busy buffer, so free normally
4783629ce218SAdrian Chadd 	 */
47849352fb7aSAdrian Chadd 	ATH_TXBUF_LOCK(sc);
4785e1a50456SAdrian Chadd 	ath_returnbuf_tail(sc, bf);
47869352fb7aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
47879352fb7aSAdrian Chadd }
47889352fb7aSAdrian Chadd 
47899352fb7aSAdrian Chadd /*
47909352fb7aSAdrian Chadd  * This is currently used by ath_tx_draintxq() and
47919352fb7aSAdrian Chadd  * ath_tx_tid_free_pkts().
47929352fb7aSAdrian Chadd  *
47939352fb7aSAdrian Chadd  * It recycles a single ath_buf.
47949352fb7aSAdrian Chadd  */
47959352fb7aSAdrian Chadd void
47969352fb7aSAdrian Chadd ath_tx_freebuf(struct ath_softc *sc, struct ath_buf *bf, int status)
47979352fb7aSAdrian Chadd {
47989352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
47999352fb7aSAdrian Chadd 	struct mbuf *m0 = bf->bf_m;
48009352fb7aSAdrian Chadd 
48013f3a5dbdSAdrian Chadd 	/*
48023f3a5dbdSAdrian Chadd 	 * Make sure that we only sync/unload if there's an mbuf.
48033f3a5dbdSAdrian Chadd 	 * If not (eg we cloned a buffer), the unload will have already
4804f6b6084bSPedro F. Giffuni 	 * occurred.
48053f3a5dbdSAdrian Chadd 	 */
48063f3a5dbdSAdrian Chadd 	if (bf->bf_m != NULL) {
48073f3a5dbdSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
48083f3a5dbdSAdrian Chadd 		    BUS_DMASYNC_POSTWRITE);
48093f3a5dbdSAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
48103f3a5dbdSAdrian Chadd 	}
48113f3a5dbdSAdrian Chadd 
48129352fb7aSAdrian Chadd 	bf->bf_node = NULL;
48139352fb7aSAdrian Chadd 	bf->bf_m = NULL;
48149352fb7aSAdrian Chadd 
48159352fb7aSAdrian Chadd 	/* Free the buffer, it's not needed any longer */
48169352fb7aSAdrian Chadd 	ath_freebuf(sc, bf);
48179352fb7aSAdrian Chadd 
4818e95f3424SAdrian Chadd 	/* Pass the buffer back to net80211 - completing it */
4819e95f3424SAdrian Chadd 	ieee80211_tx_complete(ni, m0, status);
48209352fb7aSAdrian Chadd }
48219352fb7aSAdrian Chadd 
48223feffbd7SAdrian Chadd static struct ath_buf *
48233feffbd7SAdrian Chadd ath_tx_draintxq_get_one(struct ath_softc *sc, struct ath_txq *txq)
48243feffbd7SAdrian Chadd {
48253feffbd7SAdrian Chadd 	struct ath_buf *bf;
48263feffbd7SAdrian Chadd 
48273feffbd7SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
48283feffbd7SAdrian Chadd 
48293feffbd7SAdrian Chadd 	/*
48303feffbd7SAdrian Chadd 	 * Drain the FIFO queue first, then if it's
48313feffbd7SAdrian Chadd 	 * empty, move to the normal frame queue.
48323feffbd7SAdrian Chadd 	 */
48333feffbd7SAdrian Chadd 	bf = TAILQ_FIRST(&txq->fifo.axq_q);
48343feffbd7SAdrian Chadd 	if (bf != NULL) {
48353feffbd7SAdrian Chadd 		/*
48363feffbd7SAdrian Chadd 		 * Is it the last buffer in this set?
48373feffbd7SAdrian Chadd 		 * Decrement the FIFO counter.
48383feffbd7SAdrian Chadd 		 */
48393feffbd7SAdrian Chadd 		if (bf->bf_flags & ATH_BUF_FIFOEND) {
48403feffbd7SAdrian Chadd 			if (txq->axq_fifo_depth == 0) {
48413feffbd7SAdrian Chadd 				device_printf(sc->sc_dev,
48423feffbd7SAdrian Chadd 				    "%s: Q%d: fifo_depth=0, fifo.axq_depth=%d?\n",
48433feffbd7SAdrian Chadd 				    __func__,
48443feffbd7SAdrian Chadd 				    txq->axq_qnum,
48453feffbd7SAdrian Chadd 				    txq->fifo.axq_depth);
48463feffbd7SAdrian Chadd 			} else
48473feffbd7SAdrian Chadd 				txq->axq_fifo_depth--;
48483feffbd7SAdrian Chadd 		}
48493feffbd7SAdrian Chadd 		ATH_TXQ_REMOVE(&txq->fifo, bf, bf_list);
48503feffbd7SAdrian Chadd 		return (bf);
48513feffbd7SAdrian Chadd 	}
48523feffbd7SAdrian Chadd 
48533feffbd7SAdrian Chadd 	/*
48543feffbd7SAdrian Chadd 	 * Debugging!
48553feffbd7SAdrian Chadd 	 */
48563feffbd7SAdrian Chadd 	if (txq->axq_fifo_depth != 0 || txq->fifo.axq_depth != 0) {
48573feffbd7SAdrian Chadd 		device_printf(sc->sc_dev,
48583feffbd7SAdrian Chadd 		    "%s: Q%d: fifo_depth=%d, fifo.axq_depth=%d\n",
48593feffbd7SAdrian Chadd 		    __func__,
48603feffbd7SAdrian Chadd 		    txq->axq_qnum,
48613feffbd7SAdrian Chadd 		    txq->axq_fifo_depth,
48623feffbd7SAdrian Chadd 		    txq->fifo.axq_depth);
48633feffbd7SAdrian Chadd 	}
48643feffbd7SAdrian Chadd 
48653feffbd7SAdrian Chadd 	/*
48663feffbd7SAdrian Chadd 	 * Now drain the pending queue.
48673feffbd7SAdrian Chadd 	 */
48683feffbd7SAdrian Chadd 	bf = TAILQ_FIRST(&txq->axq_q);
48693feffbd7SAdrian Chadd 	if (bf == NULL) {
48703feffbd7SAdrian Chadd 		txq->axq_link = NULL;
48713feffbd7SAdrian Chadd 		return (NULL);
48723feffbd7SAdrian Chadd 	}
48733feffbd7SAdrian Chadd 	ATH_TXQ_REMOVE(txq, bf, bf_list);
48743feffbd7SAdrian Chadd 	return (bf);
48753feffbd7SAdrian Chadd }
48763feffbd7SAdrian Chadd 
48779352fb7aSAdrian Chadd void
48781762ec94SAdrian Chadd ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
48795591b213SSam Leffler {
4880a585a9a1SSam Leffler #ifdef ATH_DEBUG
48815591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4882d2f6ed15SSam Leffler #endif
48835591b213SSam Leffler 	struct ath_buf *bf;
48847a4c5ed9SSam Leffler 	u_int ix;
48855591b213SSam Leffler 
4886c42a7b7eSSam Leffler 	/*
4887c42a7b7eSSam Leffler 	 * NB: this assumes output has been stopped and
48885d61b5e8SSam Leffler 	 *     we do not need to block ath_tx_proc
4889c42a7b7eSSam Leffler 	 */
48907a4c5ed9SSam Leffler 	for (ix = 0;; ix++) {
4891b837332dSAdrian Chadd 		ATH_TXQ_LOCK(txq);
48923feffbd7SAdrian Chadd 		bf = ath_tx_draintxq_get_one(sc, txq);
48935591b213SSam Leffler 		if (bf == NULL) {
4894b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
48955591b213SSam Leffler 			break;
48965591b213SSam Leffler 		}
48976edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
48986edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
4899a585a9a1SSam Leffler #ifdef ATH_DEBUG
49004a3ac3fcSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RESET) {
49017a79cebfSGleb Smirnoff 			struct ieee80211com *ic = &sc->sc_ic;
49021762ec94SAdrian Chadd 			int status = 0;
4903b032f27cSSam Leffler 
49041762ec94SAdrian Chadd 			/*
49051762ec94SAdrian Chadd 			 * EDMA operation has a TX completion FIFO
49061762ec94SAdrian Chadd 			 * separate from the TX descriptor, so this
49071762ec94SAdrian Chadd 			 * method of checking the "completion" status
49081762ec94SAdrian Chadd 			 * is wrong.
49091762ec94SAdrian Chadd 			 */
49101762ec94SAdrian Chadd 			if (! sc->sc_isedma) {
49111762ec94SAdrian Chadd 				status = (ath_hal_txprocdesc(ah,
49121762ec94SAdrian Chadd 				    bf->bf_lastds,
491365f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
49141762ec94SAdrian Chadd 			}
49151762ec94SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, ix, status);
4916e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ic, mtod(bf->bf_m, const uint8_t *),
49174a3ac3fcSSam Leffler 			    bf->bf_m->m_len, 0, -1);
49184a3ac3fcSSam Leffler 		}
4919a585a9a1SSam Leffler #endif /* ATH_DEBUG */
492023428eafSSam Leffler 		/*
49219352fb7aSAdrian Chadd 		 * Since we're now doing magic in the completion
49229352fb7aSAdrian Chadd 		 * functions, we -must- call it for aggregation
49239352fb7aSAdrian Chadd 		 * destinations or BAW tracking will get upset.
492423428eafSSam Leffler 		 */
49259352fb7aSAdrian Chadd 		/*
49269352fb7aSAdrian Chadd 		 * Clear ATH_BUF_BUSY; the completion handler
49279352fb7aSAdrian Chadd 		 * will free the buffer.
49289352fb7aSAdrian Chadd 		 */
4929b837332dSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
493010ad9a77SSam Leffler 		bf->bf_flags &= ~ATH_BUF_BUSY;
49319352fb7aSAdrian Chadd 		if (bf->bf_comp)
49329352fb7aSAdrian Chadd 			bf->bf_comp(sc, bf, 1);
49339352fb7aSAdrian Chadd 		else
49349352fb7aSAdrian Chadd 			ath_tx_default_comp(sc, bf, 1);
49355591b213SSam Leffler 	}
49369352fb7aSAdrian Chadd 
4937eb6f0de0SAdrian Chadd 	/*
4938629ce218SAdrian Chadd 	 * Free the holding buffer if it exists
4939629ce218SAdrian Chadd 	 */
49405e018508SAdrian Chadd 	ATH_TXQ_LOCK(txq);
4941629ce218SAdrian Chadd 	ath_txq_freeholdingbuf(sc, txq);
49425e018508SAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
4943629ce218SAdrian Chadd 
4944629ce218SAdrian Chadd 	/*
4945eb6f0de0SAdrian Chadd 	 * Drain software queued frames which are on
4946eb6f0de0SAdrian Chadd 	 * active TIDs.
4947eb6f0de0SAdrian Chadd 	 */
4948eb6f0de0SAdrian Chadd 	ath_tx_txq_drain(sc, txq);
4949c42a7b7eSSam Leffler }
4950c42a7b7eSSam Leffler 
4951c42a7b7eSSam Leffler static void
4952c42a7b7eSSam Leffler ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
4953c42a7b7eSSam Leffler {
4954c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4955c42a7b7eSSam Leffler 
49569be82a42SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
49579be82a42SAdrian Chadd 
49589d2a962bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
4959dfaf8de9SAdrian Chadd 	    "%s: tx queue [%u] %p, active=%d, hwpending=%d, flags 0x%08x, "
4960dfaf8de9SAdrian Chadd 	    "link %p, holdingbf=%p\n",
49619d2a962bSAdrian Chadd 	    __func__,
49629d2a962bSAdrian Chadd 	    txq->axq_qnum,
49636891c875SPeter Wemm 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
49648d060542SAdrian Chadd 	    (int) (!! ath_hal_txqenabled(ah, txq->axq_qnum)),
49658d060542SAdrian Chadd 	    (int) ath_hal_numtxpending(ah, txq->axq_qnum),
49669d2a962bSAdrian Chadd 	    txq->axq_flags,
4967dfaf8de9SAdrian Chadd 	    txq->axq_link,
4968dfaf8de9SAdrian Chadd 	    txq->axq_holdingbf);
4969dfaf8de9SAdrian Chadd 
49704a3ac3fcSSam Leffler 	(void) ath_hal_stoptxdma(ah, txq->axq_qnum);
49719be82a42SAdrian Chadd 	/* We've stopped TX DMA, so mark this as stopped. */
49729be82a42SAdrian Chadd 	txq->axq_flags &= ~ATH_TXQ_PUTRUNNING;
4973dfaf8de9SAdrian Chadd 
4974dfaf8de9SAdrian Chadd #ifdef	ATH_DEBUG
4975dfaf8de9SAdrian Chadd 	if ((sc->sc_debug & ATH_DEBUG_RESET)
4976dfaf8de9SAdrian Chadd 	    && (txq->axq_holdingbf != NULL)) {
4977dfaf8de9SAdrian Chadd 		ath_printtxbuf(sc, txq->axq_holdingbf, txq->axq_qnum, 0, 0);
4978dfaf8de9SAdrian Chadd 	}
4979dfaf8de9SAdrian Chadd #endif
4980c42a7b7eSSam Leffler }
4981c42a7b7eSSam Leffler 
4982bad98824SAdrian Chadd int
49832d433424SAdrian Chadd ath_stoptxdma(struct ath_softc *sc)
4984c42a7b7eSSam Leffler {
4985c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4986c42a7b7eSSam Leffler 	int i;
4987c42a7b7eSSam Leffler 
4988c42a7b7eSSam Leffler 	/* XXX return value */
49892d433424SAdrian Chadd 	if (sc->sc_invalid)
49902d433424SAdrian Chadd 		return 0;
49912d433424SAdrian Chadd 
4992c42a7b7eSSam Leffler 	if (!sc->sc_invalid) {
4993c42a7b7eSSam Leffler 		/* don't touch the hardware if marked invalid */
49944a3ac3fcSSam Leffler 		DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
49954a3ac3fcSSam Leffler 		    __func__, sc->sc_bhalq,
49964a3ac3fcSSam Leffler 		    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq),
49974a3ac3fcSSam Leffler 		    NULL);
49989be82a42SAdrian Chadd 
49999be82a42SAdrian Chadd 		/* stop the beacon queue */
5000c42a7b7eSSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
50019be82a42SAdrian Chadd 
50029be82a42SAdrian Chadd 		/* Stop the data queues */
50039be82a42SAdrian Chadd 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
50049be82a42SAdrian Chadd 			if (ATH_TXQ_SETUP(sc, i)) {
50059be82a42SAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
5006c42a7b7eSSam Leffler 				ath_tx_stopdma(sc, &sc->sc_txq[i]);
50079be82a42SAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
50089be82a42SAdrian Chadd 			}
50099be82a42SAdrian Chadd 		}
5010c42a7b7eSSam Leffler 	}
50112d433424SAdrian Chadd 
50122d433424SAdrian Chadd 	return 1;
50132d433424SAdrian Chadd }
50142d433424SAdrian Chadd 
501507187d11SAdrian Chadd #ifdef	ATH_DEBUG
50169be82a42SAdrian Chadd void
5017ed261a61SAdrian Chadd ath_tx_dump(struct ath_softc *sc, struct ath_txq *txq)
5018ed261a61SAdrian Chadd {
5019ed261a61SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
5020ed261a61SAdrian Chadd 	struct ath_buf *bf;
5021ed261a61SAdrian Chadd 	int i = 0;
5022ed261a61SAdrian Chadd 
5023ed261a61SAdrian Chadd 	if (! (sc->sc_debug & ATH_DEBUG_RESET))
5024ed261a61SAdrian Chadd 		return;
5025ed261a61SAdrian Chadd 
5026ed261a61SAdrian Chadd 	device_printf(sc->sc_dev, "%s: Q%d: begin\n",
5027ed261a61SAdrian Chadd 	    __func__, txq->axq_qnum);
5028ed261a61SAdrian Chadd 	TAILQ_FOREACH(bf, &txq->axq_q, bf_list) {
5029ed261a61SAdrian Chadd 		ath_printtxbuf(sc, bf, txq->axq_qnum, i,
5030ed261a61SAdrian Chadd 			ath_hal_txprocdesc(ah, bf->bf_lastds,
5031ed261a61SAdrian Chadd 			    &bf->bf_status.ds_txstat) == HAL_OK);
5032ed261a61SAdrian Chadd 		i++;
5033ed261a61SAdrian Chadd 	}
5034ed261a61SAdrian Chadd 	device_printf(sc->sc_dev, "%s: Q%d: end\n",
5035ed261a61SAdrian Chadd 	    __func__, txq->axq_qnum);
5036ed261a61SAdrian Chadd }
503707187d11SAdrian Chadd #endif /* ATH_DEBUG */
5038ed261a61SAdrian Chadd 
50392d433424SAdrian Chadd /*
50402d433424SAdrian Chadd  * Drain the transmit queues and reclaim resources.
50412d433424SAdrian Chadd  */
5042788e6aa9SAdrian Chadd void
5043788e6aa9SAdrian Chadd ath_legacy_tx_drain(struct ath_softc *sc, ATH_RESET_TYPE reset_type)
50442d433424SAdrian Chadd {
50452d433424SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
5046ba2c1fbcSAdrian Chadd 	struct ath_buf *bf_last;
50477a79cebfSGleb Smirnoff 	int i;
50482d433424SAdrian Chadd 
50492d433424SAdrian Chadd 	(void) ath_stoptxdma(sc);
50502d433424SAdrian Chadd 
5051ed261a61SAdrian Chadd 	/*
5052ed261a61SAdrian Chadd 	 * Dump the queue contents
5053ed261a61SAdrian Chadd 	 */
5054ef27340cSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
5055ef27340cSAdrian Chadd 		/*
5056ef27340cSAdrian Chadd 		 * XXX TODO: should we just handle the completed TX frames
5057ef27340cSAdrian Chadd 		 * here, whether or not the reset is a full one or not?
5058ef27340cSAdrian Chadd 		 */
5059ef27340cSAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
506007187d11SAdrian Chadd #ifdef	ATH_DEBUG
5061ed261a61SAdrian Chadd 			if (sc->sc_debug & ATH_DEBUG_RESET)
5062ed261a61SAdrian Chadd 				ath_tx_dump(sc, &sc->sc_txq[i]);
506307187d11SAdrian Chadd #endif	/* ATH_DEBUG */
50648328d6e4SAdrian Chadd 			if (reset_type == ATH_RESET_NOLOSS) {
5065ef27340cSAdrian Chadd 				ath_tx_processq(sc, &sc->sc_txq[i], 0);
50668328d6e4SAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
50678328d6e4SAdrian Chadd 				/*
50688328d6e4SAdrian Chadd 				 * Free the holding buffer; DMA is now
50698328d6e4SAdrian Chadd 				 * stopped.
50708328d6e4SAdrian Chadd 				 */
50718328d6e4SAdrian Chadd 				ath_txq_freeholdingbuf(sc, &sc->sc_txq[i]);
50728328d6e4SAdrian Chadd 				/*
50739be82a42SAdrian Chadd 				 * Setup the link pointer to be the
50749be82a42SAdrian Chadd 				 * _last_ buffer/descriptor in the list.
50759be82a42SAdrian Chadd 				 * If there's nothing in the list, set it
50769be82a42SAdrian Chadd 				 * to NULL.
50778328d6e4SAdrian Chadd 				 */
50789be82a42SAdrian Chadd 				bf_last = ATH_TXQ_LAST(&sc->sc_txq[i],
50799be82a42SAdrian Chadd 				    axq_q_s);
50809be82a42SAdrian Chadd 				if (bf_last != NULL) {
50819be82a42SAdrian Chadd 					ath_hal_gettxdesclinkptr(ah,
50829be82a42SAdrian Chadd 					    bf_last->bf_lastds,
50839be82a42SAdrian Chadd 					    &sc->sc_txq[i].axq_link);
50849be82a42SAdrian Chadd 				} else {
50858328d6e4SAdrian Chadd 					sc->sc_txq[i].axq_link = NULL;
50869be82a42SAdrian Chadd 				}
50878328d6e4SAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
50888328d6e4SAdrian Chadd 			} else
5089c42a7b7eSSam Leffler 				ath_tx_draintxq(sc, &sc->sc_txq[i]);
5090ef27340cSAdrian Chadd 		}
5091ef27340cSAdrian Chadd 	}
50924a3ac3fcSSam Leffler #ifdef ATH_DEBUG
50934a3ac3fcSSam Leffler 	if (sc->sc_debug & ATH_DEBUG_RESET) {
50946b349e5aSAdrian Chadd 		struct ath_buf *bf = TAILQ_FIRST(&sc->sc_bbuf);
50954a3ac3fcSSam Leffler 		if (bf != NULL && bf->bf_m != NULL) {
50966902009eSSam Leffler 			ath_printtxbuf(sc, bf, sc->sc_bhalq, 0,
50976edf1dc7SAdrian Chadd 				ath_hal_txprocdesc(ah, bf->bf_lastds,
509865f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
50997a79cebfSGleb Smirnoff 			ieee80211_dump_pkt(&sc->sc_ic,
5100e40b6ab1SSam Leffler 			    mtod(bf->bf_m, const uint8_t *), bf->bf_m->m_len,
5101e40b6ab1SSam Leffler 			    0, -1);
51024a3ac3fcSSam Leffler 		}
51034a3ac3fcSSam Leffler 	}
51044a3ac3fcSSam Leffler #endif /* ATH_DEBUG */
51052e986da5SSam Leffler 	sc->sc_wd_timer = 0;
51065591b213SSam Leffler }
51075591b213SSam Leffler 
51085591b213SSam Leffler /*
5109c42a7b7eSSam Leffler  * Update internal state after a channel change.
5110c42a7b7eSSam Leffler  */
5111c42a7b7eSSam Leffler static void
5112c42a7b7eSSam Leffler ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
5113c42a7b7eSSam Leffler {
5114c42a7b7eSSam Leffler 	enum ieee80211_phymode mode;
5115c42a7b7eSSam Leffler 
5116c42a7b7eSSam Leffler 	/*
5117c42a7b7eSSam Leffler 	 * Change channels and update the h/w rate map
5118c42a7b7eSSam Leffler 	 * if we're switching; e.g. 11a to 11b/g.
5119c42a7b7eSSam Leffler 	 */
512068e8e04eSSam Leffler 	mode = ieee80211_chan2mode(chan);
5121c42a7b7eSSam Leffler 	if (mode != sc->sc_curmode)
5122c42a7b7eSSam Leffler 		ath_setcurmode(sc, mode);
512359efa8b5SSam Leffler 	sc->sc_curchan = chan;
5124c42a7b7eSSam Leffler }
5125c42a7b7eSSam Leffler 
5126c42a7b7eSSam Leffler /*
51275591b213SSam Leffler  * Set/change channels.  If the channel is really being changed,
51284fa8d4efSDaniel Eischen  * it's done by resetting the chip.  To accomplish this we must
51295591b213SSam Leffler  * first cleanup any pending DMA, then restart stuff after a la
51305591b213SSam Leffler  * ath_init.
51315591b213SSam Leffler  */
51325591b213SSam Leffler static int
51335591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
51345591b213SSam Leffler {
51357a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
51365591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5137ef27340cSAdrian Chadd 	int ret = 0;
5138ef27340cSAdrian Chadd 
5139ef27340cSAdrian Chadd 	/* Treat this as an interface reset */
5140d52f7132SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
5141d52f7132SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
5142d52f7132SAdrian Chadd 
5143f6b6084bSPedro F. Giffuni 	/* (Try to) stop TX/RX from occurring */
5144d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
5145d52f7132SAdrian Chadd 
5146ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5147904e385eSAdrian Chadd 
514817bb5fd1SAdrian Chadd 	/* Disable interrupts */
514917bb5fd1SAdrian Chadd 	ath_hal_intrset(ah, 0);
515017bb5fd1SAdrian Chadd 
5151904e385eSAdrian Chadd 	/* Stop new RX/TX/interrupt completion */
5152ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
5153ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
5154ef27340cSAdrian Chadd 		    __func__);
5155ee321975SAdrian Chadd 	}
5156904e385eSAdrian Chadd 
5157904e385eSAdrian Chadd 	/* Stop pending RX/TX completion */
5158904e385eSAdrian Chadd 	ath_txrx_stop_locked(sc);
5159904e385eSAdrian Chadd 
5160ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5161c42a7b7eSSam Leffler 
516259efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz, flags 0x%x)\n",
516359efa8b5SSam Leffler 	    __func__, ieee80211_chan2ieee(ic, chan),
516459efa8b5SSam Leffler 	    chan->ic_freq, chan->ic_flags);
516559efa8b5SSam Leffler 	if (chan != sc->sc_curchan) {
5166c42a7b7eSSam Leffler 		HAL_STATUS status;
51675591b213SSam Leffler 		/*
51685591b213SSam Leffler 		 * To switch channels clear any pending DMA operations;
51695591b213SSam Leffler 		 * wait long enough for the RX fifo to drain, reset the
51705591b213SSam Leffler 		 * hardware at the new frequency, and then re-enable
51715591b213SSam Leffler 		 * the relevant bits of the h/w.
51725591b213SSam Leffler 		 */
5173ef27340cSAdrian Chadd #if 0
51745591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
5175ef27340cSAdrian Chadd #endif
51769a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* turn off frame recv */
51779a842e8bSAdrian Chadd 		/*
51789a842e8bSAdrian Chadd 		 * First, handle completed TX/RX frames.
51799a842e8bSAdrian Chadd 		 */
5180f8cc9b09SAdrian Chadd 		ath_rx_flush(sc);
51819a842e8bSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_NOLOSS);
51829a842e8bSAdrian Chadd 		/*
51839a842e8bSAdrian Chadd 		 * Next, flush the non-scheduled frames.
51849a842e8bSAdrian Chadd 		 */
5185517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_FULL);	/* clear pending tx frames */
51869a842e8bSAdrian Chadd 
51876322256bSAdrian Chadd 		ath_update_chainmasks(sc, chan);
51886322256bSAdrian Chadd 		ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
51896322256bSAdrian Chadd 		    sc->sc_cur_rxchainmask);
5190f50e4ebfSAdrian Chadd 		if (!ath_hal_reset(ah, sc->sc_opmode, chan, AH_TRUE,
5191f50e4ebfSAdrian Chadd 		    HAL_RESET_NORMAL, &status)) {
519276e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev, "%s: unable to reset "
519379649302SGavin Atkinson 			    "channel %u (%u MHz, flags 0x%x), hal status %u\n",
519459efa8b5SSam Leffler 			    __func__, ieee80211_chan2ieee(ic, chan),
519559efa8b5SSam Leffler 			    chan->ic_freq, chan->ic_flags, status);
5196ef27340cSAdrian Chadd 			ret = EIO;
5197ef27340cSAdrian Chadd 			goto finish;
51985591b213SSam Leffler 		}
5199c59005e9SSam Leffler 		sc->sc_diversity = ath_hal_getdiversity(ah);
5200c42a7b7eSSam Leffler 
520117bb5fd1SAdrian Chadd 		ATH_RX_LOCK(sc);
520217bb5fd1SAdrian Chadd 		sc->sc_rx_stopped = 1;
520317bb5fd1SAdrian Chadd 		sc->sc_rx_resetted = 1;
520417bb5fd1SAdrian Chadd 		ATH_RX_UNLOCK(sc);
520517bb5fd1SAdrian Chadd 
520648237774SAdrian Chadd 		/* Let DFS at it in case it's a DFS channel */
5207398bca2eSAdrian Chadd 		ath_dfs_radar_enable(sc, chan);
520848237774SAdrian Chadd 
52099af351f9SAdrian Chadd 		/* Let spectral at in case spectral is enabled */
52109af351f9SAdrian Chadd 		ath_spectral_enable(sc, chan);
52119af351f9SAdrian Chadd 
52125591b213SSam Leffler 		/*
5213b70f530bSAdrian Chadd 		 * Let bluetooth coexistence at in case it's needed for this
5214b70f530bSAdrian Chadd 		 * channel
5215b70f530bSAdrian Chadd 		 */
5216b70f530bSAdrian Chadd 		ath_btcoex_enable(sc, ic->ic_curchan);
5217b70f530bSAdrian Chadd 
5218b70f530bSAdrian Chadd 		/*
5219dd6a574eSAdrian Chadd 		 * If we're doing TDMA, enforce the TXOP limitation for chips
5220dd6a574eSAdrian Chadd 		 * that support it.
5221dd6a574eSAdrian Chadd 		 */
5222dd6a574eSAdrian Chadd 		if (sc->sc_hasenforcetxop && sc->sc_tdma)
5223dd6a574eSAdrian Chadd 			ath_hal_setenforcetxop(sc->sc_ah, 1);
5224dd6a574eSAdrian Chadd 		else
5225dd6a574eSAdrian Chadd 			ath_hal_setenforcetxop(sc->sc_ah, 0);
5226dd6a574eSAdrian Chadd 
5227dd6a574eSAdrian Chadd 		/*
52285591b213SSam Leffler 		 * Re-enable rx framework.
52295591b213SSam Leffler 		 */
52305591b213SSam Leffler 		if (ath_startrecv(sc) != 0) {
523176e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev,
523276e6fd5dSGleb Smirnoff 			    "%s: unable to restart recv logic\n", __func__);
5233ef27340cSAdrian Chadd 			ret = EIO;
5234ef27340cSAdrian Chadd 			goto finish;
52355591b213SSam Leffler 		}
52365591b213SSam Leffler 
52375591b213SSam Leffler 		/*
52385591b213SSam Leffler 		 * Change channels and update the h/w rate map
52395591b213SSam Leffler 		 * if we're switching; e.g. 11a to 11b/g.
52405591b213SSam Leffler 		 */
5241c42a7b7eSSam Leffler 		ath_chan_change(sc, chan);
52420a915fadSSam Leffler 
52430a915fadSSam Leffler 		/*
52442fd9aabbSAdrian Chadd 		 * Reset clears the beacon timers; reset them
52452fd9aabbSAdrian Chadd 		 * here if needed.
52462fd9aabbSAdrian Chadd 		 */
52472fd9aabbSAdrian Chadd 		if (sc->sc_beacons) {		/* restart beacons */
52482fd9aabbSAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
52492fd9aabbSAdrian Chadd 			if (sc->sc_tdma)
52502fd9aabbSAdrian Chadd 				ath_tdma_config(sc, NULL);
52512fd9aabbSAdrian Chadd 			else
52522fd9aabbSAdrian Chadd #endif
52532fd9aabbSAdrian Chadd 			ath_beacon_config(sc, NULL);
52542fd9aabbSAdrian Chadd 		}
52552fd9aabbSAdrian Chadd 
52562fd9aabbSAdrian Chadd 		/*
52570a915fadSSam Leffler 		 * Re-enable interrupts.
52580a915fadSSam Leffler 		 */
5259e78719adSAdrian Chadd #if 0
52600a915fadSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
5261ef27340cSAdrian Chadd #endif
52625591b213SSam Leffler 	}
5263ef27340cSAdrian Chadd 
5264ef27340cSAdrian Chadd finish:
5265ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5266ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
5267ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
5268ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
5269ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5270ef27340cSAdrian Chadd 
5271ef27340cSAdrian Chadd 	ath_txrx_start(sc);
5272ef27340cSAdrian Chadd 	/* XXX ath_start? */
5273ef27340cSAdrian Chadd 
5274ef27340cSAdrian Chadd 	return ret;
52755591b213SSam Leffler }
52765591b213SSam Leffler 
52775591b213SSam Leffler /*
52785591b213SSam Leffler  * Periodically recalibrate the PHY to account
52795591b213SSam Leffler  * for temperature/environment changes.
52805591b213SSam Leffler  */
52815591b213SSam Leffler static void
52825591b213SSam Leffler ath_calibrate(void *arg)
52835591b213SSam Leffler {
52845591b213SSam Leffler 	struct ath_softc *sc = arg;
52855591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
52867a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
5287943e37a1SAdrian Chadd 	HAL_BOOL longCal, isCalDone = AH_TRUE;
5288a108ab63SAdrian Chadd 	HAL_BOOL aniCal, shortCal = AH_FALSE;
52892dc7fcc4SSam Leffler 	int nextcal;
52905591b213SSam Leffler 
5291adcdc8f2SAdrian Chadd 	ATH_LOCK_ASSERT(sc);
52927707f31dSAdrian Chadd 
5293f5c30c4eSAdrian Chadd 	/*
5294f5c30c4eSAdrian Chadd 	 * Force the hardware awake for ANI work.
5295f5c30c4eSAdrian Chadd 	 */
5296f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
5297f5c30c4eSAdrian Chadd 
5298f5c30c4eSAdrian Chadd 	/* Skip trying to do this if we're in reset */
5299f5c30c4eSAdrian Chadd 	if (sc->sc_inreset_cnt)
5300f5c30c4eSAdrian Chadd 		goto restart;
5301f5c30c4eSAdrian Chadd 
53028d91de92SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SCAN)	/* defer, off channel */
53038d91de92SSam Leffler 		goto restart;
53042dc7fcc4SSam Leffler 	longCal = (ticks - sc->sc_lastlongcal >= ath_longcalinterval*hz);
5305a108ab63SAdrian Chadd 	aniCal = (ticks - sc->sc_lastani >= ath_anicalinterval*hz/1000);
5306a108ab63SAdrian Chadd 	if (sc->sc_doresetcal)
5307a108ab63SAdrian Chadd 		shortCal = (ticks - sc->sc_lastshortcal >= ath_shortcalinterval*hz/1000);
5308a108ab63SAdrian Chadd 
5309a108ab63SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: shortCal=%d; longCal=%d; aniCal=%d\n", __func__, shortCal, longCal, aniCal);
5310a108ab63SAdrian Chadd 	if (aniCal) {
5311a108ab63SAdrian Chadd 		sc->sc_stats.ast_ani_cal++;
5312a108ab63SAdrian Chadd 		sc->sc_lastani = ticks;
5313a108ab63SAdrian Chadd 		ath_hal_ani_poll(ah, sc->sc_curchan);
5314a108ab63SAdrian Chadd 	}
5315a108ab63SAdrian Chadd 
53162dc7fcc4SSam Leffler 	if (longCal) {
53175591b213SSam Leffler 		sc->sc_stats.ast_per_cal++;
53188197f57eSAdrian Chadd 		sc->sc_lastlongcal = ticks;
53195591b213SSam Leffler 		if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
53205591b213SSam Leffler 			/*
53215591b213SSam Leffler 			 * Rfgain is out of bounds, reset the chip
53225591b213SSam Leffler 			 * to load new gain values.
53235591b213SSam Leffler 			 */
5324370572d9SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
5325370572d9SSam Leffler 				"%s: rfgain change\n", __func__);
53265591b213SSam Leffler 			sc->sc_stats.ast_per_rfgain++;
5327ef27340cSAdrian Chadd 			sc->sc_resetcal = 0;
5328ef27340cSAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
5329d52f7132SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
5330d52f7132SAdrian Chadd 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
5331f5c30c4eSAdrian Chadd 			ath_power_restore_power_state(sc);
5332ef27340cSAdrian Chadd 			return;
53335591b213SSam Leffler 		}
53342dc7fcc4SSam Leffler 		/*
53352dc7fcc4SSam Leffler 		 * If this long cal is after an idle period, then
53362dc7fcc4SSam Leffler 		 * reset the data collection state so we start fresh.
53372dc7fcc4SSam Leffler 		 */
53382dc7fcc4SSam Leffler 		if (sc->sc_resetcal) {
533959efa8b5SSam Leffler 			(void) ath_hal_calreset(ah, sc->sc_curchan);
53402dc7fcc4SSam Leffler 			sc->sc_lastcalreset = ticks;
5341a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
53422dc7fcc4SSam Leffler 			sc->sc_resetcal = 0;
5343a108ab63SAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
53442dc7fcc4SSam Leffler 		}
53452dc7fcc4SSam Leffler 	}
5346a108ab63SAdrian Chadd 
5347a108ab63SAdrian Chadd 	/* Only call if we're doing a short/long cal, not for ANI calibration */
5348a108ab63SAdrian Chadd 	if (shortCal || longCal) {
5349943e37a1SAdrian Chadd 		isCalDone = AH_FALSE;
535059efa8b5SSam Leffler 		if (ath_hal_calibrateN(ah, sc->sc_curchan, longCal, &isCalDone)) {
53512dc7fcc4SSam Leffler 			if (longCal) {
53522dc7fcc4SSam Leffler 				/*
53532dc7fcc4SSam Leffler 				 * Calibrate noise floor data again in case of change.
53542dc7fcc4SSam Leffler 				 */
53552dc7fcc4SSam Leffler 				ath_hal_process_noisefloor(ah);
53562dc7fcc4SSam Leffler 			}
53572dc7fcc4SSam Leffler 		} else {
5358c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
5359c42a7b7eSSam Leffler 				"%s: calibration of channel %u failed\n",
536059efa8b5SSam Leffler 				__func__, sc->sc_curchan->ic_freq);
53615591b213SSam Leffler 			sc->sc_stats.ast_per_calfail++;
53625591b213SSam Leffler 		}
5363a108ab63SAdrian Chadd 		if (shortCal)
5364a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
5365a108ab63SAdrian Chadd 	}
53662dc7fcc4SSam Leffler 	if (!isCalDone) {
53678d91de92SSam Leffler restart:
53687b0c77ecSSam Leffler 		/*
53692dc7fcc4SSam Leffler 		 * Use a shorter interval to potentially collect multiple
53702dc7fcc4SSam Leffler 		 * data samples required to complete calibration.  Once
53712dc7fcc4SSam Leffler 		 * we're told the work is done we drop back to a longer
53722dc7fcc4SSam Leffler 		 * interval between requests.  We're more aggressive doing
53732dc7fcc4SSam Leffler 		 * work when operating as an AP to improve operation right
53742dc7fcc4SSam Leffler 		 * after startup.
53757b0c77ecSSam Leffler 		 */
5376a108ab63SAdrian Chadd 		sc->sc_lastshortcal = ticks;
5377a108ab63SAdrian Chadd 		nextcal = ath_shortcalinterval*hz/1000;
53782dc7fcc4SSam Leffler 		if (sc->sc_opmode != HAL_M_HOSTAP)
53792dc7fcc4SSam Leffler 			nextcal *= 10;
5380a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_TRUE;
53812dc7fcc4SSam Leffler 	} else {
5382a108ab63SAdrian Chadd 		/* nextcal should be the shortest time for next event */
53832dc7fcc4SSam Leffler 		nextcal = ath_longcalinterval*hz;
53842dc7fcc4SSam Leffler 		if (sc->sc_lastcalreset == 0)
53852dc7fcc4SSam Leffler 			sc->sc_lastcalreset = sc->sc_lastlongcal;
53862dc7fcc4SSam Leffler 		else if (ticks - sc->sc_lastcalreset >= ath_resetcalinterval*hz)
53872dc7fcc4SSam Leffler 			sc->sc_resetcal = 1;	/* setup reset next trip */
5388a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_FALSE;
5389bd5a9920SSam Leffler 	}
5390a108ab63SAdrian Chadd 	/* ANI calibration may occur more often than short/long/resetcal */
5391a108ab63SAdrian Chadd 	if (ath_anicalinterval > 0)
5392a108ab63SAdrian Chadd 		nextcal = MIN(nextcal, ath_anicalinterval*hz/1000);
5393bd5a9920SSam Leffler 
53942dc7fcc4SSam Leffler 	if (nextcal != 0) {
53952dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: next +%u (%sisCalDone)\n",
53962dc7fcc4SSam Leffler 		    __func__, nextcal, isCalDone ? "" : "!");
53972dc7fcc4SSam Leffler 		callout_reset(&sc->sc_cal_ch, nextcal, ath_calibrate, sc);
53982dc7fcc4SSam Leffler 	} else {
53992dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: calibration disabled\n",
54002dc7fcc4SSam Leffler 		    __func__);
54012dc7fcc4SSam Leffler 		/* NB: don't rearm timer */
54022dc7fcc4SSam Leffler 	}
5403f5c30c4eSAdrian Chadd 	/*
5404f5c30c4eSAdrian Chadd 	 * Restore power state now that we're done.
5405f5c30c4eSAdrian Chadd 	 */
5406f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
54075591b213SSam Leffler }
54085591b213SSam Leffler 
540968e8e04eSSam Leffler static void
541068e8e04eSSam Leffler ath_scan_start(struct ieee80211com *ic)
541168e8e04eSSam Leffler {
54123797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
541368e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
541468e8e04eSSam Leffler 	u_int32_t rfilt;
541568e8e04eSSam Leffler 
541668e8e04eSSam Leffler 	/* XXX calibration timer? */
54177a79cebfSGleb Smirnoff 	/* XXXGL: is constant ieee80211broadcastaddr a correct choice? */
541868e8e04eSSam Leffler 
5419c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
542068e8e04eSSam Leffler 	sc->sc_scanning = 1;
542168e8e04eSSam Leffler 	sc->sc_syncbeacon = 0;
542268e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5423c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
5424c98cefc5SAdrian Chadd 
5425c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
542668e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
54277a79cebfSGleb Smirnoff 	ath_hal_setassocid(ah, ieee80211broadcastaddr, 0);
5428c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
542968e8e04eSSam Leffler 
543068e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0\n",
54317a79cebfSGleb Smirnoff 		 __func__, rfilt, ether_sprintf(ieee80211broadcastaddr));
543268e8e04eSSam Leffler }
543368e8e04eSSam Leffler 
543468e8e04eSSam Leffler static void
543568e8e04eSSam Leffler ath_scan_end(struct ieee80211com *ic)
543668e8e04eSSam Leffler {
54373797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
543868e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
543968e8e04eSSam Leffler 	u_int32_t rfilt;
544068e8e04eSSam Leffler 
5441c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
544268e8e04eSSam Leffler 	sc->sc_scanning = 0;
544368e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5444c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
5445c98cefc5SAdrian Chadd 
5446c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
544768e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
544868e8e04eSSam Leffler 	ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
544968e8e04eSSam Leffler 
545068e8e04eSSam Leffler 	ath_hal_process_noisefloor(ah);
5451c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
545268e8e04eSSam Leffler 
545368e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
545468e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(sc->sc_curbssid),
545568e8e04eSSam Leffler 		 sc->sc_curaid);
545668e8e04eSSam Leffler }
545768e8e04eSSam Leffler 
5458fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
5459e7200579SAdrian Chadd /*
5460e7200579SAdrian Chadd  * For now, just do a channel change.
5461e7200579SAdrian Chadd  *
5462e7200579SAdrian Chadd  * Later, we'll go through the hard slog of suspending tx/rx, changing rate
5463e7200579SAdrian Chadd  * control state and resetting the hardware without dropping frames out
5464e7200579SAdrian Chadd  * of the queue.
5465e7200579SAdrian Chadd  *
5466e7200579SAdrian Chadd  * The unfortunate trouble here is making absolutely sure that the
5467e7200579SAdrian Chadd  * channel width change has propagated enough so the hardware
5468e7200579SAdrian Chadd  * absolutely isn't handed bogus frames for it's current operating
5469e7200579SAdrian Chadd  * mode. (Eg, 40MHz frames in 20MHz mode.) Since TX and RX can and
5470e7200579SAdrian Chadd  * does occur in parallel, we need to make certain we've blocked
5471e7200579SAdrian Chadd  * any further ongoing TX (and RX, that can cause raw TX)
5472e7200579SAdrian Chadd  * before we do this.
5473e7200579SAdrian Chadd  */
5474e7200579SAdrian Chadd static void
5475e7200579SAdrian Chadd ath_update_chw(struct ieee80211com *ic)
5476e7200579SAdrian Chadd {
54773797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
5478e7200579SAdrian Chadd 
5479e7200579SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: called\n", __func__);
5480e7200579SAdrian Chadd 	ath_set_channel(ic);
5481e7200579SAdrian Chadd }
5482fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
5483e7200579SAdrian Chadd 
548468e8e04eSSam Leffler static void
548568e8e04eSSam Leffler ath_set_channel(struct ieee80211com *ic)
548668e8e04eSSam Leffler {
54873797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
548868e8e04eSSam Leffler 
5489f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
5490f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
5491f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
5492f5c30c4eSAdrian Chadd 
549368e8e04eSSam Leffler 	(void) ath_chan_set(sc, ic->ic_curchan);
549468e8e04eSSam Leffler 	/*
549568e8e04eSSam Leffler 	 * If we are returning to our bss channel then mark state
549668e8e04eSSam Leffler 	 * so the next recv'd beacon's tsf will be used to sync the
549768e8e04eSSam Leffler 	 * beacon timers.  Note that since we only hear beacons in
549868e8e04eSSam Leffler 	 * sta/ibss mode this has no effect in other operating modes.
549968e8e04eSSam Leffler 	 */
5500a887b1e3SAdrian Chadd 	ATH_LOCK(sc);
550168e8e04eSSam Leffler 	if (!sc->sc_scanning && ic->ic_curchan == ic->ic_bsschan)
550268e8e04eSSam Leffler 		sc->sc_syncbeacon = 1;
5503f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
5504a887b1e3SAdrian Chadd 	ATH_UNLOCK(sc);
550568e8e04eSSam Leffler }
550668e8e04eSSam Leffler 
5507b032f27cSSam Leffler /*
5508b032f27cSSam Leffler  * Walk the vap list and check if there any vap's in RUN state.
5509b032f27cSSam Leffler  */
55105591b213SSam Leffler static int
5511b032f27cSSam Leffler ath_isanyrunningvaps(struct ieee80211vap *this)
55125591b213SSam Leffler {
5513b032f27cSSam Leffler 	struct ieee80211com *ic = this->iv_ic;
5514b032f27cSSam Leffler 	struct ieee80211vap *vap;
5515b032f27cSSam Leffler 
5516b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
5517b032f27cSSam Leffler 
5518b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
5519309a3e45SSam Leffler 		if (vap != this && vap->iv_state >= IEEE80211_S_RUN)
5520b032f27cSSam Leffler 			return 1;
5521b032f27cSSam Leffler 	}
5522b032f27cSSam Leffler 	return 0;
5523b032f27cSSam Leffler }
5524b032f27cSSam Leffler 
5525b032f27cSSam Leffler static int
5526b032f27cSSam Leffler ath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
5527b032f27cSSam Leffler {
5528b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
55293797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
5530b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
553145bbf62fSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5532b032f27cSSam Leffler 	struct ieee80211_node *ni = NULL;
553368e8e04eSSam Leffler 	int i, error, stamode;
55345591b213SSam Leffler 	u_int32_t rfilt;
5535f52efb6dSAdrian Chadd 	int csa_run_transition = 0;
5536f5c30c4eSAdrian Chadd 	enum ieee80211_state ostate = vap->iv_state;
5537a74ebfe5SAdrian Chadd 
55385591b213SSam Leffler 	static const HAL_LED_STATE leds[] = {
55395591b213SSam Leffler 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
55405591b213SSam Leffler 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
55415591b213SSam Leffler 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
55425591b213SSam Leffler 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
554377d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CAC */
55445591b213SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
554577d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CSA */
554677d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_SLEEP */
55475591b213SSam Leffler 	};
55485591b213SSam Leffler 
5549c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
5550f5c30c4eSAdrian Chadd 		ieee80211_state_name[ostate],
5551c42a7b7eSSam Leffler 		ieee80211_state_name[nstate]);
55525591b213SSam Leffler 
5553107fdf96SAdrian Chadd 	/*
5554107fdf96SAdrian Chadd 	 * net80211 _should_ have the comlock asserted at this point.
5555107fdf96SAdrian Chadd 	 * There are some comments around the calls to vap->iv_newstate
5556107fdf96SAdrian Chadd 	 * which indicate that it (newstate) may end up dropping the
5557107fdf96SAdrian Chadd 	 * lock.  This and the subsequent lock assert check after newstate
5558107fdf96SAdrian Chadd 	 * are an attempt to catch these and figure out how/why.
5559107fdf96SAdrian Chadd 	 */
5560107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
5561107fdf96SAdrian Chadd 
5562f5c30c4eSAdrian Chadd 	/* Before we touch the hardware - wake it up */
5563f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
55647d567ed6SAdrian Chadd 	/*
55657d567ed6SAdrian Chadd 	 * If the NIC is in anything other than SLEEP state,
55667d567ed6SAdrian Chadd 	 * we need to ensure that self-generated frames are
55677d567ed6SAdrian Chadd 	 * set for PWRMGT=0.  Otherwise we may end up with
55687d567ed6SAdrian Chadd 	 * strange situations.
55697d567ed6SAdrian Chadd 	 *
55707d567ed6SAdrian Chadd 	 * XXX TODO: is this actually the case? :-)
55717d567ed6SAdrian Chadd 	 */
55727d567ed6SAdrian Chadd 	if (nstate != IEEE80211_S_SLEEP)
55737d567ed6SAdrian Chadd 		ath_power_setselfgen(sc, HAL_PM_AWAKE);
55747d567ed6SAdrian Chadd 
55757d567ed6SAdrian Chadd 	/*
55767d567ed6SAdrian Chadd 	 * Now, wake the thing up.
55777d567ed6SAdrian Chadd 	 */
5578f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
55797707f31dSAdrian Chadd 
55807707f31dSAdrian Chadd 	/*
55817707f31dSAdrian Chadd 	 * And stop the calibration callout whilst we have
55827707f31dSAdrian Chadd 	 * ATH_LOCK held.
55837707f31dSAdrian Chadd 	 */
55847707f31dSAdrian Chadd 	callout_stop(&sc->sc_cal_ch);
5585f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
5586f5c30c4eSAdrian Chadd 
5587f5c30c4eSAdrian Chadd 	if (ostate == IEEE80211_S_CSA && nstate == IEEE80211_S_RUN)
5588f52efb6dSAdrian Chadd 		csa_run_transition = 1;
5589f52efb6dSAdrian Chadd 
55905591b213SSam Leffler 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
55915591b213SSam Leffler 
5592b032f27cSSam Leffler 	if (nstate == IEEE80211_S_SCAN) {
559358769f58SSam Leffler 		/*
5594b032f27cSSam Leffler 		 * Scanning: turn off beacon miss and don't beacon.
5595b032f27cSSam Leffler 		 * Mark beacon state so when we reach RUN state we'll
5596b032f27cSSam Leffler 		 * [re]setup beacons.  Unblock the task q thread so
5597b032f27cSSam Leffler 		 * deferred interrupt processing is done.
559858769f58SSam Leffler 		 */
5599f5c30c4eSAdrian Chadd 
5600f5c30c4eSAdrian Chadd 		/* Ensure we stay awake during scan */
5601f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
56027d567ed6SAdrian Chadd 		ath_power_setselfgen(sc, HAL_PM_AWAKE);
56038c03e55dSAdrian Chadd 		ath_power_setpower(sc, HAL_PM_AWAKE, 1);
5604f5c30c4eSAdrian Chadd 		ATH_UNLOCK(sc);
5605f5c30c4eSAdrian Chadd 
5606b032f27cSSam Leffler 		ath_hal_intrset(ah,
5607b032f27cSSam Leffler 		    sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
56085591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5609b032f27cSSam Leffler 		sc->sc_beacons = 0;
5610b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
56115591b213SSam Leffler 	}
56125591b213SSam Leffler 
561380767531SAdrian Chadd 	ni = ieee80211_ref_node(vap->iv_bss);
561468e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5615b032f27cSSam Leffler 	stamode = (vap->iv_opmode == IEEE80211_M_STA ||
56167b916f89SSam Leffler 		   vap->iv_opmode == IEEE80211_M_AHDEMO ||
5617b032f27cSSam Leffler 		   vap->iv_opmode == IEEE80211_M_IBSS);
5618f5c30c4eSAdrian Chadd 
5619f5c30c4eSAdrian Chadd 	/*
5620f5c30c4eSAdrian Chadd 	 * XXX Dont need to do this (and others) if we've transitioned
5621f5c30c4eSAdrian Chadd 	 * from SLEEP->RUN.
5622f5c30c4eSAdrian Chadd 	 */
562368e8e04eSSam Leffler 	if (stamode && nstate == IEEE80211_S_RUN) {
562468e8e04eSSam Leffler 		sc->sc_curaid = ni->ni_associd;
562568e8e04eSSam Leffler 		IEEE80211_ADDR_COPY(sc->sc_curbssid, ni->ni_bssid);
5626b032f27cSSam Leffler 		ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
5627b032f27cSSam Leffler 	}
562868e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
5629b032f27cSSam Leffler 	   __func__, rfilt, ether_sprintf(sc->sc_curbssid), sc->sc_curaid);
563068e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
563168e8e04eSSam Leffler 
5632b032f27cSSam Leffler 	/* XXX is this to restore keycache on resume? */
5633b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_STA &&
5634b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY)) {
56355591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
56365591b213SSam Leffler 			if (ath_hal_keyisvalid(ah, i))
563768e8e04eSSam Leffler 				ath_hal_keysetmac(ah, i, ni->ni_bssid);
56385591b213SSam Leffler 	}
5639b032f27cSSam Leffler 
5640b032f27cSSam Leffler 	/*
5641b032f27cSSam Leffler 	 * Invoke the parent method to do net80211 work.
5642b032f27cSSam Leffler 	 */
5643b032f27cSSam Leffler 	error = avp->av_newstate(vap, nstate, arg);
5644b032f27cSSam Leffler 	if (error != 0)
5645b032f27cSSam Leffler 		goto bad;
5646c42a7b7eSSam Leffler 
5647107fdf96SAdrian Chadd 	/*
5648107fdf96SAdrian Chadd 	 * See above: ensure av_newstate() doesn't drop the lock
5649107fdf96SAdrian Chadd 	 * on us.
5650107fdf96SAdrian Chadd 	 */
5651107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
5652107fdf96SAdrian Chadd 
565368e8e04eSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
5654b032f27cSSam Leffler 		/* NB: collect bss node again, it may have changed */
565580767531SAdrian Chadd 		ieee80211_free_node(ni);
565680767531SAdrian Chadd 		ni = ieee80211_ref_node(vap->iv_bss);
56575591b213SSam Leffler 
5658b032f27cSSam Leffler 		DPRINTF(sc, ATH_DEBUG_STATE,
5659b032f27cSSam Leffler 		    "%s(RUN): iv_flags 0x%08x bintvl %d bssid %s "
5660b032f27cSSam Leffler 		    "capinfo 0x%04x chan %d\n", __func__,
5661b032f27cSSam Leffler 		    vap->iv_flags, ni->ni_intval, ether_sprintf(ni->ni_bssid),
5662b032f27cSSam Leffler 		    ni->ni_capinfo, ieee80211_chan2ieee(ic, ic->ic_curchan));
5663b032f27cSSam Leffler 
5664b032f27cSSam Leffler 		switch (vap->iv_opmode) {
5665584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
566610ad9a77SSam Leffler 		case IEEE80211_M_AHDEMO:
566710ad9a77SSam Leffler 			if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
566810ad9a77SSam Leffler 				break;
566910ad9a77SSam Leffler 			/* fall thru... */
567010ad9a77SSam Leffler #endif
5671e8fd88a3SSam Leffler 		case IEEE80211_M_HOSTAP:
5672e8fd88a3SSam Leffler 		case IEEE80211_M_IBSS:
567359aa14a9SRui Paulo 		case IEEE80211_M_MBSS:
56745591b213SSam Leffler 			/*
5675e8fd88a3SSam Leffler 			 * Allocate and setup the beacon frame.
5676e8fd88a3SSam Leffler 			 *
5677f818612bSSam Leffler 			 * Stop any previous beacon DMA.  This may be
5678f818612bSSam Leffler 			 * necessary, for example, when an ibss merge
5679f818612bSSam Leffler 			 * causes reconfiguration; there will be a state
5680f818612bSSam Leffler 			 * transition from RUN->RUN that means we may
5681f818612bSSam Leffler 			 * be called with beacon transmission active.
5682f818612bSSam Leffler 			 */
5683f818612bSSam Leffler 			ath_hal_stoptxdma(ah, sc->sc_bhalq);
5684b032f27cSSam Leffler 
56855591b213SSam Leffler 			error = ath_beacon_alloc(sc, ni);
56865591b213SSam Leffler 			if (error != 0)
56875591b213SSam Leffler 				goto bad;
56887a04dc27SSam Leffler 			/*
568980d939bfSSam Leffler 			 * If joining an adhoc network defer beacon timer
569080d939bfSSam Leffler 			 * configuration to the next beacon frame so we
569180d939bfSSam Leffler 			 * have a current TSF to use.  Otherwise we're
5692b032f27cSSam Leffler 			 * starting an ibss/bss so there's no need to delay;
5693b032f27cSSam Leffler 			 * if this is the first vap moving to RUN state, then
5694b032f27cSSam Leffler 			 * beacon state needs to be [re]configured.
56957a04dc27SSam Leffler 			 */
5696b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_IBSS &&
5697b032f27cSSam Leffler 			    ni->ni_tstamp.tsf != 0) {
569880d939bfSSam Leffler 				sc->sc_syncbeacon = 1;
5699b032f27cSSam Leffler 			} else if (!sc->sc_beacons) {
5700584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
570110ad9a77SSam Leffler 				if (vap->iv_caps & IEEE80211_C_TDMA)
570210ad9a77SSam Leffler 					ath_tdma_config(sc, vap);
570310ad9a77SSam Leffler 				else
570410ad9a77SSam Leffler #endif
5705b032f27cSSam Leffler 					ath_beacon_config(sc, vap);
5706b032f27cSSam Leffler 				sc->sc_beacons = 1;
5707b032f27cSSam Leffler 			}
5708e8fd88a3SSam Leffler 			break;
5709e8fd88a3SSam Leffler 		case IEEE80211_M_STA:
5710e8fd88a3SSam Leffler 			/*
571180d939bfSSam Leffler 			 * Defer beacon timer configuration to the next
571280d939bfSSam Leffler 			 * beacon frame so we have a current TSF to use
571380d939bfSSam Leffler 			 * (any TSF collected when scanning is likely old).
5714f52efb6dSAdrian Chadd 			 * However if it's due to a CSA -> RUN transition,
5715f52efb6dSAdrian Chadd 			 * force a beacon update so we pick up a lack of
5716f52efb6dSAdrian Chadd 			 * beacons from an AP in CAC and thus force a
5717f52efb6dSAdrian Chadd 			 * scan.
5718a74ebfe5SAdrian Chadd 			 *
5719a74ebfe5SAdrian Chadd 			 * And, there's also corner cases here where
5720a74ebfe5SAdrian Chadd 			 * after a scan, the AP may have disappeared.
5721a74ebfe5SAdrian Chadd 			 * In that case, we may not receive an actual
5722a74ebfe5SAdrian Chadd 			 * beacon to update the beacon timer and thus we
5723a74ebfe5SAdrian Chadd 			 * won't get notified of the missing beacons.
57247a04dc27SSam Leffler 			 */
5725f5c30c4eSAdrian Chadd 			if (ostate != IEEE80211_S_RUN &&
5726f5c30c4eSAdrian Chadd 			    ostate != IEEE80211_S_SLEEP) {
5727f5c30c4eSAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_BEACON,
5728f5c30c4eSAdrian Chadd 				    "%s: STA; syncbeacon=1\n", __func__);
572980d939bfSSam Leffler 				sc->sc_syncbeacon = 1;
5730f5c30c4eSAdrian Chadd 
5731f52efb6dSAdrian Chadd 				if (csa_run_transition)
5732f52efb6dSAdrian Chadd 					ath_beacon_config(sc, vap);
5733a74ebfe5SAdrian Chadd 
5734a74ebfe5SAdrian Chadd 			/*
5735a74ebfe5SAdrian Chadd 			 * PR: kern/175227
5736a74ebfe5SAdrian Chadd 			 *
5737a74ebfe5SAdrian Chadd 			 * Reconfigure beacons during reset; as otherwise
5738a74ebfe5SAdrian Chadd 			 * we won't get the beacon timers reprogrammed
5739a74ebfe5SAdrian Chadd 			 * after a reset and thus we won't pick up a
5740a74ebfe5SAdrian Chadd 			 * beacon miss interrupt.
5741a74ebfe5SAdrian Chadd 			 *
5742a74ebfe5SAdrian Chadd 			 * Hopefully we'll see a beacon before the BMISS
5743a74ebfe5SAdrian Chadd 			 * timer fires (too often), leading to a STA
5744a74ebfe5SAdrian Chadd 			 * disassociation.
5745a74ebfe5SAdrian Chadd 			 */
5746a74ebfe5SAdrian Chadd 				sc->sc_beacons = 1;
5747f5c30c4eSAdrian Chadd 			}
5748e8fd88a3SSam Leffler 			break;
5749b032f27cSSam Leffler 		case IEEE80211_M_MONITOR:
5750b032f27cSSam Leffler 			/*
5751b032f27cSSam Leffler 			 * Monitor mode vaps have only INIT->RUN and RUN->RUN
5752b032f27cSSam Leffler 			 * transitions so we must re-enable interrupts here to
5753b032f27cSSam Leffler 			 * handle the case of a single monitor mode vap.
5754b032f27cSSam Leffler 			 */
5755b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask);
5756b032f27cSSam Leffler 			break;
5757b032f27cSSam Leffler 		case IEEE80211_M_WDS:
5758b032f27cSSam Leffler 			break;
5759e8fd88a3SSam Leffler 		default:
5760e8fd88a3SSam Leffler 			break;
57615591b213SSam Leffler 		}
57625591b213SSam Leffler 		/*
57637b0c77ecSSam Leffler 		 * Let the hal process statistics collected during a
57647b0c77ecSSam Leffler 		 * scan so it can provide calibrated noise floor data.
57657b0c77ecSSam Leffler 		 */
57667b0c77ecSSam Leffler 		ath_hal_process_noisefloor(ah);
57677b0c77ecSSam Leffler 		/*
5768ffa2cab6SSam Leffler 		 * Reset rssi stats; maybe not the best place...
5769ffa2cab6SSam Leffler 		 */
5770ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
5771ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
5772ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
5773f5c30c4eSAdrian Chadd 
5774f5c30c4eSAdrian Chadd 		/*
5775f5c30c4eSAdrian Chadd 		 * Force awake for RUN mode.
5776f5c30c4eSAdrian Chadd 		 */
5777f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
57787d567ed6SAdrian Chadd 		ath_power_setselfgen(sc, HAL_PM_AWAKE);
57798c03e55dSAdrian Chadd 		ath_power_setpower(sc, HAL_PM_AWAKE, 1);
5780f5c30c4eSAdrian Chadd 
578145bbf62fSSam Leffler 		/*
5782b032f27cSSam Leffler 		 * Finally, start any timers and the task q thread
5783b032f27cSSam Leffler 		 * (in case we didn't go through SCAN state).
578445bbf62fSSam Leffler 		 */
57852dc7fcc4SSam Leffler 		if (ath_longcalinterval != 0) {
5786c42a7b7eSSam Leffler 			/* start periodic recalibration timer */
57872dc7fcc4SSam Leffler 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
57882dc7fcc4SSam Leffler 		} else {
57892dc7fcc4SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
57902dc7fcc4SSam Leffler 			    "%s: calibration disabled\n", __func__);
5791c42a7b7eSSam Leffler 		}
57927707f31dSAdrian Chadd 		ATH_UNLOCK(sc);
5793f5c30c4eSAdrian Chadd 
5794b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
5795b032f27cSSam Leffler 	} else if (nstate == IEEE80211_S_INIT) {
5796b032f27cSSam Leffler 		/*
5797b032f27cSSam Leffler 		 * If there are no vaps left in RUN state then
5798b032f27cSSam Leffler 		 * shutdown host/driver operation:
5799b032f27cSSam Leffler 		 * o disable interrupts
5800b032f27cSSam Leffler 		 * o disable the task queue thread
5801b032f27cSSam Leffler 		 * o mark beacon processing as stopped
5802b032f27cSSam Leffler 		 */
5803b032f27cSSam Leffler 		if (!ath_isanyrunningvaps(vap)) {
5804b032f27cSSam Leffler 			sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5805b032f27cSSam Leffler 			/* disable interrupts  */
5806b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL);
5807b032f27cSSam Leffler 			taskqueue_block(sc->sc_tq);
5808b032f27cSSam Leffler 			sc->sc_beacons = 0;
5809b032f27cSSam Leffler 		}
5810584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
581110ad9a77SSam Leffler 		ath_hal_setcca(ah, AH_TRUE);
581210ad9a77SSam Leffler #endif
5813f5c30c4eSAdrian Chadd 	} else if (nstate == IEEE80211_S_SLEEP) {
5814f5c30c4eSAdrian Chadd 		/* We're going to sleep, so transition appropriately */
5815f5c30c4eSAdrian Chadd 		/* For now, only do this if we're a single STA vap */
5816f5c30c4eSAdrian Chadd 		if (sc->sc_nvaps == 1 &&
5817f5c30c4eSAdrian Chadd 		    vap->iv_opmode == IEEE80211_M_STA) {
5818f5c30c4eSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_BEACON, "%s: syncbeacon=%d\n", __func__, sc->sc_syncbeacon);
5819f5c30c4eSAdrian Chadd 			ATH_LOCK(sc);
58207d567ed6SAdrian Chadd 			/*
58217d567ed6SAdrian Chadd 			 * Always at least set the self-generated
58227d567ed6SAdrian Chadd 			 * frame config to set PWRMGT=1.
58237d567ed6SAdrian Chadd 			 */
58247d567ed6SAdrian Chadd 			ath_power_setselfgen(sc, HAL_PM_NETWORK_SLEEP);
58257d567ed6SAdrian Chadd 
58267d567ed6SAdrian Chadd 			/*
58277d567ed6SAdrian Chadd 			 * If we're not syncing beacons, transition
58287d567ed6SAdrian Chadd 			 * to NETWORK_SLEEP.
58297d567ed6SAdrian Chadd 			 *
58307d567ed6SAdrian Chadd 			 * We stay awake if syncbeacon > 0 in case
58317d567ed6SAdrian Chadd 			 * we need to listen for some beacons otherwise
58327d567ed6SAdrian Chadd 			 * our beacon timer config may be wrong.
58337d567ed6SAdrian Chadd 			 */
5834f5c30c4eSAdrian Chadd 			if (sc->sc_syncbeacon == 0) {
58358c03e55dSAdrian Chadd 				ath_power_setpower(sc, HAL_PM_NETWORK_SLEEP, 1);
5836f5c30c4eSAdrian Chadd 			}
5837f5c30c4eSAdrian Chadd 			ATH_UNLOCK(sc);
5838f5c30c4eSAdrian Chadd 		}
5839b032f27cSSam Leffler 	}
58405591b213SSam Leffler bad:
584180767531SAdrian Chadd 	ieee80211_free_node(ni);
5842f5c30c4eSAdrian Chadd 
5843f5c30c4eSAdrian Chadd 	/*
5844f5c30c4eSAdrian Chadd 	 * Restore the power state - either to what it was, or
5845f5c30c4eSAdrian Chadd 	 * to network_sleep if it's alright.
5846f5c30c4eSAdrian Chadd 	 */
5847f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
5848f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
5849f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
58505591b213SSam Leffler 	return error;
58515591b213SSam Leffler }
58525591b213SSam Leffler 
58535591b213SSam Leffler /*
5854e8fd88a3SSam Leffler  * Allocate a key cache slot to the station so we can
5855e8fd88a3SSam Leffler  * setup a mapping from key index to node. The key cache
5856e8fd88a3SSam Leffler  * slot is needed for managing antenna state and for
5857e8fd88a3SSam Leffler  * compression when stations do not use crypto.  We do
5858e8fd88a3SSam Leffler  * it uniliaterally here; if crypto is employed this slot
5859e8fd88a3SSam Leffler  * will be reassigned.
5860e8fd88a3SSam Leffler  */
5861e8fd88a3SSam Leffler static void
5862e8fd88a3SSam Leffler ath_setup_stationkey(struct ieee80211_node *ni)
5863e8fd88a3SSam Leffler {
5864b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
58653797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
5866c1225b52SSam Leffler 	ieee80211_keyix keyix, rxkeyix;
5867e8fd88a3SSam Leffler 
586880767531SAdrian Chadd 	/* XXX should take a locked ref to vap->iv_bss */
5869b032f27cSSam Leffler 	if (!ath_key_alloc(vap, &ni->ni_ucastkey, &keyix, &rxkeyix)) {
5870e8fd88a3SSam Leffler 		/*
5871e8fd88a3SSam Leffler 		 * Key cache is full; we'll fall back to doing
5872e8fd88a3SSam Leffler 		 * the more expensive lookup in software.  Note
5873e8fd88a3SSam Leffler 		 * this also means no h/w compression.
5874e8fd88a3SSam Leffler 		 */
5875e8fd88a3SSam Leffler 		/* XXX msg+statistic */
5876e8fd88a3SSam Leffler 	} else {
5877c1225b52SSam Leffler 		/* XXX locking? */
5878e8fd88a3SSam Leffler 		ni->ni_ucastkey.wk_keyix = keyix;
5879c1225b52SSam Leffler 		ni->ni_ucastkey.wk_rxkeyix = rxkeyix;
588033052833SSam Leffler 		/* NB: must mark device key to get called back on delete */
588133052833SSam Leffler 		ni->ni_ucastkey.wk_flags |= IEEE80211_KEY_DEVKEY;
5882d3ac945bSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_ucastkey.wk_macaddr, ni->ni_macaddr);
5883e8fd88a3SSam Leffler 		/* NB: this will create a pass-thru key entry */
588455c7b877SAdrian Chadd 		ath_keyset(sc, vap, &ni->ni_ucastkey, vap->iv_bss);
5885e8fd88a3SSam Leffler 	}
5886e8fd88a3SSam Leffler }
5887e8fd88a3SSam Leffler 
5888e8fd88a3SSam Leffler /*
58895591b213SSam Leffler  * Setup driver-specific state for a newly associated node.
58905591b213SSam Leffler  * Note that we're called also on a re-associate, the isnew
58915591b213SSam Leffler  * param tells us if this is the first time or not.
58925591b213SSam Leffler  */
58935591b213SSam Leffler static void
5894e9962332SSam Leffler ath_newassoc(struct ieee80211_node *ni, int isnew)
58955591b213SSam Leffler {
5896b032f27cSSam Leffler 	struct ath_node *an = ATH_NODE(ni);
5897b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
58983797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
5899c62362cbSSam Leffler 	const struct ieee80211_txparam *tp = ni->ni_txparms;
59005591b213SSam Leffler 
5901ab06fdf2SSam Leffler 	an->an_mcastrix = ath_tx_findrix(sc, tp->mcastrate);
5902ab06fdf2SSam Leffler 	an->an_mgmtrix = ath_tx_findrix(sc, tp->mgmtrate);
5903b032f27cSSam Leffler 
5904f5c30c4eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: reassoc; isnew=%d, is_powersave=%d\n",
5905f5c30c4eSAdrian Chadd 	    __func__,
5906f5c30c4eSAdrian Chadd 	    ni->ni_macaddr,
5907f5c30c4eSAdrian Chadd 	    ":",
5908f5c30c4eSAdrian Chadd 	    isnew,
5909f5c30c4eSAdrian Chadd 	    an->an_is_powersave);
5910f5c30c4eSAdrian Chadd 
5911656380e7SAdrian Chadd 	ATH_NODE_LOCK(an);
5912b032f27cSSam Leffler 	ath_rate_newassoc(sc, an, isnew);
5913656380e7SAdrian Chadd 	ATH_NODE_UNLOCK(an);
591432da86a0SAdrian Chadd 
5915e8fd88a3SSam Leffler 	if (isnew &&
5916b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey &&
5917b032f27cSSam Leffler 	    ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE)
5918e8fd88a3SSam Leffler 		ath_setup_stationkey(ni);
59194bed2b67SAdrian Chadd 
59204bed2b67SAdrian Chadd 	/*
59214bed2b67SAdrian Chadd 	 * If we're reassociating, make sure that any paused queues
59224bed2b67SAdrian Chadd 	 * get unpaused.
59234bed2b67SAdrian Chadd 	 *
5924f6b6084bSPedro F. Giffuni 	 * Now, we may have frames in the hardware queue for this node.
59254bed2b67SAdrian Chadd 	 * So if we are reassociating and there are frames in the queue,
59264bed2b67SAdrian Chadd 	 * we need to go through the cleanup path to ensure that they're
59274bed2b67SAdrian Chadd 	 * marked as non-aggregate.
59284bed2b67SAdrian Chadd 	 */
59294bed2b67SAdrian Chadd 	if (! isnew) {
593032da86a0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE,
59314bed2b67SAdrian Chadd 		    "%s: %6D: reassoc; is_powersave=%d\n",
59324bed2b67SAdrian Chadd 		    __func__,
59334bed2b67SAdrian Chadd 		    ni->ni_macaddr,
59344bed2b67SAdrian Chadd 		    ":",
59354bed2b67SAdrian Chadd 		    an->an_is_powersave);
59364bed2b67SAdrian Chadd 
59374bed2b67SAdrian Chadd 		/* XXX for now, we can't hold the lock across assoc */
59384bed2b67SAdrian Chadd 		ath_tx_node_reassoc(sc, an);
59394bed2b67SAdrian Chadd 
59404bed2b67SAdrian Chadd 		/* XXX for now, we can't hold the lock across wakeup */
59414bed2b67SAdrian Chadd 		if (an->an_is_powersave)
59424bed2b67SAdrian Chadd 			ath_tx_node_wakeup(sc, an);
59434bed2b67SAdrian Chadd 	}
5944e8fd88a3SSam Leffler }
59455591b213SSam Leffler 
59465591b213SSam Leffler static int
594759efa8b5SSam Leffler ath_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *reg,
5948b032f27cSSam Leffler 	int nchans, struct ieee80211_channel chans[])
5949b032f27cSSam Leffler {
59503797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
5951b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
595259efa8b5SSam Leffler 	HAL_STATUS status;
5953b032f27cSSam Leffler 
5954033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
595559efa8b5SSam Leffler 	    "%s: rd %u cc %u location %c%s\n",
595659efa8b5SSam Leffler 	    __func__, reg->regdomain, reg->country, reg->location,
595759efa8b5SSam Leffler 	    reg->ecm ? " ecm" : "");
5958033022a9SSam Leffler 
595959efa8b5SSam Leffler 	status = ath_hal_set_channels(ah, chans, nchans,
596059efa8b5SSam Leffler 	    reg->country, reg->regdomain);
596159efa8b5SSam Leffler 	if (status != HAL_OK) {
596259efa8b5SSam Leffler 		DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: failed, status %u\n",
596359efa8b5SSam Leffler 		    __func__, status);
596459efa8b5SSam Leffler 		return EINVAL;		/* XXX */
5965b032f27cSSam Leffler 	}
59668db87e40SAdrian Chadd 
5967b032f27cSSam Leffler 	return 0;
5968b032f27cSSam Leffler }
5969b032f27cSSam Leffler 
5970b032f27cSSam Leffler static void
5971b032f27cSSam Leffler ath_getradiocaps(struct ieee80211com *ic,
59725fe9f044SSam Leffler 	int maxchans, int *nchans, struct ieee80211_channel chans[])
5973b032f27cSSam Leffler {
59743797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
5975b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5976b032f27cSSam Leffler 
597759efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: use rd %u cc %d\n",
597859efa8b5SSam Leffler 	    __func__, SKU_DEBUG, CTRY_DEFAULT);
5979033022a9SSam Leffler 
598059efa8b5SSam Leffler 	/* XXX check return */
598159efa8b5SSam Leffler 	(void) ath_hal_getchannels(ah, chans, maxchans, nchans,
598259efa8b5SSam Leffler 	    HAL_MODE_ALL, CTRY_DEFAULT, SKU_DEBUG, AH_TRUE);
5983033022a9SSam Leffler 
5984b032f27cSSam Leffler }
5985b032f27cSSam Leffler 
5986b032f27cSSam Leffler static int
5987b032f27cSSam Leffler ath_getchannels(struct ath_softc *sc)
5988b032f27cSSam Leffler {
59897a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
5990b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
599159efa8b5SSam Leffler 	HAL_STATUS status;
5992b032f27cSSam Leffler 
5993b032f27cSSam Leffler 	/*
599459efa8b5SSam Leffler 	 * Collect channel set based on EEPROM contents.
5995b032f27cSSam Leffler 	 */
599659efa8b5SSam Leffler 	status = ath_hal_init_channels(ah, ic->ic_channels, IEEE80211_CHAN_MAX,
599759efa8b5SSam Leffler 	    &ic->ic_nchans, HAL_MODE_ALL, CTRY_DEFAULT, SKU_NONE, AH_TRUE);
599859efa8b5SSam Leffler 	if (status != HAL_OK) {
599976e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
600076e6fd5dSGleb Smirnoff 		    "%s: unable to collect channel list from hal, status %d\n",
600176e6fd5dSGleb Smirnoff 		    __func__, status);
600259efa8b5SSam Leffler 		return EINVAL;
600359efa8b5SSam Leffler 	}
6004ca876918SSam Leffler 	(void) ath_hal_getregdomain(ah, &sc->sc_eerd);
6005ca876918SSam Leffler 	ath_hal_getcountrycode(ah, &sc->sc_eecc);	/* NB: cannot fail */
600659efa8b5SSam Leffler 	/* XXX map Atheros sku's to net80211 SKU's */
600759efa8b5SSam Leffler 	/* XXX net80211 types too small */
600859efa8b5SSam Leffler 	ic->ic_regdomain.regdomain = (uint16_t) sc->sc_eerd;
600959efa8b5SSam Leffler 	ic->ic_regdomain.country = (uint16_t) sc->sc_eecc;
601059efa8b5SSam Leffler 	ic->ic_regdomain.isocc[0] = ' ';	/* XXX don't know */
601159efa8b5SSam Leffler 	ic->ic_regdomain.isocc[1] = ' ';
601259efa8b5SSam Leffler 
6013b032f27cSSam Leffler 	ic->ic_regdomain.ecm = 1;
6014b032f27cSSam Leffler 	ic->ic_regdomain.location = 'I';
6015033022a9SSam Leffler 
6016033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
601759efa8b5SSam Leffler 	    "%s: eeprom rd %u cc %u (mapped rd %u cc %u) location %c%s\n",
6018033022a9SSam Leffler 	    __func__, sc->sc_eerd, sc->sc_eecc,
6019033022a9SSam Leffler 	    ic->ic_regdomain.regdomain, ic->ic_regdomain.country,
602059efa8b5SSam Leffler 	    ic->ic_regdomain.location, ic->ic_regdomain.ecm ? " ecm" : "");
60215591b213SSam Leffler 	return 0;
60225591b213SSam Leffler }
60235591b213SSam Leffler 
60246c4612b9SSam Leffler static int
60256c4612b9SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode)
60266c4612b9SSam Leffler {
60276c4612b9SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
60286c4612b9SSam Leffler 	const HAL_RATE_TABLE *rt;
60296c4612b9SSam Leffler 
60306c4612b9SSam Leffler 	switch (mode) {
60316c4612b9SSam Leffler 	case IEEE80211_MODE_11A:
60326c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A);
60336c4612b9SSam Leffler 		break;
6034724c193aSSam Leffler 	case IEEE80211_MODE_HALF:
6035aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_HALF_RATE);
6036aaa70f2fSSam Leffler 		break;
6037724c193aSSam Leffler 	case IEEE80211_MODE_QUARTER:
6038aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_QUARTER_RATE);
6039aaa70f2fSSam Leffler 		break;
60406c4612b9SSam Leffler 	case IEEE80211_MODE_11B:
60416c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11B);
60426c4612b9SSam Leffler 		break;
60436c4612b9SSam Leffler 	case IEEE80211_MODE_11G:
60446c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11G);
60456c4612b9SSam Leffler 		break;
60466c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_A:
604768e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108A);
60486c4612b9SSam Leffler 		break;
60496c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_G:
60506c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108G);
60516c4612b9SSam Leffler 		break;
605268e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
605368e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_TURBO);
605468e8e04eSSam Leffler 		break;
605568e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
605668e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NA_HT20);
605768e8e04eSSam Leffler 		break;
605868e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
605968e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NG_HT20);
606068e8e04eSSam Leffler 		break;
60616c4612b9SSam Leffler 	default:
60626c4612b9SSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
60636c4612b9SSam Leffler 			__func__, mode);
60646c4612b9SSam Leffler 		return 0;
60656c4612b9SSam Leffler 	}
60666c4612b9SSam Leffler 	sc->sc_rates[mode] = rt;
6067aaa70f2fSSam Leffler 	return (rt != NULL);
60685591b213SSam Leffler }
60695591b213SSam Leffler 
60705591b213SSam Leffler static void
60715591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
60725591b213SSam Leffler {
60733e50ec2cSSam Leffler 	/* NB: on/off times from the Atheros NDIS driver, w/ permission */
60743e50ec2cSSam Leffler 	static const struct {
60753e50ec2cSSam Leffler 		u_int		rate;		/* tx/rx 802.11 rate */
60763e50ec2cSSam Leffler 		u_int16_t	timeOn;		/* LED on time (ms) */
60773e50ec2cSSam Leffler 		u_int16_t	timeOff;	/* LED off time (ms) */
60783e50ec2cSSam Leffler 	} blinkrates[] = {
60793e50ec2cSSam Leffler 		{ 108,  40,  10 },
60803e50ec2cSSam Leffler 		{  96,  44,  11 },
60813e50ec2cSSam Leffler 		{  72,  50,  13 },
60823e50ec2cSSam Leffler 		{  48,  57,  14 },
60833e50ec2cSSam Leffler 		{  36,  67,  16 },
60843e50ec2cSSam Leffler 		{  24,  80,  20 },
60853e50ec2cSSam Leffler 		{  22, 100,  25 },
60863e50ec2cSSam Leffler 		{  18, 133,  34 },
60873e50ec2cSSam Leffler 		{  12, 160,  40 },
60883e50ec2cSSam Leffler 		{  10, 200,  50 },
60893e50ec2cSSam Leffler 		{   6, 240,  58 },
60903e50ec2cSSam Leffler 		{   4, 267,  66 },
60913e50ec2cSSam Leffler 		{   2, 400, 100 },
60923e50ec2cSSam Leffler 		{   0, 500, 130 },
6093724c193aSSam Leffler 		/* XXX half/quarter rates */
60943e50ec2cSSam Leffler 	};
60955591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
60963e50ec2cSSam Leffler 	int i, j;
60975591b213SSam Leffler 
60985591b213SSam Leffler 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
60995591b213SSam Leffler 	rt = sc->sc_rates[mode];
61005591b213SSam Leffler 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
6101180f268dSSam Leffler 	for (i = 0; i < rt->rateCount; i++) {
6102180f268dSSam Leffler 		uint8_t ieeerate = rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
6103180f268dSSam Leffler 		if (rt->info[i].phy != IEEE80211_T_HT)
6104180f268dSSam Leffler 			sc->sc_rixmap[ieeerate] = i;
6105180f268dSSam Leffler 		else
6106180f268dSSam Leffler 			sc->sc_rixmap[ieeerate | IEEE80211_RATE_MCS] = i;
6107180f268dSSam Leffler 	}
61081b1a8e41SSam Leffler 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
6109d6166defSAdrian Chadd 	for (i = 0; i < nitems(sc->sc_hwmap); i++) {
611046d4d74cSSam Leffler 		if (i >= rt->rateCount) {
61113e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledon = (500 * hz) / 1000;
61123e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledoff = (130 * hz) / 1000;
611316b4851aSSam Leffler 			continue;
61143e50ec2cSSam Leffler 		}
61153e50ec2cSSam Leffler 		sc->sc_hwmap[i].ieeerate =
611646d4d74cSSam Leffler 			rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
611746d4d74cSSam Leffler 		if (rt->info[i].phy == IEEE80211_T_HT)
611826041a14SSam Leffler 			sc->sc_hwmap[i].ieeerate |= IEEE80211_RATE_MCS;
6119d3be6f5bSSam Leffler 		sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
612046d4d74cSSam Leffler 		if (rt->info[i].shortPreamble ||
612146d4d74cSSam Leffler 		    rt->info[i].phy == IEEE80211_T_OFDM)
6122d3be6f5bSSam Leffler 			sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE;
61235463c4a4SSam Leffler 		sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags;
6124d6166defSAdrian Chadd 		for (j = 0; j < nitems(blinkrates)-1; j++)
61253e50ec2cSSam Leffler 			if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate)
61263e50ec2cSSam Leffler 				break;
61273e50ec2cSSam Leffler 		/* NB: this uses the last entry if the rate isn't found */
61283e50ec2cSSam Leffler 		/* XXX beware of overlow */
61293e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000;
61303e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000;
6131c42a7b7eSSam Leffler 	}
61325591b213SSam Leffler 	sc->sc_currates = rt;
61335591b213SSam Leffler 	sc->sc_curmode = mode;
61345591b213SSam Leffler 	/*
6135f6b6084bSPedro F. Giffuni 	 * All protection frames are transmitted at 2Mb/s for
6136c42a7b7eSSam Leffler 	 * 11g, otherwise at 1Mb/s.
61375591b213SSam Leffler 	 */
6138913a1ba1SSam Leffler 	if (mode == IEEE80211_MODE_11G)
6139ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*2);
6140913a1ba1SSam Leffler 	else
6141ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*1);
61424fa8d4efSDaniel Eischen 	/* NB: caller is responsible for resetting rate control state */
61435591b213SSam Leffler }
61445591b213SSam Leffler 
6145c42a7b7eSSam Leffler static void
61462e986da5SSam Leffler ath_watchdog(void *arg)
6147c42a7b7eSSam Leffler {
61482e986da5SSam Leffler 	struct ath_softc *sc = arg;
61497a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
6150ef27340cSAdrian Chadd 	int do_reset = 0;
6151c42a7b7eSSam Leffler 
6152adcdc8f2SAdrian Chadd 	ATH_LOCK_ASSERT(sc);
61537707f31dSAdrian Chadd 
61542e986da5SSam Leffler 	if (sc->sc_wd_timer != 0 && --sc->sc_wd_timer == 0) {
6155459bc4f0SSam Leffler 		uint32_t hangs;
6156459bc4f0SSam Leffler 
6157f5c30c4eSAdrian Chadd 		ath_power_set_power_state(sc, HAL_PM_AWAKE);
6158f5c30c4eSAdrian Chadd 
6159459bc4f0SSam Leffler 		if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) &&
6160459bc4f0SSam Leffler 		    hangs != 0) {
616176e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev, "%s hang detected (0x%x)\n",
6162459bc4f0SSam Leffler 			    hangs & 0xff ? "bb" : "mac", hangs);
6163459bc4f0SSam Leffler 		} else
616476e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev, "device timeout\n");
6165ef27340cSAdrian Chadd 		do_reset = 1;
61667a79cebfSGleb Smirnoff 		counter_u64_add(ic->ic_oerrors, 1);
6167c42a7b7eSSam Leffler 		sc->sc_stats.ast_watchdog++;
6168f5c30c4eSAdrian Chadd 
6169f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
6170c42a7b7eSSam Leffler 	}
6171ef27340cSAdrian Chadd 
6172ef27340cSAdrian Chadd 	/*
6173ef27340cSAdrian Chadd 	 * We can't hold the lock across the ath_reset() call.
6174d52f7132SAdrian Chadd 	 *
6175d52f7132SAdrian Chadd 	 * And since this routine can't hold a lock and sleep,
6176d52f7132SAdrian Chadd 	 * do the reset deferred.
6177ef27340cSAdrian Chadd 	 */
6178ef27340cSAdrian Chadd 	if (do_reset) {
6179d52f7132SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
6180ef27340cSAdrian Chadd 	}
6181ef27340cSAdrian Chadd 
61822e986da5SSam Leffler 	callout_schedule(&sc->sc_wd_ch, hz);
6183c42a7b7eSSam Leffler }
6184c42a7b7eSSam Leffler 
61857a79cebfSGleb Smirnoff static void
61867a79cebfSGleb Smirnoff ath_parent(struct ieee80211com *ic)
6187c42a7b7eSSam Leffler {
61883797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
61897a79cebfSGleb Smirnoff 	int error = EDOOFUS;
6190c42a7b7eSSam Leffler 
61917a79cebfSGleb Smirnoff 	ATH_LOCK(sc);
61927a79cebfSGleb Smirnoff 	if (ic->ic_nrunning > 0) {
6193c42a7b7eSSam Leffler 		/*
6194c42a7b7eSSam Leffler 		 * To avoid rescanning another access point,
6195c42a7b7eSSam Leffler 		 * do not call ath_init() here.  Instead,
6196c42a7b7eSSam Leffler 		 * only reflect promisc mode settings.
6197c42a7b7eSSam Leffler 		 */
61987a79cebfSGleb Smirnoff 		if (sc->sc_running) {
61994b734a1cSAdrian Chadd 			ath_power_set_power_state(sc, HAL_PM_AWAKE);
6200c42a7b7eSSam Leffler 			ath_mode_init(sc);
62014b734a1cSAdrian Chadd 			ath_power_restore_power_state(sc);
62027a79cebfSGleb Smirnoff 		} else if (!sc->sc_invalid) {
6203c42a7b7eSSam Leffler 			/*
6204c42a7b7eSSam Leffler 			 * Beware of being called during attach/detach
6205c42a7b7eSSam Leffler 			 * to reset promiscuous mode.  In that case we
6206c42a7b7eSSam Leffler 			 * will still be marked UP but not RUNNING.
6207c42a7b7eSSam Leffler 			 * However trying to re-init the interface
6208c42a7b7eSSam Leffler 			 * is the wrong thing to do as we've already
6209c42a7b7eSSam Leffler 			 * torn down much of our state.  There's
6210c42a7b7eSSam Leffler 			 * probably a better way to deal with this.
6211c42a7b7eSSam Leffler 			 */
62127a79cebfSGleb Smirnoff 			error = ath_init(sc);
62137a79cebfSGleb Smirnoff 		}
6214d3ac945bSSam Leffler 	} else {
62157a79cebfSGleb Smirnoff 		ath_stop(sc);
6216d3ac945bSSam Leffler 		if (!sc->sc_invalid)
62178c03e55dSAdrian Chadd 			ath_power_setpower(sc, HAL_PM_FULL_SLEEP, 1);
6218410302ebSAdrian Chadd 	}
62197a79cebfSGleb Smirnoff 	ATH_UNLOCK(sc);
62207a79cebfSGleb Smirnoff 
62217a79cebfSGleb Smirnoff 	if (error == 0) {
62227a79cebfSGleb Smirnoff #ifdef ATH_TX99_DIAG
62237a79cebfSGleb Smirnoff 		if (sc->sc_tx99 != NULL)
62247a79cebfSGleb Smirnoff 			sc->sc_tx99->start(sc->sc_tx99);
62257a79cebfSGleb Smirnoff 		else
62267a79cebfSGleb Smirnoff #endif
62277a79cebfSGleb Smirnoff 		ieee80211_start_all(ic);
62287a79cebfSGleb Smirnoff 	}
62297a79cebfSGleb Smirnoff }
62307a79cebfSGleb Smirnoff 
6231c42a7b7eSSam Leffler /*
6232c42a7b7eSSam Leffler  * Announce various information on device/driver attach.
6233c42a7b7eSSam Leffler  */
6234c42a7b7eSSam Leffler static void
6235c42a7b7eSSam Leffler ath_announce(struct ath_softc *sc)
6236c42a7b7eSSam Leffler {
6237c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6238c42a7b7eSSam Leffler 
6239b2585567SAdrian Chadd 	device_printf(sc->sc_dev, "%s mac %d.%d RF%s phy %d.%d\n",
6240498657cfSSam Leffler 		ath_hal_mac_name(ah), ah->ah_macVersion, ah->ah_macRev,
6241498657cfSSam Leffler 		ath_hal_rf_name(ah), ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
624276e6fd5dSGleb Smirnoff 	device_printf(sc->sc_dev, "2GHz radio: 0x%.4x; 5GHz radio: 0x%.4x\n",
624346a924c4SAdrian Chadd 		ah->ah_analog2GhzRev, ah->ah_analog5GhzRev);
6244c42a7b7eSSam Leffler 	if (bootverbose) {
6245c42a7b7eSSam Leffler 		int i;
6246c42a7b7eSSam Leffler 		for (i = 0; i <= WME_AC_VO; i++) {
6247c42a7b7eSSam Leffler 			struct ath_txq *txq = sc->sc_ac2q[i];
624876e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev,
624976e6fd5dSGleb Smirnoff 			    "Use hw queue %u for %s traffic\n",
6250c42a7b7eSSam Leffler 			    txq->axq_qnum, ieee80211_wme_acnames[i]);
6251c42a7b7eSSam Leffler 		}
625276e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "Use hw queue %u for CAB traffic\n",
6253c42a7b7eSSam Leffler 		    sc->sc_cabq->axq_qnum);
625476e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "Use hw queue %u for beacons\n",
625576e6fd5dSGleb Smirnoff 		    sc->sc_bhalq);
6256c42a7b7eSSam Leffler 	}
6257e2d787faSSam Leffler 	if (ath_rxbuf != ATH_RXBUF)
625876e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "using %u rx buffers\n", ath_rxbuf);
6259e2d787faSSam Leffler 	if (ath_txbuf != ATH_TXBUF)
626076e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "using %u tx buffers\n", ath_txbuf);
62619ac01d39SRui Paulo 	if (sc->sc_mcastkey && bootverbose)
626276e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "using multicast key search\n");
6263c42a7b7eSSam Leffler }
626410ad9a77SSam Leffler 
626548237774SAdrian Chadd static void
626648237774SAdrian Chadd ath_dfs_tasklet(void *p, int npending)
626748237774SAdrian Chadd {
626848237774SAdrian Chadd 	struct ath_softc *sc = (struct ath_softc *) p;
62697a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
627048237774SAdrian Chadd 
627148237774SAdrian Chadd 	/*
627248237774SAdrian Chadd 	 * If previous processing has found a radar event,
627348237774SAdrian Chadd 	 * signal this to the net80211 layer to begin DFS
627448237774SAdrian Chadd 	 * processing.
627548237774SAdrian Chadd 	 */
627648237774SAdrian Chadd 	if (ath_dfs_process_radar_event(sc, sc->sc_curchan)) {
627748237774SAdrian Chadd 		/* DFS event found, initiate channel change */
627806fc4a10SAdrian Chadd 		/*
627906fc4a10SAdrian Chadd 		 * XXX doesn't currently tell us whether the event
628006fc4a10SAdrian Chadd 		 * XXX was found in the primary or extension
628106fc4a10SAdrian Chadd 		 * XXX channel!
628206fc4a10SAdrian Chadd 		 */
628306fc4a10SAdrian Chadd 		IEEE80211_LOCK(ic);
628448237774SAdrian Chadd 		ieee80211_dfs_notify_radar(ic, sc->sc_curchan);
628506fc4a10SAdrian Chadd 		IEEE80211_UNLOCK(ic);
628648237774SAdrian Chadd 	}
628748237774SAdrian Chadd }
628848237774SAdrian Chadd 
62890eb81626SAdrian Chadd /*
62900eb81626SAdrian Chadd  * Enable/disable power save.  This must be called with
62910eb81626SAdrian Chadd  * no TX driver locks currently held, so it should only
62920eb81626SAdrian Chadd  * be called from the RX path (which doesn't hold any
62930eb81626SAdrian Chadd  * TX driver locks.)
62940eb81626SAdrian Chadd  */
62950eb81626SAdrian Chadd static void
62960eb81626SAdrian Chadd ath_node_powersave(struct ieee80211_node *ni, int enable)
62970eb81626SAdrian Chadd {
6298bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
62990eb81626SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
63000eb81626SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
63013797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
63020eb81626SAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
63030eb81626SAdrian Chadd 
63040eb81626SAdrian Chadd 	/* XXX and no TXQ locks should be held here */
63050eb81626SAdrian Chadd 
63069b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, "%s: %6D: enable=%d\n",
63079b48fb4bSAdrian Chadd 	    __func__,
63089b48fb4bSAdrian Chadd 	    ni->ni_macaddr,
63099b48fb4bSAdrian Chadd 	    ":",
63109b48fb4bSAdrian Chadd 	    !! enable);
63110eb81626SAdrian Chadd 
63120eb81626SAdrian Chadd 	/* Suspend or resume software queue handling */
63130eb81626SAdrian Chadd 	if (enable)
63140eb81626SAdrian Chadd 		ath_tx_node_sleep(sc, an);
63150eb81626SAdrian Chadd 	else
63160eb81626SAdrian Chadd 		ath_tx_node_wakeup(sc, an);
63170eb81626SAdrian Chadd 
63180eb81626SAdrian Chadd 	/* Update net80211 state */
63190eb81626SAdrian Chadd 	avp->av_node_ps(ni, enable);
6320bdbb6e5bSAdrian Chadd #else
6321bdbb6e5bSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6322bdbb6e5bSAdrian Chadd 
6323bdbb6e5bSAdrian Chadd 	/* Update net80211 state */
6324bdbb6e5bSAdrian Chadd 	avp->av_node_ps(ni, enable);
6325bdbb6e5bSAdrian Chadd #endif/* ATH_SW_PSQ */
63260eb81626SAdrian Chadd }
63270eb81626SAdrian Chadd 
6328548a605dSAdrian Chadd /*
6329548a605dSAdrian Chadd  * Notification from net80211 that the powersave queue state has
6330548a605dSAdrian Chadd  * changed.
6331548a605dSAdrian Chadd  *
6332548a605dSAdrian Chadd  * Since the software queue also may have some frames:
6333548a605dSAdrian Chadd  *
6334548a605dSAdrian Chadd  * + if the node software queue has frames and the TID state
6335548a605dSAdrian Chadd  *   is 0, we set the TIM;
6336548a605dSAdrian Chadd  * + if the node and the stack are both empty, we clear the TIM bit.
6337548a605dSAdrian Chadd  * + If the stack tries to set the bit, always set it.
6338548a605dSAdrian Chadd  * + If the stack tries to clear the bit, only clear it if the
6339548a605dSAdrian Chadd  *   software queue in question is also cleared.
6340548a605dSAdrian Chadd  *
6341548a605dSAdrian Chadd  * TODO: this is called during node teardown; so let's ensure this
6342548a605dSAdrian Chadd  * is all correctly handled and that the TIM bit is cleared.
6343548a605dSAdrian Chadd  * It may be that the node flush is called _AFTER_ the net80211
6344548a605dSAdrian Chadd  * stack clears the TIM.
6345548a605dSAdrian Chadd  *
6346548a605dSAdrian Chadd  * Here is the racy part.  Since it's possible >1 concurrent,
6347548a605dSAdrian Chadd  * overlapping TXes will appear complete with a TX completion in
6348548a605dSAdrian Chadd  * another thread, it's possible that the concurrent TIM calls will
6349548a605dSAdrian Chadd  * clash.  We can't hold the node lock here because setting the
6350548a605dSAdrian Chadd  * TIM grabs the net80211 comlock and this may cause a LOR.
6351548a605dSAdrian Chadd  * The solution is either to totally serialise _everything_ at
6352548a605dSAdrian Chadd  * this point (ie, all TX, completion and any reset/flush go into
6353548a605dSAdrian Chadd  * one taskqueue) or a new "ath TIM lock" needs to be created that
6354548a605dSAdrian Chadd  * just wraps the driver state change and this call to avp->av_set_tim().
6355548a605dSAdrian Chadd  *
6356548a605dSAdrian Chadd  * The same race exists in the net80211 power save queue handling
6357548a605dSAdrian Chadd  * as well.  Since multiple transmitting threads may queue frames
6358548a605dSAdrian Chadd  * into the driver, as well as ps-poll and the driver transmitting
6359548a605dSAdrian Chadd  * frames (and thus clearing the psq), it's quite possible that
6360548a605dSAdrian Chadd  * a packet entering the PSQ and a ps-poll being handled will
6361548a605dSAdrian Chadd  * race, causing the TIM to be cleared and not re-set.
6362548a605dSAdrian Chadd  */
6363548a605dSAdrian Chadd static int
6364548a605dSAdrian Chadd ath_node_set_tim(struct ieee80211_node *ni, int enable)
6365548a605dSAdrian Chadd {
6366bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
6367548a605dSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
63683797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
6369548a605dSAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
6370548a605dSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6371548a605dSAdrian Chadd 	int changed = 0;
6372548a605dSAdrian Chadd 
63734bed2b67SAdrian Chadd 	ATH_TX_LOCK(sc);
6374548a605dSAdrian Chadd 	an->an_stack_psq = enable;
6375548a605dSAdrian Chadd 
6376548a605dSAdrian Chadd 	/*
6377548a605dSAdrian Chadd 	 * This will get called for all operating modes,
6378548a605dSAdrian Chadd 	 * even if avp->av_set_tim is unset.
6379548a605dSAdrian Chadd 	 * It's currently set for hostap/ibss modes; but
6380548a605dSAdrian Chadd 	 * the same infrastructure is used for both STA
6381548a605dSAdrian Chadd 	 * and AP/IBSS node power save.
6382548a605dSAdrian Chadd 	 */
6383548a605dSAdrian Chadd 	if (avp->av_set_tim == NULL) {
63844bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6385548a605dSAdrian Chadd 		return (0);
6386548a605dSAdrian Chadd 	}
6387548a605dSAdrian Chadd 
6388548a605dSAdrian Chadd 	/*
6389548a605dSAdrian Chadd 	 * If setting the bit, always set it here.
6390548a605dSAdrian Chadd 	 * If clearing the bit, only clear it if the
6391548a605dSAdrian Chadd 	 * software queue is also empty.
6392548a605dSAdrian Chadd 	 *
6393548a605dSAdrian Chadd 	 * If the node has left power save, just clear the TIM
6394548a605dSAdrian Chadd 	 * bit regardless of the state of the power save queue.
6395548a605dSAdrian Chadd 	 *
6396548a605dSAdrian Chadd 	 * XXX TODO: although atomics are used, it's quite possible
6397548a605dSAdrian Chadd 	 * that a race will occur between this and setting/clearing
6398548a605dSAdrian Chadd 	 * in another thread.  TX completion will occur always in
6399548a605dSAdrian Chadd 	 * one thread, however setting/clearing the TIM bit can come
6400548a605dSAdrian Chadd 	 * from a variety of different process contexts!
6401548a605dSAdrian Chadd 	 */
6402548a605dSAdrian Chadd 	if (enable && an->an_tim_set == 1) {
6403548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
64049b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, tim_set=1, ignoring\n",
64059b48fb4bSAdrian Chadd 		    __func__,
64069b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
64079b48fb4bSAdrian Chadd 		    ":",
64089b48fb4bSAdrian Chadd 		    enable);
64094bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6410548a605dSAdrian Chadd 	} else if (enable) {
6411548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
64129b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, enabling TIM\n",
64139b48fb4bSAdrian Chadd 		    __func__,
64149b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
64159b48fb4bSAdrian Chadd 		    ":",
64169b48fb4bSAdrian Chadd 		    enable);
6417548a605dSAdrian Chadd 		an->an_tim_set = 1;
64184bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6419548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6420ba83edd4SAdrian Chadd 	} else if (an->an_swq_depth == 0) {
6421548a605dSAdrian Chadd 		/* disable */
6422548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
64239b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_swq_depth == 0, disabling\n",
64249b48fb4bSAdrian Chadd 		    __func__,
64259b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
64269b48fb4bSAdrian Chadd 		    ":",
64279b48fb4bSAdrian Chadd 		    enable);
6428548a605dSAdrian Chadd 		an->an_tim_set = 0;
64294bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6430548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6431548a605dSAdrian Chadd 	} else if (! an->an_is_powersave) {
6432548a605dSAdrian Chadd 		/*
6433548a605dSAdrian Chadd 		 * disable regardless; the node isn't in powersave now
6434548a605dSAdrian Chadd 		 */
6435548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
64369b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_pwrsave=0, disabling\n",
64379b48fb4bSAdrian Chadd 		    __func__,
64389b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
64399b48fb4bSAdrian Chadd 		    ":",
64409b48fb4bSAdrian Chadd 		    enable);
6441548a605dSAdrian Chadd 		an->an_tim_set = 0;
64424bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6443548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6444548a605dSAdrian Chadd 	} else {
6445548a605dSAdrian Chadd 		/*
6446548a605dSAdrian Chadd 		 * psq disable, node is currently in powersave, node
6447548a605dSAdrian Chadd 		 * software queue isn't empty, so don't clear the TIM bit
6448548a605dSAdrian Chadd 		 * for now.
6449548a605dSAdrian Chadd 		 */
64504bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6451548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
64529b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_swq_depth > 0, ignoring\n",
64539b48fb4bSAdrian Chadd 		    __func__,
64549b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
64559b48fb4bSAdrian Chadd 		    ":",
64569b48fb4bSAdrian Chadd 		    enable);
6457548a605dSAdrian Chadd 		changed = 0;
6458548a605dSAdrian Chadd 	}
6459548a605dSAdrian Chadd 
6460548a605dSAdrian Chadd 	return (changed);
6461bdbb6e5bSAdrian Chadd #else
6462bdbb6e5bSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6463bdbb6e5bSAdrian Chadd 
646460328038SAdrian Chadd 	/*
6465661c81c3SBaptiste Daroussin 	 * Some operating modes don't set av_set_tim(), so don't
646660328038SAdrian Chadd 	 * update it here.
646760328038SAdrian Chadd 	 */
646860328038SAdrian Chadd 	if (avp->av_set_tim == NULL)
646960328038SAdrian Chadd 		return (0);
647060328038SAdrian Chadd 
6471bdbb6e5bSAdrian Chadd 	return (avp->av_set_tim(ni, enable));
6472bdbb6e5bSAdrian Chadd #endif /* ATH_SW_PSQ */
6473548a605dSAdrian Chadd }
6474548a605dSAdrian Chadd 
6475548a605dSAdrian Chadd /*
6476548a605dSAdrian Chadd  * Set or update the TIM from the software queue.
6477548a605dSAdrian Chadd  *
6478548a605dSAdrian Chadd  * Check the software queue depth before attempting to do lock
6479548a605dSAdrian Chadd  * anything; that avoids trying to obtain the lock.  Then,
6480548a605dSAdrian Chadd  * re-check afterwards to ensure nothing has changed in the
6481548a605dSAdrian Chadd  * meantime.
6482548a605dSAdrian Chadd  *
6483548a605dSAdrian Chadd  * set:   This is designed to be called from the TX path, after
6484548a605dSAdrian Chadd  *        a frame has been queued; to see if the swq > 0.
6485548a605dSAdrian Chadd  *
6486548a605dSAdrian Chadd  * clear: This is designed to be called from the buffer completion point
6487548a605dSAdrian Chadd  *        (right now it's ath_tx_default_comp()) where the state of
6488548a605dSAdrian Chadd  *        a software queue has changed.
6489548a605dSAdrian Chadd  *
6490548a605dSAdrian Chadd  * It makes sense to place it at buffer free / completion rather
6491548a605dSAdrian Chadd  * than after each software queue operation, as there's no real
6492548a605dSAdrian Chadd  * point in churning the TIM bit as the last frames in the software
6493548a605dSAdrian Chadd  * queue are transmitted.  If they fail and we retry them, we'd
6494548a605dSAdrian Chadd  * just be setting the TIM bit again anyway.
6495548a605dSAdrian Chadd  */
6496548a605dSAdrian Chadd void
6497548a605dSAdrian Chadd ath_tx_update_tim(struct ath_softc *sc, struct ieee80211_node *ni,
6498548a605dSAdrian Chadd      int enable)
6499548a605dSAdrian Chadd {
6500bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
6501548a605dSAdrian Chadd 	struct ath_node *an;
6502548a605dSAdrian Chadd 	struct ath_vap *avp;
6503548a605dSAdrian Chadd 
6504548a605dSAdrian Chadd 	/* Don't do this for broadcast/etc frames */
6505548a605dSAdrian Chadd 	if (ni == NULL)
6506548a605dSAdrian Chadd 		return;
6507548a605dSAdrian Chadd 
6508548a605dSAdrian Chadd 	an = ATH_NODE(ni);
6509548a605dSAdrian Chadd 	avp = ATH_VAP(ni->ni_vap);
6510548a605dSAdrian Chadd 
6511548a605dSAdrian Chadd 	/*
6512548a605dSAdrian Chadd 	 * And for operating modes without the TIM handler set, let's
6513548a605dSAdrian Chadd 	 * just skip those.
6514548a605dSAdrian Chadd 	 */
6515548a605dSAdrian Chadd 	if (avp->av_set_tim == NULL)
6516548a605dSAdrian Chadd 		return;
6517548a605dSAdrian Chadd 
65184bed2b67SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
6519548a605dSAdrian Chadd 
6520548a605dSAdrian Chadd 	if (enable) {
6521548a605dSAdrian Chadd 		if (an->an_is_powersave &&
6522548a605dSAdrian Chadd 		    an->an_tim_set == 0 &&
6523ba83edd4SAdrian Chadd 		    an->an_swq_depth != 0) {
6524548a605dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
65259b48fb4bSAdrian Chadd 			    "%s: %6D: swq_depth>0, tim_set=0, set!\n",
65269b48fb4bSAdrian Chadd 			    __func__,
65279b48fb4bSAdrian Chadd 			    ni->ni_macaddr,
65289b48fb4bSAdrian Chadd 			    ":");
6529548a605dSAdrian Chadd 			an->an_tim_set = 1;
6530548a605dSAdrian Chadd 			(void) avp->av_set_tim(ni, 1);
6531548a605dSAdrian Chadd 		}
6532548a605dSAdrian Chadd 	} else {
6533548a605dSAdrian Chadd 		/*
6534548a605dSAdrian Chadd 		 * Don't bother grabbing the lock unless the queue is empty.
6535548a605dSAdrian Chadd 		 */
65363b48f36eSAdrian Chadd 		if (an->an_swq_depth != 0)
6537548a605dSAdrian Chadd 			return;
6538548a605dSAdrian Chadd 
6539548a605dSAdrian Chadd 		if (an->an_is_powersave &&
6540548a605dSAdrian Chadd 		    an->an_stack_psq == 0 &&
6541548a605dSAdrian Chadd 		    an->an_tim_set == 1 &&
6542ba83edd4SAdrian Chadd 		    an->an_swq_depth == 0) {
6543548a605dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
654422a3aee6SAdrian Chadd 			    "%s: %6D: swq_depth=0, tim_set=1, psq_set=0,"
6545548a605dSAdrian Chadd 			    " clear!\n",
654622a3aee6SAdrian Chadd 			    __func__,
654722a3aee6SAdrian Chadd 			    ni->ni_macaddr,
654822a3aee6SAdrian Chadd 			    ":");
6549548a605dSAdrian Chadd 			an->an_tim_set = 0;
6550548a605dSAdrian Chadd 			(void) avp->av_set_tim(ni, 0);
6551548a605dSAdrian Chadd 		}
6552548a605dSAdrian Chadd 	}
6553bdbb6e5bSAdrian Chadd #else
6554bdbb6e5bSAdrian Chadd 	return;
6555bdbb6e5bSAdrian Chadd #endif	/* ATH_SW_PSQ */
6556548a605dSAdrian Chadd }
65570eb81626SAdrian Chadd 
655822a3aee6SAdrian Chadd /*
655922a3aee6SAdrian Chadd  * Received a ps-poll frame from net80211.
656022a3aee6SAdrian Chadd  *
656122a3aee6SAdrian Chadd  * Here we get a chance to serve out a software-queued frame ourselves
656222a3aee6SAdrian Chadd  * before we punt it to net80211 to transmit us one itself - either
656322a3aee6SAdrian Chadd  * because there's traffic in the net80211 psq, or a NULL frame to
656422a3aee6SAdrian Chadd  * indicate there's nothing else.
656522a3aee6SAdrian Chadd  */
656622a3aee6SAdrian Chadd static void
656722a3aee6SAdrian Chadd ath_node_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m)
656822a3aee6SAdrian Chadd {
656922a3aee6SAdrian Chadd #ifdef	ATH_SW_PSQ
657022a3aee6SAdrian Chadd 	struct ath_node *an;
657122a3aee6SAdrian Chadd 	struct ath_vap *avp;
657222a3aee6SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
65733797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
657422a3aee6SAdrian Chadd 	int tid;
657522a3aee6SAdrian Chadd 
657622a3aee6SAdrian Chadd 	/* Just paranoia */
657722a3aee6SAdrian Chadd 	if (ni == NULL)
657822a3aee6SAdrian Chadd 		return;
657922a3aee6SAdrian Chadd 
658022a3aee6SAdrian Chadd 	/*
658122a3aee6SAdrian Chadd 	 * Unassociated (temporary node) station.
658222a3aee6SAdrian Chadd 	 */
658322a3aee6SAdrian Chadd 	if (ni->ni_associd == 0)
658422a3aee6SAdrian Chadd 		return;
658522a3aee6SAdrian Chadd 
658622a3aee6SAdrian Chadd 	/*
658722a3aee6SAdrian Chadd 	 * We do have an active node, so let's begin looking into it.
658822a3aee6SAdrian Chadd 	 */
658922a3aee6SAdrian Chadd 	an = ATH_NODE(ni);
659022a3aee6SAdrian Chadd 	avp = ATH_VAP(ni->ni_vap);
659122a3aee6SAdrian Chadd 
659222a3aee6SAdrian Chadd 	/*
659322a3aee6SAdrian Chadd 	 * For now, we just call the original ps-poll method.
659422a3aee6SAdrian Chadd 	 * Once we're ready to flip this on:
659522a3aee6SAdrian Chadd 	 *
659622a3aee6SAdrian Chadd 	 * + Set leak to 1, as no matter what we're going to have
659722a3aee6SAdrian Chadd 	 *   to send a frame;
659822a3aee6SAdrian Chadd 	 * + Check the software queue and if there's something in it,
659922a3aee6SAdrian Chadd 	 *   schedule the highest TID thas has traffic from this node.
660022a3aee6SAdrian Chadd 	 *   Then make sure we schedule the software scheduler to
660122a3aee6SAdrian Chadd 	 *   run so it picks up said frame.
660222a3aee6SAdrian Chadd 	 *
660322a3aee6SAdrian Chadd 	 * That way whatever happens, we'll at least send _a_ frame
660422a3aee6SAdrian Chadd 	 * to the given node.
660522a3aee6SAdrian Chadd 	 *
660622a3aee6SAdrian Chadd 	 * Again, yes, it's crappy QoS if the node has multiple
660722a3aee6SAdrian Chadd 	 * TIDs worth of traffic - but let's get it working first
660822a3aee6SAdrian Chadd 	 * before we optimise it.
660922a3aee6SAdrian Chadd 	 *
661022a3aee6SAdrian Chadd 	 * Also yes, there's definitely latency here - we're not
661122a3aee6SAdrian Chadd 	 * direct dispatching to the hardware in this path (and
661222a3aee6SAdrian Chadd 	 * we're likely being called from the packet receive path,
661322a3aee6SAdrian Chadd 	 * so going back into TX may be a little hairy!) but again
661422a3aee6SAdrian Chadd 	 * I'd like to get this working first before optimising
661522a3aee6SAdrian Chadd 	 * turn-around time.
661622a3aee6SAdrian Chadd 	 */
661722a3aee6SAdrian Chadd 
661822a3aee6SAdrian Chadd 	ATH_TX_LOCK(sc);
661922a3aee6SAdrian Chadd 
662022a3aee6SAdrian Chadd 	/*
662122a3aee6SAdrian Chadd 	 * Legacy - we're called and the node isn't asleep.
662222a3aee6SAdrian Chadd 	 * Immediately punt.
662322a3aee6SAdrian Chadd 	 */
662422a3aee6SAdrian Chadd 	if (! an->an_is_powersave) {
662583bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
662622a3aee6SAdrian Chadd 		    "%s: %6D: not in powersave?\n",
662722a3aee6SAdrian Chadd 		    __func__,
662822a3aee6SAdrian Chadd 		    ni->ni_macaddr,
662922a3aee6SAdrian Chadd 		    ":");
663022a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
663122a3aee6SAdrian Chadd 		avp->av_recv_pspoll(ni, m);
663222a3aee6SAdrian Chadd 		return;
663322a3aee6SAdrian Chadd 	}
663422a3aee6SAdrian Chadd 
663522a3aee6SAdrian Chadd 	/*
663622a3aee6SAdrian Chadd 	 * We're in powersave.
663722a3aee6SAdrian Chadd 	 *
663822a3aee6SAdrian Chadd 	 * Leak a frame.
663922a3aee6SAdrian Chadd 	 */
664022a3aee6SAdrian Chadd 	an->an_leak_count = 1;
664122a3aee6SAdrian Chadd 
664222a3aee6SAdrian Chadd 	/*
664322a3aee6SAdrian Chadd 	 * Now, if there's no frames in the node, just punt to
664422a3aee6SAdrian Chadd 	 * recv_pspoll.
664522a3aee6SAdrian Chadd 	 *
664622a3aee6SAdrian Chadd 	 * Don't bother checking if the TIM bit is set, we really
664722a3aee6SAdrian Chadd 	 * only care if there are any frames here!
664822a3aee6SAdrian Chadd 	 */
664922a3aee6SAdrian Chadd 	if (an->an_swq_depth == 0) {
665022a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
665122a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
665222a3aee6SAdrian Chadd 		    "%s: %6D: SWQ empty; punting to net80211\n",
665322a3aee6SAdrian Chadd 		    __func__,
665422a3aee6SAdrian Chadd 		    ni->ni_macaddr,
665522a3aee6SAdrian Chadd 		    ":");
665622a3aee6SAdrian Chadd 		avp->av_recv_pspoll(ni, m);
665722a3aee6SAdrian Chadd 		return;
665822a3aee6SAdrian Chadd 	}
665922a3aee6SAdrian Chadd 
666022a3aee6SAdrian Chadd 	/*
666122a3aee6SAdrian Chadd 	 * Ok, let's schedule the highest TID that has traffic
666222a3aee6SAdrian Chadd 	 * and then schedule something.
666322a3aee6SAdrian Chadd 	 */
666422a3aee6SAdrian Chadd 	for (tid = IEEE80211_TID_SIZE - 1; tid >= 0; tid--) {
666522a3aee6SAdrian Chadd 		struct ath_tid *atid = &an->an_tid[tid];
666622a3aee6SAdrian Chadd 		/*
666722a3aee6SAdrian Chadd 		 * No frames? Skip.
666822a3aee6SAdrian Chadd 		 */
666922a3aee6SAdrian Chadd 		if (atid->axq_depth == 0)
667022a3aee6SAdrian Chadd 			continue;
667122a3aee6SAdrian Chadd 		ath_tx_tid_sched(sc, atid);
667222a3aee6SAdrian Chadd 		/*
667322a3aee6SAdrian Chadd 		 * XXX we could do a direct call to the TXQ
667422a3aee6SAdrian Chadd 		 * scheduler code here to optimise latency
667522a3aee6SAdrian Chadd 		 * at the expense of a REALLY deep callstack.
667622a3aee6SAdrian Chadd 		 */
667722a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
667822a3aee6SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_txqtask);
667922a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
668022a3aee6SAdrian Chadd 		    "%s: %6D: leaking frame to TID %d\n",
668122a3aee6SAdrian Chadd 		    __func__,
668222a3aee6SAdrian Chadd 		    ni->ni_macaddr,
668322a3aee6SAdrian Chadd 		    ":",
668422a3aee6SAdrian Chadd 		    tid);
668522a3aee6SAdrian Chadd 		return;
668622a3aee6SAdrian Chadd 	}
668722a3aee6SAdrian Chadd 
668822a3aee6SAdrian Chadd 	ATH_TX_UNLOCK(sc);
668922a3aee6SAdrian Chadd 
669022a3aee6SAdrian Chadd 	/*
669122a3aee6SAdrian Chadd 	 * XXX nothing in the TIDs at this point? Eek.
669222a3aee6SAdrian Chadd 	 */
669383bbd5ebSRui Paulo 	DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
669483bbd5ebSRui Paulo 	    "%s: %6D: TIDs empty, but ath_node showed traffic?!\n",
669522a3aee6SAdrian Chadd 	    __func__,
669622a3aee6SAdrian Chadd 	    ni->ni_macaddr,
669722a3aee6SAdrian Chadd 	    ":");
669822a3aee6SAdrian Chadd 	avp->av_recv_pspoll(ni, m);
669922a3aee6SAdrian Chadd #else
670022a3aee6SAdrian Chadd 	avp->av_recv_pspoll(ni, m);
670122a3aee6SAdrian Chadd #endif	/* ATH_SW_PSQ */
670222a3aee6SAdrian Chadd }
670322a3aee6SAdrian Chadd 
6704dba9c859SAdrian Chadd MODULE_VERSION(if_ath, 1);
6705dba9c859SAdrian Chadd MODULE_DEPEND(if_ath, wlan, 1, 1, 1);          /* 802.11 media layer */
67069389d5a9SAdrian Chadd #if	defined(IEEE80211_ALQ) || defined(AH_DEBUG_ALQ) || defined(ATH_DEBUG_ALQ)
670758816f3fSAdrian Chadd MODULE_DEPEND(if_ath, alq, 1, 1, 1);
670858816f3fSAdrian Chadd #endif
6709