xref: /freebsd/sys/dev/ath/if_ath.c (revision 8c03e55dd250bb0295db659043f69757fec1cf7d)
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.
288*8c03e55dSAdrian Chadd  *
289*8c03e55dSAdrian Chadd  * If the chip is going into network sleep or power off, then
290*8c03e55dSAdrian Chadd  * we will wait until all uses of the chip are done before
291*8c03e55dSAdrian Chadd  * going into network sleep or power off.
292*8c03e55dSAdrian Chadd  *
293*8c03e55dSAdrian Chadd  * If the chip is being programmed full-awake, then immediately
294*8c03e55dSAdrian Chadd  * program it full-awake so we can actually stay awake rather than
295*8c03e55dSAdrian Chadd  * the chip potentially going to sleep underneath us.
296f5c30c4eSAdrian Chadd  */
297f5c30c4eSAdrian Chadd void
298*8c03e55dSAdrian Chadd _ath_power_setpower(struct ath_softc *sc, int power_state, int selfgen,
299*8c03e55dSAdrian Chadd     const char *file, int line)
300f5c30c4eSAdrian Chadd {
301f5c30c4eSAdrian Chadd 	ATH_LOCK_ASSERT(sc);
302f5c30c4eSAdrian Chadd 
303*8c03e55dSAdrian 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,
308*8c03e55dSAdrian Chadd 	    sc->sc_powersave_refcnt,
309*8c03e55dSAdrian Chadd 	    sc->sc_target_powerstate,
310*8c03e55dSAdrian Chadd 	    sc->sc_cur_powerstate);
311f5c30c4eSAdrian Chadd 
312*8c03e55dSAdrian Chadd 	sc->sc_target_powerstate = power_state;
313*8c03e55dSAdrian Chadd 
314*8c03e55dSAdrian Chadd 	/*
315*8c03e55dSAdrian Chadd 	 * Don't program the chip into network sleep if the chip
316*8c03e55dSAdrian Chadd 	 * is being programmed elsewhere.
317*8c03e55dSAdrian Chadd 	 *
318*8c03e55dSAdrian Chadd 	 * However, if the chip is being programmed /awake/, force
319*8c03e55dSAdrian Chadd 	 * the chip awake so we stay awake.
320*8c03e55dSAdrian Chadd 	 */
321*8c03e55dSAdrian 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 		 */
334*8c03e55dSAdrian Chadd 		if (selfgen &&
335*8c03e55dSAdrian 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 
401*8c03e55dSAdrian Chadd 	/*
402*8c03e55dSAdrian Chadd 	 * Only do the power state change if we're not programming
403*8c03e55dSAdrian Chadd 	 * it elsewhere.
404*8c03e55dSAdrian 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);
1341*8c03e55dSAdrian 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);
1383*8c03e55dSAdrian 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;
16419be25f4aSAdrian Chadd 
1642b032f27cSSam Leffler 	avp->av_bslot = -1;
1643b032f27cSSam Leffler 	if (needbeacon) {
1644b032f27cSSam Leffler 		/*
1645b032f27cSSam Leffler 		 * Allocate beacon state and setup the q for buffered
1646b032f27cSSam Leffler 		 * multicast frames.  We know a beacon buffer is
1647b032f27cSSam Leffler 		 * available because we checked above.
1648b032f27cSSam Leffler 		 */
16496b349e5aSAdrian Chadd 		avp->av_bcbuf = TAILQ_FIRST(&sc->sc_bbuf);
16506b349e5aSAdrian Chadd 		TAILQ_REMOVE(&sc->sc_bbuf, avp->av_bcbuf, bf_list);
1651b032f27cSSam Leffler 		if (opmode != IEEE80211_M_IBSS || !sc->sc_hasveol) {
1652b032f27cSSam Leffler 			/*
1653b032f27cSSam Leffler 			 * Assign the vap to a beacon xmit slot.  As above
1654b032f27cSSam Leffler 			 * this cannot fail to find a free one.
1655b032f27cSSam Leffler 			 */
1656b032f27cSSam Leffler 			avp->av_bslot = assign_bslot(sc);
1657b032f27cSSam Leffler 			KASSERT(sc->sc_bslot[avp->av_bslot] == NULL,
1658b032f27cSSam Leffler 			    ("beacon slot %u not empty", avp->av_bslot));
1659b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = vap;
1660b032f27cSSam Leffler 			sc->sc_nbcnvaps++;
1661b032f27cSSam Leffler 		}
1662b032f27cSSam Leffler 		if (sc->sc_hastsfadd && sc->sc_nbcnvaps > 0) {
1663b032f27cSSam Leffler 			/*
1664b032f27cSSam Leffler 			 * Multple vaps are to transmit beacons and we
1665b032f27cSSam Leffler 			 * have h/w support for TSF adjusting; enable
1666b032f27cSSam Leffler 			 * use of staggered beacons.
1667b032f27cSSam Leffler 			 */
1668b032f27cSSam Leffler 			sc->sc_stagbeacons = 1;
1669b032f27cSSam Leffler 		}
1670b032f27cSSam Leffler 		ath_txq_init(sc, &avp->av_mcastq, ATH_TXQ_SWQ);
1671b032f27cSSam Leffler 	}
1672b032f27cSSam Leffler 
1673b032f27cSSam Leffler 	ic->ic_opmode = ic_opmode;
1674b032f27cSSam Leffler 	if (opmode != IEEE80211_M_WDS) {
1675b032f27cSSam Leffler 		sc->sc_nvaps++;
1676b032f27cSSam Leffler 		if (opmode == IEEE80211_M_STA)
1677b032f27cSSam Leffler 			sc->sc_nstavaps++;
1678fe0dd789SSam Leffler 		if (opmode == IEEE80211_M_MBSS)
1679fe0dd789SSam Leffler 			sc->sc_nmeshvaps++;
1680b032f27cSSam Leffler 	}
1681b032f27cSSam Leffler 	switch (ic_opmode) {
1682b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1683b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_IBSS;
1684b032f27cSSam Leffler 		break;
1685b032f27cSSam Leffler 	case IEEE80211_M_STA:
1686b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_STA;
1687b032f27cSSam Leffler 		break;
1688b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1689584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
169010ad9a77SSam Leffler 		if (vap->iv_caps & IEEE80211_C_TDMA) {
169110ad9a77SSam Leffler 			sc->sc_tdma = 1;
169210ad9a77SSam Leffler 			/* NB: disable tsf adjust */
169310ad9a77SSam Leffler 			sc->sc_stagbeacons = 0;
169410ad9a77SSam Leffler 		}
169510ad9a77SSam Leffler 		/*
169610ad9a77SSam Leffler 		 * NB: adhoc demo mode is a pseudo mode; to the hal it's
169710ad9a77SSam Leffler 		 * just ap mode.
169810ad9a77SSam Leffler 		 */
169910ad9a77SSam Leffler 		/* fall thru... */
170010ad9a77SSam Leffler #endif
1701b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
170259aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1703b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_HOSTAP;
1704b032f27cSSam Leffler 		break;
1705b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1706b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_MONITOR;
1707b032f27cSSam Leffler 		break;
1708b032f27cSSam Leffler 	default:
1709b032f27cSSam Leffler 		/* XXX should not happen */
1710b032f27cSSam Leffler 		break;
1711b032f27cSSam Leffler 	}
1712b032f27cSSam Leffler 	if (sc->sc_hastsfadd) {
1713b032f27cSSam Leffler 		/*
1714b032f27cSSam Leffler 		 * Configure whether or not TSF adjust should be done.
1715b032f27cSSam Leffler 		 */
1716b032f27cSSam Leffler 		ath_hal_settsfadjust(sc->sc_ah, sc->sc_stagbeacons);
1717b032f27cSSam Leffler 	}
171810ad9a77SSam Leffler 	if (flags & IEEE80211_CLONE_NOBEACONS) {
171910ad9a77SSam Leffler 		/*
172010ad9a77SSam Leffler 		 * Enable s/w beacon miss handling.
172110ad9a77SSam Leffler 		 */
172210ad9a77SSam Leffler 		sc->sc_swbmiss = 1;
172310ad9a77SSam Leffler 	}
1724b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1725b032f27cSSam Leffler 
1726b032f27cSSam Leffler 	/* complete setup */
17277a79cebfSGleb Smirnoff 	ieee80211_vap_attach(vap, ath_media_change, ieee80211_media_status,
17287a79cebfSGleb Smirnoff 	    mac);
1729b032f27cSSam Leffler 	return vap;
1730b032f27cSSam Leffler bad2:
1731b032f27cSSam Leffler 	reclaim_address(sc, mac);
1732b032f27cSSam Leffler 	ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1733b032f27cSSam Leffler bad:
1734b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1735b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1736b032f27cSSam Leffler 	return NULL;
1737b032f27cSSam Leffler }
1738b032f27cSSam Leffler 
1739b032f27cSSam Leffler static void
1740b032f27cSSam Leffler ath_vap_delete(struct ieee80211vap *vap)
1741b032f27cSSam Leffler {
1742b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
17433797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
1744b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1745b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
1746b032f27cSSam Leffler 
1747f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1748f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1749f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1750f5c30c4eSAdrian Chadd 
1751f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
17527a79cebfSGleb Smirnoff 	if (sc->sc_running) {
1753b032f27cSSam Leffler 		/*
1754b032f27cSSam Leffler 		 * Quiesce the hardware while we remove the vap.  In
1755b032f27cSSam Leffler 		 * particular we need to reclaim all references to
1756b032f27cSSam Leffler 		 * the vap state by any frames pending on the tx queues.
1757b032f27cSSam Leffler 		 */
1758b032f27cSSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
1759517526efSAdrian Chadd 		/* XXX Do all frames from all vaps/nodes need draining here? */
17609a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* stop recv side */
1761062cf7d9SAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);		/* stop hw xmit side */
1762b032f27cSSam Leffler 	}
1763b032f27cSSam Leffler 
1764f5c30c4eSAdrian Chadd 	/* .. leave the hardware awake for now. */
1765f5c30c4eSAdrian Chadd 
1766b032f27cSSam Leffler 	ieee80211_vap_detach(vap);
176716d4de92SAdrian Chadd 
176816d4de92SAdrian Chadd 	/*
176916d4de92SAdrian Chadd 	 * XXX Danger Will Robinson! Danger!
177016d4de92SAdrian Chadd 	 *
177116d4de92SAdrian Chadd 	 * Because ieee80211_vap_detach() can queue a frame (the station
177216d4de92SAdrian Chadd 	 * diassociate message?) after we've drained the TXQ and
177316d4de92SAdrian Chadd 	 * flushed the software TXQ, we will end up with a frame queued
177416d4de92SAdrian Chadd 	 * to a node whose vap is about to be freed.
177516d4de92SAdrian Chadd 	 *
177616d4de92SAdrian Chadd 	 * To work around this, flush the hardware/software again.
177716d4de92SAdrian Chadd 	 * This may be racy - the ath task may be running and the packet
177816d4de92SAdrian Chadd 	 * may be being scheduled between sw->hw txq. Tsk.
177916d4de92SAdrian Chadd 	 *
178016d4de92SAdrian Chadd 	 * TODO: figure out why a new node gets allocated somewhere around
17817a79cebfSGleb Smirnoff 	 * here (after the ath_tx_swq() call; and after an ath_stop()
178216d4de92SAdrian Chadd 	 * call!)
178316d4de92SAdrian Chadd 	 */
178416d4de92SAdrian Chadd 
178516d4de92SAdrian Chadd 	ath_draintxq(sc, ATH_RESET_DEFAULT);
178616d4de92SAdrian Chadd 
1787b032f27cSSam Leffler 	ATH_LOCK(sc);
1788b032f27cSSam Leffler 	/*
1789b032f27cSSam Leffler 	 * Reclaim beacon state.  Note this must be done before
1790b032f27cSSam Leffler 	 * the vap instance is reclaimed as we may have a reference
1791b032f27cSSam Leffler 	 * to it in the buffer for the beacon frame.
1792b032f27cSSam Leffler 	 */
1793b032f27cSSam Leffler 	if (avp->av_bcbuf != NULL) {
1794b032f27cSSam Leffler 		if (avp->av_bslot != -1) {
1795b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = NULL;
1796b032f27cSSam Leffler 			sc->sc_nbcnvaps--;
1797b032f27cSSam Leffler 		}
1798b032f27cSSam Leffler 		ath_beacon_return(sc, avp->av_bcbuf);
1799b032f27cSSam Leffler 		avp->av_bcbuf = NULL;
1800b032f27cSSam Leffler 		if (sc->sc_nbcnvaps == 0) {
1801b032f27cSSam Leffler 			sc->sc_stagbeacons = 0;
1802b032f27cSSam Leffler 			if (sc->sc_hastsfadd)
1803b032f27cSSam Leffler 				ath_hal_settsfadjust(sc->sc_ah, 0);
1804b032f27cSSam Leffler 		}
1805b032f27cSSam Leffler 		/*
1806b032f27cSSam Leffler 		 * Reclaim any pending mcast frames for the vap.
1807b032f27cSSam Leffler 		 */
1808b032f27cSSam Leffler 		ath_tx_draintxq(sc, &avp->av_mcastq);
1809b032f27cSSam Leffler 	}
1810b032f27cSSam Leffler 	/*
1811b032f27cSSam Leffler 	 * Update bookkeeping.
1812b032f27cSSam Leffler 	 */
1813b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
1814b032f27cSSam Leffler 		sc->sc_nstavaps--;
1815b032f27cSSam Leffler 		if (sc->sc_nstavaps == 0 && sc->sc_swbmiss)
1816b032f27cSSam Leffler 			sc->sc_swbmiss = 0;
181759aa14a9SRui Paulo 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
181859aa14a9SRui Paulo 	    vap->iv_opmode == IEEE80211_M_MBSS) {
1819b032f27cSSam Leffler 		reclaim_address(sc, vap->iv_myaddr);
1820b032f27cSSam Leffler 		ath_hal_setbssidmask(ah, sc->sc_hwbssidmask);
1821fe0dd789SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_MBSS)
1822fe0dd789SSam Leffler 			sc->sc_nmeshvaps--;
1823b032f27cSSam Leffler 	}
1824b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_WDS)
1825b032f27cSSam Leffler 		sc->sc_nvaps--;
1826584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
182710ad9a77SSam Leffler 	/* TDMA operation ceases when the last vap is destroyed */
182810ad9a77SSam Leffler 	if (sc->sc_tdma && sc->sc_nvaps == 0) {
182910ad9a77SSam Leffler 		sc->sc_tdma = 0;
183010ad9a77SSam Leffler 		sc->sc_swbmiss = 0;
183110ad9a77SSam Leffler 	}
183210ad9a77SSam Leffler #endif
1833b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1834b032f27cSSam Leffler 
18357a79cebfSGleb Smirnoff 	if (sc->sc_running) {
1836b032f27cSSam Leffler 		/*
1837b032f27cSSam Leffler 		 * Restart rx+tx machines if still running (RUNNING will
1838b032f27cSSam Leffler 		 * be reset if we just destroyed the last vap).
1839b032f27cSSam Leffler 		 */
1840b032f27cSSam Leffler 		if (ath_startrecv(sc) != 0)
184176e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev,
184276e6fd5dSGleb Smirnoff 			    "%s: unable to restart recv logic\n", __func__);
1843c89b957aSSam Leffler 		if (sc->sc_beacons) {		/* restart beacons */
1844c89b957aSSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
1845c89b957aSSam Leffler 			if (sc->sc_tdma)
1846c89b957aSSam Leffler 				ath_tdma_config(sc, NULL);
1847c89b957aSSam Leffler 			else
1848c89b957aSSam Leffler #endif
1849b032f27cSSam Leffler 				ath_beacon_config(sc, NULL);
1850c89b957aSSam Leffler 		}
1851b032f27cSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
1852b032f27cSSam Leffler 	}
1853f5c30c4eSAdrian Chadd 
1854f5c30c4eSAdrian Chadd 	/* Ok, let the hardware asleep. */
1855f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
185616d4de92SAdrian Chadd 	ATH_UNLOCK(sc);
1857b032f27cSSam Leffler }
1858b032f27cSSam Leffler 
18595591b213SSam Leffler void
18605591b213SSam Leffler ath_suspend(struct ath_softc *sc)
18615591b213SSam Leffler {
18627a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
18635591b213SSam Leffler 
18647a79cebfSGleb Smirnoff 	sc->sc_resume_up = ic->ic_nrunning != 0;
1865d1328898SAdrian Chadd 
1866d3ac945bSSam Leffler 	ieee80211_suspend_all(ic);
1867d3ac945bSSam Leffler 	/*
1868d3ac945bSSam Leffler 	 * NB: don't worry about putting the chip in low power
1869d3ac945bSSam Leffler 	 * mode; pci will power off our socket on suspend and
1870f29b8b7fSWarner Losh 	 * CardBus detaches the device.
187117bb5fd1SAdrian Chadd 	 *
187217bb5fd1SAdrian Chadd 	 * XXX TODO: well, that's great, except for non-cardbus
187317bb5fd1SAdrian Chadd 	 * devices!
1874d3ac945bSSam Leffler 	 */
1875d73df6d5SAdrian Chadd 
1876ae2a0aa4SAdrian Chadd 	/*
187717bb5fd1SAdrian Chadd 	 * XXX This doesn't wait until all pending taskqueue
187817bb5fd1SAdrian Chadd 	 * items and parallel transmit/receive/other threads
187917bb5fd1SAdrian Chadd 	 * are running!
188017bb5fd1SAdrian Chadd 	 */
188117bb5fd1SAdrian Chadd 	ath_hal_intrset(sc->sc_ah, 0);
188217bb5fd1SAdrian Chadd 	taskqueue_block(sc->sc_tq);
18837707f31dSAdrian Chadd 
18847707f31dSAdrian Chadd 	ATH_LOCK(sc);
18857707f31dSAdrian Chadd 	callout_stop(&sc->sc_cal_ch);
18867707f31dSAdrian Chadd 	ATH_UNLOCK(sc);
188717bb5fd1SAdrian Chadd 
188817bb5fd1SAdrian Chadd 	/*
1889ae2a0aa4SAdrian Chadd 	 * XXX ensure sc_invalid is 1
1890ae2a0aa4SAdrian Chadd 	 */
1891ae2a0aa4SAdrian Chadd 
1892ae2a0aa4SAdrian Chadd 	/* Disable the PCIe PHY, complete with workarounds */
1893ae2a0aa4SAdrian Chadd 	ath_hal_enablepcie(sc->sc_ah, 1, 1);
1894d3ac945bSSam Leffler }
1895d3ac945bSSam Leffler 
1896d3ac945bSSam Leffler /*
1897d3ac945bSSam Leffler  * Reset the key cache since some parts do not reset the
1898d3ac945bSSam Leffler  * contents on resume.  First we clear all entries, then
1899d3ac945bSSam Leffler  * re-load keys that the 802.11 layer assumes are setup
1900d3ac945bSSam Leffler  * in h/w.
1901d3ac945bSSam Leffler  */
1902d3ac945bSSam Leffler static void
1903d3ac945bSSam Leffler ath_reset_keycache(struct ath_softc *sc)
1904d3ac945bSSam Leffler {
19057a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
1906d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1907d3ac945bSSam Leffler 	int i;
1908d3ac945bSSam Leffler 
1909f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1910f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1911d3ac945bSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
1912d3ac945bSSam Leffler 		ath_hal_keyreset(ah, i);
1913f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
1914f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1915d3ac945bSSam Leffler 	ieee80211_crypto_reload_keys(ic);
19165591b213SSam Leffler }
19175591b213SSam Leffler 
19186322256bSAdrian Chadd /*
19196322256bSAdrian Chadd  * Fetch the current chainmask configuration based on the current
19206322256bSAdrian Chadd  * operating channel and options.
19216322256bSAdrian Chadd  */
19226322256bSAdrian Chadd static void
19236322256bSAdrian Chadd ath_update_chainmasks(struct ath_softc *sc, struct ieee80211_channel *chan)
19246322256bSAdrian Chadd {
19256322256bSAdrian Chadd 
19266322256bSAdrian Chadd 	/*
19276322256bSAdrian Chadd 	 * Set TX chainmask to the currently configured chainmask;
19286322256bSAdrian Chadd 	 * the TX chainmask depends upon the current operating mode.
19296322256bSAdrian Chadd 	 */
19306322256bSAdrian Chadd 	sc->sc_cur_rxchainmask = sc->sc_rxchainmask;
19316322256bSAdrian Chadd 	if (IEEE80211_IS_CHAN_HT(chan)) {
19326322256bSAdrian Chadd 		sc->sc_cur_txchainmask = sc->sc_txchainmask;
19336322256bSAdrian Chadd 	} else {
19346322256bSAdrian Chadd 		sc->sc_cur_txchainmask = 1;
19356322256bSAdrian Chadd 	}
19367904f516SAdrian Chadd 
19377904f516SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
19387904f516SAdrian Chadd 	    "%s: TX chainmask is now 0x%x, RX is now 0x%x\n",
19397904f516SAdrian Chadd 	    __func__,
19407904f516SAdrian Chadd 	    sc->sc_cur_txchainmask,
19417904f516SAdrian Chadd 	    sc->sc_cur_rxchainmask);
19426322256bSAdrian Chadd }
19436322256bSAdrian Chadd 
19445591b213SSam Leffler void
19455591b213SSam Leffler ath_resume(struct ath_softc *sc)
19465591b213SSam Leffler {
19477a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
1948d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1949d3ac945bSSam Leffler 	HAL_STATUS status;
19505591b213SSam Leffler 
1951ae2a0aa4SAdrian Chadd 	ath_hal_enablepcie(ah, 0, 0);
1952d73df6d5SAdrian Chadd 
1953d3ac945bSSam Leffler 	/*
1954d3ac945bSSam Leffler 	 * Must reset the chip before we reload the
1955d3ac945bSSam Leffler 	 * keycache as we were powered down on suspend.
1956d3ac945bSSam Leffler 	 */
19576322256bSAdrian Chadd 	ath_update_chainmasks(sc,
19586322256bSAdrian Chadd 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan);
19596322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
19606322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
1961f5c30c4eSAdrian Chadd 
1962f5c30c4eSAdrian Chadd 	/* Ensure we set the current power state to on */
1963f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
19647d567ed6SAdrian Chadd 	ath_power_setselfgen(sc, HAL_PM_AWAKE);
1965f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1966*8c03e55dSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_AWAKE, 1);
1967f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1968f5c30c4eSAdrian Chadd 
1969054d7b69SSam Leffler 	ath_hal_reset(ah, sc->sc_opmode,
1970054d7b69SSam Leffler 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan,
1971f50e4ebfSAdrian Chadd 	    AH_FALSE, HAL_RESET_NORMAL, &status);
1972d3ac945bSSam Leffler 	ath_reset_keycache(sc);
19737e5eb44dSAdrian Chadd 
197417bb5fd1SAdrian Chadd 	ATH_RX_LOCK(sc);
197517bb5fd1SAdrian Chadd 	sc->sc_rx_stopped = 1;
197617bb5fd1SAdrian Chadd 	sc->sc_rx_resetted = 1;
197717bb5fd1SAdrian Chadd 	ATH_RX_UNLOCK(sc);
197817bb5fd1SAdrian Chadd 
19797e5eb44dSAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
19807e5eb44dSAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
19817e5eb44dSAdrian Chadd 
19829af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
19839af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
19849af351f9SAdrian Chadd 
1985dd6a574eSAdrian Chadd 	/*
1986b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
1987b70f530bSAdrian Chadd 	 */
1988b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
1989b70f530bSAdrian Chadd 
1990b70f530bSAdrian Chadd 	/*
1991dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
1992dd6a574eSAdrian Chadd 	 * support it.
1993dd6a574eSAdrian Chadd 	 */
1994dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
1995dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
1996dd6a574eSAdrian Chadd 	else
1997dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
1998dd6a574eSAdrian Chadd 
1999a497cd88SAdrian Chadd 	/* Restore the LED configuration */
2000a497cd88SAdrian Chadd 	ath_led_config(sc);
2001a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
2002a497cd88SAdrian Chadd 
2003d1328898SAdrian Chadd 	if (sc->sc_resume_up)
2004021a0db5SAdrian Chadd 		ieee80211_resume_all(ic);
20052fd9aabbSAdrian Chadd 
2006f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2007f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2008f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2009f5c30c4eSAdrian Chadd 
20102fd9aabbSAdrian Chadd 	/* XXX beacons ? */
20116b59f5e3SSam Leffler }
20125591b213SSam Leffler 
20135591b213SSam Leffler void
20145591b213SSam Leffler ath_shutdown(struct ath_softc *sc)
20155591b213SSam Leffler {
20165591b213SSam Leffler 
20177a79cebfSGleb Smirnoff 	ATH_LOCK(sc);
20187a79cebfSGleb Smirnoff 	ath_stop(sc);
20197a79cebfSGleb Smirnoff 	ATH_UNLOCK(sc);
2020d3ac945bSSam Leffler 	/* NB: no point powering down chip as we're about to reboot */
20215591b213SSam Leffler }
20225591b213SSam Leffler 
2023c42a7b7eSSam Leffler /*
2024c42a7b7eSSam Leffler  * Interrupt handler.  Most of the actual processing is deferred.
2025c42a7b7eSSam Leffler  */
20265591b213SSam Leffler void
20275591b213SSam Leffler ath_intr(void *arg)
20285591b213SSam Leffler {
20295591b213SSam Leffler 	struct ath_softc *sc = arg;
20305591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
20316f5fe81eSAdrian Chadd 	HAL_INT status = 0;
20328f939e79SAdrian Chadd 	uint32_t txqs;
20335591b213SSam Leffler 
2034ef27340cSAdrian Chadd 	/*
2035ef27340cSAdrian Chadd 	 * If we're inside a reset path, just print a warning and
2036ef27340cSAdrian Chadd 	 * clear the ISR. The reset routine will finish it for us.
2037ef27340cSAdrian Chadd 	 */
2038ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2039ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt) {
2040ef27340cSAdrian Chadd 		HAL_INT status;
2041ef27340cSAdrian Chadd 		ath_hal_getisr(ah, &status);	/* clear ISR */
2042ef27340cSAdrian Chadd 		ath_hal_intrset(ah, 0);		/* disable further intr's */
2043ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_ANY,
2044ef27340cSAdrian Chadd 		    "%s: in reset, ignoring: status=0x%x\n",
2045ef27340cSAdrian Chadd 		    __func__, status);
2046ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2047ef27340cSAdrian Chadd 		return;
2048ef27340cSAdrian Chadd 	}
2049ef27340cSAdrian Chadd 
20505591b213SSam Leffler 	if (sc->sc_invalid) {
20515591b213SSam Leffler 		/*
2052b58b3803SSam Leffler 		 * The hardware is not ready/present, don't touch anything.
2053b58b3803SSam Leffler 		 * Note this can happen early on if the IRQ is shared.
20545591b213SSam Leffler 		 */
2055c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
2056ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
20575591b213SSam Leffler 		return;
20585591b213SSam Leffler 	}
2059ef27340cSAdrian Chadd 	if (!ath_hal_intrpend(ah)) {		/* shared irq, not for us */
2060ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2061fdd758d4SSam Leffler 		return;
2062ef27340cSAdrian Chadd 	}
2063ef27340cSAdrian Chadd 
2064f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2065f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2066f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2067f5c30c4eSAdrian Chadd 
20687a79cebfSGleb Smirnoff 	if (sc->sc_ic.ic_nrunning == 0 && sc->sc_running == 0) {
206968e8e04eSSam Leffler 		HAL_INT status;
207068e8e04eSSam Leffler 
20717a79cebfSGleb Smirnoff 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: ic_nrunning %d sc_running %d\n",
20727a79cebfSGleb Smirnoff 		    __func__, sc->sc_ic.ic_nrunning, sc->sc_running);
20735591b213SSam Leffler 		ath_hal_getisr(ah, &status);	/* clear ISR */
20745591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable further intr's */
2075ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2076f5c30c4eSAdrian Chadd 
2077f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
2078f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
2079f5c30c4eSAdrian Chadd 		ATH_UNLOCK(sc);
20805591b213SSam Leffler 		return;
20815591b213SSam Leffler 	}
2082ef27340cSAdrian Chadd 
2083c42a7b7eSSam Leffler 	/*
2084c42a7b7eSSam Leffler 	 * Figure out the reason(s) for the interrupt.  Note
2085c42a7b7eSSam Leffler 	 * that the hal returns a pseudo-ISR that may include
2086c42a7b7eSSam Leffler 	 * bits we haven't explicitly enabled so we mask the
2087c42a7b7eSSam Leffler 	 * value to insure we only process bits we requested.
2088c42a7b7eSSam Leffler 	 */
20895591b213SSam Leffler 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
2090c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
209103682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 1, "ath_intr: mask=0x%.8x", status);
2092a26f3327SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
2093a26f3327SAdrian Chadd 	if_ath_alq_post_intr(&sc->sc_alq, status, ah->ah_intrstate,
2094a26f3327SAdrian Chadd 	    ah->ah_syncstate);
2095a26f3327SAdrian Chadd #endif	/* ATH_DEBUG_ALQ */
209631fdf3d6SAdrian Chadd #ifdef	ATH_KTR_INTR_DEBUG
209703682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 5,
2098f52d3452SAdrian Chadd 	    "ath_intr: ISR=0x%.8x, ISR_S0=0x%.8x, ISR_S1=0x%.8x, ISR_S2=0x%.8x, ISR_S5=0x%.8x",
2099f52d3452SAdrian Chadd 	    ah->ah_intrstate[0],
2100f52d3452SAdrian Chadd 	    ah->ah_intrstate[1],
2101f52d3452SAdrian Chadd 	    ah->ah_intrstate[2],
2102f52d3452SAdrian Chadd 	    ah->ah_intrstate[3],
2103f52d3452SAdrian Chadd 	    ah->ah_intrstate[6]);
210431fdf3d6SAdrian Chadd #endif
21059467e3f3SAdrian Chadd 
21069467e3f3SAdrian Chadd 	/* Squirrel away SYNC interrupt debugging */
21079467e3f3SAdrian Chadd 	if (ah->ah_syncstate != 0) {
21089467e3f3SAdrian Chadd 		int i;
21099467e3f3SAdrian Chadd 		for (i = 0; i < 32; i++)
21109467e3f3SAdrian Chadd 			if (ah->ah_syncstate & (i << i))
21119467e3f3SAdrian Chadd 				sc->sc_intr_stats.sync_intr[i]++;
21129467e3f3SAdrian Chadd 	}
21139467e3f3SAdrian Chadd 
2114ecddff40SSam Leffler 	status &= sc->sc_imask;			/* discard unasked for bits */
21156f5fe81eSAdrian Chadd 
21166f5fe81eSAdrian Chadd 	/* Short-circuit un-handled interrupts */
2117ef27340cSAdrian Chadd 	if (status == 0x0) {
2118ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2119f5c30c4eSAdrian Chadd 
2120f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
2121f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
2122f5c30c4eSAdrian Chadd 		ATH_UNLOCK(sc);
2123f5c30c4eSAdrian Chadd 
21246f5fe81eSAdrian Chadd 		return;
2125ef27340cSAdrian Chadd 	}
21266f5fe81eSAdrian Chadd 
2127ef27340cSAdrian Chadd 	/*
2128ef27340cSAdrian Chadd 	 * Take a note that we're inside the interrupt handler, so
2129ef27340cSAdrian Chadd 	 * the reset routines know to wait.
2130ef27340cSAdrian Chadd 	 */
2131ef27340cSAdrian Chadd 	sc->sc_intr_cnt++;
2132ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2133ef27340cSAdrian Chadd 
2134ef27340cSAdrian Chadd 	/*
2135ef27340cSAdrian Chadd 	 * Handle the interrupt. We won't run concurrent with the reset
2136ef27340cSAdrian Chadd 	 * or channel change routines as they'll wait for sc_intr_cnt
2137ef27340cSAdrian Chadd 	 * to be 0 before continuing.
2138ef27340cSAdrian Chadd 	 */
21395591b213SSam Leffler 	if (status & HAL_INT_FATAL) {
21405591b213SSam Leffler 		sc->sc_stats.ast_hardware++;
21415591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
2142f846cf42SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_fataltask);
21435591b213SSam Leffler 	} else {
2144c42a7b7eSSam Leffler 		if (status & HAL_INT_SWBA) {
2145c42a7b7eSSam Leffler 			/*
2146c42a7b7eSSam Leffler 			 * Software beacon alert--time to send a beacon.
2147c42a7b7eSSam Leffler 			 * Handle beacon transmission directly; deferring
2148c42a7b7eSSam Leffler 			 * this is too slow to meet timing constraints
2149c42a7b7eSSam Leffler 			 * under load.
2150c42a7b7eSSam Leffler 			 */
2151584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
215210ad9a77SSam Leffler 			if (sc->sc_tdma) {
215310ad9a77SSam Leffler 				if (sc->sc_tdmaswba == 0) {
21547a79cebfSGleb Smirnoff 					struct ieee80211com *ic = &sc->sc_ic;
215510ad9a77SSam Leffler 					struct ieee80211vap *vap =
215610ad9a77SSam Leffler 					    TAILQ_FIRST(&ic->ic_vaps);
215710ad9a77SSam Leffler 					ath_tdma_beacon_send(sc, vap);
215810ad9a77SSam Leffler 					sc->sc_tdmaswba =
215910ad9a77SSam Leffler 					    vap->iv_tdma->tdma_bintval;
216010ad9a77SSam Leffler 				} else
216110ad9a77SSam Leffler 					sc->sc_tdmaswba--;
216210ad9a77SSam Leffler 			} else
216310ad9a77SSam Leffler #endif
2164339ccfb3SSam Leffler 			{
2165c42a7b7eSSam Leffler 				ath_beacon_proc(sc, 0);
2166339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
2167339ccfb3SSam Leffler 				/*
2168339ccfb3SSam Leffler 				 * Schedule the rx taskq in case there's no
2169339ccfb3SSam Leffler 				 * traffic so any frames held on the staging
2170339ccfb3SSam Leffler 				 * queue are aged and potentially flushed.
2171339ccfb3SSam Leffler 				 */
2172f0db652cSAdrian Chadd 				sc->sc_rx.recv_sched(sc, 1);
2173339ccfb3SSam Leffler #endif
2174339ccfb3SSam Leffler 			}
2175c42a7b7eSSam Leffler 		}
21765591b213SSam Leffler 		if (status & HAL_INT_RXEOL) {
21778f939e79SAdrian Chadd 			int imask;
217803682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXEOL");
217917bb5fd1SAdrian Chadd 			if (! sc->sc_isedma) {
2180ef27340cSAdrian Chadd 				ATH_PCU_LOCK(sc);
21815591b213SSam Leffler 				/*
21825591b213SSam Leffler 				 * NB: the hardware should re-read the link when
21835591b213SSam Leffler 				 *     RXE bit is written, but it doesn't work at
21845591b213SSam Leffler 				 *     least on older hardware revs.
21855591b213SSam Leffler 				 */
21865591b213SSam Leffler 				sc->sc_stats.ast_rxeol++;
218773f895fcSAdrian Chadd 				/*
218873f895fcSAdrian Chadd 				 * Disable RXEOL/RXORN - prevent an interrupt
218973f895fcSAdrian Chadd 				 * storm until the PCU logic can be reset.
21901fdadc0fSAdrian Chadd 				 * In case the interface is reset some other
21911fdadc0fSAdrian Chadd 				 * way before "sc_kickpcu" is called, don't
21921fdadc0fSAdrian Chadd 				 * modify sc_imask - that way if it is reset
21931fdadc0fSAdrian Chadd 				 * by a call to ath_reset() somehow, the
21941fdadc0fSAdrian Chadd 				 * interrupt mask will be correctly reprogrammed.
219573f895fcSAdrian Chadd 				 */
21968f939e79SAdrian Chadd 				imask = sc->sc_imask;
21971fdadc0fSAdrian Chadd 				imask &= ~(HAL_INT_RXEOL | HAL_INT_RXORN);
21981fdadc0fSAdrian Chadd 				ath_hal_intrset(ah, imask);
21991fdadc0fSAdrian Chadd 				/*
22008f939e79SAdrian Chadd 				 * Only blank sc_rxlink if we've not yet kicked
22018f939e79SAdrian Chadd 				 * the PCU.
22028f939e79SAdrian Chadd 				 *
22038f939e79SAdrian Chadd 				 * This isn't entirely correct - the correct solution
22048f939e79SAdrian Chadd 				 * would be to have a PCU lock and engage that for
22058f939e79SAdrian Chadd 				 * the duration of the PCU fiddling; which would include
22068f939e79SAdrian Chadd 				 * running the RX process. Otherwise we could end up
22078f939e79SAdrian Chadd 				 * messing up the RX descriptor chain and making the
22088f939e79SAdrian Chadd 				 * RX desc list much shorter.
22098f939e79SAdrian Chadd 				 */
22108f939e79SAdrian Chadd 				if (! sc->sc_kickpcu)
22118f939e79SAdrian Chadd 					sc->sc_rxlink = NULL;
22128f939e79SAdrian Chadd 				sc->sc_kickpcu = 1;
2213f0db652cSAdrian Chadd 				ATH_PCU_UNLOCK(sc);
221417bb5fd1SAdrian Chadd 			}
22158f939e79SAdrian Chadd 			/*
221617bb5fd1SAdrian Chadd 			 * Enqueue an RX proc to handle whatever
22171fdadc0fSAdrian Chadd 			 * is in the RX queue.
221817bb5fd1SAdrian Chadd 			 * This will then kick the PCU if required.
22191fdadc0fSAdrian Chadd 			 */
2220f0db652cSAdrian Chadd 			sc->sc_rx.recv_sched(sc, 1);
22215591b213SSam Leffler 		}
22225591b213SSam Leffler 		if (status & HAL_INT_TXURN) {
22235591b213SSam Leffler 			sc->sc_stats.ast_txurn++;
22245591b213SSam Leffler 			/* bump tx trigger level */
22255591b213SSam Leffler 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
22265591b213SSam Leffler 		}
2227bcbb08ceSAdrian Chadd 		/*
2228bcbb08ceSAdrian Chadd 		 * Handle both the legacy and RX EDMA interrupt bits.
2229bcbb08ceSAdrian Chadd 		 * Note that HAL_INT_RXLP is also HAL_INT_RXDESC.
2230bcbb08ceSAdrian Chadd 		 */
2231bcbb08ceSAdrian Chadd 		if (status & (HAL_INT_RX | HAL_INT_RXHP | HAL_INT_RXLP)) {
22328f939e79SAdrian Chadd 			sc->sc_stats.ast_rx_intr++;
2233f0db652cSAdrian Chadd 			sc->sc_rx.recv_sched(sc, 1);
22348f939e79SAdrian Chadd 		}
22358f939e79SAdrian Chadd 		if (status & HAL_INT_TX) {
22368f939e79SAdrian Chadd 			sc->sc_stats.ast_tx_intr++;
22378f939e79SAdrian Chadd 			/*
22388f939e79SAdrian Chadd 			 * Grab all the currently set bits in the HAL txq bitmap
22398f939e79SAdrian Chadd 			 * and blank them. This is the only place we should be
22408f939e79SAdrian Chadd 			 * doing this.
22418f939e79SAdrian Chadd 			 */
2242bad98824SAdrian Chadd 			if (! sc->sc_isedma) {
2243ef27340cSAdrian Chadd 				ATH_PCU_LOCK(sc);
22448f939e79SAdrian Chadd 				txqs = 0xffffffff;
22458f939e79SAdrian Chadd 				ath_hal_gettxintrtxqs(sc->sc_ah, &txqs);
224603682514SAdrian Chadd 				ATH_KTR(sc, ATH_KTR_INTERRUPTS, 3,
224703682514SAdrian Chadd 				    "ath_intr: TX; txqs=0x%08x, txq_active was 0x%08x, now 0x%08x",
224803682514SAdrian Chadd 				    txqs,
224903682514SAdrian Chadd 				    sc->sc_txq_active,
225003682514SAdrian Chadd 				    sc->sc_txq_active | txqs);
22518f939e79SAdrian Chadd 				sc->sc_txq_active |= txqs;
2252ef27340cSAdrian Chadd 				ATH_PCU_UNLOCK(sc);
22538f939e79SAdrian Chadd 			}
2254bad98824SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask);
2255bad98824SAdrian Chadd 		}
22565591b213SSam Leffler 		if (status & HAL_INT_BMISS) {
22575591b213SSam Leffler 			sc->sc_stats.ast_bmiss++;
22580bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask);
22595591b213SSam Leffler 		}
22606ad02dbaSAdrian Chadd 		if (status & HAL_INT_GTT)
22616ad02dbaSAdrian Chadd 			sc->sc_stats.ast_tx_timeout++;
22625594f5c0SAdrian Chadd 		if (status & HAL_INT_CST)
22635594f5c0SAdrian Chadd 			sc->sc_stats.ast_tx_cst++;
2264c42a7b7eSSam Leffler 		if (status & HAL_INT_MIB) {
2265c42a7b7eSSam Leffler 			sc->sc_stats.ast_mib++;
2266ef27340cSAdrian Chadd 			ATH_PCU_LOCK(sc);
2267c42a7b7eSSam Leffler 			/*
2268c42a7b7eSSam Leffler 			 * Disable interrupts until we service the MIB
2269c42a7b7eSSam Leffler 			 * interrupt; otherwise it will continue to fire.
2270c42a7b7eSSam Leffler 			 */
2271c42a7b7eSSam Leffler 			ath_hal_intrset(ah, 0);
2272c42a7b7eSSam Leffler 			/*
2273c42a7b7eSSam Leffler 			 * Let the hal handle the event.  We assume it will
2274c42a7b7eSSam Leffler 			 * clear whatever condition caused the interrupt.
2275c42a7b7eSSam Leffler 			 */
2276ffa2cab6SSam Leffler 			ath_hal_mibevent(ah, &sc->sc_halstats);
22778f939e79SAdrian Chadd 			/*
22788f939e79SAdrian Chadd 			 * Don't reset the interrupt if we've just
22798f939e79SAdrian Chadd 			 * kicked the PCU, or we may get a nested
22808f939e79SAdrian Chadd 			 * RXEOL before the rxproc has had a chance
22818f939e79SAdrian Chadd 			 * to run.
22828f939e79SAdrian Chadd 			 */
22838f939e79SAdrian Chadd 			if (sc->sc_kickpcu == 0)
2284c42a7b7eSSam Leffler 				ath_hal_intrset(ah, sc->sc_imask);
2285ef27340cSAdrian Chadd 			ATH_PCU_UNLOCK(sc);
2286c42a7b7eSSam Leffler 		}
22879c4fc1e8SSam Leffler 		if (status & HAL_INT_RXORN) {
22889c4fc1e8SSam Leffler 			/* NB: hal marks HAL_INT_FATAL when RXORN is fatal */
228903682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXORN");
22909c4fc1e8SSam Leffler 			sc->sc_stats.ast_rxorn++;
22919c4fc1e8SSam Leffler 		}
2292f5c30c4eSAdrian Chadd 		if (status & HAL_INT_TSFOOR) {
2293*8c03e55dSAdrian Chadd 			/* out of range beacon - wake the chip up,
2294*8c03e55dSAdrian Chadd 			 * but don't modify self-gen frame config */
2295f5c30c4eSAdrian Chadd 			device_printf(sc->sc_dev, "%s: TSFOOR\n", __func__);
2296f5c30c4eSAdrian Chadd 			sc->sc_syncbeacon = 1;
2297*8c03e55dSAdrian Chadd 			ATH_LOCK(sc);
2298*8c03e55dSAdrian Chadd 			ath_power_setpower(sc, HAL_PM_AWAKE, 0);
2299*8c03e55dSAdrian Chadd 			ATH_UNLOCK(sc);
2300f5c30c4eSAdrian Chadd 		}
2301bcf5fc49SAdrian Chadd 		if (status & HAL_INT_MCI) {
2302bcf5fc49SAdrian Chadd 			ath_btcoex_mci_intr(sc);
2303bcf5fc49SAdrian Chadd 		}
23045591b213SSam Leffler 	}
2305ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2306ef27340cSAdrian Chadd 	sc->sc_intr_cnt--;
2307ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2308f5c30c4eSAdrian Chadd 
2309f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2310f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2311f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
23125591b213SSam Leffler }
23135591b213SSam Leffler 
23145591b213SSam Leffler static void
23155591b213SSam Leffler ath_fatal_proc(void *arg, int pending)
23165591b213SSam Leffler {
23175591b213SSam Leffler 	struct ath_softc *sc = arg;
231816c8acaaSSam Leffler 	u_int32_t *state;
231916c8acaaSSam Leffler 	u_int32_t len;
232068e8e04eSSam Leffler 	void *sp;
23215591b213SSam Leffler 
232270c81b20SAdrian Chadd 	if (sc->sc_invalid)
232370c81b20SAdrian Chadd 		return;
232470c81b20SAdrian Chadd 
232576e6fd5dSGleb Smirnoff 	device_printf(sc->sc_dev, "hardware error; resetting\n");
232616c8acaaSSam Leffler 	/*
232716c8acaaSSam Leffler 	 * Fatal errors are unrecoverable.  Typically these
232816c8acaaSSam Leffler 	 * are caused by DMA errors.  Collect h/w state from
232916c8acaaSSam Leffler 	 * the hal so we can diagnose what's going on.
233016c8acaaSSam Leffler 	 */
233168e8e04eSSam Leffler 	if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) {
233216c8acaaSSam Leffler 		KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len));
233368e8e04eSSam Leffler 		state = sp;
233476e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
233576e6fd5dSGleb Smirnoff 		    "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n", state[0],
233676e6fd5dSGleb Smirnoff 		    state[1] , state[2], state[3], state[4], state[5]);
233716c8acaaSSam Leffler 	}
23387a79cebfSGleb Smirnoff 	ath_reset(sc, ATH_RESET_NOLOSS);
23395591b213SSam Leffler }
23405591b213SSam Leffler 
23415591b213SSam Leffler static void
2342b032f27cSSam Leffler ath_bmiss_vap(struct ieee80211vap *vap)
23435591b213SSam Leffler {
23443797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
2345f5c30c4eSAdrian Chadd 
234659fbb257SSam Leffler 	/*
234759fbb257SSam Leffler 	 * Workaround phantom bmiss interrupts by sanity-checking
234859fbb257SSam Leffler 	 * the time of our last rx'd frame.  If it is within the
234959fbb257SSam Leffler 	 * beacon miss interval then ignore the interrupt.  If it's
235059fbb257SSam Leffler 	 * truly a bmiss we'll get another interrupt soon and that'll
235159fbb257SSam Leffler 	 * be dispatched up for processing.  Note this applies only
235259fbb257SSam Leffler 	 * for h/w beacon miss events.
235359fbb257SSam Leffler 	 */
2354f5c30c4eSAdrian Chadd 
2355f5c30c4eSAdrian Chadd 	/*
2356f5c30c4eSAdrian Chadd 	 * XXX TODO: Just read the TSF during the interrupt path;
2357f5c30c4eSAdrian Chadd 	 * that way we don't have to wake up again just to read it
2358f5c30c4eSAdrian Chadd 	 * again.
2359f5c30c4eSAdrian Chadd 	 */
2360f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2361f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2362f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2363f5c30c4eSAdrian Chadd 
236459fbb257SSam Leffler 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
2365d7736e13SSam Leffler 		u_int64_t lastrx = sc->sc_lastrx;
2366d7736e13SSam Leffler 		u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah);
236780767531SAdrian Chadd 		/* XXX should take a locked ref to iv_bss */
2368d7736e13SSam Leffler 		u_int bmisstimeout =
2369b032f27cSSam Leffler 			vap->iv_bmissthreshold * vap->iv_bss->ni_intval * 1024;
2370d7736e13SSam Leffler 
2371d7736e13SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2372d7736e13SSam Leffler 		    "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n",
2373d7736e13SSam Leffler 		    __func__, (unsigned long long) tsf,
2374d7736e13SSam Leffler 		    (unsigned long long)(tsf - lastrx),
2375d7736e13SSam Leffler 		    (unsigned long long) lastrx, bmisstimeout);
237659fbb257SSam Leffler 
237759fbb257SSam Leffler 		if (tsf - lastrx <= bmisstimeout) {
2378d7736e13SSam Leffler 			sc->sc_stats.ast_bmiss_phantom++;
2379f5c30c4eSAdrian Chadd 
2380f5c30c4eSAdrian Chadd 			ATH_LOCK(sc);
2381f5c30c4eSAdrian Chadd 			ath_power_restore_power_state(sc);
2382f5c30c4eSAdrian Chadd 			ATH_UNLOCK(sc);
2383f5c30c4eSAdrian Chadd 
238459fbb257SSam Leffler 			return;
238559fbb257SSam Leffler 		}
238659fbb257SSam Leffler 	}
2387f5c30c4eSAdrian Chadd 
2388f5c30c4eSAdrian Chadd 	/*
2389*8c03e55dSAdrian Chadd 	 * Keep the hardware awake if it's asleep (and leave self-gen
2390*8c03e55dSAdrian Chadd 	 * frame config alone) until the next beacon, so we can resync
2391*8c03e55dSAdrian Chadd 	 * against the next beacon.
2392*8c03e55dSAdrian Chadd 	 *
2393*8c03e55dSAdrian Chadd 	 * This handles three common beacon miss cases in STA powersave mode -
2394*8c03e55dSAdrian Chadd 	 * (a) the beacon TBTT isnt a multiple of bintval;
2395*8c03e55dSAdrian Chadd 	 * (b) the beacon was missed; and
2396*8c03e55dSAdrian Chadd 	 * (c) the beacons are being delayed because the AP is busy and
2397*8c03e55dSAdrian Chadd 	 *     isn't reliably able to meet its TBTT.
2398f5c30c4eSAdrian Chadd 	 */
2399f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2400*8c03e55dSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_AWAKE, 0);
2401f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2402f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2403*8c03e55dSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_BEACON,
2404*8c03e55dSAdrian Chadd 	    "%s: forced awake; force syncbeacon=1\n", __func__);
2405f5c30c4eSAdrian Chadd 
2406f5c30c4eSAdrian Chadd 	/*
2407f5c30c4eSAdrian Chadd 	 * Attempt to force a beacon resync.
2408f5c30c4eSAdrian Chadd 	 */
2409f5c30c4eSAdrian Chadd 	sc->sc_syncbeacon = 1;
2410f5c30c4eSAdrian Chadd 
241159fbb257SSam Leffler 	ATH_VAP(vap)->av_bmiss(vap);
2412e585d188SSam Leffler }
2413b032f27cSSam Leffler 
2414f5c30c4eSAdrian Chadd /* XXX this needs a force wakeup! */
2415b837332dSAdrian Chadd int
2416459bc4f0SSam Leffler ath_hal_gethangstate(struct ath_hal *ah, uint32_t mask, uint32_t *hangs)
2417459bc4f0SSam Leffler {
2418459bc4f0SSam Leffler 	uint32_t rsize;
2419459bc4f0SSam Leffler 	void *sp;
2420459bc4f0SSam Leffler 
242125c96056SAdrian Chadd 	if (!ath_hal_getdiagstate(ah, HAL_DIAG_CHECK_HANGS, &mask, sizeof(mask), &sp, &rsize))
2422459bc4f0SSam Leffler 		return 0;
2423459bc4f0SSam Leffler 	KASSERT(rsize == sizeof(uint32_t), ("resultsize %u", rsize));
2424459bc4f0SSam Leffler 	*hangs = *(uint32_t *)sp;
2425459bc4f0SSam Leffler 	return 1;
2426459bc4f0SSam Leffler }
2427459bc4f0SSam Leffler 
2428b032f27cSSam Leffler static void
2429b032f27cSSam Leffler ath_bmiss_proc(void *arg, int pending)
2430b032f27cSSam Leffler {
2431b032f27cSSam Leffler 	struct ath_softc *sc = arg;
2432459bc4f0SSam Leffler 	uint32_t hangs;
2433b032f27cSSam Leffler 
2434b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
2435459bc4f0SSam Leffler 
2436f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2437f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2438f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2439f5c30c4eSAdrian Chadd 
2440f5c30c4eSAdrian Chadd 	ath_beacon_miss(sc);
2441f5c30c4eSAdrian Chadd 
2442a74ebfe5SAdrian Chadd 	/*
2443a74ebfe5SAdrian Chadd 	 * Do a reset upon any becaon miss event.
2444a74ebfe5SAdrian Chadd 	 *
2445a74ebfe5SAdrian Chadd 	 * It may be a non-recognised RX clear hang which needs a reset
2446a74ebfe5SAdrian Chadd 	 * to clear.
2447a74ebfe5SAdrian Chadd 	 */
2448459bc4f0SSam Leffler 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) {
24497a79cebfSGleb Smirnoff 		ath_reset(sc, ATH_RESET_NOLOSS);
245076e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
245176e6fd5dSGleb Smirnoff 		    "bb hang detected (0x%x), resetting\n", hangs);
2452a74ebfe5SAdrian Chadd 	} else {
24537a79cebfSGleb Smirnoff 		ath_reset(sc, ATH_RESET_NOLOSS);
24547a79cebfSGleb Smirnoff 		ieee80211_beacon_miss(&sc->sc_ic);
24555591b213SSam Leffler 	}
2456f5c30c4eSAdrian Chadd 
2457f5c30c4eSAdrian Chadd 	/* Force a beacon resync, in case they've drifted */
2458f5c30c4eSAdrian Chadd 	sc->sc_syncbeacon = 1;
2459f5c30c4eSAdrian Chadd 
2460f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2461f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2462f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2463a74ebfe5SAdrian Chadd }
24645591b213SSam Leffler 
2465724c193aSSam Leffler /*
2466b032f27cSSam Leffler  * Handle TKIP MIC setup to deal hardware that doesn't do MIC
2467b032f27cSSam Leffler  * calcs together with WME.  If necessary disable the crypto
2468b032f27cSSam Leffler  * hardware and mark the 802.11 state so keys will be setup
2469b032f27cSSam Leffler  * with the MIC work done in software.
2470b032f27cSSam Leffler  */
2471b032f27cSSam Leffler static void
2472b032f27cSSam Leffler ath_settkipmic(struct ath_softc *sc)
2473b032f27cSSam Leffler {
24747a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
2475b032f27cSSam Leffler 
2476b032f27cSSam Leffler 	if ((ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIP) && !sc->sc_wmetkipmic) {
2477b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WME) {
2478b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_FALSE);
2479b032f27cSSam Leffler 			ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC;
2480b032f27cSSam Leffler 		} else {
2481b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_TRUE);
2482b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
2483b032f27cSSam Leffler 		}
2484b032f27cSSam Leffler 	}
2485b032f27cSSam Leffler }
2486b032f27cSSam Leffler 
24877a79cebfSGleb Smirnoff static int
24887a79cebfSGleb Smirnoff ath_init(struct ath_softc *sc)
24895591b213SSam Leffler {
24907a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
24915591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
24925591b213SSam Leffler 	HAL_STATUS status;
24935591b213SSam Leffler 
24947a79cebfSGleb Smirnoff 	ATH_LOCK_ASSERT(sc);
24955591b213SSam Leffler 
24965591b213SSam Leffler 	/*
2497f5c30c4eSAdrian Chadd 	 * Force the sleep state awake.
2498f5c30c4eSAdrian Chadd 	 */
24997d567ed6SAdrian Chadd 	ath_power_setselfgen(sc, HAL_PM_AWAKE);
2500f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2501*8c03e55dSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_AWAKE, 1);
2502f5c30c4eSAdrian Chadd 
2503f5c30c4eSAdrian Chadd 	/*
25045591b213SSam Leffler 	 * Stop anything previously setup.  This is safe
25055591b213SSam Leffler 	 * whether this is the first time through or not.
25065591b213SSam Leffler 	 */
25077a79cebfSGleb Smirnoff 	ath_stop(sc);
25085591b213SSam Leffler 
25095591b213SSam Leffler 	/*
25105591b213SSam Leffler 	 * The basic interface to setting the hardware in a good
25115591b213SSam Leffler 	 * state is ``reset''.  On return the hardware is known to
25125591b213SSam Leffler 	 * be powered up and with interrupts disabled.  This must
25135591b213SSam Leffler 	 * be followed by initialization of the appropriate bits
25145591b213SSam Leffler 	 * and then setup of the interrupt mask.
25155591b213SSam Leffler 	 */
2516b032f27cSSam Leffler 	ath_settkipmic(sc);
25176322256bSAdrian Chadd 	ath_update_chainmasks(sc, ic->ic_curchan);
25186322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
25196322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
2520f5c30c4eSAdrian Chadd 
252176e6fd5dSGleb Smirnoff 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_FALSE,
2522f50e4ebfSAdrian Chadd 	    HAL_RESET_NORMAL, &status)) {
252376e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
252476e6fd5dSGleb Smirnoff 		    "unable to reset hardware; hal status %u\n", status);
25257a79cebfSGleb Smirnoff 		return (ENODEV);
25265591b213SSam Leffler 	}
252717bb5fd1SAdrian Chadd 
252817bb5fd1SAdrian Chadd 	ATH_RX_LOCK(sc);
252917bb5fd1SAdrian Chadd 	sc->sc_rx_stopped = 1;
253017bb5fd1SAdrian Chadd 	sc->sc_rx_resetted = 1;
253117bb5fd1SAdrian Chadd 	ATH_RX_UNLOCK(sc);
253217bb5fd1SAdrian Chadd 
2533b032f27cSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
25345591b213SSam Leffler 
253548237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
253648237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
253748237774SAdrian Chadd 
25389af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
25399af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
25409af351f9SAdrian Chadd 
25415591b213SSam Leffler 	/*
2542b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
2543b70f530bSAdrian Chadd 	 */
2544b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
2545b70f530bSAdrian Chadd 
2546b70f530bSAdrian Chadd 	/*
2547dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
2548dd6a574eSAdrian Chadd 	 * support it.
2549dd6a574eSAdrian Chadd 	 */
2550dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
2551dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
2552dd6a574eSAdrian Chadd 	else
2553dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
2554dd6a574eSAdrian Chadd 
2555dd6a574eSAdrian Chadd 	/*
2556c59005e9SSam Leffler 	 * Likewise this is set during reset so update
2557c59005e9SSam Leffler 	 * state cached in the driver.
2558c59005e9SSam Leffler 	 */
2559c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
25609bbfde1eSAdrian Chadd 	sc->sc_lastlongcal = ticks;
25612dc7fcc4SSam Leffler 	sc->sc_resetcal = 1;
25622dc7fcc4SSam Leffler 	sc->sc_lastcalreset = 0;
25639bbfde1eSAdrian Chadd 	sc->sc_lastani = ticks;
25649bbfde1eSAdrian Chadd 	sc->sc_lastshortcal = ticks;
2565a108ab63SAdrian Chadd 	sc->sc_doresetcal = AH_FALSE;
25662fd9aabbSAdrian Chadd 	/*
25672fd9aabbSAdrian Chadd 	 * Beacon timers were cleared here; give ath_newstate()
25682fd9aabbSAdrian Chadd 	 * a hint that the beacon timers should be poked when
25692fd9aabbSAdrian Chadd 	 * things transition to the RUN state.
25702fd9aabbSAdrian Chadd 	 */
25712fd9aabbSAdrian Chadd 	sc->sc_beacons = 0;
2572c42a7b7eSSam Leffler 
2573c42a7b7eSSam Leffler 	/*
25745591b213SSam Leffler 	 * Setup the hardware after reset: the key cache
25755591b213SSam Leffler 	 * is filled as needed and the receive engine is
25765591b213SSam Leffler 	 * set going.  Frame transmit is handled entirely
25775591b213SSam Leffler 	 * in the frame output path; there's nothing to do
25785591b213SSam Leffler 	 * here except setup the interrupt mask.
25795591b213SSam Leffler 	 */
25805591b213SSam Leffler 	if (ath_startrecv(sc) != 0) {
258176e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "unable to start recv logic\n");
2582f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
25837a79cebfSGleb Smirnoff 		return (ENODEV);
25845591b213SSam Leffler 	}
25855591b213SSam Leffler 
25865591b213SSam Leffler 	/*
25875591b213SSam Leffler 	 * Enable interrupts.
25885591b213SSam Leffler 	 */
25895591b213SSam Leffler 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
259017bb5fd1SAdrian Chadd 		  | HAL_INT_RXORN | HAL_INT_TXURN
25915591b213SSam Leffler 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
2592bcbb08ceSAdrian Chadd 
2593bcbb08ceSAdrian Chadd 	/*
2594bcbb08ceSAdrian Chadd 	 * Enable RX EDMA bits.  Note these overlap with
2595bcbb08ceSAdrian Chadd 	 * HAL_INT_RX and HAL_INT_RXDESC respectively.
2596bcbb08ceSAdrian Chadd 	 */
2597bcbb08ceSAdrian Chadd 	if (sc->sc_isedma)
2598bcbb08ceSAdrian Chadd 		sc->sc_imask |= (HAL_INT_RXHP | HAL_INT_RXLP);
2599bcbb08ceSAdrian Chadd 
2600c42a7b7eSSam Leffler 	/*
260117bb5fd1SAdrian Chadd 	 * If we're an EDMA NIC, we don't care about RXEOL.
260217bb5fd1SAdrian Chadd 	 * Writing a new descriptor in will simply restart
260317bb5fd1SAdrian Chadd 	 * RX DMA.
260417bb5fd1SAdrian Chadd 	 */
260517bb5fd1SAdrian Chadd 	if (! sc->sc_isedma)
260617bb5fd1SAdrian Chadd 		sc->sc_imask |= HAL_INT_RXEOL;
260717bb5fd1SAdrian Chadd 
260817bb5fd1SAdrian Chadd 	/*
2609bcf5fc49SAdrian Chadd 	 * Enable MCI interrupt for MCI devices.
2610bcf5fc49SAdrian Chadd 	 */
2611bcf5fc49SAdrian Chadd 	if (sc->sc_btcoex_mci)
2612bcf5fc49SAdrian Chadd 		sc->sc_imask |= HAL_INT_MCI;
2613bcf5fc49SAdrian Chadd 
2614bcf5fc49SAdrian Chadd 	/*
2615c42a7b7eSSam Leffler 	 * Enable MIB interrupts when there are hardware phy counters.
2616c42a7b7eSSam Leffler 	 * Note we only do this (at the moment) for station mode.
2617c42a7b7eSSam Leffler 	 */
2618c42a7b7eSSam Leffler 	if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
2619c42a7b7eSSam Leffler 		sc->sc_imask |= HAL_INT_MIB;
26205591b213SSam Leffler 
2621f5c30c4eSAdrian Chadd 	/*
2622f5c30c4eSAdrian Chadd 	 * XXX add capability for this.
2623f5c30c4eSAdrian Chadd 	 *
2624f5c30c4eSAdrian Chadd 	 * If we're in STA mode (and maybe IBSS?) then register for
2625f5c30c4eSAdrian Chadd 	 * TSFOOR interrupts.
2626f5c30c4eSAdrian Chadd 	 */
2627f5c30c4eSAdrian Chadd 	if (ic->ic_opmode == IEEE80211_M_STA)
2628f5c30c4eSAdrian Chadd 		sc->sc_imask |= HAL_INT_TSFOOR;
2629f5c30c4eSAdrian Chadd 
26305594f5c0SAdrian Chadd 	/* Enable global TX timeout and carrier sense timeout if available */
26316ad02dbaSAdrian Chadd 	if (ath_hal_gtxto_supported(ah))
26323788ebedSAdrian Chadd 		sc->sc_imask |= HAL_INT_GTT;
2633d0a0ebc6SAdrian Chadd 
2634d0a0ebc6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: imask=0x%x\n",
2635d0a0ebc6SAdrian Chadd 		__func__, sc->sc_imask);
26366ad02dbaSAdrian Chadd 
26377a79cebfSGleb Smirnoff 	sc->sc_running = 1;
26382e986da5SSam Leffler 	callout_reset(&sc->sc_wd_ch, hz, ath_watchdog, sc);
2639b032f27cSSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
26405591b213SSam Leffler 
2641f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2642b032f27cSSam Leffler 
26437a79cebfSGleb Smirnoff 	return (0);
26445591b213SSam Leffler }
26455591b213SSam Leffler 
26465591b213SSam Leffler static void
26477a79cebfSGleb Smirnoff ath_stop(struct ath_softc *sc)
26485591b213SSam Leffler {
26495591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
26505591b213SSam Leffler 
2651c42a7b7eSSam Leffler 	ATH_LOCK_ASSERT(sc);
2652f5c30c4eSAdrian Chadd 
2653f5c30c4eSAdrian Chadd 	/*
2654f5c30c4eSAdrian Chadd 	 * Wake the hardware up before fiddling with it.
2655f5c30c4eSAdrian Chadd 	 */
2656f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2657f5c30c4eSAdrian Chadd 
26587a79cebfSGleb Smirnoff 	if (sc->sc_running) {
26595591b213SSam Leffler 		/*
26605591b213SSam Leffler 		 * Shutdown the hardware and driver:
2661c42a7b7eSSam Leffler 		 *    reset 802.11 state machine
26625591b213SSam Leffler 		 *    turn off timers
2663c42a7b7eSSam Leffler 		 *    disable interrupts
2664c42a7b7eSSam Leffler 		 *    turn off the radio
26655591b213SSam Leffler 		 *    clear transmit machinery
26665591b213SSam Leffler 		 *    clear receive machinery
26675591b213SSam Leffler 		 *    drain and release tx queues
26685591b213SSam Leffler 		 *    reclaim beacon resources
26695591b213SSam Leffler 		 *    power down hardware
26705591b213SSam Leffler 		 *
26715591b213SSam Leffler 		 * Note that some of this work is not possible if the
26725591b213SSam Leffler 		 * hardware is gone (invalid).
26735591b213SSam Leffler 		 */
267486e07743SSam Leffler #ifdef ATH_TX99_DIAG
267586e07743SSam Leffler 		if (sc->sc_tx99 != NULL)
267686e07743SSam Leffler 			sc->sc_tx99->stop(sc->sc_tx99);
267786e07743SSam Leffler #endif
26782e986da5SSam Leffler 		callout_stop(&sc->sc_wd_ch);
26792e986da5SSam Leffler 		sc->sc_wd_timer = 0;
26807a79cebfSGleb Smirnoff 		sc->sc_running = 0;
2681c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
26823e50ec2cSSam Leffler 			if (sc->sc_softled) {
26833e50ec2cSSam Leffler 				callout_stop(&sc->sc_ledtimer);
26843e50ec2cSSam Leffler 				ath_hal_gpioset(ah, sc->sc_ledpin,
26853e50ec2cSSam Leffler 					!sc->sc_ledon);
26863e50ec2cSSam Leffler 				sc->sc_blinking = 0;
26873e50ec2cSSam Leffler 			}
26885591b213SSam Leffler 			ath_hal_intrset(ah, 0);
2689c42a7b7eSSam Leffler 		}
2690062cf7d9SAdrian Chadd 		/* XXX we should stop RX regardless of whether it's valid */
2691c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
26929a842e8bSAdrian Chadd 			ath_stoprecv(sc, 1);
2693c42a7b7eSSam Leffler 			ath_hal_phydisable(ah);
2694c42a7b7eSSam Leffler 		} else
26955591b213SSam Leffler 			sc->sc_rxlink = NULL;
2696062cf7d9SAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);
2697b032f27cSSam Leffler 		ath_beacon_free(sc);	/* XXX not needed */
2698c42a7b7eSSam Leffler 	}
2699f5c30c4eSAdrian Chadd 
2700f5c30c4eSAdrian Chadd 	/* And now, restore the current power state */
2701f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2702c42a7b7eSSam Leffler }
2703c42a7b7eSSam Leffler 
2704f5c30c4eSAdrian Chadd /*
2705f5c30c4eSAdrian Chadd  * Wait until all pending TX/RX has completed.
2706f5c30c4eSAdrian Chadd  *
2707f5c30c4eSAdrian Chadd  * This waits until all existing transmit, receive and interrupts
2708f5c30c4eSAdrian Chadd  * have completed.  It's assumed that the caller has first
2709f5c30c4eSAdrian Chadd  * grabbed the reset lock so it doesn't try to do overlapping
2710f5c30c4eSAdrian Chadd  * chip resets.
2711f5c30c4eSAdrian Chadd  */
2712f5c30c4eSAdrian Chadd #define	MAX_TXRX_ITERATIONS	100
2713ef27340cSAdrian Chadd static void
271421008bf1SAdrian Chadd ath_txrx_stop_locked(struct ath_softc *sc)
2715ef27340cSAdrian Chadd {
2716ef27340cSAdrian Chadd 	int i = MAX_TXRX_ITERATIONS;
2717ef27340cSAdrian Chadd 
2718ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
271921008bf1SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
272021008bf1SAdrian Chadd 
2721ef27340cSAdrian Chadd 	/*
2722ef27340cSAdrian Chadd 	 * Sleep until all the pending operations have completed.
2723ef27340cSAdrian Chadd 	 *
2724ef27340cSAdrian Chadd 	 * The caller must ensure that reset has been incremented
2725ef27340cSAdrian Chadd 	 * or the pending operations may continue being queued.
2726ef27340cSAdrian Chadd 	 */
2727ef27340cSAdrian Chadd 	while (sc->sc_rxproc_cnt || sc->sc_txproc_cnt ||
2728ef27340cSAdrian Chadd 	    sc->sc_txstart_cnt || sc->sc_intr_cnt) {
2729ef27340cSAdrian Chadd 		if (i <= 0)
2730ef27340cSAdrian Chadd 			break;
2731f5c30c4eSAdrian Chadd 		msleep(sc, &sc->sc_pcu_mtx, 0, "ath_txrx_stop",
2732f5c30c4eSAdrian Chadd 		    msecs_to_ticks(10));
2733ef27340cSAdrian Chadd 		i--;
2734ef27340cSAdrian Chadd 	}
2735ef27340cSAdrian Chadd 
2736ef27340cSAdrian Chadd 	if (i <= 0)
2737ef27340cSAdrian Chadd 		device_printf(sc->sc_dev,
2738ef27340cSAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2739ef27340cSAdrian Chadd 		    __func__, MAX_TXRX_ITERATIONS);
2740ef27340cSAdrian Chadd }
2741ef27340cSAdrian Chadd #undef	MAX_TXRX_ITERATIONS
2742ef27340cSAdrian Chadd 
2743e78719adSAdrian Chadd #if 0
2744ef27340cSAdrian Chadd static void
274521008bf1SAdrian Chadd ath_txrx_stop(struct ath_softc *sc)
274621008bf1SAdrian Chadd {
274721008bf1SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
274821008bf1SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
274921008bf1SAdrian Chadd 
275021008bf1SAdrian Chadd 	ATH_PCU_LOCK(sc);
275121008bf1SAdrian Chadd 	ath_txrx_stop_locked(sc);
275221008bf1SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
275321008bf1SAdrian Chadd }
2754e78719adSAdrian Chadd #endif
275521008bf1SAdrian Chadd 
275621008bf1SAdrian Chadd static void
2757ef27340cSAdrian Chadd ath_txrx_start(struct ath_softc *sc)
2758ef27340cSAdrian Chadd {
2759ef27340cSAdrian Chadd 
2760ef27340cSAdrian Chadd 	taskqueue_unblock(sc->sc_tq);
2761ef27340cSAdrian Chadd }
2762ef27340cSAdrian Chadd 
2763ee321975SAdrian Chadd /*
2764ee321975SAdrian Chadd  * Grab the reset lock, and wait around until no one else
2765ee321975SAdrian Chadd  * is trying to do anything with it.
2766ee321975SAdrian Chadd  *
2767ee321975SAdrian Chadd  * This is totally horrible but we can't hold this lock for
2768ee321975SAdrian Chadd  * long enough to do TX/RX or we end up with net80211/ip stack
2769ee321975SAdrian Chadd  * LORs and eventual deadlock.
2770ee321975SAdrian Chadd  *
2771ee321975SAdrian Chadd  * "dowait" signals whether to spin, waiting for the reset
2772ee321975SAdrian Chadd  * lock count to reach 0. This should (for now) only be used
2773ee321975SAdrian Chadd  * during the reset path, as the rest of the code may not
2774ee321975SAdrian Chadd  * be locking-reentrant enough to behave correctly.
2775ee321975SAdrian Chadd  *
2776ee321975SAdrian Chadd  * Another, cleaner way should be found to serialise all of
2777ee321975SAdrian Chadd  * these operations.
2778ee321975SAdrian Chadd  */
2779f5c30c4eSAdrian Chadd #define	MAX_RESET_ITERATIONS	25
2780ee321975SAdrian Chadd static int
2781ee321975SAdrian Chadd ath_reset_grablock(struct ath_softc *sc, int dowait)
2782ee321975SAdrian Chadd {
2783ee321975SAdrian Chadd 	int w = 0;
2784ee321975SAdrian Chadd 	int i = MAX_RESET_ITERATIONS;
2785ee321975SAdrian Chadd 
2786ee321975SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
2787ee321975SAdrian Chadd 	do {
2788ee321975SAdrian Chadd 		if (sc->sc_inreset_cnt == 0) {
2789ee321975SAdrian Chadd 			w = 1;
2790ee321975SAdrian Chadd 			break;
2791ee321975SAdrian Chadd 		}
2792ee321975SAdrian Chadd 		if (dowait == 0) {
2793ee321975SAdrian Chadd 			w = 0;
2794ee321975SAdrian Chadd 			break;
2795ee321975SAdrian Chadd 		}
2796ee321975SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2797f5c30c4eSAdrian Chadd 		/*
2798f5c30c4eSAdrian Chadd 		 * 1 tick is likely not enough time for long calibrations
2799f5c30c4eSAdrian Chadd 		 * to complete.  So we should wait quite a while.
2800f5c30c4eSAdrian Chadd 		 */
2801f5c30c4eSAdrian Chadd 		pause("ath_reset_grablock", msecs_to_ticks(100));
2802ee321975SAdrian Chadd 		i--;
2803ee321975SAdrian Chadd 		ATH_PCU_LOCK(sc);
2804ee321975SAdrian Chadd 	} while (i > 0);
2805ee321975SAdrian Chadd 
2806ee321975SAdrian Chadd 	/*
2807ee321975SAdrian Chadd 	 * We always increment the refcounter, regardless
2808ee321975SAdrian Chadd 	 * of whether we succeeded to get it in an exclusive
2809ee321975SAdrian Chadd 	 * way.
2810ee321975SAdrian Chadd 	 */
2811ee321975SAdrian Chadd 	sc->sc_inreset_cnt++;
2812ee321975SAdrian Chadd 
2813ee321975SAdrian Chadd 	if (i <= 0)
2814ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2815ee321975SAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2816ee321975SAdrian Chadd 		    __func__, MAX_RESET_ITERATIONS);
2817ee321975SAdrian Chadd 
2818ee321975SAdrian Chadd 	if (w == 0)
2819ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2820ee321975SAdrian Chadd 		    "%s: warning, recursive reset path!\n",
2821ee321975SAdrian Chadd 		    __func__);
2822ee321975SAdrian Chadd 
2823ee321975SAdrian Chadd 	return w;
2824ee321975SAdrian Chadd }
2825ee321975SAdrian Chadd #undef MAX_RESET_ITERATIONS
2826ee321975SAdrian Chadd 
2827ee321975SAdrian Chadd /*
28285591b213SSam Leffler  * Reset the hardware w/o losing operational state.  This is
28295591b213SSam Leffler  * basically a more efficient way of doing ath_stop, ath_init,
28305591b213SSam Leffler  * followed by state transitions to the current 802.11
2831c42a7b7eSSam Leffler  * operational state.  Used to recover from various errors and
2832c42a7b7eSSam Leffler  * to reset or reload hardware state.
28335591b213SSam Leffler  */
28346079fdbeSAdrian Chadd int
28357a79cebfSGleb Smirnoff ath_reset(struct ath_softc *sc, ATH_RESET_TYPE reset_type)
28365591b213SSam Leffler {
28377a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
28385591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
28395591b213SSam Leffler 	HAL_STATUS status;
2840ef27340cSAdrian Chadd 	int i;
28415591b213SSam Leffler 
2842f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
284316d4de92SAdrian Chadd 
2844ee321975SAdrian Chadd 	/* Ensure ATH_LOCK isn't held; ath_rx_proc can't be locked */
2845ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
2846ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
2847ef27340cSAdrian Chadd 
2848f6b6084bSPedro F. Giffuni 	/* Try to (stop any further TX/RX from occurring */
2849d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
2850d52f7132SAdrian Chadd 
2851f5c30c4eSAdrian Chadd 	/*
2852f5c30c4eSAdrian Chadd 	 * Wake the hardware up.
2853f5c30c4eSAdrian Chadd 	 */
2854f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2855f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2856f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2857f5c30c4eSAdrian Chadd 
2858ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2859904e385eSAdrian Chadd 
2860904e385eSAdrian Chadd 	/*
2861904e385eSAdrian Chadd 	 * Grab the reset lock before TX/RX is stopped.
2862904e385eSAdrian Chadd 	 *
2863904e385eSAdrian Chadd 	 * This is needed to ensure that when the TX/RX actually does finish,
2864904e385eSAdrian Chadd 	 * no further TX/RX/reset runs in parallel with this.
2865904e385eSAdrian Chadd 	 */
2866ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
2867ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
2868ef27340cSAdrian Chadd 		    __func__);
2869ef27340cSAdrian Chadd 	}
2870904e385eSAdrian Chadd 
2871904e385eSAdrian Chadd 	/* disable interrupts */
2872904e385eSAdrian Chadd 	ath_hal_intrset(ah, 0);
2873904e385eSAdrian Chadd 
2874904e385eSAdrian Chadd 	/*
2875904e385eSAdrian Chadd 	 * Now, ensure that any in progress TX/RX completes before we
2876904e385eSAdrian Chadd 	 * continue.
2877904e385eSAdrian Chadd 	 */
2878904e385eSAdrian Chadd 	ath_txrx_stop_locked(sc);
2879904e385eSAdrian Chadd 
2880ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2881ef27340cSAdrian Chadd 
2882f52d3452SAdrian Chadd 	/*
2883ef27340cSAdrian Chadd 	 * Regardless of whether we're doing a no-loss flush or
2884ef27340cSAdrian Chadd 	 * not, stop the PCU and handle what's in the RX queue.
2885ef27340cSAdrian Chadd 	 * That way frames aren't dropped which shouldn't be.
2886ef27340cSAdrian Chadd 	 */
28879a842e8bSAdrian Chadd 	ath_stoprecv(sc, (reset_type != ATH_RESET_NOLOSS));
2888f8cc9b09SAdrian Chadd 	ath_rx_flush(sc);
2889ef27340cSAdrian Chadd 
2890062cf7d9SAdrian Chadd 	/*
2891062cf7d9SAdrian Chadd 	 * Should now wait for pending TX/RX to complete
2892f6b6084bSPedro F. Giffuni 	 * and block future ones from occurring. This needs to be
2893062cf7d9SAdrian Chadd 	 * done before the TX queue is drained.
2894062cf7d9SAdrian Chadd 	 */
2895062cf7d9SAdrian Chadd 	ath_draintxq(sc, reset_type);	/* stop xmit side */
2896062cf7d9SAdrian Chadd 
2897b032f27cSSam Leffler 	ath_settkipmic(sc);		/* configure TKIP MIC handling */
28985591b213SSam Leffler 	/* NB: indicate channel change so we do a full reset */
28996322256bSAdrian Chadd 	ath_update_chainmasks(sc, ic->ic_curchan);
29006322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
29016322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
2902f50e4ebfSAdrian Chadd 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_TRUE,
2903f50e4ebfSAdrian Chadd 	    HAL_RESET_NORMAL, &status))
290476e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
290576e6fd5dSGleb Smirnoff 		    "%s: unable to reset hardware; hal status %u\n",
29065591b213SSam Leffler 		    __func__, status);
2907c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
290848237774SAdrian Chadd 
290917bb5fd1SAdrian Chadd 	ATH_RX_LOCK(sc);
291017bb5fd1SAdrian Chadd 	sc->sc_rx_stopped = 1;
291117bb5fd1SAdrian Chadd 	sc->sc_rx_resetted = 1;
291217bb5fd1SAdrian Chadd 	ATH_RX_UNLOCK(sc);
291317bb5fd1SAdrian Chadd 
291448237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
291548237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
291648237774SAdrian Chadd 
29179af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
29189af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
29199af351f9SAdrian Chadd 
2920dd6a574eSAdrian Chadd 	/*
2921b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
2922b70f530bSAdrian Chadd 	 */
2923b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
2924b70f530bSAdrian Chadd 
2925b70f530bSAdrian Chadd 	/*
2926dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
2927dd6a574eSAdrian Chadd 	 * support it.
2928dd6a574eSAdrian Chadd 	 */
2929dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
2930dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
2931dd6a574eSAdrian Chadd 	else
2932dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
2933dd6a574eSAdrian Chadd 
293468e8e04eSSam Leffler 	if (ath_startrecv(sc) != 0)	/* restart recv */
293576e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
293676e6fd5dSGleb Smirnoff 		    "%s: unable to start recv logic\n", __func__);
2937c42a7b7eSSam Leffler 	/*
2938c42a7b7eSSam Leffler 	 * We may be doing a reset in response to an ioctl
2939c42a7b7eSSam Leffler 	 * that changes the channel so update any state that
2940c42a7b7eSSam Leffler 	 * might change as a result.
2941c42a7b7eSSam Leffler 	 */
2942724c193aSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
2943c89b957aSSam Leffler 	if (sc->sc_beacons) {		/* restart beacons */
2944584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
294510ad9a77SSam Leffler 		if (sc->sc_tdma)
294610ad9a77SSam Leffler 			ath_tdma_config(sc, NULL);
294710ad9a77SSam Leffler 		else
294810ad9a77SSam Leffler #endif
2949c89b957aSSam Leffler 			ath_beacon_config(sc, NULL);
295010ad9a77SSam Leffler 	}
2951c42a7b7eSSam Leffler 
2952ef27340cSAdrian Chadd 	/*
2953ef27340cSAdrian Chadd 	 * Release the reset lock and re-enable interrupts here.
2954ef27340cSAdrian Chadd 	 * If an interrupt was being processed in ath_intr(),
2955ef27340cSAdrian Chadd 	 * it would disable interrupts at this point. So we have
2956ef27340cSAdrian Chadd 	 * to atomically enable interrupts and decrement the
2957ef27340cSAdrian Chadd 	 * reset counter - this way ath_intr() doesn't end up
2958ef27340cSAdrian Chadd 	 * disabling interrupts without a corresponding enable
2959ef27340cSAdrian Chadd 	 * in the rest or channel change path.
2960f5c30c4eSAdrian Chadd 	 *
2961f5c30c4eSAdrian Chadd 	 * Grab the TX reference in case we need to transmit.
2962f5c30c4eSAdrian Chadd 	 * That way a parallel transmit doesn't.
2963ef27340cSAdrian Chadd 	 */
2964ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2965ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
2966f5c30c4eSAdrian Chadd 	sc->sc_txstart_cnt++;
2967ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
2968ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
2969ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2970ef27340cSAdrian Chadd 
2971ef27340cSAdrian Chadd 	/*
2972ef27340cSAdrian Chadd 	 * TX and RX can be started here. If it were started with
2973ef27340cSAdrian Chadd 	 * sc_inreset_cnt > 0, the TX and RX path would abort.
2974ef27340cSAdrian Chadd 	 * Thus if this is a nested call through the reset or
2975ef27340cSAdrian Chadd 	 * channel change code, TX completion will occur but
2976ef27340cSAdrian Chadd 	 * RX completion and ath_start / ath_tx_start will not
2977ef27340cSAdrian Chadd 	 * run.
2978ef27340cSAdrian Chadd 	 */
2979ef27340cSAdrian Chadd 
2980ef27340cSAdrian Chadd 	/* Restart TX/RX as needed */
2981ef27340cSAdrian Chadd 	ath_txrx_start(sc);
2982ef27340cSAdrian Chadd 
2983f5c30c4eSAdrian Chadd 	/* XXX TODO: we need to hold the tx refcount here! */
2984f5c30c4eSAdrian Chadd 
2985375307d4SAdrian Chadd 	/* Restart TX completion and pending TX */
2986ef27340cSAdrian Chadd 	if (reset_type == ATH_RESET_NOLOSS) {
2987ef27340cSAdrian Chadd 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
2988ef27340cSAdrian Chadd 			if (ATH_TXQ_SETUP(sc, i)) {
2989b837332dSAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
2990ef27340cSAdrian Chadd 				ath_txq_restart_dma(sc, &sc->sc_txq[i]);
2991b837332dSAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
2992b837332dSAdrian Chadd 
2993b837332dSAdrian Chadd 				ATH_TX_LOCK(sc);
2994ef27340cSAdrian Chadd 				ath_txq_sched(sc, &sc->sc_txq[i]);
2995375307d4SAdrian Chadd 				ATH_TX_UNLOCK(sc);
2996ef27340cSAdrian Chadd 			}
2997b837332dSAdrian Chadd 		}
2998b837332dSAdrian Chadd 	}
2999ef27340cSAdrian Chadd 
3000f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3001f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3002f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3003f5c30c4eSAdrian Chadd 
3004f5c30c4eSAdrian Chadd 	ATH_PCU_LOCK(sc);
3005f5c30c4eSAdrian Chadd 	sc->sc_txstart_cnt--;
3006f5c30c4eSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3007f5c30c4eSAdrian Chadd 
3008ef27340cSAdrian Chadd 	/* Handle any frames in the TX queue */
3009ef27340cSAdrian Chadd 	/*
3010ef27340cSAdrian Chadd 	 * XXX should this be done by the caller, rather than
3011ef27340cSAdrian Chadd 	 * ath_reset() ?
3012ef27340cSAdrian Chadd 	 */
30138e739394SAdrian Chadd 	ath_tx_kick(sc);		/* restart xmit */
3014c42a7b7eSSam Leffler 	return 0;
30155591b213SSam Leffler }
30165591b213SSam Leffler 
301768e8e04eSSam Leffler static int
3018b032f27cSSam Leffler ath_reset_vap(struct ieee80211vap *vap, u_long cmd)
3019b032f27cSSam Leffler {
30204b54a231SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
30213797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
30224b54a231SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
30234b54a231SSam Leffler 
30244b54a231SSam Leffler 	switch (cmd) {
30254b54a231SSam Leffler 	case IEEE80211_IOC_TXPOWER:
30264b54a231SSam Leffler 		/*
30274b54a231SSam Leffler 		 * If per-packet TPC is enabled, then we have nothing
30284b54a231SSam Leffler 		 * to do; otherwise we need to force the global limit.
30294b54a231SSam Leffler 		 * All this can happen directly; no need to reset.
30304b54a231SSam Leffler 		 */
30314b54a231SSam Leffler 		if (!ath_hal_gettpc(ah))
30324b54a231SSam Leffler 			ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
30334b54a231SSam Leffler 		return 0;
30344b54a231SSam Leffler 	}
3035517526efSAdrian Chadd 	/* XXX? Full or NOLOSS? */
30367a79cebfSGleb Smirnoff 	return ath_reset(sc, ATH_RESET_FULL);
3037b032f27cSSam Leffler }
3038b032f27cSSam Leffler 
3039b8e788a5SAdrian Chadd struct ath_buf *
3040af33d486SAdrian Chadd _ath_getbuf_locked(struct ath_softc *sc, ath_buf_type_t btype)
304110ad9a77SSam Leffler {
304210ad9a77SSam Leffler 	struct ath_buf *bf;
304310ad9a77SSam Leffler 
304410ad9a77SSam Leffler 	ATH_TXBUF_LOCK_ASSERT(sc);
304510ad9a77SSam Leffler 
3046af33d486SAdrian Chadd 	if (btype == ATH_BUFTYPE_MGMT)
3047af33d486SAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_txbuf_mgmt);
3048af33d486SAdrian Chadd 	else
30496b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_txbuf);
3050af33d486SAdrian Chadd 
3051e346b073SAdrian Chadd 	if (bf == NULL) {
3052e346b073SAdrian Chadd 		sc->sc_stats.ast_tx_getnobuf++;
3053e346b073SAdrian Chadd 	} else {
3054e346b073SAdrian Chadd 		if (bf->bf_flags & ATH_BUF_BUSY) {
3055e346b073SAdrian Chadd 			sc->sc_stats.ast_tx_getbusybuf++;
3056e346b073SAdrian Chadd 			bf = NULL;
3057e346b073SAdrian Chadd 		}
3058e346b073SAdrian Chadd 	}
3059e346b073SAdrian Chadd 
3060af33d486SAdrian Chadd 	if (bf != NULL && (bf->bf_flags & ATH_BUF_BUSY) == 0) {
3061af33d486SAdrian Chadd 		if (btype == ATH_BUFTYPE_MGMT)
3062af33d486SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_txbuf_mgmt, bf, bf_list);
306323ced6c1SAdrian Chadd 		else {
3064af33d486SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
306523ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt--;
306623ced6c1SAdrian Chadd 
306723ced6c1SAdrian Chadd 			/*
306823ced6c1SAdrian Chadd 			 * This shuldn't happen; however just to be
306923ced6c1SAdrian Chadd 			 * safe print a warning and fudge the txbuf
307023ced6c1SAdrian Chadd 			 * count.
307123ced6c1SAdrian Chadd 			 */
307223ced6c1SAdrian Chadd 			if (sc->sc_txbuf_cnt < 0) {
307323ced6c1SAdrian Chadd 				device_printf(sc->sc_dev,
307423ced6c1SAdrian Chadd 				    "%s: sc_txbuf_cnt < 0?\n",
307523ced6c1SAdrian Chadd 				    __func__);
307623ced6c1SAdrian Chadd 				sc->sc_txbuf_cnt = 0;
307723ced6c1SAdrian Chadd 			}
307823ced6c1SAdrian Chadd 		}
3079af33d486SAdrian Chadd 	} else
308010ad9a77SSam Leffler 		bf = NULL;
3081e346b073SAdrian Chadd 
308210ad9a77SSam Leffler 	if (bf == NULL) {
3083af33d486SAdrian Chadd 		/* XXX should check which list, mgmt or otherwise */
308410ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: %s\n", __func__,
30856b349e5aSAdrian Chadd 		    TAILQ_FIRST(&sc->sc_txbuf) == NULL ?
308610ad9a77SSam Leffler 			"out of xmit buffers" : "xmit buffer busy");
3087e346b073SAdrian Chadd 		return NULL;
308810ad9a77SSam Leffler 	}
3089e346b073SAdrian Chadd 
3090af33d486SAdrian Chadd 	/* XXX TODO: should do this at buffer list initialisation */
3091af33d486SAdrian Chadd 	/* XXX (then, ensure the buffer has the right flag set) */
30923feffbd7SAdrian Chadd 	bf->bf_flags = 0;
3093af33d486SAdrian Chadd 	if (btype == ATH_BUFTYPE_MGMT)
3094af33d486SAdrian Chadd 		bf->bf_flags |= ATH_BUF_MGMT;
3095af33d486SAdrian Chadd 	else
3096af33d486SAdrian Chadd 		bf->bf_flags &= (~ATH_BUF_MGMT);
3097af33d486SAdrian Chadd 
3098e346b073SAdrian Chadd 	/* Valid bf here; clear some basic fields */
3099e346b073SAdrian Chadd 	bf->bf_next = NULL;	/* XXX just to be sure */
3100e346b073SAdrian Chadd 	bf->bf_last = NULL;	/* XXX again, just to be sure */
3101e346b073SAdrian Chadd 	bf->bf_comp = NULL;	/* XXX again, just to be sure */
3102e346b073SAdrian Chadd 	bzero(&bf->bf_state, sizeof(bf->bf_state));
3103e346b073SAdrian Chadd 
310485bf9bc3SAdrian Chadd 	/*
310585bf9bc3SAdrian Chadd 	 * Track the descriptor ID only if doing EDMA
310685bf9bc3SAdrian Chadd 	 */
310785bf9bc3SAdrian Chadd 	if (sc->sc_isedma) {
310885bf9bc3SAdrian Chadd 		bf->bf_descid = sc->sc_txbuf_descid;
310985bf9bc3SAdrian Chadd 		sc->sc_txbuf_descid++;
311085bf9bc3SAdrian Chadd 	}
311185bf9bc3SAdrian Chadd 
311210ad9a77SSam Leffler 	return bf;
311310ad9a77SSam Leffler }
311410ad9a77SSam Leffler 
3115e346b073SAdrian Chadd /*
3116e346b073SAdrian Chadd  * When retrying a software frame, buffers marked ATH_BUF_BUSY
3117e346b073SAdrian Chadd  * can't be thrown back on the queue as they could still be
3118e346b073SAdrian Chadd  * in use by the hardware.
3119e346b073SAdrian Chadd  *
3120e346b073SAdrian Chadd  * This duplicates the buffer, or returns NULL.
3121e346b073SAdrian Chadd  *
3122e346b073SAdrian Chadd  * The descriptor is also copied but the link pointers and
3123e346b073SAdrian Chadd  * the DMA segments aren't copied; this frame should thus
3124e346b073SAdrian Chadd  * be again passed through the descriptor setup/chain routines
3125e346b073SAdrian Chadd  * so the link is correct.
3126e346b073SAdrian Chadd  *
3127e346b073SAdrian Chadd  * The caller must free the buffer using ath_freebuf().
3128e346b073SAdrian Chadd  */
3129e346b073SAdrian Chadd struct ath_buf *
31303f3a5dbdSAdrian Chadd ath_buf_clone(struct ath_softc *sc, struct ath_buf *bf)
3131e346b073SAdrian Chadd {
3132e346b073SAdrian Chadd 	struct ath_buf *tbf;
3133e346b073SAdrian Chadd 
3134af33d486SAdrian Chadd 	tbf = ath_getbuf(sc,
3135af33d486SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_MGMT) ?
3136af33d486SAdrian Chadd 	     ATH_BUFTYPE_MGMT : ATH_BUFTYPE_NORMAL);
3137e346b073SAdrian Chadd 	if (tbf == NULL)
3138e346b073SAdrian Chadd 		return NULL;	/* XXX failure? Why? */
3139e346b073SAdrian Chadd 
3140e346b073SAdrian Chadd 	/* Copy basics */
3141e346b073SAdrian Chadd 	tbf->bf_next = NULL;
3142e346b073SAdrian Chadd 	tbf->bf_nseg = bf->bf_nseg;
31433feffbd7SAdrian Chadd 	tbf->bf_flags = bf->bf_flags & ATH_BUF_FLAGS_CLONE;
3144e346b073SAdrian Chadd 	tbf->bf_status = bf->bf_status;
3145e346b073SAdrian Chadd 	tbf->bf_m = bf->bf_m;
3146e346b073SAdrian Chadd 	tbf->bf_node = bf->bf_node;
3147f5c30c4eSAdrian Chadd 	KASSERT((bf->bf_node != NULL), ("%s: bf_node=NULL!", __func__));
3148e346b073SAdrian Chadd 	/* will be setup by the chain/setup function */
3149e346b073SAdrian Chadd 	tbf->bf_lastds = NULL;
3150e346b073SAdrian Chadd 	/* for now, last == self */
3151e346b073SAdrian Chadd 	tbf->bf_last = tbf;
3152e346b073SAdrian Chadd 	tbf->bf_comp = bf->bf_comp;
3153e346b073SAdrian Chadd 
3154e346b073SAdrian Chadd 	/* NOTE: DMA segments will be setup by the setup/chain functions */
3155e346b073SAdrian Chadd 
3156e346b073SAdrian Chadd 	/* The caller has to re-init the descriptor + links */
3157e346b073SAdrian Chadd 
31583f3a5dbdSAdrian Chadd 	/*
31593f3a5dbdSAdrian Chadd 	 * Free the DMA mapping here, before we NULL the mbuf.
31603f3a5dbdSAdrian Chadd 	 * We must only call bus_dmamap_unload() once per mbuf chain
31613f3a5dbdSAdrian Chadd 	 * or behaviour is undefined.
31623f3a5dbdSAdrian Chadd 	 */
31633f3a5dbdSAdrian Chadd 	if (bf->bf_m != NULL) {
31643f3a5dbdSAdrian Chadd 		/*
31653f3a5dbdSAdrian Chadd 		 * XXX is this POSTWRITE call required?
31663f3a5dbdSAdrian Chadd 		 */
31673f3a5dbdSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
31683f3a5dbdSAdrian Chadd 		    BUS_DMASYNC_POSTWRITE);
31693f3a5dbdSAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
31703f3a5dbdSAdrian Chadd 	}
31713f3a5dbdSAdrian Chadd 
31723f3a5dbdSAdrian Chadd 	bf->bf_m = NULL;
31733f3a5dbdSAdrian Chadd 	bf->bf_node = NULL;
31743f3a5dbdSAdrian Chadd 
3175e346b073SAdrian Chadd 	/* Copy state */
3176e346b073SAdrian Chadd 	memcpy(&tbf->bf_state, &bf->bf_state, sizeof(bf->bf_state));
3177e346b073SAdrian Chadd 
3178e346b073SAdrian Chadd 	return tbf;
3179e346b073SAdrian Chadd }
3180e346b073SAdrian Chadd 
3181b8e788a5SAdrian Chadd struct ath_buf *
3182af33d486SAdrian Chadd ath_getbuf(struct ath_softc *sc, ath_buf_type_t btype)
318310ad9a77SSam Leffler {
318410ad9a77SSam Leffler 	struct ath_buf *bf;
318510ad9a77SSam Leffler 
318610ad9a77SSam Leffler 	ATH_TXBUF_LOCK(sc);
3187af33d486SAdrian Chadd 	bf = _ath_getbuf_locked(sc, btype);
3188af33d486SAdrian Chadd 	/*
3189af33d486SAdrian Chadd 	 * If a mgmt buffer was requested but we're out of those,
3190af33d486SAdrian Chadd 	 * try requesting a normal one.
3191af33d486SAdrian Chadd 	 */
3192af33d486SAdrian Chadd 	if (bf == NULL && btype == ATH_BUFTYPE_MGMT)
3193af33d486SAdrian Chadd 		bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL);
3194e4e7938aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
319510ad9a77SSam Leffler 	if (bf == NULL) {
319610ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: stop queue\n", __func__);
319710ad9a77SSam Leffler 		sc->sc_stats.ast_tx_qstop++;
319810ad9a77SSam Leffler 	}
319910ad9a77SSam Leffler 	return bf;
320010ad9a77SSam Leffler }
320110ad9a77SSam Leffler 
32027dcb2beaSAdrian Chadd /*
3203cd7dffd0SAdrian Chadd  * Transmit a single frame.
3204cd7dffd0SAdrian Chadd  *
3205cd7dffd0SAdrian Chadd  * net80211 will free the node reference if the transmit
3206cd7dffd0SAdrian Chadd  * fails, so don't free the node reference here.
32077dcb2beaSAdrian Chadd  */
3208cd7dffd0SAdrian Chadd static int
32097a79cebfSGleb Smirnoff ath_transmit(struct ieee80211com *ic, struct mbuf *m)
3210cd7dffd0SAdrian Chadd {
32113797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
3212cd7dffd0SAdrian Chadd 	struct ieee80211_node *ni;
3213cd7dffd0SAdrian Chadd 	struct mbuf *next;
3214cd7dffd0SAdrian Chadd 	struct ath_buf *bf;
3215cd7dffd0SAdrian Chadd 	ath_bufhead frags;
3216cd7dffd0SAdrian Chadd 	int retval = 0;
3217cd7dffd0SAdrian Chadd 
3218cd7dffd0SAdrian Chadd 	/*
3219cd7dffd0SAdrian Chadd 	 * Tell the reset path that we're currently transmitting.
3220cd7dffd0SAdrian Chadd 	 */
3221cd7dffd0SAdrian Chadd 	ATH_PCU_LOCK(sc);
3222cd7dffd0SAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
322383bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
3224cd7dffd0SAdrian Chadd 		    "%s: sc_inreset_cnt > 0; bailing\n", __func__);
3225cd7dffd0SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
3226cd7dffd0SAdrian Chadd 		sc->sc_stats.ast_tx_qstop++;
3227cd7dffd0SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TX, 0, "ath_start_task: OACTIVE, finish");
3228cd7dffd0SAdrian Chadd 		return (ENOBUFS);	/* XXX should be EINVAL or? */
3229cd7dffd0SAdrian Chadd 	}
3230cd7dffd0SAdrian Chadd 	sc->sc_txstart_cnt++;
3231cd7dffd0SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3232cd7dffd0SAdrian Chadd 
3233f5c30c4eSAdrian Chadd 	/* Wake the hardware up already */
3234f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3235f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3236f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3237f5c30c4eSAdrian Chadd 
3238cd7dffd0SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_transmit: start");
3239cd7dffd0SAdrian Chadd 	/*
3240cd7dffd0SAdrian Chadd 	 * Grab the TX lock - it's ok to do this here; we haven't
3241cd7dffd0SAdrian Chadd 	 * yet started transmitting.
3242cd7dffd0SAdrian Chadd 	 */
3243cd7dffd0SAdrian Chadd 	ATH_TX_LOCK(sc);
3244cd7dffd0SAdrian Chadd 
3245cd7dffd0SAdrian Chadd 	/*
3246cd7dffd0SAdrian Chadd 	 * Node reference, if there's one.
3247cd7dffd0SAdrian Chadd 	 */
32487dcb2beaSAdrian Chadd 	ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
32497dcb2beaSAdrian Chadd 
32507dcb2beaSAdrian Chadd 	/*
32517dcb2beaSAdrian Chadd 	 * Enforce how deep a node queue can get.
32527dcb2beaSAdrian Chadd 	 *
32537dcb2beaSAdrian Chadd 	 * XXX it would be nicer if we kept an mbuf queue per
32547dcb2beaSAdrian Chadd 	 * node and only whacked them into ath_bufs when we
32557dcb2beaSAdrian Chadd 	 * are ready to schedule some traffic from them.
32567dcb2beaSAdrian Chadd 	 * .. that may come later.
32577dcb2beaSAdrian Chadd 	 *
32587dcb2beaSAdrian Chadd 	 * XXX we should also track the per-node hardware queue
32597dcb2beaSAdrian Chadd 	 * depth so it is easy to limit the _SUM_ of the swq and
32607dcb2beaSAdrian Chadd 	 * hwq frames.  Since we only schedule two HWQ frames
32617dcb2beaSAdrian Chadd 	 * at a time, this should be OK for now.
32627dcb2beaSAdrian Chadd 	 */
32637dcb2beaSAdrian Chadd 	if ((!(m->m_flags & M_EAPOL)) &&
32647dcb2beaSAdrian Chadd 	    (ATH_NODE(ni)->an_swq_depth > sc->sc_txq_node_maxdepth)) {
32657dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nodeq_overflow++;
3266cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
3267cd7dffd0SAdrian Chadd 		goto finish;
32687dcb2beaSAdrian Chadd 	}
32697dcb2beaSAdrian Chadd 
32707dcb2beaSAdrian Chadd 	/*
32717dcb2beaSAdrian Chadd 	 * Check how many TX buffers are available.
32727dcb2beaSAdrian Chadd 	 *
32737dcb2beaSAdrian Chadd 	 * If this is for non-EAPOL traffic, just leave some
32747dcb2beaSAdrian Chadd 	 * space free in order for buffer cloning and raw
32757dcb2beaSAdrian Chadd 	 * frame transmission to occur.
32767dcb2beaSAdrian Chadd 	 *
32777dcb2beaSAdrian Chadd 	 * If it's for EAPOL traffic, ignore this for now.
32787dcb2beaSAdrian Chadd 	 * Management traffic will be sent via the raw transmit
32797dcb2beaSAdrian Chadd 	 * method which bypasses this check.
32807dcb2beaSAdrian Chadd 	 *
32817dcb2beaSAdrian Chadd 	 * This is needed to ensure that EAPOL frames during
32827dcb2beaSAdrian Chadd 	 * (re) keying have a chance to go out.
32837dcb2beaSAdrian Chadd 	 *
32847dcb2beaSAdrian Chadd 	 * See kern/138379 for more information.
32857dcb2beaSAdrian Chadd 	 */
32867dcb2beaSAdrian Chadd 	if ((!(m->m_flags & M_EAPOL)) &&
32877dcb2beaSAdrian Chadd 	    (sc->sc_txbuf_cnt <= sc->sc_txq_data_minfree)) {
32887dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
3289cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
3290cd7dffd0SAdrian Chadd 		goto finish;
329123ced6c1SAdrian Chadd 	}
329223ced6c1SAdrian Chadd 
32935591b213SSam Leffler 	/*
32945591b213SSam Leffler 	 * Grab a TX buffer and associated resources.
32957dcb2beaSAdrian Chadd 	 *
32967dcb2beaSAdrian Chadd 	 * If it's an EAPOL frame, allocate a MGMT ath_buf.
32977dcb2beaSAdrian Chadd 	 * That way even with temporary buffer exhaustion due to
32987dcb2beaSAdrian Chadd 	 * the data path doesn't leave us without the ability
32997dcb2beaSAdrian Chadd 	 * to transmit management frames.
33007dcb2beaSAdrian Chadd 	 *
33017dcb2beaSAdrian Chadd 	 * Otherwise allocate a normal buffer.
33025591b213SSam Leffler 	 */
33037dcb2beaSAdrian Chadd 	if (m->m_flags & M_EAPOL)
33047dcb2beaSAdrian Chadd 		bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT);
33057dcb2beaSAdrian Chadd 	else
3306af33d486SAdrian Chadd 		bf = ath_getbuf(sc, ATH_BUFTYPE_NORMAL);
33071a85141aSAdrian Chadd 
33087dcb2beaSAdrian Chadd 	if (bf == NULL) {
33097dcb2beaSAdrian Chadd 		/*
3310cd7dffd0SAdrian Chadd 		 * If we failed to allocate a buffer, fail.
33117dcb2beaSAdrian Chadd 		 *
33127dcb2beaSAdrian Chadd 		 * We shouldn't fail normally, due to the check
33137dcb2beaSAdrian Chadd 		 * above.
33147dcb2beaSAdrian Chadd 		 */
33157dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
3316cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
3317cd7dffd0SAdrian Chadd 		goto finish;
3318b032f27cSSam Leffler 	}
33197dcb2beaSAdrian Chadd 
3320cd7dffd0SAdrian Chadd 	/*
3321cd7dffd0SAdrian Chadd 	 * At this point we have a buffer; so we need to free it
3322cd7dffd0SAdrian Chadd 	 * if we hit any error conditions.
3323cd7dffd0SAdrian Chadd 	 */
33247dcb2beaSAdrian Chadd 
332568e8e04eSSam Leffler 	/*
332668e8e04eSSam Leffler 	 * Check for fragmentation.  If this frame
332768e8e04eSSam Leffler 	 * has been broken up verify we have enough
332868e8e04eSSam Leffler 	 * buffers to send all the fragments so all
332968e8e04eSSam Leffler 	 * go out or none...
333068e8e04eSSam Leffler 	 */
33316b349e5aSAdrian Chadd 	TAILQ_INIT(&frags);
33321a85141aSAdrian Chadd 	if ((m->m_flags & M_FRAG) &&
33331a85141aSAdrian Chadd 	    !ath_txfrag_setup(sc, &frags, m, ni)) {
333468e8e04eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT,
333568e8e04eSSam Leffler 		    "%s: out of txfrag buffers\n", __func__);
333636c6be9aSSam Leffler 		sc->sc_stats.ast_tx_nofrag++;
33377a79cebfSGleb Smirnoff 		if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
33387a79cebfSGleb Smirnoff 		/*
33397a79cebfSGleb Smirnoff 		 * XXXGL: is mbuf valid after ath_txfrag_setup? If yes,
33407a79cebfSGleb Smirnoff 		 * we shouldn't free it but return back.
33417a79cebfSGleb Smirnoff 		 */
3342d07be335SAdrian Chadd 		ieee80211_free_mbuf(m);
33437a79cebfSGleb Smirnoff 		m = NULL;
334468e8e04eSSam Leffler 		goto bad;
334568e8e04eSSam Leffler 	}
3346cd7dffd0SAdrian Chadd 
3347cd7dffd0SAdrian Chadd 	/*
3348cd7dffd0SAdrian Chadd 	 * At this point if we have any TX fragments, then we will
3349cd7dffd0SAdrian Chadd 	 * have bumped the node reference once for each of those.
3350cd7dffd0SAdrian Chadd 	 */
3351cd7dffd0SAdrian Chadd 
3352cd7dffd0SAdrian Chadd 	/*
3353cd7dffd0SAdrian Chadd 	 * XXX Is there anything actually _enforcing_ that the
3354cd7dffd0SAdrian Chadd 	 * fragments are being transmitted in one hit, rather than
3355cd7dffd0SAdrian Chadd 	 * being interleaved with other transmissions on that
3356cd7dffd0SAdrian Chadd 	 * hardware queue?
3357cd7dffd0SAdrian Chadd 	 *
3358cd7dffd0SAdrian Chadd 	 * The ATH TX output lock is the only thing serialising this
3359cd7dffd0SAdrian Chadd 	 * right now.
3360cd7dffd0SAdrian Chadd 	 */
3361cd7dffd0SAdrian Chadd 
3362cd7dffd0SAdrian Chadd 	/*
3363cd7dffd0SAdrian Chadd 	 * Calculate the "next fragment" length field in ath_buf
3364cd7dffd0SAdrian Chadd 	 * in order to let the transmit path know enough about
3365cd7dffd0SAdrian Chadd 	 * what to next write to the hardware.
3366cd7dffd0SAdrian Chadd 	 */
3367cd7dffd0SAdrian Chadd 	if (m->m_flags & M_FRAG) {
3368cd7dffd0SAdrian Chadd 		struct ath_buf *fbf = bf;
3369cd7dffd0SAdrian Chadd 		struct ath_buf *n_fbf = NULL;
3370cd7dffd0SAdrian Chadd 		struct mbuf *fm = m->m_nextpkt;
3371cd7dffd0SAdrian Chadd 
3372cd7dffd0SAdrian Chadd 		/*
3373cd7dffd0SAdrian Chadd 		 * We need to walk the list of fragments and set
3374cd7dffd0SAdrian Chadd 		 * the next size to the following buffer.
3375cd7dffd0SAdrian Chadd 		 * However, the first buffer isn't in the frag
3376cd7dffd0SAdrian Chadd 		 * list, so we have to do some gymnastics here.
3377cd7dffd0SAdrian Chadd 		 */
3378cd7dffd0SAdrian Chadd 		TAILQ_FOREACH(n_fbf, &frags, bf_list) {
3379cd7dffd0SAdrian Chadd 			fbf->bf_nextfraglen = fm->m_pkthdr.len;
3380cd7dffd0SAdrian Chadd 			fbf = n_fbf;
3381cd7dffd0SAdrian Chadd 			fm = fm->m_nextpkt;
3382cd7dffd0SAdrian Chadd 		}
3383cd7dffd0SAdrian Chadd 	}
3384cd7dffd0SAdrian Chadd 
33851a85141aSAdrian Chadd nextfrag:
338668e8e04eSSam Leffler 	/*
33871a85141aSAdrian Chadd 	 * Pass the frame to the h/w for transmission.
33881a85141aSAdrian Chadd 	 * Fragmented frames have each frag chained together
33891a85141aSAdrian Chadd 	 * with m_nextpkt.  We know there are sufficient ath_buf's
33901a85141aSAdrian Chadd 	 * to send all the frags because of work done by
33911a85141aSAdrian Chadd 	 * ath_txfrag_setup.  We leave m_nextpkt set while
33921a85141aSAdrian Chadd 	 * calling ath_tx_start so it can use it to extend the
33931a85141aSAdrian Chadd 	 * the tx duration to cover the subsequent frag and
33941a85141aSAdrian Chadd 	 * so it can reclaim all the mbufs in case of an error;
33951a85141aSAdrian Chadd 	 * ath_tx_start clears m_nextpkt once it commits to
33961a85141aSAdrian Chadd 	 * handing the frame to the hardware.
3397cd7dffd0SAdrian Chadd 	 *
3398cd7dffd0SAdrian Chadd 	 * Note: if this fails, then the mbufs are freed but
3399cd7dffd0SAdrian Chadd 	 * not the node reference.
3400da4552abSAdrian Chadd 	 *
3401da4552abSAdrian Chadd 	 * So, we now have to free the node reference ourselves here
3402da4552abSAdrian Chadd 	 * and return OK up to the stack.
340368e8e04eSSam Leffler 	 */
34041a85141aSAdrian Chadd 	next = m->m_nextpkt;
34051a85141aSAdrian Chadd 	if (ath_tx_start(sc, ni, bf, m)) {
34065591b213SSam Leffler bad:
34077a79cebfSGleb Smirnoff 		if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
34081a85141aSAdrian Chadd reclaim:
340968e8e04eSSam Leffler 		bf->bf_m = NULL;
341068e8e04eSSam Leffler 		bf->bf_node = NULL;
3411c42a7b7eSSam Leffler 		ATH_TXBUF_LOCK(sc);
3412e1a50456SAdrian Chadd 		ath_returnbuf_head(sc, bf);
3413cd7dffd0SAdrian Chadd 		/*
3414cd7dffd0SAdrian Chadd 		 * Free the rest of the node references and
3415cd7dffd0SAdrian Chadd 		 * buffers for the fragment list.
3416cd7dffd0SAdrian Chadd 		 */
341768e8e04eSSam Leffler 		ath_txfrag_cleanup(sc, &frags, ni);
3418c42a7b7eSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
3419da4552abSAdrian Chadd 
3420da4552abSAdrian Chadd 		/*
3421da4552abSAdrian Chadd 		 * XXX: And free the node/return OK; ath_tx_start() may have
3422da4552abSAdrian Chadd 		 *      modified the buffer.  We currently have no way to
3423da4552abSAdrian Chadd 		 *      signify that the mbuf was freed but there was an error.
3424da4552abSAdrian Chadd 		 */
3425da4552abSAdrian Chadd 		ieee80211_free_node(ni);
3426da4552abSAdrian Chadd 		retval = 0;
3427cd7dffd0SAdrian Chadd 		goto finish;
34281a85141aSAdrian Chadd 	}
34291a85141aSAdrian Chadd 
3430548a605dSAdrian Chadd 	/*
3431548a605dSAdrian Chadd 	 * Check here if the node is in power save state.
3432548a605dSAdrian Chadd 	 */
3433548a605dSAdrian Chadd 	ath_tx_update_tim(sc, ni, 1);
3434548a605dSAdrian Chadd 
34351a85141aSAdrian Chadd 	if (next != NULL) {
343668e8e04eSSam Leffler 		/*
34371a85141aSAdrian Chadd 		 * Beware of state changing between frags.
34381a85141aSAdrian Chadd 		 * XXX check sta power-save state?
343968e8e04eSSam Leffler 		 */
34401a85141aSAdrian Chadd 		if (ni->ni_vap->iv_state != IEEE80211_S_RUN) {
3441c5239edbSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
34421a85141aSAdrian Chadd 			    "%s: flush fragmented packet, state %s\n",
34431a85141aSAdrian Chadd 			    __func__,
34441a85141aSAdrian Chadd 			    ieee80211_state_name[ni->ni_vap->iv_state]);
3445a91ab3c0SAdrian Chadd 			/* XXX dmamap */
3446d07be335SAdrian Chadd 			ieee80211_free_mbuf(next);
34471a85141aSAdrian Chadd 			goto reclaim;
3448c5239edbSAdrian Chadd 		}
34491a85141aSAdrian Chadd 		m = next;
34501a85141aSAdrian Chadd 		bf = TAILQ_FIRST(&frags);
34511a85141aSAdrian Chadd 		KASSERT(bf != NULL, ("no buf for txfrag"));
34521a85141aSAdrian Chadd 		TAILQ_REMOVE(&frags, bf, bf_list);
34531a85141aSAdrian Chadd 		goto nextfrag;
3454c5239edbSAdrian Chadd 	}
3455c5239edbSAdrian Chadd 
3456cd7dffd0SAdrian Chadd 	/*
3457cd7dffd0SAdrian Chadd 	 * Bump watchdog timer.
3458cd7dffd0SAdrian Chadd 	 */
34591a85141aSAdrian Chadd 	sc->sc_wd_timer = 5;
3460cd7dffd0SAdrian Chadd 
3461cd7dffd0SAdrian Chadd finish:
3462cd7dffd0SAdrian Chadd 	ATH_TX_UNLOCK(sc);
3463cd7dffd0SAdrian Chadd 
3464cd7dffd0SAdrian Chadd 	/*
3465cd7dffd0SAdrian Chadd 	 * Finished transmitting!
3466cd7dffd0SAdrian Chadd 	 */
3467cd7dffd0SAdrian Chadd 	ATH_PCU_LOCK(sc);
3468cd7dffd0SAdrian Chadd 	sc->sc_txstart_cnt--;
3469cd7dffd0SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3470cd7dffd0SAdrian Chadd 
3471f5c30c4eSAdrian Chadd 	/* Sleep the hardware if required */
3472f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3473f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3474f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3475f5c30c4eSAdrian Chadd 
3476cd7dffd0SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_transmit: finished");
3477cd7dffd0SAdrian Chadd 
3478cd7dffd0SAdrian Chadd 	return (retval);
34795591b213SSam Leffler }
3480cd7dffd0SAdrian Chadd 
34815591b213SSam Leffler static int
34825591b213SSam Leffler ath_media_change(struct ifnet *ifp)
34835591b213SSam Leffler {
3484b032f27cSSam Leffler 	int error = ieee80211_media_change(ifp);
3485b032f27cSSam Leffler 	/* NB: only the fixed rate can change and that doesn't need a reset */
3486b032f27cSSam Leffler 	return (error == ENETRESET ? 0 : error);
34875591b213SSam Leffler }
34885591b213SSam Leffler 
3489c42a7b7eSSam Leffler /*
3490c42a7b7eSSam Leffler  * Block/unblock tx+rx processing while a key change is done.
3491c42a7b7eSSam Leffler  * We assume the caller serializes key management operations
3492c42a7b7eSSam Leffler  * so we only need to worry about synchronization with other
3493c42a7b7eSSam Leffler  * uses that originate in the driver.
3494c42a7b7eSSam Leffler  */
3495c42a7b7eSSam Leffler static void
3496b032f27cSSam Leffler ath_key_update_begin(struct ieee80211vap *vap)
3497c42a7b7eSSam Leffler {
34983797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
3499c42a7b7eSSam Leffler 
3500c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
3501b032f27cSSam Leffler 	taskqueue_block(sc->sc_tq);
3502c42a7b7eSSam Leffler }
3503c42a7b7eSSam Leffler 
3504c42a7b7eSSam Leffler static void
3505b032f27cSSam Leffler ath_key_update_end(struct ieee80211vap *vap)
3506c42a7b7eSSam Leffler {
35073797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
3508c42a7b7eSSam Leffler 
3509c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
3510b032f27cSSam Leffler 	taskqueue_unblock(sc->sc_tq);
3511c42a7b7eSSam Leffler }
35125591b213SSam Leffler 
3513b032f27cSSam Leffler static void
3514272f6adeSGleb Smirnoff ath_update_promisc(struct ieee80211com *ic)
3515b032f27cSSam Leffler {
3516272f6adeSGleb Smirnoff 	struct ath_softc *sc = ic->ic_softc;
3517b032f27cSSam Leffler 	u_int32_t rfilt;
3518b032f27cSSam Leffler 
3519b032f27cSSam Leffler 	/* configure rx filter */
3520f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3521f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3522b032f27cSSam Leffler 	rfilt = ath_calcrxfilter(sc);
3523b032f27cSSam Leffler 	ath_hal_setrxfilter(sc->sc_ah, rfilt);
3524f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3525f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3526b032f27cSSam Leffler 
3527b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x\n", __func__, rfilt);
3528b032f27cSSam Leffler }
3529b032f27cSSam Leffler 
3530e5bd159eSAdrian Chadd /*
3531e5bd159eSAdrian Chadd  * Driver-internal mcast update call.
3532e5bd159eSAdrian Chadd  *
3533e5bd159eSAdrian Chadd  * Assumes the hardware is already awake.
3534e5bd159eSAdrian Chadd  */
3535b032f27cSSam Leffler static void
3536e5bd159eSAdrian Chadd ath_update_mcast_hw(struct ath_softc *sc)
3537b032f27cSSam Leffler {
35387a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
3539b032f27cSSam Leffler 	u_int32_t mfilt[2];
3540b032f27cSSam Leffler 
3541b032f27cSSam Leffler 	/* calculate and install multicast filter */
35427a79cebfSGleb Smirnoff 	if (ic->ic_allmulti == 0) {
35437a79cebfSGleb Smirnoff 		struct ieee80211vap *vap;
35447a79cebfSGleb Smirnoff 		struct ifnet *ifp;
3545b032f27cSSam Leffler 		struct ifmultiaddr *ifma;
35467a79cebfSGleb Smirnoff 
3547b032f27cSSam Leffler 		/*
3548b032f27cSSam Leffler 		 * Merge multicast addresses to form the hardware filter.
3549b032f27cSSam Leffler 		 */
3550b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = 0;
35517a79cebfSGleb Smirnoff 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
35527a79cebfSGleb Smirnoff 			ifp = vap->iv_ifp;
35537a79cebfSGleb Smirnoff 			if_maddr_rlock(ifp);
3554b032f27cSSam Leffler 			TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3555b032f27cSSam Leffler 				caddr_t dl;
35567a79cebfSGleb Smirnoff 				uint32_t val;
35577a79cebfSGleb Smirnoff 				uint8_t pos;
3558b032f27cSSam Leffler 
3559b032f27cSSam Leffler 				/* calculate XOR of eight 6bit values */
35607a79cebfSGleb Smirnoff 				dl = LLADDR((struct sockaddr_dl *)
35617a79cebfSGleb Smirnoff 				    ifma->ifma_addr);
356231021a2bSAndriy Voskoboinyk 				val = le32dec(dl + 0);
35637a79cebfSGleb Smirnoff 				pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^
35647a79cebfSGleb Smirnoff 				    val;
356531021a2bSAndriy Voskoboinyk 				val = le32dec(dl + 3);
35667a79cebfSGleb Smirnoff 				pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^
35677a79cebfSGleb Smirnoff 				    val;
3568b032f27cSSam Leffler 				pos &= 0x3f;
3569b032f27cSSam Leffler 				mfilt[pos / 32] |= (1 << (pos % 32));
3570b032f27cSSam Leffler 			}
3571eb956cd0SRobert Watson 			if_maddr_runlock(ifp);
35727a79cebfSGleb Smirnoff 		}
3573b032f27cSSam Leffler 	} else
3574b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = ~0;
3575e5bd159eSAdrian Chadd 
3576b032f27cSSam Leffler 	ath_hal_setmcastfilter(sc->sc_ah, mfilt[0], mfilt[1]);
3577e5bd159eSAdrian Chadd 
3578b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: MC filter %08x:%08x\n",
3579b032f27cSSam Leffler 		__func__, mfilt[0], mfilt[1]);
35804bc0e754SSam Leffler }
35814bc0e754SSam Leffler 
3582e5bd159eSAdrian Chadd /*
3583e5bd159eSAdrian Chadd  * Called from the net80211 layer - force the hardware
3584e5bd159eSAdrian Chadd  * awake before operating.
3585e5bd159eSAdrian Chadd  */
3586e5bd159eSAdrian Chadd static void
3587272f6adeSGleb Smirnoff ath_update_mcast(struct ieee80211com *ic)
3588e5bd159eSAdrian Chadd {
3589272f6adeSGleb Smirnoff 	struct ath_softc *sc = ic->ic_softc;
3590e5bd159eSAdrian Chadd 
3591e5bd159eSAdrian Chadd 	ATH_LOCK(sc);
3592e5bd159eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3593e5bd159eSAdrian Chadd 	ATH_UNLOCK(sc);
3594e5bd159eSAdrian Chadd 
3595e5bd159eSAdrian Chadd 	ath_update_mcast_hw(sc);
3596e5bd159eSAdrian Chadd 
3597e5bd159eSAdrian Chadd 	ATH_LOCK(sc);
3598e5bd159eSAdrian Chadd 	ath_power_restore_power_state(sc);
3599e5bd159eSAdrian Chadd 	ATH_UNLOCK(sc);
3600e5bd159eSAdrian Chadd }
3601e5bd159eSAdrian Chadd 
3602e60c4fc2SAdrian Chadd void
36035591b213SSam Leffler ath_mode_init(struct ath_softc *sc)
36045591b213SSam Leffler {
36057a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
3606b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3607b032f27cSSam Leffler 	u_int32_t rfilt;
36085591b213SSam Leffler 
36094bc0e754SSam Leffler 	/* configure rx filter */
361068e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
36114bc0e754SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
36124bc0e754SSam Leffler 
36135591b213SSam Leffler 	/* configure operational mode */
3614c42a7b7eSSam Leffler 	ath_hal_setopmode(ah);
3615c42a7b7eSSam Leffler 
361629aca940SSam Leffler 	/* handle any link-level address change */
36177a79cebfSGleb Smirnoff 	ath_hal_setmac(ah, ic->ic_macaddr);
36185591b213SSam Leffler 
36195591b213SSam Leffler 	/* calculate and install multicast filter */
3620e5bd159eSAdrian Chadd 	ath_update_mcast_hw(sc);
36215591b213SSam Leffler }
36225591b213SSam Leffler 
3623c42a7b7eSSam Leffler /*
3624c42a7b7eSSam Leffler  * Set the slot time based on the current setting.
3625c42a7b7eSSam Leffler  */
3626ba5c15d9SAdrian Chadd void
3627c42a7b7eSSam Leffler ath_setslottime(struct ath_softc *sc)
3628c42a7b7eSSam Leffler {
36297a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
3630c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3631aaa70f2fSSam Leffler 	u_int usec;
3632c42a7b7eSSam Leffler 
3633aaa70f2fSSam Leffler 	if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan))
3634aaa70f2fSSam Leffler 		usec = 13;
3635aaa70f2fSSam Leffler 	else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan))
3636aaa70f2fSSam Leffler 		usec = 21;
3637724c193aSSam Leffler 	else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) {
3638724c193aSSam Leffler 		/* honor short/long slot time only in 11g */
3639724c193aSSam Leffler 		/* XXX shouldn't honor on pure g or turbo g channel */
3640724c193aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
3641aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_9;
3642aaa70f2fSSam Leffler 		else
3643aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_20;
3644724c193aSSam Leffler 	} else
3645724c193aSSam Leffler 		usec = HAL_SLOT_TIME_9;
3646aaa70f2fSSam Leffler 
3647aaa70f2fSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
3648aaa70f2fSSam Leffler 	    "%s: chan %u MHz flags 0x%x %s slot, %u usec\n",
3649aaa70f2fSSam Leffler 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
3650aaa70f2fSSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec);
3651aaa70f2fSSam Leffler 
3652f5c30c4eSAdrian Chadd 	/* Wake up the hardware first before updating the slot time */
3653f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3654f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3655aaa70f2fSSam Leffler 	ath_hal_setslottime(ah, usec);
3656f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3657c42a7b7eSSam Leffler 	sc->sc_updateslot = OK;
3658f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3659c42a7b7eSSam Leffler }
3660c42a7b7eSSam Leffler 
3661c42a7b7eSSam Leffler /*
3662c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update the
3663c42a7b7eSSam Leffler  * slot time based on the current setting.
3664c42a7b7eSSam Leffler  */
3665c42a7b7eSSam Leffler static void
3666272f6adeSGleb Smirnoff ath_updateslot(struct ieee80211com *ic)
3667c42a7b7eSSam Leffler {
3668272f6adeSGleb Smirnoff 	struct ath_softc *sc = ic->ic_softc;
3669c42a7b7eSSam Leffler 
3670c42a7b7eSSam Leffler 	/*
3671c42a7b7eSSam Leffler 	 * When not coordinating the BSS, change the hardware
3672c42a7b7eSSam Leffler 	 * immediately.  For other operation we defer the change
3673c42a7b7eSSam Leffler 	 * until beacon updates have propagated to the stations.
3674f5c30c4eSAdrian Chadd 	 *
3675f5c30c4eSAdrian Chadd 	 * XXX sc_updateslot isn't changed behind a lock?
3676c42a7b7eSSam Leffler 	 */
367759aa14a9SRui Paulo 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
367859aa14a9SRui Paulo 	    ic->ic_opmode == IEEE80211_M_MBSS)
3679c42a7b7eSSam Leffler 		sc->sc_updateslot = UPDATE;
3680c42a7b7eSSam Leffler 	else
3681c42a7b7eSSam Leffler 		ath_setslottime(sc);
3682c42a7b7eSSam Leffler }
3683c42a7b7eSSam Leffler 
3684c42a7b7eSSam Leffler /*
3685622b3fd2SSam Leffler  * Append the contents of src to dst; both queues
3686622b3fd2SSam Leffler  * are assumed to be locked.
3687622b3fd2SSam Leffler  */
3688ba5c15d9SAdrian Chadd void
3689622b3fd2SSam Leffler ath_txqmove(struct ath_txq *dst, struct ath_txq *src)
3690622b3fd2SSam Leffler {
3691e86fd7a7SAdrian Chadd 
3692b837332dSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(src);
3693b837332dSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(dst);
3694b837332dSAdrian Chadd 
36956b349e5aSAdrian Chadd 	TAILQ_CONCAT(&dst->axq_q, &src->axq_q, bf_list);
3696622b3fd2SSam Leffler 	dst->axq_link = src->axq_link;
3697622b3fd2SSam Leffler 	src->axq_link = NULL;
3698622b3fd2SSam Leffler 	dst->axq_depth += src->axq_depth;
36996edf1dc7SAdrian Chadd 	dst->axq_aggr_depth += src->axq_aggr_depth;
3700622b3fd2SSam Leffler 	src->axq_depth = 0;
37016edf1dc7SAdrian Chadd 	src->axq_aggr_depth = 0;
3702622b3fd2SSam Leffler }
3703622b3fd2SSam Leffler 
3704622b3fd2SSam Leffler /*
3705d52f7132SAdrian Chadd  * Reset the hardware, with no loss.
3706d52f7132SAdrian Chadd  *
3707d52f7132SAdrian Chadd  * This can't be used for a general case reset.
3708d52f7132SAdrian Chadd  */
3709d52f7132SAdrian Chadd static void
3710d52f7132SAdrian Chadd ath_reset_proc(void *arg, int pending)
3711d52f7132SAdrian Chadd {
3712d52f7132SAdrian Chadd 	struct ath_softc *sc = arg;
3713d52f7132SAdrian Chadd 
3714d52f7132SAdrian Chadd #if 0
371576e6fd5dSGleb Smirnoff 	device_printf(sc->sc_dev, "%s: resetting\n", __func__);
3716d52f7132SAdrian Chadd #endif
37177a79cebfSGleb Smirnoff 	ath_reset(sc, ATH_RESET_NOLOSS);
3718d52f7132SAdrian Chadd }
3719d52f7132SAdrian Chadd 
3720d52f7132SAdrian Chadd /*
3721c42a7b7eSSam Leffler  * Reset the hardware after detecting beacons have stopped.
3722c42a7b7eSSam Leffler  */
3723c42a7b7eSSam Leffler static void
3724c42a7b7eSSam Leffler ath_bstuck_proc(void *arg, int pending)
3725c42a7b7eSSam Leffler {
3726c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
372716d4de92SAdrian Chadd 	uint32_t hangs = 0;
372816d4de92SAdrian Chadd 
372916d4de92SAdrian Chadd 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0)
373076e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "bb hang detected (0x%x)\n", hangs);
3731c42a7b7eSSam Leffler 
3732370f81faSAdrian Chadd #ifdef	ATH_DEBUG_ALQ
3733370f81faSAdrian Chadd 	if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_STUCK_BEACON))
3734370f81faSAdrian Chadd 		if_ath_alq_post(&sc->sc_alq, ATH_ALQ_STUCK_BEACON, 0, NULL);
3735370f81faSAdrian Chadd #endif
3736370f81faSAdrian Chadd 
373776e6fd5dSGleb Smirnoff 	device_printf(sc->sc_dev, "stuck beacon; resetting (bmiss count %u)\n",
3738c42a7b7eSSam Leffler 	    sc->sc_bmisscount);
3739c2e34459SSam Leffler 	sc->sc_stats.ast_bstuck++;
374016d4de92SAdrian Chadd 	/*
374116d4de92SAdrian Chadd 	 * This assumes that there's no simultaneous channel mode change
3742f6b6084bSPedro F. Giffuni 	 * occurring.
374316d4de92SAdrian Chadd 	 */
37447a79cebfSGleb Smirnoff 	ath_reset(sc, ATH_RESET_NOLOSS);
3745c42a7b7eSSam Leffler }
3746c42a7b7eSSam Leffler 
3747c42a7b7eSSam Leffler static int
37485591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc)
37495591b213SSam Leffler {
3750c42a7b7eSSam Leffler 	int error;
37515591b213SSam Leffler 
3752c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
375309067b6eSAdrian Chadd 		    "tx", sc->sc_tx_desclen, ath_txbuf, ATH_MAX_SCATTER);
3754c42a7b7eSSam Leffler 	if (error != 0) {
37555591b213SSam Leffler 		return error;
3756c42a7b7eSSam Leffler 	}
375723ced6c1SAdrian Chadd 	sc->sc_txbuf_cnt = ath_txbuf;
3758c42a7b7eSSam Leffler 
3759af33d486SAdrian Chadd 	error = ath_descdma_setup(sc, &sc->sc_txdma_mgmt, &sc->sc_txbuf_mgmt,
37601006fc0cSAdrian Chadd 		    "tx_mgmt", sc->sc_tx_desclen, ath_txbuf_mgmt,
37611006fc0cSAdrian Chadd 		    ATH_TXDESC);
3762af33d486SAdrian Chadd 	if (error != 0) {
3763af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3764af33d486SAdrian Chadd 		return error;
3765af33d486SAdrian Chadd 	}
3766af33d486SAdrian Chadd 
3767af33d486SAdrian Chadd 	/*
3768af33d486SAdrian Chadd 	 * XXX mark txbuf_mgmt frames with ATH_BUF_MGMT, so the
3769af33d486SAdrian Chadd 	 * flag doesn't have to be set in ath_getbuf_locked().
3770af33d486SAdrian Chadd 	 */
3771af33d486SAdrian Chadd 
3772c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
37731006fc0cSAdrian Chadd 			"beacon", sc->sc_tx_desclen, ATH_BCBUF, 1);
3774c42a7b7eSSam Leffler 	if (error != 0) {
3775af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3776af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
3777af33d486SAdrian Chadd 		    &sc->sc_txbuf_mgmt);
3778c42a7b7eSSam Leffler 		return error;
3779c42a7b7eSSam Leffler 	}
37805591b213SSam Leffler 	return 0;
37815591b213SSam Leffler }
37825591b213SSam Leffler 
37835591b213SSam Leffler static void
37845591b213SSam Leffler ath_desc_free(struct ath_softc *sc)
37855591b213SSam Leffler {
37865591b213SSam Leffler 
3787c42a7b7eSSam Leffler 	if (sc->sc_bdma.dd_desc_len != 0)
3788c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
3789c42a7b7eSSam Leffler 	if (sc->sc_txdma.dd_desc_len != 0)
3790c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3791af33d486SAdrian Chadd 	if (sc->sc_txdma_mgmt.dd_desc_len != 0)
3792af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
3793af33d486SAdrian Chadd 		    &sc->sc_txbuf_mgmt);
37945591b213SSam Leffler }
37955591b213SSam Leffler 
37965591b213SSam Leffler static struct ieee80211_node *
379738c208f8SSam Leffler ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
37985591b213SSam Leffler {
379938c208f8SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
38003797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
3801c42a7b7eSSam Leffler 	const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
3802c42a7b7eSSam Leffler 	struct ath_node *an;
3803c42a7b7eSSam Leffler 
3804c42a7b7eSSam Leffler 	an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
3805c42a7b7eSSam Leffler 	if (an == NULL) {
3806c42a7b7eSSam Leffler 		/* XXX stat+msg */
3807de5af704SSam Leffler 		return NULL;
38085591b213SSam Leffler 	}
3809c42a7b7eSSam Leffler 	ath_rate_node_init(sc, an);
38105591b213SSam Leffler 
38113dd85b26SAdrian Chadd 	/* Setup the mutex - there's no associd yet so set the name to NULL */
38123dd85b26SAdrian Chadd 	snprintf(an->an_name, sizeof(an->an_name), "%s: node %p",
38133dd85b26SAdrian Chadd 	    device_get_nameunit(sc->sc_dev), an);
38143dd85b26SAdrian Chadd 	mtx_init(&an->an_mtx, an->an_name, NULL, MTX_DEF);
38153dd85b26SAdrian Chadd 
3816eb6f0de0SAdrian Chadd 	/* XXX setup ath_tid */
3817eb6f0de0SAdrian Chadd 	ath_tx_tid_init(sc, an);
3818eb6f0de0SAdrian Chadd 
38199b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__, mac, ":", an);
3820c42a7b7eSSam Leffler 	return &an->an_node;
3821c42a7b7eSSam Leffler }
3822c42a7b7eSSam Leffler 
38235591b213SSam Leffler static void
38244afa805eSAdrian Chadd ath_node_cleanup(struct ieee80211_node *ni)
38254afa805eSAdrian Chadd {
38264afa805eSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
38273797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
38284afa805eSAdrian Chadd 
38299b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__,
38309b48fb4bSAdrian Chadd 	    ni->ni_macaddr, ":", ATH_NODE(ni));
38319b48fb4bSAdrian Chadd 
38324afa805eSAdrian Chadd 	/* Cleanup ath_tid, free unused bufs, unlink bufs in TXQ */
3833eb6f0de0SAdrian Chadd 	ath_tx_node_flush(sc, ATH_NODE(ni));
38344afa805eSAdrian Chadd 	ath_rate_node_cleanup(sc, ATH_NODE(ni));
38354afa805eSAdrian Chadd 	sc->sc_node_cleanup(ni);
38364afa805eSAdrian Chadd }
38374afa805eSAdrian Chadd 
38384afa805eSAdrian Chadd static void
3839c42a7b7eSSam Leffler ath_node_free(struct ieee80211_node *ni)
38405591b213SSam Leffler {
3841c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
38423797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
38431e774079SSam Leffler 
38449b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__,
38459b48fb4bSAdrian Chadd 	    ni->ni_macaddr, ":", ATH_NODE(ni));
38463dd85b26SAdrian Chadd 	mtx_destroy(&ATH_NODE(ni)->an_mtx);
3847c42a7b7eSSam Leffler 	sc->sc_node_free(ni);
38485591b213SSam Leffler }
38495591b213SSam Leffler 
385068e8e04eSSam Leffler static void
385168e8e04eSSam Leffler ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
385268e8e04eSSam Leffler {
385368e8e04eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
38543797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
385568e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
385668e8e04eSSam Leffler 
3857b032f27cSSam Leffler 	*rssi = ic->ic_node_getrssi(ni);
385859efa8b5SSam Leffler 	if (ni->ni_chan != IEEE80211_CHAN_ANYC)
385959efa8b5SSam Leffler 		*noise = ath_hal_getchannoise(ah, ni->ni_chan);
386059efa8b5SSam Leffler 	else
386168e8e04eSSam Leffler 		*noise = -95;		/* nominally correct */
386268e8e04eSSam Leffler }
386368e8e04eSSam Leffler 
3864c42a7b7eSSam Leffler /*
3865c42a7b7eSSam Leffler  * Set the default antenna.
3866c42a7b7eSSam Leffler  */
3867e60c4fc2SAdrian Chadd void
3868c42a7b7eSSam Leffler ath_setdefantenna(struct ath_softc *sc, u_int antenna)
3869c42a7b7eSSam Leffler {
3870c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3871c42a7b7eSSam Leffler 
3872c42a7b7eSSam Leffler 	/* XXX block beacon interrupts */
3873c42a7b7eSSam Leffler 	ath_hal_setdefantenna(ah, antenna);
3874c42a7b7eSSam Leffler 	if (sc->sc_defant != antenna)
3875c42a7b7eSSam Leffler 		sc->sc_stats.ast_ant_defswitch++;
3876c42a7b7eSSam Leffler 	sc->sc_defant = antenna;
3877c42a7b7eSSam Leffler 	sc->sc_rxotherant = 0;
3878c42a7b7eSSam Leffler }
3879c42a7b7eSSam Leffler 
38805463c4a4SSam Leffler static void
3881622b3fd2SSam Leffler ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum)
3882622b3fd2SSam Leffler {
3883622b3fd2SSam Leffler 	txq->axq_qnum = qnum;
3884339ccfb3SSam Leffler 	txq->axq_ac = 0;
3885622b3fd2SSam Leffler 	txq->axq_depth = 0;
388616d4de92SAdrian Chadd 	txq->axq_aggr_depth = 0;
3887622b3fd2SSam Leffler 	txq->axq_intrcnt = 0;
3888622b3fd2SSam Leffler 	txq->axq_link = NULL;
38896b349e5aSAdrian Chadd 	txq->axq_softc = sc;
38906b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_q);
38916b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_tidq);
38923feffbd7SAdrian Chadd 	TAILQ_INIT(&txq->fifo.axq_q);
3893b837332dSAdrian Chadd 	ATH_TXQ_LOCK_INIT(sc, txq);
3894622b3fd2SSam Leffler }
3895622b3fd2SSam Leffler 
38965591b213SSam Leffler /*
3897c42a7b7eSSam Leffler  * Setup a h/w transmit queue.
38985591b213SSam Leffler  */
3899c42a7b7eSSam Leffler static struct ath_txq *
3900c42a7b7eSSam Leffler ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
3901c42a7b7eSSam Leffler {
3902c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3903c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
3904c42a7b7eSSam Leffler 	int qnum;
3905c42a7b7eSSam Leffler 
3906c42a7b7eSSam Leffler 	memset(&qi, 0, sizeof(qi));
3907c42a7b7eSSam Leffler 	qi.tqi_subtype = subtype;
3908c42a7b7eSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
3909c42a7b7eSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
3910c42a7b7eSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
3911c42a7b7eSSam Leffler 	/*
3912c42a7b7eSSam Leffler 	 * Enable interrupts only for EOL and DESC conditions.
3913c42a7b7eSSam Leffler 	 * We mark tx descriptors to receive a DESC interrupt
3914c42a7b7eSSam Leffler 	 * when a tx queue gets deep; otherwise waiting for the
3915c42a7b7eSSam Leffler 	 * EOL to reap descriptors.  Note that this is done to
3916c42a7b7eSSam Leffler 	 * reduce interrupt load and this only defers reaping
3917c42a7b7eSSam Leffler 	 * descriptors, never transmitting frames.  Aside from
3918c42a7b7eSSam Leffler 	 * reducing interrupts this also permits more concurrency.
3919c42a7b7eSSam Leffler 	 * The only potential downside is if the tx queue backs
3920c42a7b7eSSam Leffler 	 * up in which case the top half of the kernel may backup
3921c42a7b7eSSam Leffler 	 * due to a lack of tx descriptors.
3922c42a7b7eSSam Leffler 	 */
39236961e9edSAdrian Chadd 	if (sc->sc_isedma)
39246961e9edSAdrian Chadd 		qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE |
39256961e9edSAdrian Chadd 		    HAL_TXQ_TXOKINT_ENABLE;
39266961e9edSAdrian Chadd 	else
39276961e9edSAdrian Chadd 		qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE |
39286961e9edSAdrian Chadd 		    HAL_TXQ_TXDESCINT_ENABLE;
39296961e9edSAdrian Chadd 
3930c42a7b7eSSam Leffler 	qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
3931c42a7b7eSSam Leffler 	if (qnum == -1) {
3932c42a7b7eSSam Leffler 		/*
3933c42a7b7eSSam Leffler 		 * NB: don't print a message, this happens
3934a614e076SSam Leffler 		 * normally on parts with too few tx queues
3935c42a7b7eSSam Leffler 		 */
3936c42a7b7eSSam Leffler 		return NULL;
3937c42a7b7eSSam Leffler 	}
3938d6166defSAdrian Chadd 	if (qnum >= nitems(sc->sc_txq)) {
39396891c875SPeter Wemm 		device_printf(sc->sc_dev,
39406891c875SPeter Wemm 			"hal qnum %u out of range, max %zu!\n",
3941d6166defSAdrian Chadd 			qnum, nitems(sc->sc_txq));
3942c42a7b7eSSam Leffler 		ath_hal_releasetxqueue(ah, qnum);
3943c42a7b7eSSam Leffler 		return NULL;
3944c42a7b7eSSam Leffler 	}
3945c42a7b7eSSam Leffler 	if (!ATH_TXQ_SETUP(sc, qnum)) {
3946622b3fd2SSam Leffler 		ath_txq_init(sc, &sc->sc_txq[qnum], qnum);
3947c42a7b7eSSam Leffler 		sc->sc_txqsetup |= 1<<qnum;
3948c42a7b7eSSam Leffler 	}
3949c42a7b7eSSam Leffler 	return &sc->sc_txq[qnum];
3950c42a7b7eSSam Leffler }
3951c42a7b7eSSam Leffler 
3952c42a7b7eSSam Leffler /*
3953c42a7b7eSSam Leffler  * Setup a hardware data transmit queue for the specified
3954c42a7b7eSSam Leffler  * access control.  The hal may not support all requested
3955c42a7b7eSSam Leffler  * queues in which case it will return a reference to a
3956c42a7b7eSSam Leffler  * previously setup queue.  We record the mapping from ac's
3957c42a7b7eSSam Leffler  * to h/w queues for use by ath_tx_start and also track
3958c42a7b7eSSam Leffler  * the set of h/w queues being used to optimize work in the
3959c42a7b7eSSam Leffler  * transmit interrupt handler and related routines.
3960c42a7b7eSSam Leffler  */
3961c42a7b7eSSam Leffler static int
3962c42a7b7eSSam Leffler ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
3963c42a7b7eSSam Leffler {
3964c42a7b7eSSam Leffler 	struct ath_txq *txq;
3965c42a7b7eSSam Leffler 
3966d6166defSAdrian Chadd 	if (ac >= nitems(sc->sc_ac2q)) {
39676891c875SPeter Wemm 		device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
3968d6166defSAdrian Chadd 			ac, nitems(sc->sc_ac2q));
3969c42a7b7eSSam Leffler 		return 0;
3970c42a7b7eSSam Leffler 	}
3971c42a7b7eSSam Leffler 	txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
3972c42a7b7eSSam Leffler 	if (txq != NULL) {
3973339ccfb3SSam Leffler 		txq->axq_ac = ac;
3974c42a7b7eSSam Leffler 		sc->sc_ac2q[ac] = txq;
3975c42a7b7eSSam Leffler 		return 1;
3976c42a7b7eSSam Leffler 	} else
3977c42a7b7eSSam Leffler 		return 0;
3978c42a7b7eSSam Leffler }
3979c42a7b7eSSam Leffler 
3980c42a7b7eSSam Leffler /*
3981c42a7b7eSSam Leffler  * Update WME parameters for a transmit queue.
3982c42a7b7eSSam Leffler  */
3983c42a7b7eSSam Leffler static int
3984c42a7b7eSSam Leffler ath_txq_update(struct ath_softc *sc, int ac)
3985c42a7b7eSSam Leffler {
3986c42a7b7eSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<v)-1)
39877a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
3988c42a7b7eSSam Leffler 	struct ath_txq *txq = sc->sc_ac2q[ac];
3989c42a7b7eSSam Leffler 	struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
3990c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3991c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
3992c42a7b7eSSam Leffler 
3993c42a7b7eSSam Leffler 	ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
3994584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
399510ad9a77SSam Leffler 	if (sc->sc_tdma) {
399610ad9a77SSam Leffler 		/*
399710ad9a77SSam Leffler 		 * AIFS is zero so there's no pre-transmit wait.  The
399810ad9a77SSam Leffler 		 * burst time defines the slot duration and is configured
399909be6601SSam Leffler 		 * through net80211.  The QCU is setup to not do post-xmit
400010ad9a77SSam Leffler 		 * back off, lockout all lower-priority QCU's, and fire
400110ad9a77SSam Leffler 		 * off the DMA beacon alert timer which is setup based
400210ad9a77SSam Leffler 		 * on the slot configuration.
400310ad9a77SSam Leffler 		 */
400410ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
400510ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
400610ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
400710ad9a77SSam Leffler 			      | HAL_TXQ_TXEOLINT_ENABLE
400810ad9a77SSam Leffler 			      | HAL_TXQ_DBA_GATED
400910ad9a77SSam Leffler 			      | HAL_TXQ_BACKOFF_DISABLE
401010ad9a77SSam Leffler 			      | HAL_TXQ_ARB_LOCKOUT_GLOBAL
401110ad9a77SSam Leffler 			      ;
401210ad9a77SSam Leffler 		qi.tqi_aifs = 0;
401310ad9a77SSam Leffler 		/* XXX +dbaprep? */
401410ad9a77SSam Leffler 		qi.tqi_readyTime = sc->sc_tdmaslotlen;
401510ad9a77SSam Leffler 		qi.tqi_burstTime = qi.tqi_readyTime;
401610ad9a77SSam Leffler 	} else {
401710ad9a77SSam Leffler #endif
401816d4de92SAdrian Chadd 		/*
401916d4de92SAdrian Chadd 		 * XXX shouldn't this just use the default flags
402016d4de92SAdrian Chadd 		 * used in the previous queue setup?
402116d4de92SAdrian Chadd 		 */
402210ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
402310ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
402410ad9a77SSam Leffler 			      | HAL_TXQ_TXDESCINT_ENABLE
402510ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
40261f25c0f7SAdrian Chadd 			      | HAL_TXQ_TXEOLINT_ENABLE
402710ad9a77SSam Leffler 			      ;
4028c42a7b7eSSam Leffler 		qi.tqi_aifs = wmep->wmep_aifsn;
4029c42a7b7eSSam Leffler 		qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
4030c42a7b7eSSam Leffler 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
403110ad9a77SSam Leffler 		qi.tqi_readyTime = 0;
4032d6166defSAdrian Chadd 		qi.tqi_burstTime = IEEE80211_TXOP_TO_US(wmep->wmep_txopLimit);
4033584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
403410ad9a77SSam Leffler 	}
403510ad9a77SSam Leffler #endif
403610ad9a77SSam Leffler 
403710ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
403810ad9a77SSam Leffler 	    "%s: Q%u qflags 0x%x aifs %u cwmin %u cwmax %u burstTime %u\n",
403910ad9a77SSam Leffler 	    __func__, txq->axq_qnum, qi.tqi_qflags,
404010ad9a77SSam Leffler 	    qi.tqi_aifs, qi.tqi_cwmin, qi.tqi_cwmax, qi.tqi_burstTime);
4041c42a7b7eSSam Leffler 
4042c42a7b7eSSam Leffler 	if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
404376e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "unable to update hardware queue "
404476e6fd5dSGleb Smirnoff 		    "parameters for %s traffic!\n", ieee80211_wme_acnames[ac]);
4045c42a7b7eSSam Leffler 		return 0;
4046c42a7b7eSSam Leffler 	} else {
4047c42a7b7eSSam Leffler 		ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
4048c42a7b7eSSam Leffler 		return 1;
4049c42a7b7eSSam Leffler 	}
4050c42a7b7eSSam Leffler #undef ATH_EXPONENT_TO_VALUE
4051c42a7b7eSSam Leffler }
4052c42a7b7eSSam Leffler 
4053c42a7b7eSSam Leffler /*
4054c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update WME parameters.
4055c42a7b7eSSam Leffler  */
4056a35dae8dSAdrian Chadd int
4057c42a7b7eSSam Leffler ath_wme_update(struct ieee80211com *ic)
4058c42a7b7eSSam Leffler {
40593797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
4060c42a7b7eSSam Leffler 
4061c42a7b7eSSam Leffler 	return !ath_txq_update(sc, WME_AC_BE) ||
4062c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_BK) ||
4063c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VI) ||
4064c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
4065c42a7b7eSSam Leffler }
4066c42a7b7eSSam Leffler 
4067c42a7b7eSSam Leffler /*
4068c42a7b7eSSam Leffler  * Reclaim resources for a setup queue.
4069c42a7b7eSSam Leffler  */
4070c42a7b7eSSam Leffler static void
4071c42a7b7eSSam Leffler ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
4072c42a7b7eSSam Leffler {
4073c42a7b7eSSam Leffler 
4074c42a7b7eSSam Leffler 	ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
4075c42a7b7eSSam Leffler 	sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
4076b837332dSAdrian Chadd 	ATH_TXQ_LOCK_DESTROY(txq);
4077c42a7b7eSSam Leffler }
4078c42a7b7eSSam Leffler 
4079c42a7b7eSSam Leffler /*
4080c42a7b7eSSam Leffler  * Reclaim all tx queue resources.
4081c42a7b7eSSam Leffler  */
4082c42a7b7eSSam Leffler static void
4083c42a7b7eSSam Leffler ath_tx_cleanup(struct ath_softc *sc)
4084c42a7b7eSSam Leffler {
4085c42a7b7eSSam Leffler 	int i;
4086c42a7b7eSSam Leffler 
4087c42a7b7eSSam Leffler 	ATH_TXBUF_LOCK_DESTROY(sc);
4088c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4089c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
4090c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, &sc->sc_txq[i]);
4091c42a7b7eSSam Leffler }
40925591b213SSam Leffler 
409399d258fdSSam Leffler /*
4094ab06fdf2SSam Leffler  * Return h/w rate index for an IEEE rate (w/o basic rate bit)
4095ab06fdf2SSam Leffler  * using the current rates in sc_rixmap.
40968b5341deSSam Leffler  */
4097b8e788a5SAdrian Chadd int
4098ab06fdf2SSam Leffler ath_tx_findrix(const struct ath_softc *sc, uint8_t rate)
40998b5341deSSam Leffler {
4100ab06fdf2SSam Leffler 	int rix = sc->sc_rixmap[rate];
4101ab06fdf2SSam Leffler 	/* NB: return lowest rix for invalid rate */
4102ab06fdf2SSam Leffler 	return (rix == 0xff ? 0 : rix);
41038b5341deSSam Leffler }
41048b5341deSSam Leffler 
41059352fb7aSAdrian Chadd static void
41069352fb7aSAdrian Chadd ath_tx_update_stats(struct ath_softc *sc, struct ath_tx_status *ts,
41079352fb7aSAdrian Chadd     struct ath_buf *bf)
41089352fb7aSAdrian Chadd {
41099352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
41107a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
41119352fb7aSAdrian Chadd 	int sr, lr, pri;
41129352fb7aSAdrian Chadd 
41139352fb7aSAdrian Chadd 	if (ts->ts_status == 0) {
41149352fb7aSAdrian Chadd 		u_int8_t txant = ts->ts_antenna;
41159352fb7aSAdrian Chadd 		sc->sc_stats.ast_ant_tx[txant]++;
41169352fb7aSAdrian Chadd 		sc->sc_ant_tx[txant]++;
41179352fb7aSAdrian Chadd 		if (ts->ts_finaltsi != 0)
41189352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_altrate++;
41199352fb7aSAdrian Chadd 		pri = M_WME_GETAC(bf->bf_m);
41209352fb7aSAdrian Chadd 		if (pri >= WME_AC_VO)
41219352fb7aSAdrian Chadd 			ic->ic_wme.wme_hipri_traffic++;
4122875a9451SAdrian Chadd 		if ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)
41239352fb7aSAdrian Chadd 			ni->ni_inact = ni->ni_inact_reload;
41249352fb7aSAdrian Chadd 	} else {
41259352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XRETRY)
41269352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xretries++;
41279352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FIFO)
41289352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_fifoerr++;
41299352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FILT)
41309352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_filtered++;
41319352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XTXOP)
41329352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xtxop++;
41339352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_TIMER_EXPIRED)
41349352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_timerexpired++;
41359352fb7aSAdrian Chadd 
41369352fb7aSAdrian Chadd 		if (bf->bf_m->m_flags & M_FF)
41379352fb7aSAdrian Chadd 			sc->sc_stats.ast_ff_txerr++;
41389352fb7aSAdrian Chadd 	}
41399352fb7aSAdrian Chadd 	/* XXX when is this valid? */
4140158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DESC_CFG_ERR)
41419352fb7aSAdrian Chadd 		sc->sc_stats.ast_tx_desccfgerr++;
4142158cb431SAdrian Chadd 	/*
4143158cb431SAdrian Chadd 	 * This can be valid for successful frame transmission!
4144158cb431SAdrian Chadd 	 * If there's a TX FIFO underrun during aggregate transmission,
4145158cb431SAdrian Chadd 	 * the MAC will pad the rest of the aggregate with delimiters.
4146158cb431SAdrian Chadd 	 * If a BA is returned, the frame is marked as "OK" and it's up
4147158cb431SAdrian Chadd 	 * to the TX completion code to notice which frames weren't
4148158cb431SAdrian Chadd 	 * successfully transmitted.
4149158cb431SAdrian Chadd 	 */
4150158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DATA_UNDERRUN)
4151158cb431SAdrian Chadd 		sc->sc_stats.ast_tx_data_underrun++;
4152158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DELIM_UNDERRUN)
4153158cb431SAdrian Chadd 		sc->sc_stats.ast_tx_delim_underrun++;
41549352fb7aSAdrian Chadd 
41559352fb7aSAdrian Chadd 	sr = ts->ts_shortretry;
41569352fb7aSAdrian Chadd 	lr = ts->ts_longretry;
41579352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_shortretry += sr;
41589352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_longretry += lr;
41599352fb7aSAdrian Chadd 
41609352fb7aSAdrian Chadd }
41619352fb7aSAdrian Chadd 
41629352fb7aSAdrian Chadd /*
41639352fb7aSAdrian Chadd  * The default completion. If fail is 1, this means
41649352fb7aSAdrian Chadd  * "please don't retry the frame, and just return -1 status
41659352fb7aSAdrian Chadd  * to the net80211 stack.
41669352fb7aSAdrian Chadd  */
41679352fb7aSAdrian Chadd void
41689352fb7aSAdrian Chadd ath_tx_default_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
41699352fb7aSAdrian Chadd {
41709352fb7aSAdrian Chadd 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
41719352fb7aSAdrian Chadd 	int st;
41729352fb7aSAdrian Chadd 
41739352fb7aSAdrian Chadd 	if (fail == 1)
41749352fb7aSAdrian Chadd 		st = -1;
41759352fb7aSAdrian Chadd 	else
4176875a9451SAdrian Chadd 		st = ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) ?
41779352fb7aSAdrian Chadd 		    ts->ts_status : HAL_TXERR_XRETRY;
41789352fb7aSAdrian Chadd 
4179ce597531SAdrian Chadd #if 0
41809352fb7aSAdrian Chadd 	if (bf->bf_state.bfs_dobaw)
41819352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
4182a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: dobaw should've been cleared!\n",
4183a66d5089SAdrian Chadd 		    __func__,
4184a66d5089SAdrian Chadd 		    bf,
4185a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
4186ce597531SAdrian Chadd #endif
41879352fb7aSAdrian Chadd 	if (bf->bf_next != NULL)
41889352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
4189a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: bf_next not NULL!\n",
4190a66d5089SAdrian Chadd 		    __func__,
4191a66d5089SAdrian Chadd 		    bf,
4192a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
41939352fb7aSAdrian Chadd 
41949352fb7aSAdrian Chadd 	/*
4195548a605dSAdrian Chadd 	 * Check if the node software queue is empty; if so
4196548a605dSAdrian Chadd 	 * then clear the TIM.
4197548a605dSAdrian Chadd 	 *
4198548a605dSAdrian Chadd 	 * This needs to be done before the buffer is freed as
4199548a605dSAdrian Chadd 	 * otherwise the node reference will have been released
4200548a605dSAdrian Chadd 	 * and the node may not actually exist any longer.
4201548a605dSAdrian Chadd 	 *
4202548a605dSAdrian Chadd 	 * XXX I don't like this belonging here, but it's cleaner
4203548a605dSAdrian Chadd 	 * to do it here right now then all the other places
4204548a605dSAdrian Chadd 	 * where ath_tx_default_comp() is called.
4205548a605dSAdrian Chadd 	 *
4206548a605dSAdrian Chadd 	 * XXX TODO: during drain, ensure that the callback is
4207548a605dSAdrian Chadd 	 * being called so we get a chance to update the TIM.
4208548a605dSAdrian Chadd 	 */
42094bed2b67SAdrian Chadd 	if (bf->bf_node) {
42104bed2b67SAdrian Chadd 		ATH_TX_LOCK(sc);
4211548a605dSAdrian Chadd 		ath_tx_update_tim(sc, bf->bf_node, 0);
42124bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
42134bed2b67SAdrian Chadd 	}
4214548a605dSAdrian Chadd 
4215548a605dSAdrian Chadd 	/*
42169352fb7aSAdrian Chadd 	 * Do any tx complete callback.  Note this must
42179352fb7aSAdrian Chadd 	 * be done before releasing the node reference.
42189352fb7aSAdrian Chadd 	 * This will free the mbuf, release the net80211
42199352fb7aSAdrian Chadd 	 * node and recycle the ath_buf.
42209352fb7aSAdrian Chadd 	 */
42219352fb7aSAdrian Chadd 	ath_tx_freebuf(sc, bf, st);
42229352fb7aSAdrian Chadd }
42239352fb7aSAdrian Chadd 
42249352fb7aSAdrian Chadd /*
4225eb6f0de0SAdrian Chadd  * Update rate control with the given completion status.
4226eb6f0de0SAdrian Chadd  */
4227eb6f0de0SAdrian Chadd void
4228eb6f0de0SAdrian Chadd ath_tx_update_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
4229eb6f0de0SAdrian Chadd     struct ath_rc_series *rc, struct ath_tx_status *ts, int frmlen,
4230eb6f0de0SAdrian Chadd     int nframes, int nbad)
4231eb6f0de0SAdrian Chadd {
4232eb6f0de0SAdrian Chadd 	struct ath_node *an;
4233eb6f0de0SAdrian Chadd 
4234eb6f0de0SAdrian Chadd 	/* Only for unicast frames */
4235eb6f0de0SAdrian Chadd 	if (ni == NULL)
4236eb6f0de0SAdrian Chadd 		return;
4237eb6f0de0SAdrian Chadd 
4238eb6f0de0SAdrian Chadd 	an = ATH_NODE(ni);
4239548a605dSAdrian Chadd 	ATH_NODE_UNLOCK_ASSERT(an);
4240eb6f0de0SAdrian Chadd 
4241eb6f0de0SAdrian Chadd 	if ((ts->ts_status & HAL_TXERR_FILT) == 0) {
4242eb6f0de0SAdrian Chadd 		ATH_NODE_LOCK(an);
4243eb6f0de0SAdrian Chadd 		ath_rate_tx_complete(sc, an, rc, ts, frmlen, nframes, nbad);
4244eb6f0de0SAdrian Chadd 		ATH_NODE_UNLOCK(an);
4245eb6f0de0SAdrian Chadd 	}
4246eb6f0de0SAdrian Chadd }
4247eb6f0de0SAdrian Chadd 
4248eb6f0de0SAdrian Chadd /*
4249bad98824SAdrian Chadd  * Process the completion of the given buffer.
4250bad98824SAdrian Chadd  *
4251bad98824SAdrian Chadd  * This calls the rate control update and then the buffer completion.
4252bad98824SAdrian Chadd  * This will either free the buffer or requeue it.  In any case, the
4253bad98824SAdrian Chadd  * bf pointer should be treated as invalid after this function is called.
4254bad98824SAdrian Chadd  */
4255bad98824SAdrian Chadd void
4256bad98824SAdrian Chadd ath_tx_process_buf_completion(struct ath_softc *sc, struct ath_txq *txq,
4257bad98824SAdrian Chadd     struct ath_tx_status *ts, struct ath_buf *bf)
4258bad98824SAdrian Chadd {
4259bad98824SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
4260bad98824SAdrian Chadd 
4261375307d4SAdrian Chadd 	ATH_TX_UNLOCK_ASSERT(sc);
42625e018508SAdrian Chadd 	ATH_TXQ_UNLOCK_ASSERT(txq);
4263bad98824SAdrian Chadd 
4264bad98824SAdrian Chadd 	/* If unicast frame, update general statistics */
4265bad98824SAdrian Chadd 	if (ni != NULL) {
4266bad98824SAdrian Chadd 		/* update statistics */
4267bad98824SAdrian Chadd 		ath_tx_update_stats(sc, ts, bf);
4268bad98824SAdrian Chadd 	}
4269bad98824SAdrian Chadd 
4270bad98824SAdrian Chadd 	/*
4271bad98824SAdrian Chadd 	 * Call the completion handler.
4272bad98824SAdrian Chadd 	 * The completion handler is responsible for
4273bad98824SAdrian Chadd 	 * calling the rate control code.
4274bad98824SAdrian Chadd 	 *
4275bad98824SAdrian Chadd 	 * Frames with no completion handler get the
4276bad98824SAdrian Chadd 	 * rate control code called here.
4277bad98824SAdrian Chadd 	 */
4278bad98824SAdrian Chadd 	if (bf->bf_comp == NULL) {
4279bad98824SAdrian Chadd 		if ((ts->ts_status & HAL_TXERR_FILT) == 0 &&
4280bad98824SAdrian Chadd 		    (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) {
4281bad98824SAdrian Chadd 			/*
4282bad98824SAdrian Chadd 			 * XXX assume this isn't an aggregate
4283bad98824SAdrian Chadd 			 * frame.
4284bad98824SAdrian Chadd 			 */
4285bad98824SAdrian Chadd 			ath_tx_update_ratectrl(sc, ni,
4286bad98824SAdrian Chadd 			     bf->bf_state.bfs_rc, ts,
4287bad98824SAdrian Chadd 			    bf->bf_state.bfs_pktlen, 1,
4288bad98824SAdrian Chadd 			    (ts->ts_status == 0 ? 0 : 1));
4289bad98824SAdrian Chadd 		}
4290bad98824SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4291bad98824SAdrian Chadd 	} else
4292bad98824SAdrian Chadd 		bf->bf_comp(sc, bf, 0);
4293bad98824SAdrian Chadd }
4294bad98824SAdrian Chadd 
4295bad98824SAdrian Chadd 
4296bad98824SAdrian Chadd 
4297bad98824SAdrian Chadd /*
4298c42a7b7eSSam Leffler  * Process completed xmit descriptors from the specified queue.
4299eb6f0de0SAdrian Chadd  * Kick the packet scheduler if needed. This can occur from this
4300eb6f0de0SAdrian Chadd  * particular task.
4301c42a7b7eSSam Leffler  */
4302788e6aa9SAdrian Chadd static int
4303788e6aa9SAdrian Chadd ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq, int dosched)
43045591b213SSam Leffler {
43055591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
43069352fb7aSAdrian Chadd 	struct ath_buf *bf;
43076edf1dc7SAdrian Chadd 	struct ath_desc *ds;
430865f9edeeSSam Leffler 	struct ath_tx_status *ts;
43095591b213SSam Leffler 	struct ieee80211_node *ni;
431053e98d5aSAdrian Chadd #ifdef	IEEE80211_SUPPORT_SUPERG
43117a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
431253e98d5aSAdrian Chadd #endif	/* IEEE80211_SUPPORT_SUPERG */
43139352fb7aSAdrian Chadd 	int nacked;
43145591b213SSam Leffler 	HAL_STATUS status;
43155591b213SSam Leffler 
4316c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
4317c42a7b7eSSam Leffler 		__func__, txq->axq_qnum,
4318c42a7b7eSSam Leffler 		(caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
4319c42a7b7eSSam Leffler 		txq->axq_link);
432003682514SAdrian Chadd 
432103682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 4,
432203682514SAdrian Chadd 	    "ath_tx_processq: txq=%u head %p link %p depth %p",
432303682514SAdrian Chadd 	    txq->axq_qnum,
432403682514SAdrian Chadd 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
432503682514SAdrian Chadd 	    txq->axq_link,
432603682514SAdrian Chadd 	    txq->axq_depth);
432703682514SAdrian Chadd 
4328d7736e13SSam Leffler 	nacked = 0;
43295591b213SSam Leffler 	for (;;) {
4330b837332dSAdrian Chadd 		ATH_TXQ_LOCK(txq);
4331c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;	/* reset periodic desc intr count */
43326b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&txq->axq_q);
43335591b213SSam Leffler 		if (bf == NULL) {
4334b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
43355591b213SSam Leffler 			break;
43365591b213SSam Leffler 		}
43376edf1dc7SAdrian Chadd 		ds = bf->bf_lastds;	/* XXX must be setup correctly! */
433865f9edeeSSam Leffler 		ts = &bf->bf_status.ds_txstat;
433903682514SAdrian Chadd 
434065f9edeeSSam Leffler 		status = ath_hal_txprocdesc(ah, ds, ts);
4341a585a9a1SSam Leffler #ifdef ATH_DEBUG
4342c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
43436902009eSSam Leffler 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
43446902009eSSam Leffler 			    status == HAL_OK);
434503682514SAdrian Chadd 		else if ((sc->sc_debug & ATH_DEBUG_RESET) && (dosched == 0))
4346d6b20023SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
4347d6b20023SAdrian Chadd 			    status == HAL_OK);
43485591b213SSam Leffler #endif
4349bb327d28SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
4350bb327d28SAdrian Chadd 		if (if_ath_alq_checkdebug(&sc->sc_alq,
4351bb327d28SAdrian Chadd 		    ATH_ALQ_EDMA_TXSTATUS)) {
4352bb327d28SAdrian Chadd 			if_ath_alq_post(&sc->sc_alq, ATH_ALQ_EDMA_TXSTATUS,
4353bb327d28SAdrian Chadd 			sc->sc_tx_statuslen,
4354bb327d28SAdrian Chadd 			(char *) ds);
4355bb327d28SAdrian Chadd 		}
4356bb327d28SAdrian Chadd #endif
435703682514SAdrian Chadd 
43585591b213SSam Leffler 		if (status == HAL_EINPROGRESS) {
435903682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_TXCOMP, 3,
436003682514SAdrian Chadd 			    "ath_tx_processq: txq=%u, bf=%p ds=%p, HAL_EINPROGRESS",
436103682514SAdrian Chadd 			    txq->axq_qnum, bf, ds);
4362b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
43635591b213SSam Leffler 			break;
43645591b213SSam Leffler 		}
43656b349e5aSAdrian Chadd 		ATH_TXQ_REMOVE(txq, bf, bf_list);
43665e018508SAdrian Chadd 
43675e018508SAdrian Chadd 		/*
43685e018508SAdrian Chadd 		 * Sanity check.
43695e018508SAdrian Chadd 		 */
43705e018508SAdrian Chadd 		if (txq->axq_qnum != bf->bf_state.bfs_tx_queue) {
43715e018508SAdrian Chadd 			device_printf(sc->sc_dev,
43725e018508SAdrian Chadd 			    "%s: TXQ=%d: bf=%p, bfs_tx_queue=%d\n",
43735e018508SAdrian Chadd 			    __func__,
43745e018508SAdrian Chadd 			    txq->axq_qnum,
43755e018508SAdrian Chadd 			    bf,
43765e018508SAdrian Chadd 			    bf->bf_state.bfs_tx_queue);
43775e018508SAdrian Chadd 		}
43785e018508SAdrian Chadd 		if (txq->axq_qnum != bf->bf_last->bf_state.bfs_tx_queue) {
43795e018508SAdrian Chadd 			device_printf(sc->sc_dev,
43805e018508SAdrian Chadd 			    "%s: TXQ=%d: bf_last=%p, bfs_tx_queue=%d\n",
43815e018508SAdrian Chadd 			    __func__,
43825e018508SAdrian Chadd 			    txq->axq_qnum,
43835e018508SAdrian Chadd 			    bf->bf_last,
43845e018508SAdrian Chadd 			    bf->bf_last->bf_state.bfs_tx_queue);
43855e018508SAdrian Chadd 		}
43865e018508SAdrian Chadd 
43875e018508SAdrian Chadd #if 0
4388d3731e4bSAdrian Chadd 		if (txq->axq_depth > 0) {
438910ad9a77SSam Leffler 			/*
4390d3731e4bSAdrian Chadd 			 * More frames follow.  Mark the buffer busy
4391d3731e4bSAdrian Chadd 			 * so it's not re-used while the hardware may
4392d3731e4bSAdrian Chadd 			 * still re-read the link field in the descriptor.
43936edf1dc7SAdrian Chadd 			 *
4394d3731e4bSAdrian Chadd 			 * Use the last buffer in an aggregate as that
4395d3731e4bSAdrian Chadd 			 * is where the hardware may be - intermediate
4396d3731e4bSAdrian Chadd 			 * descriptors won't be "busy".
439710ad9a77SSam Leffler 			 */
43986edf1dc7SAdrian Chadd 			bf->bf_last->bf_flags |= ATH_BUF_BUSY;
4399d3731e4bSAdrian Chadd 		} else
4400d3731e4bSAdrian Chadd 			txq->axq_link = NULL;
44015e018508SAdrian Chadd #else
44025e018508SAdrian Chadd 		bf->bf_last->bf_flags |= ATH_BUF_BUSY;
44035e018508SAdrian Chadd #endif
44046edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
44056edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
44065591b213SSam Leffler 
44075591b213SSam Leffler 		ni = bf->bf_node;
440803682514SAdrian Chadd 
440903682514SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TXCOMP, 5,
441003682514SAdrian Chadd 		    "ath_tx_processq: txq=%u, bf=%p, ds=%p, ni=%p, ts_status=0x%08x",
441103682514SAdrian Chadd 		    txq->axq_qnum, bf, ds, ni, ts->ts_status);
4412c42a7b7eSSam Leffler 		/*
44139352fb7aSAdrian Chadd 		 * If unicast frame was ack'd update RSSI,
441484784be1SSam Leffler 		 * including the last rx time used to
441584784be1SSam Leffler 		 * workaround phantom bmiss interrupts.
4416d7736e13SSam Leffler 		 */
44179352fb7aSAdrian Chadd 		if (ni != NULL && ts->ts_status == 0 &&
4418875a9451SAdrian Chadd 		    ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)) {
4419d7736e13SSam Leffler 			nacked++;
442084784be1SSam Leffler 			sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
442184784be1SSam Leffler 			ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi,
442284784be1SSam Leffler 				ts->ts_rssi);
442384784be1SSam Leffler 		}
4424b837332dSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
44259352fb7aSAdrian Chadd 
4426bad98824SAdrian Chadd 		/*
4427bad98824SAdrian Chadd 		 * Update statistics and call completion
4428bad98824SAdrian Chadd 		 */
4429bad98824SAdrian Chadd 		ath_tx_process_buf_completion(sc, txq, ts, bf);
4430548a605dSAdrian Chadd 
4431548a605dSAdrian Chadd 		/* XXX at this point, bf and ni may be totally invalid */
44325591b213SSam Leffler 	}
4433339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
443468e8e04eSSam Leffler 	/*
443568e8e04eSSam Leffler 	 * Flush fast-frame staging queue when traffic slows.
443668e8e04eSSam Leffler 	 */
443768e8e04eSSam Leffler 	if (txq->axq_depth <= 1)
443804f19fd6SSam Leffler 		ieee80211_ff_flush(ic, txq->axq_ac);
4439339ccfb3SSam Leffler #endif
4440eb6f0de0SAdrian Chadd 
444121bca442SAdrian Chadd 	/* Kick the software TXQ scheduler */
4442eb6f0de0SAdrian Chadd 	if (dosched) {
4443a40880adSAdrian Chadd 		ATH_TX_LOCK(sc);
4444a40880adSAdrian Chadd 		ath_txq_sched(sc, txq);
4445a40880adSAdrian Chadd 		ATH_TX_UNLOCK(sc);
4446eb6f0de0SAdrian Chadd 	}
4447eb6f0de0SAdrian Chadd 
444803682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
444903682514SAdrian Chadd 	    "ath_tx_processq: txq=%u: done",
445003682514SAdrian Chadd 	    txq->axq_qnum);
445103682514SAdrian Chadd 
4452d7736e13SSam Leffler 	return nacked;
4453d7736e13SSam Leffler }
4454d7736e13SSam Leffler 
44558f939e79SAdrian Chadd #define	TXQACTIVE(t, q)		( (t) & (1 << (q)))
4456c42a7b7eSSam Leffler 
4457c42a7b7eSSam Leffler /*
4458c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4459c42a7b7eSSam Leffler  * for a single hardware transmit queue (e.g. 5210 and 5211).
4460c42a7b7eSSam Leffler  */
4461c42a7b7eSSam Leffler static void
4462c42a7b7eSSam Leffler ath_tx_proc_q0(void *arg, int npending)
4463c42a7b7eSSam Leffler {
4464c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
44658f939e79SAdrian Chadd 	uint32_t txqs;
4466c42a7b7eSSam Leffler 
4467ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4468ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
44698f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
44708f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4471ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
44728f939e79SAdrian Chadd 
4473f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4474f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4475f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4476f5c30c4eSAdrian Chadd 
447703682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
447803682514SAdrian Chadd 	    "ath_tx_proc_q0: txqs=0x%08x", txqs);
447903682514SAdrian Chadd 
448096ff485dSAdrian Chadd 	if (TXQACTIVE(txqs, 0) && ath_tx_processq(sc, &sc->sc_txq[0], 1))
44818f939e79SAdrian Chadd 		/* XXX why is lastrx updated in tx code? */
4482d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
44838f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
448496ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
44852e986da5SSam Leffler 	sc->sc_wd_timer = 0;
44865591b213SSam Leffler 
44873e50ec2cSSam Leffler 	if (sc->sc_softled)
448846d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
44893e50ec2cSSam Leffler 
4490ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4491ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4492ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
44931a85141aSAdrian Chadd 
4494f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4495f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4496f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4497f5c30c4eSAdrian Chadd 
44981a85141aSAdrian Chadd 	ath_tx_kick(sc);
44995591b213SSam Leffler }
45005591b213SSam Leffler 
45015591b213SSam Leffler /*
4502c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4503c42a7b7eSSam Leffler  * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
45045591b213SSam Leffler  */
45055591b213SSam Leffler static void
4506c42a7b7eSSam Leffler ath_tx_proc_q0123(void *arg, int npending)
4507c42a7b7eSSam Leffler {
4508c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4509d7736e13SSam Leffler 	int nacked;
45108f939e79SAdrian Chadd 	uint32_t txqs;
45118f939e79SAdrian Chadd 
4512ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4513ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
45148f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
45158f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4516ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4517c42a7b7eSSam Leffler 
4518f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4519f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4520f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4521f5c30c4eSAdrian Chadd 
452203682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
452303682514SAdrian Chadd 	    "ath_tx_proc_q0123: txqs=0x%08x", txqs);
452403682514SAdrian Chadd 
4525c42a7b7eSSam Leffler 	/*
4526c42a7b7eSSam Leffler 	 * Process each active queue.
4527c42a7b7eSSam Leffler 	 */
4528d7736e13SSam Leffler 	nacked = 0;
45298f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 0))
453096ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[0], 1);
45318f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 1))
453296ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[1], 1);
45338f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 2))
453496ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[2], 1);
45358f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 3))
453696ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[3], 1);
45378f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
453896ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
4539d7736e13SSam Leffler 	if (nacked)
4540d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4541c42a7b7eSSam Leffler 
45422e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4543c42a7b7eSSam Leffler 
45443e50ec2cSSam Leffler 	if (sc->sc_softled)
454546d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
45463e50ec2cSSam Leffler 
4547ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4548ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4549ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
45501a85141aSAdrian Chadd 
4551f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4552f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4553f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4554f5c30c4eSAdrian Chadd 
45551a85141aSAdrian Chadd 	ath_tx_kick(sc);
4556c42a7b7eSSam Leffler }
4557c42a7b7eSSam Leffler 
4558c42a7b7eSSam Leffler /*
4559c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt.
4560c42a7b7eSSam Leffler  */
4561c42a7b7eSSam Leffler static void
4562c42a7b7eSSam Leffler ath_tx_proc(void *arg, int npending)
4563c42a7b7eSSam Leffler {
4564c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4565d7736e13SSam Leffler 	int i, nacked;
45668f939e79SAdrian Chadd 	uint32_t txqs;
45678f939e79SAdrian Chadd 
4568ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4569ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
45708f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
45718f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4572ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4573c42a7b7eSSam Leffler 
4574f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4575f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4576f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4577f5c30c4eSAdrian Chadd 
457803682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1, "ath_tx_proc: txqs=0x%08x", txqs);
457903682514SAdrian Chadd 
4580c42a7b7eSSam Leffler 	/*
4581c42a7b7eSSam Leffler 	 * Process each active queue.
4582c42a7b7eSSam Leffler 	 */
4583d7736e13SSam Leffler 	nacked = 0;
4584c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
45858f939e79SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i) && TXQACTIVE(txqs, i))
458696ff485dSAdrian Chadd 			nacked += ath_tx_processq(sc, &sc->sc_txq[i], 1);
4587d7736e13SSam Leffler 	if (nacked)
4588d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4589c42a7b7eSSam Leffler 
45902e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4591c42a7b7eSSam Leffler 
45923e50ec2cSSam Leffler 	if (sc->sc_softled)
459346d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
45943e50ec2cSSam Leffler 
4595ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4596ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4597ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
45981a85141aSAdrian Chadd 
4599f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4600f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4601f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4602f5c30c4eSAdrian Chadd 
46031a85141aSAdrian Chadd 	ath_tx_kick(sc);
4604c42a7b7eSSam Leffler }
460516d4de92SAdrian Chadd #undef	TXQACTIVE
4606c42a7b7eSSam Leffler 
46079352fb7aSAdrian Chadd /*
460803e9308fSAdrian Chadd  * Deferred processing of TXQ rescheduling.
460903e9308fSAdrian Chadd  */
461003e9308fSAdrian Chadd static void
461103e9308fSAdrian Chadd ath_txq_sched_tasklet(void *arg, int npending)
461203e9308fSAdrian Chadd {
461303e9308fSAdrian Chadd 	struct ath_softc *sc = arg;
461403e9308fSAdrian Chadd 	int i;
461503e9308fSAdrian Chadd 
461603e9308fSAdrian Chadd 	/* XXX is skipping ok? */
461703e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
461803e9308fSAdrian Chadd #if 0
461903e9308fSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
462003e9308fSAdrian Chadd 		device_printf(sc->sc_dev,
462103e9308fSAdrian Chadd 		    "%s: sc_inreset_cnt > 0; skipping\n", __func__);
462203e9308fSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
462303e9308fSAdrian Chadd 		return;
462403e9308fSAdrian Chadd 	}
462503e9308fSAdrian Chadd #endif
462603e9308fSAdrian Chadd 	sc->sc_txproc_cnt++;
462703e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
462803e9308fSAdrian Chadd 
4629f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4630f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4631f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4632f5c30c4eSAdrian Chadd 
4633375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
463403e9308fSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
4635b5a9dfd5SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
463603e9308fSAdrian Chadd 			ath_txq_sched(sc, &sc->sc_txq[i]);
4637b5a9dfd5SAdrian Chadd 		}
463803e9308fSAdrian Chadd 	}
4639375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
464003e9308fSAdrian Chadd 
4641f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4642f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4643f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4644f5c30c4eSAdrian Chadd 
464503e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
464603e9308fSAdrian Chadd 	sc->sc_txproc_cnt--;
464703e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
464803e9308fSAdrian Chadd }
464903e9308fSAdrian Chadd 
4650e1a50456SAdrian Chadd void
4651e1a50456SAdrian Chadd ath_returnbuf_tail(struct ath_softc *sc, struct ath_buf *bf)
4652e1a50456SAdrian Chadd {
4653e1a50456SAdrian Chadd 
4654e1a50456SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
4655e1a50456SAdrian Chadd 
4656af33d486SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_MGMT)
4657af33d486SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf_mgmt, bf, bf_list);
465823ced6c1SAdrian Chadd 	else {
4659e1a50456SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
466023ced6c1SAdrian Chadd 		sc->sc_txbuf_cnt++;
466123ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt > ath_txbuf) {
466223ced6c1SAdrian Chadd 			device_printf(sc->sc_dev,
466323ced6c1SAdrian Chadd 			    "%s: sc_txbuf_cnt > %d?\n",
466423ced6c1SAdrian Chadd 			    __func__,
466523ced6c1SAdrian Chadd 			    ath_txbuf);
466623ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt = ath_txbuf;
466723ced6c1SAdrian Chadd 		}
466823ced6c1SAdrian Chadd 	}
4669e1a50456SAdrian Chadd }
4670e1a50456SAdrian Chadd 
4671e1a50456SAdrian Chadd void
4672e1a50456SAdrian Chadd ath_returnbuf_head(struct ath_softc *sc, struct ath_buf *bf)
4673e1a50456SAdrian Chadd {
4674e1a50456SAdrian Chadd 
4675e1a50456SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
4676e1a50456SAdrian Chadd 
4677af33d486SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_MGMT)
4678af33d486SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf_mgmt, bf, bf_list);
467923ced6c1SAdrian Chadd 	else {
4680e1a50456SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
468123ced6c1SAdrian Chadd 		sc->sc_txbuf_cnt++;
468223ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt > ATH_TXBUF) {
468323ced6c1SAdrian Chadd 			device_printf(sc->sc_dev,
468423ced6c1SAdrian Chadd 			    "%s: sc_txbuf_cnt > %d?\n",
468523ced6c1SAdrian Chadd 			    __func__,
468623ced6c1SAdrian Chadd 			    ATH_TXBUF);
468723ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt = ATH_TXBUF;
468823ced6c1SAdrian Chadd 		}
468923ced6c1SAdrian Chadd 	}
4690e1a50456SAdrian Chadd }
4691e1a50456SAdrian Chadd 
469203e9308fSAdrian Chadd /*
4693629ce218SAdrian Chadd  * Free the holding buffer if it exists
4694629ce218SAdrian Chadd  */
46953feffbd7SAdrian Chadd void
4696629ce218SAdrian Chadd ath_txq_freeholdingbuf(struct ath_softc *sc, struct ath_txq *txq)
4697629ce218SAdrian Chadd {
46985e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK_ASSERT(sc);
46995e018508SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
4700629ce218SAdrian Chadd 
4701629ce218SAdrian Chadd 	if (txq->axq_holdingbf == NULL)
4702629ce218SAdrian Chadd 		return;
4703629ce218SAdrian Chadd 
4704629ce218SAdrian Chadd 	txq->axq_holdingbf->bf_flags &= ~ATH_BUF_BUSY;
47055e018508SAdrian Chadd 
47065e018508SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
4707629ce218SAdrian Chadd 	ath_returnbuf_tail(sc, txq->axq_holdingbf);
47085e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
47095e018508SAdrian Chadd 
4710629ce218SAdrian Chadd 	txq->axq_holdingbf = NULL;
4711629ce218SAdrian Chadd }
4712629ce218SAdrian Chadd 
4713629ce218SAdrian Chadd /*
4714629ce218SAdrian Chadd  * Add this buffer to the holding queue, freeing the previous
4715629ce218SAdrian Chadd  * one if it exists.
4716629ce218SAdrian Chadd  */
4717629ce218SAdrian Chadd static void
4718629ce218SAdrian Chadd ath_txq_addholdingbuf(struct ath_softc *sc, struct ath_buf *bf)
4719629ce218SAdrian Chadd {
4720629ce218SAdrian Chadd 	struct ath_txq *txq;
4721629ce218SAdrian Chadd 
47225e018508SAdrian Chadd 	txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue];
47235e018508SAdrian Chadd 
47245e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK_ASSERT(sc);
47255e018508SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
47265f2f0e61SAdrian Chadd 
4727629ce218SAdrian Chadd 	/* XXX assert ATH_BUF_BUSY is set */
4728629ce218SAdrian Chadd 
4729629ce218SAdrian Chadd 	/* XXX assert the tx queue is under the max number */
4730629ce218SAdrian Chadd 	if (bf->bf_state.bfs_tx_queue > HAL_NUM_TX_QUEUES) {
4731629ce218SAdrian Chadd 		device_printf(sc->sc_dev, "%s: bf=%p: invalid tx queue (%d)\n",
4732629ce218SAdrian Chadd 		    __func__,
4733629ce218SAdrian Chadd 		    bf,
4734629ce218SAdrian Chadd 		    bf->bf_state.bfs_tx_queue);
4735629ce218SAdrian Chadd 		bf->bf_flags &= ~ATH_BUF_BUSY;
4736629ce218SAdrian Chadd 		ath_returnbuf_tail(sc, bf);
4737629ce218SAdrian Chadd 		return;
4738629ce218SAdrian Chadd 	}
4739629ce218SAdrian Chadd 	ath_txq_freeholdingbuf(sc, txq);
4740629ce218SAdrian Chadd 	txq->axq_holdingbf = bf;
4741629ce218SAdrian Chadd }
4742629ce218SAdrian Chadd 
4743629ce218SAdrian Chadd /*
47449352fb7aSAdrian Chadd  * Return a buffer to the pool and update the 'busy' flag on the
47459352fb7aSAdrian Chadd  * previous 'tail' entry.
47469352fb7aSAdrian Chadd  *
47479352fb7aSAdrian Chadd  * This _must_ only be called when the buffer is involved in a completed
47489352fb7aSAdrian Chadd  * TX. The logic is that if it was part of an active TX, the previous
47499352fb7aSAdrian Chadd  * buffer on the list is now not involved in a halted TX DMA queue, waiting
47509352fb7aSAdrian Chadd  * for restart (eg for TDMA.)
47519352fb7aSAdrian Chadd  *
47529352fb7aSAdrian Chadd  * The caller must free the mbuf and recycle the node reference.
47535e018508SAdrian Chadd  *
47545e018508SAdrian Chadd  * XXX This method of handling busy / holding buffers is insanely stupid.
47555e018508SAdrian Chadd  * It requires bf_state.bfs_tx_queue to be correctly assigned.  It would
47565e018508SAdrian Chadd  * be much nicer if buffers in the processq() methods would instead be
47575e018508SAdrian Chadd  * always completed there (pushed onto a txq or ath_bufhead) so we knew
47585e018508SAdrian Chadd  * exactly what hardware queue they came from in the first place.
47599352fb7aSAdrian Chadd  */
47609352fb7aSAdrian Chadd void
47619352fb7aSAdrian Chadd ath_freebuf(struct ath_softc *sc, struct ath_buf *bf)
47629352fb7aSAdrian Chadd {
47635e018508SAdrian Chadd 	struct ath_txq *txq;
47645e018508SAdrian Chadd 
47655e018508SAdrian Chadd 	txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue];
47665e018508SAdrian Chadd 
47679352fb7aSAdrian Chadd 	KASSERT((bf->bf_node == NULL), ("%s: bf->bf_node != NULL\n", __func__));
47689352fb7aSAdrian Chadd 	KASSERT((bf->bf_m == NULL), ("%s: bf->bf_m != NULL\n", __func__));
47699352fb7aSAdrian Chadd 
4770629ce218SAdrian Chadd 	/*
47715e018508SAdrian Chadd 	 * If this buffer is busy, push it onto the holding queue.
4772629ce218SAdrian Chadd 	 */
4773629ce218SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_BUSY) {
47745e018508SAdrian Chadd 		ATH_TXQ_LOCK(txq);
4775629ce218SAdrian Chadd 		ath_txq_addholdingbuf(sc, bf);
47765e018508SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
4777629ce218SAdrian Chadd 		return;
4778629ce218SAdrian Chadd 	}
4779629ce218SAdrian Chadd 
4780629ce218SAdrian Chadd 	/*
4781629ce218SAdrian Chadd 	 * Not a busy buffer, so free normally
4782629ce218SAdrian Chadd 	 */
47839352fb7aSAdrian Chadd 	ATH_TXBUF_LOCK(sc);
4784e1a50456SAdrian Chadd 	ath_returnbuf_tail(sc, bf);
47859352fb7aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
47869352fb7aSAdrian Chadd }
47879352fb7aSAdrian Chadd 
47889352fb7aSAdrian Chadd /*
47899352fb7aSAdrian Chadd  * This is currently used by ath_tx_draintxq() and
47909352fb7aSAdrian Chadd  * ath_tx_tid_free_pkts().
47919352fb7aSAdrian Chadd  *
47929352fb7aSAdrian Chadd  * It recycles a single ath_buf.
47939352fb7aSAdrian Chadd  */
47949352fb7aSAdrian Chadd void
47959352fb7aSAdrian Chadd ath_tx_freebuf(struct ath_softc *sc, struct ath_buf *bf, int status)
47969352fb7aSAdrian Chadd {
47979352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
47989352fb7aSAdrian Chadd 	struct mbuf *m0 = bf->bf_m;
47999352fb7aSAdrian Chadd 
48003f3a5dbdSAdrian Chadd 	/*
48013f3a5dbdSAdrian Chadd 	 * Make sure that we only sync/unload if there's an mbuf.
48023f3a5dbdSAdrian Chadd 	 * If not (eg we cloned a buffer), the unload will have already
4803f6b6084bSPedro F. Giffuni 	 * occurred.
48043f3a5dbdSAdrian Chadd 	 */
48053f3a5dbdSAdrian Chadd 	if (bf->bf_m != NULL) {
48063f3a5dbdSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
48073f3a5dbdSAdrian Chadd 		    BUS_DMASYNC_POSTWRITE);
48083f3a5dbdSAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
48093f3a5dbdSAdrian Chadd 	}
48103f3a5dbdSAdrian Chadd 
48119352fb7aSAdrian Chadd 	bf->bf_node = NULL;
48129352fb7aSAdrian Chadd 	bf->bf_m = NULL;
48139352fb7aSAdrian Chadd 
48149352fb7aSAdrian Chadd 	/* Free the buffer, it's not needed any longer */
48159352fb7aSAdrian Chadd 	ath_freebuf(sc, bf);
48169352fb7aSAdrian Chadd 
4817e95f3424SAdrian Chadd 	/* Pass the buffer back to net80211 - completing it */
4818e95f3424SAdrian Chadd 	ieee80211_tx_complete(ni, m0, status);
48199352fb7aSAdrian Chadd }
48209352fb7aSAdrian Chadd 
48213feffbd7SAdrian Chadd static struct ath_buf *
48223feffbd7SAdrian Chadd ath_tx_draintxq_get_one(struct ath_softc *sc, struct ath_txq *txq)
48233feffbd7SAdrian Chadd {
48243feffbd7SAdrian Chadd 	struct ath_buf *bf;
48253feffbd7SAdrian Chadd 
48263feffbd7SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
48273feffbd7SAdrian Chadd 
48283feffbd7SAdrian Chadd 	/*
48293feffbd7SAdrian Chadd 	 * Drain the FIFO queue first, then if it's
48303feffbd7SAdrian Chadd 	 * empty, move to the normal frame queue.
48313feffbd7SAdrian Chadd 	 */
48323feffbd7SAdrian Chadd 	bf = TAILQ_FIRST(&txq->fifo.axq_q);
48333feffbd7SAdrian Chadd 	if (bf != NULL) {
48343feffbd7SAdrian Chadd 		/*
48353feffbd7SAdrian Chadd 		 * Is it the last buffer in this set?
48363feffbd7SAdrian Chadd 		 * Decrement the FIFO counter.
48373feffbd7SAdrian Chadd 		 */
48383feffbd7SAdrian Chadd 		if (bf->bf_flags & ATH_BUF_FIFOEND) {
48393feffbd7SAdrian Chadd 			if (txq->axq_fifo_depth == 0) {
48403feffbd7SAdrian Chadd 				device_printf(sc->sc_dev,
48413feffbd7SAdrian Chadd 				    "%s: Q%d: fifo_depth=0, fifo.axq_depth=%d?\n",
48423feffbd7SAdrian Chadd 				    __func__,
48433feffbd7SAdrian Chadd 				    txq->axq_qnum,
48443feffbd7SAdrian Chadd 				    txq->fifo.axq_depth);
48453feffbd7SAdrian Chadd 			} else
48463feffbd7SAdrian Chadd 				txq->axq_fifo_depth--;
48473feffbd7SAdrian Chadd 		}
48483feffbd7SAdrian Chadd 		ATH_TXQ_REMOVE(&txq->fifo, bf, bf_list);
48493feffbd7SAdrian Chadd 		return (bf);
48503feffbd7SAdrian Chadd 	}
48513feffbd7SAdrian Chadd 
48523feffbd7SAdrian Chadd 	/*
48533feffbd7SAdrian Chadd 	 * Debugging!
48543feffbd7SAdrian Chadd 	 */
48553feffbd7SAdrian Chadd 	if (txq->axq_fifo_depth != 0 || txq->fifo.axq_depth != 0) {
48563feffbd7SAdrian Chadd 		device_printf(sc->sc_dev,
48573feffbd7SAdrian Chadd 		    "%s: Q%d: fifo_depth=%d, fifo.axq_depth=%d\n",
48583feffbd7SAdrian Chadd 		    __func__,
48593feffbd7SAdrian Chadd 		    txq->axq_qnum,
48603feffbd7SAdrian Chadd 		    txq->axq_fifo_depth,
48613feffbd7SAdrian Chadd 		    txq->fifo.axq_depth);
48623feffbd7SAdrian Chadd 	}
48633feffbd7SAdrian Chadd 
48643feffbd7SAdrian Chadd 	/*
48653feffbd7SAdrian Chadd 	 * Now drain the pending queue.
48663feffbd7SAdrian Chadd 	 */
48673feffbd7SAdrian Chadd 	bf = TAILQ_FIRST(&txq->axq_q);
48683feffbd7SAdrian Chadd 	if (bf == NULL) {
48693feffbd7SAdrian Chadd 		txq->axq_link = NULL;
48703feffbd7SAdrian Chadd 		return (NULL);
48713feffbd7SAdrian Chadd 	}
48723feffbd7SAdrian Chadd 	ATH_TXQ_REMOVE(txq, bf, bf_list);
48733feffbd7SAdrian Chadd 	return (bf);
48743feffbd7SAdrian Chadd }
48753feffbd7SAdrian Chadd 
48769352fb7aSAdrian Chadd void
48771762ec94SAdrian Chadd ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
48785591b213SSam Leffler {
4879a585a9a1SSam Leffler #ifdef ATH_DEBUG
48805591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4881d2f6ed15SSam Leffler #endif
48825591b213SSam Leffler 	struct ath_buf *bf;
48837a4c5ed9SSam Leffler 	u_int ix;
48845591b213SSam Leffler 
4885c42a7b7eSSam Leffler 	/*
4886c42a7b7eSSam Leffler 	 * NB: this assumes output has been stopped and
48875d61b5e8SSam Leffler 	 *     we do not need to block ath_tx_proc
4888c42a7b7eSSam Leffler 	 */
48897a4c5ed9SSam Leffler 	for (ix = 0;; ix++) {
4890b837332dSAdrian Chadd 		ATH_TXQ_LOCK(txq);
48913feffbd7SAdrian Chadd 		bf = ath_tx_draintxq_get_one(sc, txq);
48925591b213SSam Leffler 		if (bf == NULL) {
4893b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
48945591b213SSam Leffler 			break;
48955591b213SSam Leffler 		}
48966edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
48976edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
4898a585a9a1SSam Leffler #ifdef ATH_DEBUG
48994a3ac3fcSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RESET) {
49007a79cebfSGleb Smirnoff 			struct ieee80211com *ic = &sc->sc_ic;
49011762ec94SAdrian Chadd 			int status = 0;
4902b032f27cSSam Leffler 
49031762ec94SAdrian Chadd 			/*
49041762ec94SAdrian Chadd 			 * EDMA operation has a TX completion FIFO
49051762ec94SAdrian Chadd 			 * separate from the TX descriptor, so this
49061762ec94SAdrian Chadd 			 * method of checking the "completion" status
49071762ec94SAdrian Chadd 			 * is wrong.
49081762ec94SAdrian Chadd 			 */
49091762ec94SAdrian Chadd 			if (! sc->sc_isedma) {
49101762ec94SAdrian Chadd 				status = (ath_hal_txprocdesc(ah,
49111762ec94SAdrian Chadd 				    bf->bf_lastds,
491265f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
49131762ec94SAdrian Chadd 			}
49141762ec94SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, ix, status);
4915e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ic, mtod(bf->bf_m, const uint8_t *),
49164a3ac3fcSSam Leffler 			    bf->bf_m->m_len, 0, -1);
49174a3ac3fcSSam Leffler 		}
4918a585a9a1SSam Leffler #endif /* ATH_DEBUG */
491923428eafSSam Leffler 		/*
49209352fb7aSAdrian Chadd 		 * Since we're now doing magic in the completion
49219352fb7aSAdrian Chadd 		 * functions, we -must- call it for aggregation
49229352fb7aSAdrian Chadd 		 * destinations or BAW tracking will get upset.
492323428eafSSam Leffler 		 */
49249352fb7aSAdrian Chadd 		/*
49259352fb7aSAdrian Chadd 		 * Clear ATH_BUF_BUSY; the completion handler
49269352fb7aSAdrian Chadd 		 * will free the buffer.
49279352fb7aSAdrian Chadd 		 */
4928b837332dSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
492910ad9a77SSam Leffler 		bf->bf_flags &= ~ATH_BUF_BUSY;
49309352fb7aSAdrian Chadd 		if (bf->bf_comp)
49319352fb7aSAdrian Chadd 			bf->bf_comp(sc, bf, 1);
49329352fb7aSAdrian Chadd 		else
49339352fb7aSAdrian Chadd 			ath_tx_default_comp(sc, bf, 1);
49345591b213SSam Leffler 	}
49359352fb7aSAdrian Chadd 
4936eb6f0de0SAdrian Chadd 	/*
4937629ce218SAdrian Chadd 	 * Free the holding buffer if it exists
4938629ce218SAdrian Chadd 	 */
49395e018508SAdrian Chadd 	ATH_TXQ_LOCK(txq);
4940629ce218SAdrian Chadd 	ath_txq_freeholdingbuf(sc, txq);
49415e018508SAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
4942629ce218SAdrian Chadd 
4943629ce218SAdrian Chadd 	/*
4944eb6f0de0SAdrian Chadd 	 * Drain software queued frames which are on
4945eb6f0de0SAdrian Chadd 	 * active TIDs.
4946eb6f0de0SAdrian Chadd 	 */
4947eb6f0de0SAdrian Chadd 	ath_tx_txq_drain(sc, txq);
4948c42a7b7eSSam Leffler }
4949c42a7b7eSSam Leffler 
4950c42a7b7eSSam Leffler static void
4951c42a7b7eSSam Leffler ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
4952c42a7b7eSSam Leffler {
4953c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4954c42a7b7eSSam Leffler 
49559be82a42SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
49569be82a42SAdrian Chadd 
49579d2a962bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
4958dfaf8de9SAdrian Chadd 	    "%s: tx queue [%u] %p, active=%d, hwpending=%d, flags 0x%08x, "
4959dfaf8de9SAdrian Chadd 	    "link %p, holdingbf=%p\n",
49609d2a962bSAdrian Chadd 	    __func__,
49619d2a962bSAdrian Chadd 	    txq->axq_qnum,
49626891c875SPeter Wemm 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
49638d060542SAdrian Chadd 	    (int) (!! ath_hal_txqenabled(ah, txq->axq_qnum)),
49648d060542SAdrian Chadd 	    (int) ath_hal_numtxpending(ah, txq->axq_qnum),
49659d2a962bSAdrian Chadd 	    txq->axq_flags,
4966dfaf8de9SAdrian Chadd 	    txq->axq_link,
4967dfaf8de9SAdrian Chadd 	    txq->axq_holdingbf);
4968dfaf8de9SAdrian Chadd 
49694a3ac3fcSSam Leffler 	(void) ath_hal_stoptxdma(ah, txq->axq_qnum);
49709be82a42SAdrian Chadd 	/* We've stopped TX DMA, so mark this as stopped. */
49719be82a42SAdrian Chadd 	txq->axq_flags &= ~ATH_TXQ_PUTRUNNING;
4972dfaf8de9SAdrian Chadd 
4973dfaf8de9SAdrian Chadd #ifdef	ATH_DEBUG
4974dfaf8de9SAdrian Chadd 	if ((sc->sc_debug & ATH_DEBUG_RESET)
4975dfaf8de9SAdrian Chadd 	    && (txq->axq_holdingbf != NULL)) {
4976dfaf8de9SAdrian Chadd 		ath_printtxbuf(sc, txq->axq_holdingbf, txq->axq_qnum, 0, 0);
4977dfaf8de9SAdrian Chadd 	}
4978dfaf8de9SAdrian Chadd #endif
4979c42a7b7eSSam Leffler }
4980c42a7b7eSSam Leffler 
4981bad98824SAdrian Chadd int
49822d433424SAdrian Chadd ath_stoptxdma(struct ath_softc *sc)
4983c42a7b7eSSam Leffler {
4984c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4985c42a7b7eSSam Leffler 	int i;
4986c42a7b7eSSam Leffler 
4987c42a7b7eSSam Leffler 	/* XXX return value */
49882d433424SAdrian Chadd 	if (sc->sc_invalid)
49892d433424SAdrian Chadd 		return 0;
49902d433424SAdrian Chadd 
4991c42a7b7eSSam Leffler 	if (!sc->sc_invalid) {
4992c42a7b7eSSam Leffler 		/* don't touch the hardware if marked invalid */
49934a3ac3fcSSam Leffler 		DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
49944a3ac3fcSSam Leffler 		    __func__, sc->sc_bhalq,
49954a3ac3fcSSam Leffler 		    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq),
49964a3ac3fcSSam Leffler 		    NULL);
49979be82a42SAdrian Chadd 
49989be82a42SAdrian Chadd 		/* stop the beacon queue */
4999c42a7b7eSSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
50009be82a42SAdrian Chadd 
50019be82a42SAdrian Chadd 		/* Stop the data queues */
50029be82a42SAdrian Chadd 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
50039be82a42SAdrian Chadd 			if (ATH_TXQ_SETUP(sc, i)) {
50049be82a42SAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
5005c42a7b7eSSam Leffler 				ath_tx_stopdma(sc, &sc->sc_txq[i]);
50069be82a42SAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
50079be82a42SAdrian Chadd 			}
50089be82a42SAdrian Chadd 		}
5009c42a7b7eSSam Leffler 	}
50102d433424SAdrian Chadd 
50112d433424SAdrian Chadd 	return 1;
50122d433424SAdrian Chadd }
50132d433424SAdrian Chadd 
501407187d11SAdrian Chadd #ifdef	ATH_DEBUG
50159be82a42SAdrian Chadd void
5016ed261a61SAdrian Chadd ath_tx_dump(struct ath_softc *sc, struct ath_txq *txq)
5017ed261a61SAdrian Chadd {
5018ed261a61SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
5019ed261a61SAdrian Chadd 	struct ath_buf *bf;
5020ed261a61SAdrian Chadd 	int i = 0;
5021ed261a61SAdrian Chadd 
5022ed261a61SAdrian Chadd 	if (! (sc->sc_debug & ATH_DEBUG_RESET))
5023ed261a61SAdrian Chadd 		return;
5024ed261a61SAdrian Chadd 
5025ed261a61SAdrian Chadd 	device_printf(sc->sc_dev, "%s: Q%d: begin\n",
5026ed261a61SAdrian Chadd 	    __func__, txq->axq_qnum);
5027ed261a61SAdrian Chadd 	TAILQ_FOREACH(bf, &txq->axq_q, bf_list) {
5028ed261a61SAdrian Chadd 		ath_printtxbuf(sc, bf, txq->axq_qnum, i,
5029ed261a61SAdrian Chadd 			ath_hal_txprocdesc(ah, bf->bf_lastds,
5030ed261a61SAdrian Chadd 			    &bf->bf_status.ds_txstat) == HAL_OK);
5031ed261a61SAdrian Chadd 		i++;
5032ed261a61SAdrian Chadd 	}
5033ed261a61SAdrian Chadd 	device_printf(sc->sc_dev, "%s: Q%d: end\n",
5034ed261a61SAdrian Chadd 	    __func__, txq->axq_qnum);
5035ed261a61SAdrian Chadd }
503607187d11SAdrian Chadd #endif /* ATH_DEBUG */
5037ed261a61SAdrian Chadd 
50382d433424SAdrian Chadd /*
50392d433424SAdrian Chadd  * Drain the transmit queues and reclaim resources.
50402d433424SAdrian Chadd  */
5041788e6aa9SAdrian Chadd void
5042788e6aa9SAdrian Chadd ath_legacy_tx_drain(struct ath_softc *sc, ATH_RESET_TYPE reset_type)
50432d433424SAdrian Chadd {
50442d433424SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
5045ba2c1fbcSAdrian Chadd 	struct ath_buf *bf_last;
50467a79cebfSGleb Smirnoff 	int i;
50472d433424SAdrian Chadd 
50482d433424SAdrian Chadd 	(void) ath_stoptxdma(sc);
50492d433424SAdrian Chadd 
5050ed261a61SAdrian Chadd 	/*
5051ed261a61SAdrian Chadd 	 * Dump the queue contents
5052ed261a61SAdrian Chadd 	 */
5053ef27340cSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
5054ef27340cSAdrian Chadd 		/*
5055ef27340cSAdrian Chadd 		 * XXX TODO: should we just handle the completed TX frames
5056ef27340cSAdrian Chadd 		 * here, whether or not the reset is a full one or not?
5057ef27340cSAdrian Chadd 		 */
5058ef27340cSAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
505907187d11SAdrian Chadd #ifdef	ATH_DEBUG
5060ed261a61SAdrian Chadd 			if (sc->sc_debug & ATH_DEBUG_RESET)
5061ed261a61SAdrian Chadd 				ath_tx_dump(sc, &sc->sc_txq[i]);
506207187d11SAdrian Chadd #endif	/* ATH_DEBUG */
50638328d6e4SAdrian Chadd 			if (reset_type == ATH_RESET_NOLOSS) {
5064ef27340cSAdrian Chadd 				ath_tx_processq(sc, &sc->sc_txq[i], 0);
50658328d6e4SAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
50668328d6e4SAdrian Chadd 				/*
50678328d6e4SAdrian Chadd 				 * Free the holding buffer; DMA is now
50688328d6e4SAdrian Chadd 				 * stopped.
50698328d6e4SAdrian Chadd 				 */
50708328d6e4SAdrian Chadd 				ath_txq_freeholdingbuf(sc, &sc->sc_txq[i]);
50718328d6e4SAdrian Chadd 				/*
50729be82a42SAdrian Chadd 				 * Setup the link pointer to be the
50739be82a42SAdrian Chadd 				 * _last_ buffer/descriptor in the list.
50749be82a42SAdrian Chadd 				 * If there's nothing in the list, set it
50759be82a42SAdrian Chadd 				 * to NULL.
50768328d6e4SAdrian Chadd 				 */
50779be82a42SAdrian Chadd 				bf_last = ATH_TXQ_LAST(&sc->sc_txq[i],
50789be82a42SAdrian Chadd 				    axq_q_s);
50799be82a42SAdrian Chadd 				if (bf_last != NULL) {
50809be82a42SAdrian Chadd 					ath_hal_gettxdesclinkptr(ah,
50819be82a42SAdrian Chadd 					    bf_last->bf_lastds,
50829be82a42SAdrian Chadd 					    &sc->sc_txq[i].axq_link);
50839be82a42SAdrian Chadd 				} else {
50848328d6e4SAdrian Chadd 					sc->sc_txq[i].axq_link = NULL;
50859be82a42SAdrian Chadd 				}
50868328d6e4SAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
50878328d6e4SAdrian Chadd 			} else
5088c42a7b7eSSam Leffler 				ath_tx_draintxq(sc, &sc->sc_txq[i]);
5089ef27340cSAdrian Chadd 		}
5090ef27340cSAdrian Chadd 	}
50914a3ac3fcSSam Leffler #ifdef ATH_DEBUG
50924a3ac3fcSSam Leffler 	if (sc->sc_debug & ATH_DEBUG_RESET) {
50936b349e5aSAdrian Chadd 		struct ath_buf *bf = TAILQ_FIRST(&sc->sc_bbuf);
50944a3ac3fcSSam Leffler 		if (bf != NULL && bf->bf_m != NULL) {
50956902009eSSam Leffler 			ath_printtxbuf(sc, bf, sc->sc_bhalq, 0,
50966edf1dc7SAdrian Chadd 				ath_hal_txprocdesc(ah, bf->bf_lastds,
509765f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
50987a79cebfSGleb Smirnoff 			ieee80211_dump_pkt(&sc->sc_ic,
5099e40b6ab1SSam Leffler 			    mtod(bf->bf_m, const uint8_t *), bf->bf_m->m_len,
5100e40b6ab1SSam Leffler 			    0, -1);
51014a3ac3fcSSam Leffler 		}
51024a3ac3fcSSam Leffler 	}
51034a3ac3fcSSam Leffler #endif /* ATH_DEBUG */
51042e986da5SSam Leffler 	sc->sc_wd_timer = 0;
51055591b213SSam Leffler }
51065591b213SSam Leffler 
51075591b213SSam Leffler /*
5108c42a7b7eSSam Leffler  * Update internal state after a channel change.
5109c42a7b7eSSam Leffler  */
5110c42a7b7eSSam Leffler static void
5111c42a7b7eSSam Leffler ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
5112c42a7b7eSSam Leffler {
5113c42a7b7eSSam Leffler 	enum ieee80211_phymode mode;
5114c42a7b7eSSam Leffler 
5115c42a7b7eSSam Leffler 	/*
5116c42a7b7eSSam Leffler 	 * Change channels and update the h/w rate map
5117c42a7b7eSSam Leffler 	 * if we're switching; e.g. 11a to 11b/g.
5118c42a7b7eSSam Leffler 	 */
511968e8e04eSSam Leffler 	mode = ieee80211_chan2mode(chan);
5120c42a7b7eSSam Leffler 	if (mode != sc->sc_curmode)
5121c42a7b7eSSam Leffler 		ath_setcurmode(sc, mode);
512259efa8b5SSam Leffler 	sc->sc_curchan = chan;
5123c42a7b7eSSam Leffler }
5124c42a7b7eSSam Leffler 
5125c42a7b7eSSam Leffler /*
51265591b213SSam Leffler  * Set/change channels.  If the channel is really being changed,
51274fa8d4efSDaniel Eischen  * it's done by resetting the chip.  To accomplish this we must
51285591b213SSam Leffler  * first cleanup any pending DMA, then restart stuff after a la
51295591b213SSam Leffler  * ath_init.
51305591b213SSam Leffler  */
51315591b213SSam Leffler static int
51325591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
51335591b213SSam Leffler {
51347a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
51355591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5136ef27340cSAdrian Chadd 	int ret = 0;
5137ef27340cSAdrian Chadd 
5138ef27340cSAdrian Chadd 	/* Treat this as an interface reset */
5139d52f7132SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
5140d52f7132SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
5141d52f7132SAdrian Chadd 
5142f6b6084bSPedro F. Giffuni 	/* (Try to) stop TX/RX from occurring */
5143d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
5144d52f7132SAdrian Chadd 
5145ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5146904e385eSAdrian Chadd 
514717bb5fd1SAdrian Chadd 	/* Disable interrupts */
514817bb5fd1SAdrian Chadd 	ath_hal_intrset(ah, 0);
514917bb5fd1SAdrian Chadd 
5150904e385eSAdrian Chadd 	/* Stop new RX/TX/interrupt completion */
5151ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
5152ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
5153ef27340cSAdrian Chadd 		    __func__);
5154ee321975SAdrian Chadd 	}
5155904e385eSAdrian Chadd 
5156904e385eSAdrian Chadd 	/* Stop pending RX/TX completion */
5157904e385eSAdrian Chadd 	ath_txrx_stop_locked(sc);
5158904e385eSAdrian Chadd 
5159ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5160c42a7b7eSSam Leffler 
516159efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz, flags 0x%x)\n",
516259efa8b5SSam Leffler 	    __func__, ieee80211_chan2ieee(ic, chan),
516359efa8b5SSam Leffler 	    chan->ic_freq, chan->ic_flags);
516459efa8b5SSam Leffler 	if (chan != sc->sc_curchan) {
5165c42a7b7eSSam Leffler 		HAL_STATUS status;
51665591b213SSam Leffler 		/*
51675591b213SSam Leffler 		 * To switch channels clear any pending DMA operations;
51685591b213SSam Leffler 		 * wait long enough for the RX fifo to drain, reset the
51695591b213SSam Leffler 		 * hardware at the new frequency, and then re-enable
51705591b213SSam Leffler 		 * the relevant bits of the h/w.
51715591b213SSam Leffler 		 */
5172ef27340cSAdrian Chadd #if 0
51735591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
5174ef27340cSAdrian Chadd #endif
51759a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* turn off frame recv */
51769a842e8bSAdrian Chadd 		/*
51779a842e8bSAdrian Chadd 		 * First, handle completed TX/RX frames.
51789a842e8bSAdrian Chadd 		 */
5179f8cc9b09SAdrian Chadd 		ath_rx_flush(sc);
51809a842e8bSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_NOLOSS);
51819a842e8bSAdrian Chadd 		/*
51829a842e8bSAdrian Chadd 		 * Next, flush the non-scheduled frames.
51839a842e8bSAdrian Chadd 		 */
5184517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_FULL);	/* clear pending tx frames */
51859a842e8bSAdrian Chadd 
51866322256bSAdrian Chadd 		ath_update_chainmasks(sc, chan);
51876322256bSAdrian Chadd 		ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
51886322256bSAdrian Chadd 		    sc->sc_cur_rxchainmask);
5189f50e4ebfSAdrian Chadd 		if (!ath_hal_reset(ah, sc->sc_opmode, chan, AH_TRUE,
5190f50e4ebfSAdrian Chadd 		    HAL_RESET_NORMAL, &status)) {
519176e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev, "%s: unable to reset "
519279649302SGavin Atkinson 			    "channel %u (%u MHz, flags 0x%x), hal status %u\n",
519359efa8b5SSam Leffler 			    __func__, ieee80211_chan2ieee(ic, chan),
519459efa8b5SSam Leffler 			    chan->ic_freq, chan->ic_flags, status);
5195ef27340cSAdrian Chadd 			ret = EIO;
5196ef27340cSAdrian Chadd 			goto finish;
51975591b213SSam Leffler 		}
5198c59005e9SSam Leffler 		sc->sc_diversity = ath_hal_getdiversity(ah);
5199c42a7b7eSSam Leffler 
520017bb5fd1SAdrian Chadd 		ATH_RX_LOCK(sc);
520117bb5fd1SAdrian Chadd 		sc->sc_rx_stopped = 1;
520217bb5fd1SAdrian Chadd 		sc->sc_rx_resetted = 1;
520317bb5fd1SAdrian Chadd 		ATH_RX_UNLOCK(sc);
520417bb5fd1SAdrian Chadd 
520548237774SAdrian Chadd 		/* Let DFS at it in case it's a DFS channel */
5206398bca2eSAdrian Chadd 		ath_dfs_radar_enable(sc, chan);
520748237774SAdrian Chadd 
52089af351f9SAdrian Chadd 		/* Let spectral at in case spectral is enabled */
52099af351f9SAdrian Chadd 		ath_spectral_enable(sc, chan);
52109af351f9SAdrian Chadd 
52115591b213SSam Leffler 		/*
5212b70f530bSAdrian Chadd 		 * Let bluetooth coexistence at in case it's needed for this
5213b70f530bSAdrian Chadd 		 * channel
5214b70f530bSAdrian Chadd 		 */
5215b70f530bSAdrian Chadd 		ath_btcoex_enable(sc, ic->ic_curchan);
5216b70f530bSAdrian Chadd 
5217b70f530bSAdrian Chadd 		/*
5218dd6a574eSAdrian Chadd 		 * If we're doing TDMA, enforce the TXOP limitation for chips
5219dd6a574eSAdrian Chadd 		 * that support it.
5220dd6a574eSAdrian Chadd 		 */
5221dd6a574eSAdrian Chadd 		if (sc->sc_hasenforcetxop && sc->sc_tdma)
5222dd6a574eSAdrian Chadd 			ath_hal_setenforcetxop(sc->sc_ah, 1);
5223dd6a574eSAdrian Chadd 		else
5224dd6a574eSAdrian Chadd 			ath_hal_setenforcetxop(sc->sc_ah, 0);
5225dd6a574eSAdrian Chadd 
5226dd6a574eSAdrian Chadd 		/*
52275591b213SSam Leffler 		 * Re-enable rx framework.
52285591b213SSam Leffler 		 */
52295591b213SSam Leffler 		if (ath_startrecv(sc) != 0) {
523076e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev,
523176e6fd5dSGleb Smirnoff 			    "%s: unable to restart recv logic\n", __func__);
5232ef27340cSAdrian Chadd 			ret = EIO;
5233ef27340cSAdrian Chadd 			goto finish;
52345591b213SSam Leffler 		}
52355591b213SSam Leffler 
52365591b213SSam Leffler 		/*
52375591b213SSam Leffler 		 * Change channels and update the h/w rate map
52385591b213SSam Leffler 		 * if we're switching; e.g. 11a to 11b/g.
52395591b213SSam Leffler 		 */
5240c42a7b7eSSam Leffler 		ath_chan_change(sc, chan);
52410a915fadSSam Leffler 
52420a915fadSSam Leffler 		/*
52432fd9aabbSAdrian Chadd 		 * Reset clears the beacon timers; reset them
52442fd9aabbSAdrian Chadd 		 * here if needed.
52452fd9aabbSAdrian Chadd 		 */
52462fd9aabbSAdrian Chadd 		if (sc->sc_beacons) {		/* restart beacons */
52472fd9aabbSAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
52482fd9aabbSAdrian Chadd 			if (sc->sc_tdma)
52492fd9aabbSAdrian Chadd 				ath_tdma_config(sc, NULL);
52502fd9aabbSAdrian Chadd 			else
52512fd9aabbSAdrian Chadd #endif
52522fd9aabbSAdrian Chadd 			ath_beacon_config(sc, NULL);
52532fd9aabbSAdrian Chadd 		}
52542fd9aabbSAdrian Chadd 
52552fd9aabbSAdrian Chadd 		/*
52560a915fadSSam Leffler 		 * Re-enable interrupts.
52570a915fadSSam Leffler 		 */
5258e78719adSAdrian Chadd #if 0
52590a915fadSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
5260ef27340cSAdrian Chadd #endif
52615591b213SSam Leffler 	}
5262ef27340cSAdrian Chadd 
5263ef27340cSAdrian Chadd finish:
5264ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5265ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
5266ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
5267ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
5268ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5269ef27340cSAdrian Chadd 
5270ef27340cSAdrian Chadd 	ath_txrx_start(sc);
5271ef27340cSAdrian Chadd 	/* XXX ath_start? */
5272ef27340cSAdrian Chadd 
5273ef27340cSAdrian Chadd 	return ret;
52745591b213SSam Leffler }
52755591b213SSam Leffler 
52765591b213SSam Leffler /*
52775591b213SSam Leffler  * Periodically recalibrate the PHY to account
52785591b213SSam Leffler  * for temperature/environment changes.
52795591b213SSam Leffler  */
52805591b213SSam Leffler static void
52815591b213SSam Leffler ath_calibrate(void *arg)
52825591b213SSam Leffler {
52835591b213SSam Leffler 	struct ath_softc *sc = arg;
52845591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
52857a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
5286943e37a1SAdrian Chadd 	HAL_BOOL longCal, isCalDone = AH_TRUE;
5287a108ab63SAdrian Chadd 	HAL_BOOL aniCal, shortCal = AH_FALSE;
52882dc7fcc4SSam Leffler 	int nextcal;
52895591b213SSam Leffler 
5290adcdc8f2SAdrian Chadd 	ATH_LOCK_ASSERT(sc);
52917707f31dSAdrian Chadd 
5292f5c30c4eSAdrian Chadd 	/*
5293f5c30c4eSAdrian Chadd 	 * Force the hardware awake for ANI work.
5294f5c30c4eSAdrian Chadd 	 */
5295f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
5296f5c30c4eSAdrian Chadd 
5297f5c30c4eSAdrian Chadd 	/* Skip trying to do this if we're in reset */
5298f5c30c4eSAdrian Chadd 	if (sc->sc_inreset_cnt)
5299f5c30c4eSAdrian Chadd 		goto restart;
5300f5c30c4eSAdrian Chadd 
53018d91de92SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SCAN)	/* defer, off channel */
53028d91de92SSam Leffler 		goto restart;
53032dc7fcc4SSam Leffler 	longCal = (ticks - sc->sc_lastlongcal >= ath_longcalinterval*hz);
5304a108ab63SAdrian Chadd 	aniCal = (ticks - sc->sc_lastani >= ath_anicalinterval*hz/1000);
5305a108ab63SAdrian Chadd 	if (sc->sc_doresetcal)
5306a108ab63SAdrian Chadd 		shortCal = (ticks - sc->sc_lastshortcal >= ath_shortcalinterval*hz/1000);
5307a108ab63SAdrian Chadd 
5308a108ab63SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: shortCal=%d; longCal=%d; aniCal=%d\n", __func__, shortCal, longCal, aniCal);
5309a108ab63SAdrian Chadd 	if (aniCal) {
5310a108ab63SAdrian Chadd 		sc->sc_stats.ast_ani_cal++;
5311a108ab63SAdrian Chadd 		sc->sc_lastani = ticks;
5312a108ab63SAdrian Chadd 		ath_hal_ani_poll(ah, sc->sc_curchan);
5313a108ab63SAdrian Chadd 	}
5314a108ab63SAdrian Chadd 
53152dc7fcc4SSam Leffler 	if (longCal) {
53165591b213SSam Leffler 		sc->sc_stats.ast_per_cal++;
53178197f57eSAdrian Chadd 		sc->sc_lastlongcal = ticks;
53185591b213SSam Leffler 		if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
53195591b213SSam Leffler 			/*
53205591b213SSam Leffler 			 * Rfgain is out of bounds, reset the chip
53215591b213SSam Leffler 			 * to load new gain values.
53225591b213SSam Leffler 			 */
5323370572d9SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
5324370572d9SSam Leffler 				"%s: rfgain change\n", __func__);
53255591b213SSam Leffler 			sc->sc_stats.ast_per_rfgain++;
5326ef27340cSAdrian Chadd 			sc->sc_resetcal = 0;
5327ef27340cSAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
5328d52f7132SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
5329d52f7132SAdrian Chadd 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
5330f5c30c4eSAdrian Chadd 			ath_power_restore_power_state(sc);
5331ef27340cSAdrian Chadd 			return;
53325591b213SSam Leffler 		}
53332dc7fcc4SSam Leffler 		/*
53342dc7fcc4SSam Leffler 		 * If this long cal is after an idle period, then
53352dc7fcc4SSam Leffler 		 * reset the data collection state so we start fresh.
53362dc7fcc4SSam Leffler 		 */
53372dc7fcc4SSam Leffler 		if (sc->sc_resetcal) {
533859efa8b5SSam Leffler 			(void) ath_hal_calreset(ah, sc->sc_curchan);
53392dc7fcc4SSam Leffler 			sc->sc_lastcalreset = ticks;
5340a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
53412dc7fcc4SSam Leffler 			sc->sc_resetcal = 0;
5342a108ab63SAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
53432dc7fcc4SSam Leffler 		}
53442dc7fcc4SSam Leffler 	}
5345a108ab63SAdrian Chadd 
5346a108ab63SAdrian Chadd 	/* Only call if we're doing a short/long cal, not for ANI calibration */
5347a108ab63SAdrian Chadd 	if (shortCal || longCal) {
5348943e37a1SAdrian Chadd 		isCalDone = AH_FALSE;
534959efa8b5SSam Leffler 		if (ath_hal_calibrateN(ah, sc->sc_curchan, longCal, &isCalDone)) {
53502dc7fcc4SSam Leffler 			if (longCal) {
53512dc7fcc4SSam Leffler 				/*
53522dc7fcc4SSam Leffler 				 * Calibrate noise floor data again in case of change.
53532dc7fcc4SSam Leffler 				 */
53542dc7fcc4SSam Leffler 				ath_hal_process_noisefloor(ah);
53552dc7fcc4SSam Leffler 			}
53562dc7fcc4SSam Leffler 		} else {
5357c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
5358c42a7b7eSSam Leffler 				"%s: calibration of channel %u failed\n",
535959efa8b5SSam Leffler 				__func__, sc->sc_curchan->ic_freq);
53605591b213SSam Leffler 			sc->sc_stats.ast_per_calfail++;
53615591b213SSam Leffler 		}
5362a108ab63SAdrian Chadd 		if (shortCal)
5363a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
5364a108ab63SAdrian Chadd 	}
53652dc7fcc4SSam Leffler 	if (!isCalDone) {
53668d91de92SSam Leffler restart:
53677b0c77ecSSam Leffler 		/*
53682dc7fcc4SSam Leffler 		 * Use a shorter interval to potentially collect multiple
53692dc7fcc4SSam Leffler 		 * data samples required to complete calibration.  Once
53702dc7fcc4SSam Leffler 		 * we're told the work is done we drop back to a longer
53712dc7fcc4SSam Leffler 		 * interval between requests.  We're more aggressive doing
53722dc7fcc4SSam Leffler 		 * work when operating as an AP to improve operation right
53732dc7fcc4SSam Leffler 		 * after startup.
53747b0c77ecSSam Leffler 		 */
5375a108ab63SAdrian Chadd 		sc->sc_lastshortcal = ticks;
5376a108ab63SAdrian Chadd 		nextcal = ath_shortcalinterval*hz/1000;
53772dc7fcc4SSam Leffler 		if (sc->sc_opmode != HAL_M_HOSTAP)
53782dc7fcc4SSam Leffler 			nextcal *= 10;
5379a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_TRUE;
53802dc7fcc4SSam Leffler 	} else {
5381a108ab63SAdrian Chadd 		/* nextcal should be the shortest time for next event */
53822dc7fcc4SSam Leffler 		nextcal = ath_longcalinterval*hz;
53832dc7fcc4SSam Leffler 		if (sc->sc_lastcalreset == 0)
53842dc7fcc4SSam Leffler 			sc->sc_lastcalreset = sc->sc_lastlongcal;
53852dc7fcc4SSam Leffler 		else if (ticks - sc->sc_lastcalreset >= ath_resetcalinterval*hz)
53862dc7fcc4SSam Leffler 			sc->sc_resetcal = 1;	/* setup reset next trip */
5387a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_FALSE;
5388bd5a9920SSam Leffler 	}
5389a108ab63SAdrian Chadd 	/* ANI calibration may occur more often than short/long/resetcal */
5390a108ab63SAdrian Chadd 	if (ath_anicalinterval > 0)
5391a108ab63SAdrian Chadd 		nextcal = MIN(nextcal, ath_anicalinterval*hz/1000);
5392bd5a9920SSam Leffler 
53932dc7fcc4SSam Leffler 	if (nextcal != 0) {
53942dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: next +%u (%sisCalDone)\n",
53952dc7fcc4SSam Leffler 		    __func__, nextcal, isCalDone ? "" : "!");
53962dc7fcc4SSam Leffler 		callout_reset(&sc->sc_cal_ch, nextcal, ath_calibrate, sc);
53972dc7fcc4SSam Leffler 	} else {
53982dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: calibration disabled\n",
53992dc7fcc4SSam Leffler 		    __func__);
54002dc7fcc4SSam Leffler 		/* NB: don't rearm timer */
54012dc7fcc4SSam Leffler 	}
5402f5c30c4eSAdrian Chadd 	/*
5403f5c30c4eSAdrian Chadd 	 * Restore power state now that we're done.
5404f5c30c4eSAdrian Chadd 	 */
5405f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
54065591b213SSam Leffler }
54075591b213SSam Leffler 
540868e8e04eSSam Leffler static void
540968e8e04eSSam Leffler ath_scan_start(struct ieee80211com *ic)
541068e8e04eSSam Leffler {
54113797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
541268e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
541368e8e04eSSam Leffler 	u_int32_t rfilt;
541468e8e04eSSam Leffler 
541568e8e04eSSam Leffler 	/* XXX calibration timer? */
54167a79cebfSGleb Smirnoff 	/* XXXGL: is constant ieee80211broadcastaddr a correct choice? */
541768e8e04eSSam Leffler 
5418c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
541968e8e04eSSam Leffler 	sc->sc_scanning = 1;
542068e8e04eSSam Leffler 	sc->sc_syncbeacon = 0;
542168e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5422c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
5423c98cefc5SAdrian Chadd 
5424c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
542568e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
54267a79cebfSGleb Smirnoff 	ath_hal_setassocid(ah, ieee80211broadcastaddr, 0);
5427c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
542868e8e04eSSam Leffler 
542968e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0\n",
54307a79cebfSGleb Smirnoff 		 __func__, rfilt, ether_sprintf(ieee80211broadcastaddr));
543168e8e04eSSam Leffler }
543268e8e04eSSam Leffler 
543368e8e04eSSam Leffler static void
543468e8e04eSSam Leffler ath_scan_end(struct ieee80211com *ic)
543568e8e04eSSam Leffler {
54363797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
543768e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
543868e8e04eSSam Leffler 	u_int32_t rfilt;
543968e8e04eSSam Leffler 
5440c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
544168e8e04eSSam Leffler 	sc->sc_scanning = 0;
544268e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5443c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
5444c98cefc5SAdrian Chadd 
5445c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
544668e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
544768e8e04eSSam Leffler 	ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
544868e8e04eSSam Leffler 
544968e8e04eSSam Leffler 	ath_hal_process_noisefloor(ah);
5450c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
545168e8e04eSSam Leffler 
545268e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
545368e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(sc->sc_curbssid),
545468e8e04eSSam Leffler 		 sc->sc_curaid);
545568e8e04eSSam Leffler }
545668e8e04eSSam Leffler 
5457fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
5458e7200579SAdrian Chadd /*
5459e7200579SAdrian Chadd  * For now, just do a channel change.
5460e7200579SAdrian Chadd  *
5461e7200579SAdrian Chadd  * Later, we'll go through the hard slog of suspending tx/rx, changing rate
5462e7200579SAdrian Chadd  * control state and resetting the hardware without dropping frames out
5463e7200579SAdrian Chadd  * of the queue.
5464e7200579SAdrian Chadd  *
5465e7200579SAdrian Chadd  * The unfortunate trouble here is making absolutely sure that the
5466e7200579SAdrian Chadd  * channel width change has propagated enough so the hardware
5467e7200579SAdrian Chadd  * absolutely isn't handed bogus frames for it's current operating
5468e7200579SAdrian Chadd  * mode. (Eg, 40MHz frames in 20MHz mode.) Since TX and RX can and
5469e7200579SAdrian Chadd  * does occur in parallel, we need to make certain we've blocked
5470e7200579SAdrian Chadd  * any further ongoing TX (and RX, that can cause raw TX)
5471e7200579SAdrian Chadd  * before we do this.
5472e7200579SAdrian Chadd  */
5473e7200579SAdrian Chadd static void
5474e7200579SAdrian Chadd ath_update_chw(struct ieee80211com *ic)
5475e7200579SAdrian Chadd {
54763797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
5477e7200579SAdrian Chadd 
5478e7200579SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: called\n", __func__);
5479e7200579SAdrian Chadd 	ath_set_channel(ic);
5480e7200579SAdrian Chadd }
5481fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
5482e7200579SAdrian Chadd 
548368e8e04eSSam Leffler static void
548468e8e04eSSam Leffler ath_set_channel(struct ieee80211com *ic)
548568e8e04eSSam Leffler {
54863797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
548768e8e04eSSam Leffler 
5488f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
5489f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
5490f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
5491f5c30c4eSAdrian Chadd 
549268e8e04eSSam Leffler 	(void) ath_chan_set(sc, ic->ic_curchan);
549368e8e04eSSam Leffler 	/*
549468e8e04eSSam Leffler 	 * If we are returning to our bss channel then mark state
549568e8e04eSSam Leffler 	 * so the next recv'd beacon's tsf will be used to sync the
549668e8e04eSSam Leffler 	 * beacon timers.  Note that since we only hear beacons in
549768e8e04eSSam Leffler 	 * sta/ibss mode this has no effect in other operating modes.
549868e8e04eSSam Leffler 	 */
5499a887b1e3SAdrian Chadd 	ATH_LOCK(sc);
550068e8e04eSSam Leffler 	if (!sc->sc_scanning && ic->ic_curchan == ic->ic_bsschan)
550168e8e04eSSam Leffler 		sc->sc_syncbeacon = 1;
5502f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
5503a887b1e3SAdrian Chadd 	ATH_UNLOCK(sc);
550468e8e04eSSam Leffler }
550568e8e04eSSam Leffler 
5506b032f27cSSam Leffler /*
5507b032f27cSSam Leffler  * Walk the vap list and check if there any vap's in RUN state.
5508b032f27cSSam Leffler  */
55095591b213SSam Leffler static int
5510b032f27cSSam Leffler ath_isanyrunningvaps(struct ieee80211vap *this)
55115591b213SSam Leffler {
5512b032f27cSSam Leffler 	struct ieee80211com *ic = this->iv_ic;
5513b032f27cSSam Leffler 	struct ieee80211vap *vap;
5514b032f27cSSam Leffler 
5515b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
5516b032f27cSSam Leffler 
5517b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
5518309a3e45SSam Leffler 		if (vap != this && vap->iv_state >= IEEE80211_S_RUN)
5519b032f27cSSam Leffler 			return 1;
5520b032f27cSSam Leffler 	}
5521b032f27cSSam Leffler 	return 0;
5522b032f27cSSam Leffler }
5523b032f27cSSam Leffler 
5524b032f27cSSam Leffler static int
5525b032f27cSSam Leffler ath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
5526b032f27cSSam Leffler {
5527b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
55283797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
5529b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
553045bbf62fSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5531b032f27cSSam Leffler 	struct ieee80211_node *ni = NULL;
553268e8e04eSSam Leffler 	int i, error, stamode;
55335591b213SSam Leffler 	u_int32_t rfilt;
5534f52efb6dSAdrian Chadd 	int csa_run_transition = 0;
5535f5c30c4eSAdrian Chadd 	enum ieee80211_state ostate = vap->iv_state;
5536a74ebfe5SAdrian Chadd 
55375591b213SSam Leffler 	static const HAL_LED_STATE leds[] = {
55385591b213SSam Leffler 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
55395591b213SSam Leffler 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
55405591b213SSam Leffler 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
55415591b213SSam Leffler 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
554277d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CAC */
55435591b213SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
554477d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CSA */
554577d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_SLEEP */
55465591b213SSam Leffler 	};
55475591b213SSam Leffler 
5548c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
5549f5c30c4eSAdrian Chadd 		ieee80211_state_name[ostate],
5550c42a7b7eSSam Leffler 		ieee80211_state_name[nstate]);
55515591b213SSam Leffler 
5552107fdf96SAdrian Chadd 	/*
5553107fdf96SAdrian Chadd 	 * net80211 _should_ have the comlock asserted at this point.
5554107fdf96SAdrian Chadd 	 * There are some comments around the calls to vap->iv_newstate
5555107fdf96SAdrian Chadd 	 * which indicate that it (newstate) may end up dropping the
5556107fdf96SAdrian Chadd 	 * lock.  This and the subsequent lock assert check after newstate
5557107fdf96SAdrian Chadd 	 * are an attempt to catch these and figure out how/why.
5558107fdf96SAdrian Chadd 	 */
5559107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
5560107fdf96SAdrian Chadd 
5561f5c30c4eSAdrian Chadd 	/* Before we touch the hardware - wake it up */
5562f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
55637d567ed6SAdrian Chadd 	/*
55647d567ed6SAdrian Chadd 	 * If the NIC is in anything other than SLEEP state,
55657d567ed6SAdrian Chadd 	 * we need to ensure that self-generated frames are
55667d567ed6SAdrian Chadd 	 * set for PWRMGT=0.  Otherwise we may end up with
55677d567ed6SAdrian Chadd 	 * strange situations.
55687d567ed6SAdrian Chadd 	 *
55697d567ed6SAdrian Chadd 	 * XXX TODO: is this actually the case? :-)
55707d567ed6SAdrian Chadd 	 */
55717d567ed6SAdrian Chadd 	if (nstate != IEEE80211_S_SLEEP)
55727d567ed6SAdrian Chadd 		ath_power_setselfgen(sc, HAL_PM_AWAKE);
55737d567ed6SAdrian Chadd 
55747d567ed6SAdrian Chadd 	/*
55757d567ed6SAdrian Chadd 	 * Now, wake the thing up.
55767d567ed6SAdrian Chadd 	 */
5577f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
55787707f31dSAdrian Chadd 
55797707f31dSAdrian Chadd 	/*
55807707f31dSAdrian Chadd 	 * And stop the calibration callout whilst we have
55817707f31dSAdrian Chadd 	 * ATH_LOCK held.
55827707f31dSAdrian Chadd 	 */
55837707f31dSAdrian Chadd 	callout_stop(&sc->sc_cal_ch);
5584f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
5585f5c30c4eSAdrian Chadd 
5586f5c30c4eSAdrian Chadd 	if (ostate == IEEE80211_S_CSA && nstate == IEEE80211_S_RUN)
5587f52efb6dSAdrian Chadd 		csa_run_transition = 1;
5588f52efb6dSAdrian Chadd 
55895591b213SSam Leffler 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
55905591b213SSam Leffler 
5591b032f27cSSam Leffler 	if (nstate == IEEE80211_S_SCAN) {
559258769f58SSam Leffler 		/*
5593b032f27cSSam Leffler 		 * Scanning: turn off beacon miss and don't beacon.
5594b032f27cSSam Leffler 		 * Mark beacon state so when we reach RUN state we'll
5595b032f27cSSam Leffler 		 * [re]setup beacons.  Unblock the task q thread so
5596b032f27cSSam Leffler 		 * deferred interrupt processing is done.
559758769f58SSam Leffler 		 */
5598f5c30c4eSAdrian Chadd 
5599f5c30c4eSAdrian Chadd 		/* Ensure we stay awake during scan */
5600f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
56017d567ed6SAdrian Chadd 		ath_power_setselfgen(sc, HAL_PM_AWAKE);
5602*8c03e55dSAdrian Chadd 		ath_power_setpower(sc, HAL_PM_AWAKE, 1);
5603f5c30c4eSAdrian Chadd 		ATH_UNLOCK(sc);
5604f5c30c4eSAdrian Chadd 
5605b032f27cSSam Leffler 		ath_hal_intrset(ah,
5606b032f27cSSam Leffler 		    sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
56075591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5608b032f27cSSam Leffler 		sc->sc_beacons = 0;
5609b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
56105591b213SSam Leffler 	}
56115591b213SSam Leffler 
561280767531SAdrian Chadd 	ni = ieee80211_ref_node(vap->iv_bss);
561368e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5614b032f27cSSam Leffler 	stamode = (vap->iv_opmode == IEEE80211_M_STA ||
56157b916f89SSam Leffler 		   vap->iv_opmode == IEEE80211_M_AHDEMO ||
5616b032f27cSSam Leffler 		   vap->iv_opmode == IEEE80211_M_IBSS);
5617f5c30c4eSAdrian Chadd 
5618f5c30c4eSAdrian Chadd 	/*
5619f5c30c4eSAdrian Chadd 	 * XXX Dont need to do this (and others) if we've transitioned
5620f5c30c4eSAdrian Chadd 	 * from SLEEP->RUN.
5621f5c30c4eSAdrian Chadd 	 */
562268e8e04eSSam Leffler 	if (stamode && nstate == IEEE80211_S_RUN) {
562368e8e04eSSam Leffler 		sc->sc_curaid = ni->ni_associd;
562468e8e04eSSam Leffler 		IEEE80211_ADDR_COPY(sc->sc_curbssid, ni->ni_bssid);
5625b032f27cSSam Leffler 		ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
5626b032f27cSSam Leffler 	}
562768e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
5628b032f27cSSam Leffler 	   __func__, rfilt, ether_sprintf(sc->sc_curbssid), sc->sc_curaid);
562968e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
563068e8e04eSSam Leffler 
5631b032f27cSSam Leffler 	/* XXX is this to restore keycache on resume? */
5632b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_STA &&
5633b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY)) {
56345591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
56355591b213SSam Leffler 			if (ath_hal_keyisvalid(ah, i))
563668e8e04eSSam Leffler 				ath_hal_keysetmac(ah, i, ni->ni_bssid);
56375591b213SSam Leffler 	}
5638b032f27cSSam Leffler 
5639b032f27cSSam Leffler 	/*
5640b032f27cSSam Leffler 	 * Invoke the parent method to do net80211 work.
5641b032f27cSSam Leffler 	 */
5642b032f27cSSam Leffler 	error = avp->av_newstate(vap, nstate, arg);
5643b032f27cSSam Leffler 	if (error != 0)
5644b032f27cSSam Leffler 		goto bad;
5645c42a7b7eSSam Leffler 
5646107fdf96SAdrian Chadd 	/*
5647107fdf96SAdrian Chadd 	 * See above: ensure av_newstate() doesn't drop the lock
5648107fdf96SAdrian Chadd 	 * on us.
5649107fdf96SAdrian Chadd 	 */
5650107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
5651107fdf96SAdrian Chadd 
565268e8e04eSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
5653b032f27cSSam Leffler 		/* NB: collect bss node again, it may have changed */
565480767531SAdrian Chadd 		ieee80211_free_node(ni);
565580767531SAdrian Chadd 		ni = ieee80211_ref_node(vap->iv_bss);
56565591b213SSam Leffler 
5657b032f27cSSam Leffler 		DPRINTF(sc, ATH_DEBUG_STATE,
5658b032f27cSSam Leffler 		    "%s(RUN): iv_flags 0x%08x bintvl %d bssid %s "
5659b032f27cSSam Leffler 		    "capinfo 0x%04x chan %d\n", __func__,
5660b032f27cSSam Leffler 		    vap->iv_flags, ni->ni_intval, ether_sprintf(ni->ni_bssid),
5661b032f27cSSam Leffler 		    ni->ni_capinfo, ieee80211_chan2ieee(ic, ic->ic_curchan));
5662b032f27cSSam Leffler 
5663b032f27cSSam Leffler 		switch (vap->iv_opmode) {
5664584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
566510ad9a77SSam Leffler 		case IEEE80211_M_AHDEMO:
566610ad9a77SSam Leffler 			if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
566710ad9a77SSam Leffler 				break;
566810ad9a77SSam Leffler 			/* fall thru... */
566910ad9a77SSam Leffler #endif
5670e8fd88a3SSam Leffler 		case IEEE80211_M_HOSTAP:
5671e8fd88a3SSam Leffler 		case IEEE80211_M_IBSS:
567259aa14a9SRui Paulo 		case IEEE80211_M_MBSS:
56735591b213SSam Leffler 			/*
5674e8fd88a3SSam Leffler 			 * Allocate and setup the beacon frame.
5675e8fd88a3SSam Leffler 			 *
5676f818612bSSam Leffler 			 * Stop any previous beacon DMA.  This may be
5677f818612bSSam Leffler 			 * necessary, for example, when an ibss merge
5678f818612bSSam Leffler 			 * causes reconfiguration; there will be a state
5679f818612bSSam Leffler 			 * transition from RUN->RUN that means we may
5680f818612bSSam Leffler 			 * be called with beacon transmission active.
5681f818612bSSam Leffler 			 */
5682f818612bSSam Leffler 			ath_hal_stoptxdma(ah, sc->sc_bhalq);
5683b032f27cSSam Leffler 
56845591b213SSam Leffler 			error = ath_beacon_alloc(sc, ni);
56855591b213SSam Leffler 			if (error != 0)
56865591b213SSam Leffler 				goto bad;
56877a04dc27SSam Leffler 			/*
568880d939bfSSam Leffler 			 * If joining an adhoc network defer beacon timer
568980d939bfSSam Leffler 			 * configuration to the next beacon frame so we
569080d939bfSSam Leffler 			 * have a current TSF to use.  Otherwise we're
5691b032f27cSSam Leffler 			 * starting an ibss/bss so there's no need to delay;
5692b032f27cSSam Leffler 			 * if this is the first vap moving to RUN state, then
5693b032f27cSSam Leffler 			 * beacon state needs to be [re]configured.
56947a04dc27SSam Leffler 			 */
5695b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_IBSS &&
5696b032f27cSSam Leffler 			    ni->ni_tstamp.tsf != 0) {
569780d939bfSSam Leffler 				sc->sc_syncbeacon = 1;
5698b032f27cSSam Leffler 			} else if (!sc->sc_beacons) {
5699584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
570010ad9a77SSam Leffler 				if (vap->iv_caps & IEEE80211_C_TDMA)
570110ad9a77SSam Leffler 					ath_tdma_config(sc, vap);
570210ad9a77SSam Leffler 				else
570310ad9a77SSam Leffler #endif
5704b032f27cSSam Leffler 					ath_beacon_config(sc, vap);
5705b032f27cSSam Leffler 				sc->sc_beacons = 1;
5706b032f27cSSam Leffler 			}
5707e8fd88a3SSam Leffler 			break;
5708e8fd88a3SSam Leffler 		case IEEE80211_M_STA:
5709e8fd88a3SSam Leffler 			/*
571080d939bfSSam Leffler 			 * Defer beacon timer configuration to the next
571180d939bfSSam Leffler 			 * beacon frame so we have a current TSF to use
571280d939bfSSam Leffler 			 * (any TSF collected when scanning is likely old).
5713f52efb6dSAdrian Chadd 			 * However if it's due to a CSA -> RUN transition,
5714f52efb6dSAdrian Chadd 			 * force a beacon update so we pick up a lack of
5715f52efb6dSAdrian Chadd 			 * beacons from an AP in CAC and thus force a
5716f52efb6dSAdrian Chadd 			 * scan.
5717a74ebfe5SAdrian Chadd 			 *
5718a74ebfe5SAdrian Chadd 			 * And, there's also corner cases here where
5719a74ebfe5SAdrian Chadd 			 * after a scan, the AP may have disappeared.
5720a74ebfe5SAdrian Chadd 			 * In that case, we may not receive an actual
5721a74ebfe5SAdrian Chadd 			 * beacon to update the beacon timer and thus we
5722a74ebfe5SAdrian Chadd 			 * won't get notified of the missing beacons.
57237a04dc27SSam Leffler 			 */
5724f5c30c4eSAdrian Chadd 			if (ostate != IEEE80211_S_RUN &&
5725f5c30c4eSAdrian Chadd 			    ostate != IEEE80211_S_SLEEP) {
5726f5c30c4eSAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_BEACON,
5727f5c30c4eSAdrian Chadd 				    "%s: STA; syncbeacon=1\n", __func__);
572880d939bfSSam Leffler 				sc->sc_syncbeacon = 1;
5729f5c30c4eSAdrian Chadd 
5730f52efb6dSAdrian Chadd 				if (csa_run_transition)
5731f52efb6dSAdrian Chadd 					ath_beacon_config(sc, vap);
5732a74ebfe5SAdrian Chadd 
5733a74ebfe5SAdrian Chadd 			/*
5734a74ebfe5SAdrian Chadd 			 * PR: kern/175227
5735a74ebfe5SAdrian Chadd 			 *
5736a74ebfe5SAdrian Chadd 			 * Reconfigure beacons during reset; as otherwise
5737a74ebfe5SAdrian Chadd 			 * we won't get the beacon timers reprogrammed
5738a74ebfe5SAdrian Chadd 			 * after a reset and thus we won't pick up a
5739a74ebfe5SAdrian Chadd 			 * beacon miss interrupt.
5740a74ebfe5SAdrian Chadd 			 *
5741a74ebfe5SAdrian Chadd 			 * Hopefully we'll see a beacon before the BMISS
5742a74ebfe5SAdrian Chadd 			 * timer fires (too often), leading to a STA
5743a74ebfe5SAdrian Chadd 			 * disassociation.
5744a74ebfe5SAdrian Chadd 			 */
5745a74ebfe5SAdrian Chadd 				sc->sc_beacons = 1;
5746f5c30c4eSAdrian Chadd 			}
5747e8fd88a3SSam Leffler 			break;
5748b032f27cSSam Leffler 		case IEEE80211_M_MONITOR:
5749b032f27cSSam Leffler 			/*
5750b032f27cSSam Leffler 			 * Monitor mode vaps have only INIT->RUN and RUN->RUN
5751b032f27cSSam Leffler 			 * transitions so we must re-enable interrupts here to
5752b032f27cSSam Leffler 			 * handle the case of a single monitor mode vap.
5753b032f27cSSam Leffler 			 */
5754b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask);
5755b032f27cSSam Leffler 			break;
5756b032f27cSSam Leffler 		case IEEE80211_M_WDS:
5757b032f27cSSam Leffler 			break;
5758e8fd88a3SSam Leffler 		default:
5759e8fd88a3SSam Leffler 			break;
57605591b213SSam Leffler 		}
57615591b213SSam Leffler 		/*
57627b0c77ecSSam Leffler 		 * Let the hal process statistics collected during a
57637b0c77ecSSam Leffler 		 * scan so it can provide calibrated noise floor data.
57647b0c77ecSSam Leffler 		 */
57657b0c77ecSSam Leffler 		ath_hal_process_noisefloor(ah);
57667b0c77ecSSam Leffler 		/*
5767ffa2cab6SSam Leffler 		 * Reset rssi stats; maybe not the best place...
5768ffa2cab6SSam Leffler 		 */
5769ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
5770ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
5771ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
5772f5c30c4eSAdrian Chadd 
5773f5c30c4eSAdrian Chadd 		/*
5774f5c30c4eSAdrian Chadd 		 * Force awake for RUN mode.
5775f5c30c4eSAdrian Chadd 		 */
5776f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
57777d567ed6SAdrian Chadd 		ath_power_setselfgen(sc, HAL_PM_AWAKE);
5778*8c03e55dSAdrian Chadd 		ath_power_setpower(sc, HAL_PM_AWAKE, 1);
5779f5c30c4eSAdrian Chadd 
578045bbf62fSSam Leffler 		/*
5781b032f27cSSam Leffler 		 * Finally, start any timers and the task q thread
5782b032f27cSSam Leffler 		 * (in case we didn't go through SCAN state).
578345bbf62fSSam Leffler 		 */
57842dc7fcc4SSam Leffler 		if (ath_longcalinterval != 0) {
5785c42a7b7eSSam Leffler 			/* start periodic recalibration timer */
57862dc7fcc4SSam Leffler 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
57872dc7fcc4SSam Leffler 		} else {
57882dc7fcc4SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
57892dc7fcc4SSam Leffler 			    "%s: calibration disabled\n", __func__);
5790c42a7b7eSSam Leffler 		}
57917707f31dSAdrian Chadd 		ATH_UNLOCK(sc);
5792f5c30c4eSAdrian Chadd 
5793b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
5794b032f27cSSam Leffler 	} else if (nstate == IEEE80211_S_INIT) {
5795b032f27cSSam Leffler 		/*
5796b032f27cSSam Leffler 		 * If there are no vaps left in RUN state then
5797b032f27cSSam Leffler 		 * shutdown host/driver operation:
5798b032f27cSSam Leffler 		 * o disable interrupts
5799b032f27cSSam Leffler 		 * o disable the task queue thread
5800b032f27cSSam Leffler 		 * o mark beacon processing as stopped
5801b032f27cSSam Leffler 		 */
5802b032f27cSSam Leffler 		if (!ath_isanyrunningvaps(vap)) {
5803b032f27cSSam Leffler 			sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5804b032f27cSSam Leffler 			/* disable interrupts  */
5805b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL);
5806b032f27cSSam Leffler 			taskqueue_block(sc->sc_tq);
5807b032f27cSSam Leffler 			sc->sc_beacons = 0;
5808b032f27cSSam Leffler 		}
5809584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
581010ad9a77SSam Leffler 		ath_hal_setcca(ah, AH_TRUE);
581110ad9a77SSam Leffler #endif
5812f5c30c4eSAdrian Chadd 	} else if (nstate == IEEE80211_S_SLEEP) {
5813f5c30c4eSAdrian Chadd 		/* We're going to sleep, so transition appropriately */
5814f5c30c4eSAdrian Chadd 		/* For now, only do this if we're a single STA vap */
5815f5c30c4eSAdrian Chadd 		if (sc->sc_nvaps == 1 &&
5816f5c30c4eSAdrian Chadd 		    vap->iv_opmode == IEEE80211_M_STA) {
5817f5c30c4eSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_BEACON, "%s: syncbeacon=%d\n", __func__, sc->sc_syncbeacon);
5818f5c30c4eSAdrian Chadd 			ATH_LOCK(sc);
58197d567ed6SAdrian Chadd 			/*
58207d567ed6SAdrian Chadd 			 * Always at least set the self-generated
58217d567ed6SAdrian Chadd 			 * frame config to set PWRMGT=1.
58227d567ed6SAdrian Chadd 			 */
58237d567ed6SAdrian Chadd 			ath_power_setselfgen(sc, HAL_PM_NETWORK_SLEEP);
58247d567ed6SAdrian Chadd 
58257d567ed6SAdrian Chadd 			/*
58267d567ed6SAdrian Chadd 			 * If we're not syncing beacons, transition
58277d567ed6SAdrian Chadd 			 * to NETWORK_SLEEP.
58287d567ed6SAdrian Chadd 			 *
58297d567ed6SAdrian Chadd 			 * We stay awake if syncbeacon > 0 in case
58307d567ed6SAdrian Chadd 			 * we need to listen for some beacons otherwise
58317d567ed6SAdrian Chadd 			 * our beacon timer config may be wrong.
58327d567ed6SAdrian Chadd 			 */
5833f5c30c4eSAdrian Chadd 			if (sc->sc_syncbeacon == 0) {
5834*8c03e55dSAdrian Chadd 				ath_power_setpower(sc, HAL_PM_NETWORK_SLEEP, 1);
5835f5c30c4eSAdrian Chadd 			}
5836f5c30c4eSAdrian Chadd 			ATH_UNLOCK(sc);
5837f5c30c4eSAdrian Chadd 		}
5838b032f27cSSam Leffler 	}
58395591b213SSam Leffler bad:
584080767531SAdrian Chadd 	ieee80211_free_node(ni);
5841f5c30c4eSAdrian Chadd 
5842f5c30c4eSAdrian Chadd 	/*
5843f5c30c4eSAdrian Chadd 	 * Restore the power state - either to what it was, or
5844f5c30c4eSAdrian Chadd 	 * to network_sleep if it's alright.
5845f5c30c4eSAdrian Chadd 	 */
5846f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
5847f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
5848f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
58495591b213SSam Leffler 	return error;
58505591b213SSam Leffler }
58515591b213SSam Leffler 
58525591b213SSam Leffler /*
5853e8fd88a3SSam Leffler  * Allocate a key cache slot to the station so we can
5854e8fd88a3SSam Leffler  * setup a mapping from key index to node. The key cache
5855e8fd88a3SSam Leffler  * slot is needed for managing antenna state and for
5856e8fd88a3SSam Leffler  * compression when stations do not use crypto.  We do
5857e8fd88a3SSam Leffler  * it uniliaterally here; if crypto is employed this slot
5858e8fd88a3SSam Leffler  * will be reassigned.
5859e8fd88a3SSam Leffler  */
5860e8fd88a3SSam Leffler static void
5861e8fd88a3SSam Leffler ath_setup_stationkey(struct ieee80211_node *ni)
5862e8fd88a3SSam Leffler {
5863b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
58643797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
5865c1225b52SSam Leffler 	ieee80211_keyix keyix, rxkeyix;
5866e8fd88a3SSam Leffler 
586780767531SAdrian Chadd 	/* XXX should take a locked ref to vap->iv_bss */
5868b032f27cSSam Leffler 	if (!ath_key_alloc(vap, &ni->ni_ucastkey, &keyix, &rxkeyix)) {
5869e8fd88a3SSam Leffler 		/*
5870e8fd88a3SSam Leffler 		 * Key cache is full; we'll fall back to doing
5871e8fd88a3SSam Leffler 		 * the more expensive lookup in software.  Note
5872e8fd88a3SSam Leffler 		 * this also means no h/w compression.
5873e8fd88a3SSam Leffler 		 */
5874e8fd88a3SSam Leffler 		/* XXX msg+statistic */
5875e8fd88a3SSam Leffler 	} else {
5876c1225b52SSam Leffler 		/* XXX locking? */
5877e8fd88a3SSam Leffler 		ni->ni_ucastkey.wk_keyix = keyix;
5878c1225b52SSam Leffler 		ni->ni_ucastkey.wk_rxkeyix = rxkeyix;
587933052833SSam Leffler 		/* NB: must mark device key to get called back on delete */
588033052833SSam Leffler 		ni->ni_ucastkey.wk_flags |= IEEE80211_KEY_DEVKEY;
5881d3ac945bSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_ucastkey.wk_macaddr, ni->ni_macaddr);
5882e8fd88a3SSam Leffler 		/* NB: this will create a pass-thru key entry */
588355c7b877SAdrian Chadd 		ath_keyset(sc, vap, &ni->ni_ucastkey, vap->iv_bss);
5884e8fd88a3SSam Leffler 	}
5885e8fd88a3SSam Leffler }
5886e8fd88a3SSam Leffler 
5887e8fd88a3SSam Leffler /*
58885591b213SSam Leffler  * Setup driver-specific state for a newly associated node.
58895591b213SSam Leffler  * Note that we're called also on a re-associate, the isnew
58905591b213SSam Leffler  * param tells us if this is the first time or not.
58915591b213SSam Leffler  */
58925591b213SSam Leffler static void
5893e9962332SSam Leffler ath_newassoc(struct ieee80211_node *ni, int isnew)
58945591b213SSam Leffler {
5895b032f27cSSam Leffler 	struct ath_node *an = ATH_NODE(ni);
5896b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
58973797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
5898c62362cbSSam Leffler 	const struct ieee80211_txparam *tp = ni->ni_txparms;
58995591b213SSam Leffler 
5900ab06fdf2SSam Leffler 	an->an_mcastrix = ath_tx_findrix(sc, tp->mcastrate);
5901ab06fdf2SSam Leffler 	an->an_mgmtrix = ath_tx_findrix(sc, tp->mgmtrate);
5902b032f27cSSam Leffler 
5903f5c30c4eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: reassoc; isnew=%d, is_powersave=%d\n",
5904f5c30c4eSAdrian Chadd 	    __func__,
5905f5c30c4eSAdrian Chadd 	    ni->ni_macaddr,
5906f5c30c4eSAdrian Chadd 	    ":",
5907f5c30c4eSAdrian Chadd 	    isnew,
5908f5c30c4eSAdrian Chadd 	    an->an_is_powersave);
5909f5c30c4eSAdrian Chadd 
5910656380e7SAdrian Chadd 	ATH_NODE_LOCK(an);
5911b032f27cSSam Leffler 	ath_rate_newassoc(sc, an, isnew);
5912656380e7SAdrian Chadd 	ATH_NODE_UNLOCK(an);
591332da86a0SAdrian Chadd 
5914e8fd88a3SSam Leffler 	if (isnew &&
5915b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey &&
5916b032f27cSSam Leffler 	    ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE)
5917e8fd88a3SSam Leffler 		ath_setup_stationkey(ni);
59184bed2b67SAdrian Chadd 
59194bed2b67SAdrian Chadd 	/*
59204bed2b67SAdrian Chadd 	 * If we're reassociating, make sure that any paused queues
59214bed2b67SAdrian Chadd 	 * get unpaused.
59224bed2b67SAdrian Chadd 	 *
5923f6b6084bSPedro F. Giffuni 	 * Now, we may have frames in the hardware queue for this node.
59244bed2b67SAdrian Chadd 	 * So if we are reassociating and there are frames in the queue,
59254bed2b67SAdrian Chadd 	 * we need to go through the cleanup path to ensure that they're
59264bed2b67SAdrian Chadd 	 * marked as non-aggregate.
59274bed2b67SAdrian Chadd 	 */
59284bed2b67SAdrian Chadd 	if (! isnew) {
592932da86a0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE,
59304bed2b67SAdrian Chadd 		    "%s: %6D: reassoc; is_powersave=%d\n",
59314bed2b67SAdrian Chadd 		    __func__,
59324bed2b67SAdrian Chadd 		    ni->ni_macaddr,
59334bed2b67SAdrian Chadd 		    ":",
59344bed2b67SAdrian Chadd 		    an->an_is_powersave);
59354bed2b67SAdrian Chadd 
59364bed2b67SAdrian Chadd 		/* XXX for now, we can't hold the lock across assoc */
59374bed2b67SAdrian Chadd 		ath_tx_node_reassoc(sc, an);
59384bed2b67SAdrian Chadd 
59394bed2b67SAdrian Chadd 		/* XXX for now, we can't hold the lock across wakeup */
59404bed2b67SAdrian Chadd 		if (an->an_is_powersave)
59414bed2b67SAdrian Chadd 			ath_tx_node_wakeup(sc, an);
59424bed2b67SAdrian Chadd 	}
5943e8fd88a3SSam Leffler }
59445591b213SSam Leffler 
59455591b213SSam Leffler static int
594659efa8b5SSam Leffler ath_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *reg,
5947b032f27cSSam Leffler 	int nchans, struct ieee80211_channel chans[])
5948b032f27cSSam Leffler {
59493797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
5950b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
595159efa8b5SSam Leffler 	HAL_STATUS status;
5952b032f27cSSam Leffler 
5953033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
595459efa8b5SSam Leffler 	    "%s: rd %u cc %u location %c%s\n",
595559efa8b5SSam Leffler 	    __func__, reg->regdomain, reg->country, reg->location,
595659efa8b5SSam Leffler 	    reg->ecm ? " ecm" : "");
5957033022a9SSam Leffler 
595859efa8b5SSam Leffler 	status = ath_hal_set_channels(ah, chans, nchans,
595959efa8b5SSam Leffler 	    reg->country, reg->regdomain);
596059efa8b5SSam Leffler 	if (status != HAL_OK) {
596159efa8b5SSam Leffler 		DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: failed, status %u\n",
596259efa8b5SSam Leffler 		    __func__, status);
596359efa8b5SSam Leffler 		return EINVAL;		/* XXX */
5964b032f27cSSam Leffler 	}
59658db87e40SAdrian Chadd 
5966b032f27cSSam Leffler 	return 0;
5967b032f27cSSam Leffler }
5968b032f27cSSam Leffler 
5969b032f27cSSam Leffler static void
5970b032f27cSSam Leffler ath_getradiocaps(struct ieee80211com *ic,
59715fe9f044SSam Leffler 	int maxchans, int *nchans, struct ieee80211_channel chans[])
5972b032f27cSSam Leffler {
59733797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
5974b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5975b032f27cSSam Leffler 
597659efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: use rd %u cc %d\n",
597759efa8b5SSam Leffler 	    __func__, SKU_DEBUG, CTRY_DEFAULT);
5978033022a9SSam Leffler 
597959efa8b5SSam Leffler 	/* XXX check return */
598059efa8b5SSam Leffler 	(void) ath_hal_getchannels(ah, chans, maxchans, nchans,
598159efa8b5SSam Leffler 	    HAL_MODE_ALL, CTRY_DEFAULT, SKU_DEBUG, AH_TRUE);
5982033022a9SSam Leffler 
5983b032f27cSSam Leffler }
5984b032f27cSSam Leffler 
5985b032f27cSSam Leffler static int
5986b032f27cSSam Leffler ath_getchannels(struct ath_softc *sc)
5987b032f27cSSam Leffler {
59887a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
5989b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
599059efa8b5SSam Leffler 	HAL_STATUS status;
5991b032f27cSSam Leffler 
5992b032f27cSSam Leffler 	/*
599359efa8b5SSam Leffler 	 * Collect channel set based on EEPROM contents.
5994b032f27cSSam Leffler 	 */
599559efa8b5SSam Leffler 	status = ath_hal_init_channels(ah, ic->ic_channels, IEEE80211_CHAN_MAX,
599659efa8b5SSam Leffler 	    &ic->ic_nchans, HAL_MODE_ALL, CTRY_DEFAULT, SKU_NONE, AH_TRUE);
599759efa8b5SSam Leffler 	if (status != HAL_OK) {
599876e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
599976e6fd5dSGleb Smirnoff 		    "%s: unable to collect channel list from hal, status %d\n",
600076e6fd5dSGleb Smirnoff 		    __func__, status);
600159efa8b5SSam Leffler 		return EINVAL;
600259efa8b5SSam Leffler 	}
6003ca876918SSam Leffler 	(void) ath_hal_getregdomain(ah, &sc->sc_eerd);
6004ca876918SSam Leffler 	ath_hal_getcountrycode(ah, &sc->sc_eecc);	/* NB: cannot fail */
600559efa8b5SSam Leffler 	/* XXX map Atheros sku's to net80211 SKU's */
600659efa8b5SSam Leffler 	/* XXX net80211 types too small */
600759efa8b5SSam Leffler 	ic->ic_regdomain.regdomain = (uint16_t) sc->sc_eerd;
600859efa8b5SSam Leffler 	ic->ic_regdomain.country = (uint16_t) sc->sc_eecc;
600959efa8b5SSam Leffler 	ic->ic_regdomain.isocc[0] = ' ';	/* XXX don't know */
601059efa8b5SSam Leffler 	ic->ic_regdomain.isocc[1] = ' ';
601159efa8b5SSam Leffler 
6012b032f27cSSam Leffler 	ic->ic_regdomain.ecm = 1;
6013b032f27cSSam Leffler 	ic->ic_regdomain.location = 'I';
6014033022a9SSam Leffler 
6015033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
601659efa8b5SSam Leffler 	    "%s: eeprom rd %u cc %u (mapped rd %u cc %u) location %c%s\n",
6017033022a9SSam Leffler 	    __func__, sc->sc_eerd, sc->sc_eecc,
6018033022a9SSam Leffler 	    ic->ic_regdomain.regdomain, ic->ic_regdomain.country,
601959efa8b5SSam Leffler 	    ic->ic_regdomain.location, ic->ic_regdomain.ecm ? " ecm" : "");
60205591b213SSam Leffler 	return 0;
60215591b213SSam Leffler }
60225591b213SSam Leffler 
60236c4612b9SSam Leffler static int
60246c4612b9SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode)
60256c4612b9SSam Leffler {
60266c4612b9SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
60276c4612b9SSam Leffler 	const HAL_RATE_TABLE *rt;
60286c4612b9SSam Leffler 
60296c4612b9SSam Leffler 	switch (mode) {
60306c4612b9SSam Leffler 	case IEEE80211_MODE_11A:
60316c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A);
60326c4612b9SSam Leffler 		break;
6033724c193aSSam Leffler 	case IEEE80211_MODE_HALF:
6034aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_HALF_RATE);
6035aaa70f2fSSam Leffler 		break;
6036724c193aSSam Leffler 	case IEEE80211_MODE_QUARTER:
6037aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_QUARTER_RATE);
6038aaa70f2fSSam Leffler 		break;
60396c4612b9SSam Leffler 	case IEEE80211_MODE_11B:
60406c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11B);
60416c4612b9SSam Leffler 		break;
60426c4612b9SSam Leffler 	case IEEE80211_MODE_11G:
60436c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11G);
60446c4612b9SSam Leffler 		break;
60456c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_A:
604668e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108A);
60476c4612b9SSam Leffler 		break;
60486c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_G:
60496c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108G);
60506c4612b9SSam Leffler 		break;
605168e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
605268e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_TURBO);
605368e8e04eSSam Leffler 		break;
605468e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
605568e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NA_HT20);
605668e8e04eSSam Leffler 		break;
605768e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
605868e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NG_HT20);
605968e8e04eSSam Leffler 		break;
60606c4612b9SSam Leffler 	default:
60616c4612b9SSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
60626c4612b9SSam Leffler 			__func__, mode);
60636c4612b9SSam Leffler 		return 0;
60646c4612b9SSam Leffler 	}
60656c4612b9SSam Leffler 	sc->sc_rates[mode] = rt;
6066aaa70f2fSSam Leffler 	return (rt != NULL);
60675591b213SSam Leffler }
60685591b213SSam Leffler 
60695591b213SSam Leffler static void
60705591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
60715591b213SSam Leffler {
60723e50ec2cSSam Leffler 	/* NB: on/off times from the Atheros NDIS driver, w/ permission */
60733e50ec2cSSam Leffler 	static const struct {
60743e50ec2cSSam Leffler 		u_int		rate;		/* tx/rx 802.11 rate */
60753e50ec2cSSam Leffler 		u_int16_t	timeOn;		/* LED on time (ms) */
60763e50ec2cSSam Leffler 		u_int16_t	timeOff;	/* LED off time (ms) */
60773e50ec2cSSam Leffler 	} blinkrates[] = {
60783e50ec2cSSam Leffler 		{ 108,  40,  10 },
60793e50ec2cSSam Leffler 		{  96,  44,  11 },
60803e50ec2cSSam Leffler 		{  72,  50,  13 },
60813e50ec2cSSam Leffler 		{  48,  57,  14 },
60823e50ec2cSSam Leffler 		{  36,  67,  16 },
60833e50ec2cSSam Leffler 		{  24,  80,  20 },
60843e50ec2cSSam Leffler 		{  22, 100,  25 },
60853e50ec2cSSam Leffler 		{  18, 133,  34 },
60863e50ec2cSSam Leffler 		{  12, 160,  40 },
60873e50ec2cSSam Leffler 		{  10, 200,  50 },
60883e50ec2cSSam Leffler 		{   6, 240,  58 },
60893e50ec2cSSam Leffler 		{   4, 267,  66 },
60903e50ec2cSSam Leffler 		{   2, 400, 100 },
60913e50ec2cSSam Leffler 		{   0, 500, 130 },
6092724c193aSSam Leffler 		/* XXX half/quarter rates */
60933e50ec2cSSam Leffler 	};
60945591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
60953e50ec2cSSam Leffler 	int i, j;
60965591b213SSam Leffler 
60975591b213SSam Leffler 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
60985591b213SSam Leffler 	rt = sc->sc_rates[mode];
60995591b213SSam Leffler 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
6100180f268dSSam Leffler 	for (i = 0; i < rt->rateCount; i++) {
6101180f268dSSam Leffler 		uint8_t ieeerate = rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
6102180f268dSSam Leffler 		if (rt->info[i].phy != IEEE80211_T_HT)
6103180f268dSSam Leffler 			sc->sc_rixmap[ieeerate] = i;
6104180f268dSSam Leffler 		else
6105180f268dSSam Leffler 			sc->sc_rixmap[ieeerate | IEEE80211_RATE_MCS] = i;
6106180f268dSSam Leffler 	}
61071b1a8e41SSam Leffler 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
6108d6166defSAdrian Chadd 	for (i = 0; i < nitems(sc->sc_hwmap); i++) {
610946d4d74cSSam Leffler 		if (i >= rt->rateCount) {
61103e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledon = (500 * hz) / 1000;
61113e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledoff = (130 * hz) / 1000;
611216b4851aSSam Leffler 			continue;
61133e50ec2cSSam Leffler 		}
61143e50ec2cSSam Leffler 		sc->sc_hwmap[i].ieeerate =
611546d4d74cSSam Leffler 			rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
611646d4d74cSSam Leffler 		if (rt->info[i].phy == IEEE80211_T_HT)
611726041a14SSam Leffler 			sc->sc_hwmap[i].ieeerate |= IEEE80211_RATE_MCS;
6118d3be6f5bSSam Leffler 		sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
611946d4d74cSSam Leffler 		if (rt->info[i].shortPreamble ||
612046d4d74cSSam Leffler 		    rt->info[i].phy == IEEE80211_T_OFDM)
6121d3be6f5bSSam Leffler 			sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE;
61225463c4a4SSam Leffler 		sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags;
6123d6166defSAdrian Chadd 		for (j = 0; j < nitems(blinkrates)-1; j++)
61243e50ec2cSSam Leffler 			if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate)
61253e50ec2cSSam Leffler 				break;
61263e50ec2cSSam Leffler 		/* NB: this uses the last entry if the rate isn't found */
61273e50ec2cSSam Leffler 		/* XXX beware of overlow */
61283e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000;
61293e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000;
6130c42a7b7eSSam Leffler 	}
61315591b213SSam Leffler 	sc->sc_currates = rt;
61325591b213SSam Leffler 	sc->sc_curmode = mode;
61335591b213SSam Leffler 	/*
6134f6b6084bSPedro F. Giffuni 	 * All protection frames are transmitted at 2Mb/s for
6135c42a7b7eSSam Leffler 	 * 11g, otherwise at 1Mb/s.
61365591b213SSam Leffler 	 */
6137913a1ba1SSam Leffler 	if (mode == IEEE80211_MODE_11G)
6138ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*2);
6139913a1ba1SSam Leffler 	else
6140ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*1);
61414fa8d4efSDaniel Eischen 	/* NB: caller is responsible for resetting rate control state */
61425591b213SSam Leffler }
61435591b213SSam Leffler 
6144c42a7b7eSSam Leffler static void
61452e986da5SSam Leffler ath_watchdog(void *arg)
6146c42a7b7eSSam Leffler {
61472e986da5SSam Leffler 	struct ath_softc *sc = arg;
61487a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
6149ef27340cSAdrian Chadd 	int do_reset = 0;
6150c42a7b7eSSam Leffler 
6151adcdc8f2SAdrian Chadd 	ATH_LOCK_ASSERT(sc);
61527707f31dSAdrian Chadd 
61532e986da5SSam Leffler 	if (sc->sc_wd_timer != 0 && --sc->sc_wd_timer == 0) {
6154459bc4f0SSam Leffler 		uint32_t hangs;
6155459bc4f0SSam Leffler 
6156f5c30c4eSAdrian Chadd 		ath_power_set_power_state(sc, HAL_PM_AWAKE);
6157f5c30c4eSAdrian Chadd 
6158459bc4f0SSam Leffler 		if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) &&
6159459bc4f0SSam Leffler 		    hangs != 0) {
616076e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev, "%s hang detected (0x%x)\n",
6161459bc4f0SSam Leffler 			    hangs & 0xff ? "bb" : "mac", hangs);
6162459bc4f0SSam Leffler 		} else
616376e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev, "device timeout\n");
6164ef27340cSAdrian Chadd 		do_reset = 1;
61657a79cebfSGleb Smirnoff 		counter_u64_add(ic->ic_oerrors, 1);
6166c42a7b7eSSam Leffler 		sc->sc_stats.ast_watchdog++;
6167f5c30c4eSAdrian Chadd 
6168f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
6169c42a7b7eSSam Leffler 	}
6170ef27340cSAdrian Chadd 
6171ef27340cSAdrian Chadd 	/*
6172ef27340cSAdrian Chadd 	 * We can't hold the lock across the ath_reset() call.
6173d52f7132SAdrian Chadd 	 *
6174d52f7132SAdrian Chadd 	 * And since this routine can't hold a lock and sleep,
6175d52f7132SAdrian Chadd 	 * do the reset deferred.
6176ef27340cSAdrian Chadd 	 */
6177ef27340cSAdrian Chadd 	if (do_reset) {
6178d52f7132SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
6179ef27340cSAdrian Chadd 	}
6180ef27340cSAdrian Chadd 
61812e986da5SSam Leffler 	callout_schedule(&sc->sc_wd_ch, hz);
6182c42a7b7eSSam Leffler }
6183c42a7b7eSSam Leffler 
61847a79cebfSGleb Smirnoff static void
61857a79cebfSGleb Smirnoff ath_parent(struct ieee80211com *ic)
6186c42a7b7eSSam Leffler {
61873797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
61887a79cebfSGleb Smirnoff 	int error = EDOOFUS;
6189c42a7b7eSSam Leffler 
61907a79cebfSGleb Smirnoff 	ATH_LOCK(sc);
61917a79cebfSGleb Smirnoff 	if (ic->ic_nrunning > 0) {
6192c42a7b7eSSam Leffler 		/*
6193c42a7b7eSSam Leffler 		 * To avoid rescanning another access point,
6194c42a7b7eSSam Leffler 		 * do not call ath_init() here.  Instead,
6195c42a7b7eSSam Leffler 		 * only reflect promisc mode settings.
6196c42a7b7eSSam Leffler 		 */
61977a79cebfSGleb Smirnoff 		if (sc->sc_running) {
61984b734a1cSAdrian Chadd 			ath_power_set_power_state(sc, HAL_PM_AWAKE);
6199c42a7b7eSSam Leffler 			ath_mode_init(sc);
62004b734a1cSAdrian Chadd 			ath_power_restore_power_state(sc);
62017a79cebfSGleb Smirnoff 		} else if (!sc->sc_invalid) {
6202c42a7b7eSSam Leffler 			/*
6203c42a7b7eSSam Leffler 			 * Beware of being called during attach/detach
6204c42a7b7eSSam Leffler 			 * to reset promiscuous mode.  In that case we
6205c42a7b7eSSam Leffler 			 * will still be marked UP but not RUNNING.
6206c42a7b7eSSam Leffler 			 * However trying to re-init the interface
6207c42a7b7eSSam Leffler 			 * is the wrong thing to do as we've already
6208c42a7b7eSSam Leffler 			 * torn down much of our state.  There's
6209c42a7b7eSSam Leffler 			 * probably a better way to deal with this.
6210c42a7b7eSSam Leffler 			 */
62117a79cebfSGleb Smirnoff 			error = ath_init(sc);
62127a79cebfSGleb Smirnoff 		}
6213d3ac945bSSam Leffler 	} else {
62147a79cebfSGleb Smirnoff 		ath_stop(sc);
6215d3ac945bSSam Leffler 		if (!sc->sc_invalid)
6216*8c03e55dSAdrian Chadd 			ath_power_setpower(sc, HAL_PM_FULL_SLEEP, 1);
6217410302ebSAdrian Chadd 	}
62187a79cebfSGleb Smirnoff 	ATH_UNLOCK(sc);
62197a79cebfSGleb Smirnoff 
62207a79cebfSGleb Smirnoff 	if (error == 0) {
62217a79cebfSGleb Smirnoff #ifdef ATH_TX99_DIAG
62227a79cebfSGleb Smirnoff 		if (sc->sc_tx99 != NULL)
62237a79cebfSGleb Smirnoff 			sc->sc_tx99->start(sc->sc_tx99);
62247a79cebfSGleb Smirnoff 		else
62257a79cebfSGleb Smirnoff #endif
62267a79cebfSGleb Smirnoff 		ieee80211_start_all(ic);
62277a79cebfSGleb Smirnoff 	}
62287a79cebfSGleb Smirnoff }
62297a79cebfSGleb Smirnoff 
6230c42a7b7eSSam Leffler /*
6231c42a7b7eSSam Leffler  * Announce various information on device/driver attach.
6232c42a7b7eSSam Leffler  */
6233c42a7b7eSSam Leffler static void
6234c42a7b7eSSam Leffler ath_announce(struct ath_softc *sc)
6235c42a7b7eSSam Leffler {
6236c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6237c42a7b7eSSam Leffler 
6238b2585567SAdrian Chadd 	device_printf(sc->sc_dev, "%s mac %d.%d RF%s phy %d.%d\n",
6239498657cfSSam Leffler 		ath_hal_mac_name(ah), ah->ah_macVersion, ah->ah_macRev,
6240498657cfSSam Leffler 		ath_hal_rf_name(ah), ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
624176e6fd5dSGleb Smirnoff 	device_printf(sc->sc_dev, "2GHz radio: 0x%.4x; 5GHz radio: 0x%.4x\n",
624246a924c4SAdrian Chadd 		ah->ah_analog2GhzRev, ah->ah_analog5GhzRev);
6243c42a7b7eSSam Leffler 	if (bootverbose) {
6244c42a7b7eSSam Leffler 		int i;
6245c42a7b7eSSam Leffler 		for (i = 0; i <= WME_AC_VO; i++) {
6246c42a7b7eSSam Leffler 			struct ath_txq *txq = sc->sc_ac2q[i];
624776e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev,
624876e6fd5dSGleb Smirnoff 			    "Use hw queue %u for %s traffic\n",
6249c42a7b7eSSam Leffler 			    txq->axq_qnum, ieee80211_wme_acnames[i]);
6250c42a7b7eSSam Leffler 		}
625176e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "Use hw queue %u for CAB traffic\n",
6252c42a7b7eSSam Leffler 		    sc->sc_cabq->axq_qnum);
625376e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "Use hw queue %u for beacons\n",
625476e6fd5dSGleb Smirnoff 		    sc->sc_bhalq);
6255c42a7b7eSSam Leffler 	}
6256e2d787faSSam Leffler 	if (ath_rxbuf != ATH_RXBUF)
625776e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "using %u rx buffers\n", ath_rxbuf);
6258e2d787faSSam Leffler 	if (ath_txbuf != ATH_TXBUF)
625976e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "using %u tx buffers\n", ath_txbuf);
62609ac01d39SRui Paulo 	if (sc->sc_mcastkey && bootverbose)
626176e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "using multicast key search\n");
6262c42a7b7eSSam Leffler }
626310ad9a77SSam Leffler 
626448237774SAdrian Chadd static void
626548237774SAdrian Chadd ath_dfs_tasklet(void *p, int npending)
626648237774SAdrian Chadd {
626748237774SAdrian Chadd 	struct ath_softc *sc = (struct ath_softc *) p;
62687a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
626948237774SAdrian Chadd 
627048237774SAdrian Chadd 	/*
627148237774SAdrian Chadd 	 * If previous processing has found a radar event,
627248237774SAdrian Chadd 	 * signal this to the net80211 layer to begin DFS
627348237774SAdrian Chadd 	 * processing.
627448237774SAdrian Chadd 	 */
627548237774SAdrian Chadd 	if (ath_dfs_process_radar_event(sc, sc->sc_curchan)) {
627648237774SAdrian Chadd 		/* DFS event found, initiate channel change */
627706fc4a10SAdrian Chadd 		/*
627806fc4a10SAdrian Chadd 		 * XXX doesn't currently tell us whether the event
627906fc4a10SAdrian Chadd 		 * XXX was found in the primary or extension
628006fc4a10SAdrian Chadd 		 * XXX channel!
628106fc4a10SAdrian Chadd 		 */
628206fc4a10SAdrian Chadd 		IEEE80211_LOCK(ic);
628348237774SAdrian Chadd 		ieee80211_dfs_notify_radar(ic, sc->sc_curchan);
628406fc4a10SAdrian Chadd 		IEEE80211_UNLOCK(ic);
628548237774SAdrian Chadd 	}
628648237774SAdrian Chadd }
628748237774SAdrian Chadd 
62880eb81626SAdrian Chadd /*
62890eb81626SAdrian Chadd  * Enable/disable power save.  This must be called with
62900eb81626SAdrian Chadd  * no TX driver locks currently held, so it should only
62910eb81626SAdrian Chadd  * be called from the RX path (which doesn't hold any
62920eb81626SAdrian Chadd  * TX driver locks.)
62930eb81626SAdrian Chadd  */
62940eb81626SAdrian Chadd static void
62950eb81626SAdrian Chadd ath_node_powersave(struct ieee80211_node *ni, int enable)
62960eb81626SAdrian Chadd {
6297bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
62980eb81626SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
62990eb81626SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
63003797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
63010eb81626SAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
63020eb81626SAdrian Chadd 
63030eb81626SAdrian Chadd 	/* XXX and no TXQ locks should be held here */
63040eb81626SAdrian Chadd 
63059b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, "%s: %6D: enable=%d\n",
63069b48fb4bSAdrian Chadd 	    __func__,
63079b48fb4bSAdrian Chadd 	    ni->ni_macaddr,
63089b48fb4bSAdrian Chadd 	    ":",
63099b48fb4bSAdrian Chadd 	    !! enable);
63100eb81626SAdrian Chadd 
63110eb81626SAdrian Chadd 	/* Suspend or resume software queue handling */
63120eb81626SAdrian Chadd 	if (enable)
63130eb81626SAdrian Chadd 		ath_tx_node_sleep(sc, an);
63140eb81626SAdrian Chadd 	else
63150eb81626SAdrian Chadd 		ath_tx_node_wakeup(sc, an);
63160eb81626SAdrian Chadd 
63170eb81626SAdrian Chadd 	/* Update net80211 state */
63180eb81626SAdrian Chadd 	avp->av_node_ps(ni, enable);
6319bdbb6e5bSAdrian Chadd #else
6320bdbb6e5bSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6321bdbb6e5bSAdrian Chadd 
6322bdbb6e5bSAdrian Chadd 	/* Update net80211 state */
6323bdbb6e5bSAdrian Chadd 	avp->av_node_ps(ni, enable);
6324bdbb6e5bSAdrian Chadd #endif/* ATH_SW_PSQ */
63250eb81626SAdrian Chadd }
63260eb81626SAdrian Chadd 
6327548a605dSAdrian Chadd /*
6328548a605dSAdrian Chadd  * Notification from net80211 that the powersave queue state has
6329548a605dSAdrian Chadd  * changed.
6330548a605dSAdrian Chadd  *
6331548a605dSAdrian Chadd  * Since the software queue also may have some frames:
6332548a605dSAdrian Chadd  *
6333548a605dSAdrian Chadd  * + if the node software queue has frames and the TID state
6334548a605dSAdrian Chadd  *   is 0, we set the TIM;
6335548a605dSAdrian Chadd  * + if the node and the stack are both empty, we clear the TIM bit.
6336548a605dSAdrian Chadd  * + If the stack tries to set the bit, always set it.
6337548a605dSAdrian Chadd  * + If the stack tries to clear the bit, only clear it if the
6338548a605dSAdrian Chadd  *   software queue in question is also cleared.
6339548a605dSAdrian Chadd  *
6340548a605dSAdrian Chadd  * TODO: this is called during node teardown; so let's ensure this
6341548a605dSAdrian Chadd  * is all correctly handled and that the TIM bit is cleared.
6342548a605dSAdrian Chadd  * It may be that the node flush is called _AFTER_ the net80211
6343548a605dSAdrian Chadd  * stack clears the TIM.
6344548a605dSAdrian Chadd  *
6345548a605dSAdrian Chadd  * Here is the racy part.  Since it's possible >1 concurrent,
6346548a605dSAdrian Chadd  * overlapping TXes will appear complete with a TX completion in
6347548a605dSAdrian Chadd  * another thread, it's possible that the concurrent TIM calls will
6348548a605dSAdrian Chadd  * clash.  We can't hold the node lock here because setting the
6349548a605dSAdrian Chadd  * TIM grabs the net80211 comlock and this may cause a LOR.
6350548a605dSAdrian Chadd  * The solution is either to totally serialise _everything_ at
6351548a605dSAdrian Chadd  * this point (ie, all TX, completion and any reset/flush go into
6352548a605dSAdrian Chadd  * one taskqueue) or a new "ath TIM lock" needs to be created that
6353548a605dSAdrian Chadd  * just wraps the driver state change and this call to avp->av_set_tim().
6354548a605dSAdrian Chadd  *
6355548a605dSAdrian Chadd  * The same race exists in the net80211 power save queue handling
6356548a605dSAdrian Chadd  * as well.  Since multiple transmitting threads may queue frames
6357548a605dSAdrian Chadd  * into the driver, as well as ps-poll and the driver transmitting
6358548a605dSAdrian Chadd  * frames (and thus clearing the psq), it's quite possible that
6359548a605dSAdrian Chadd  * a packet entering the PSQ and a ps-poll being handled will
6360548a605dSAdrian Chadd  * race, causing the TIM to be cleared and not re-set.
6361548a605dSAdrian Chadd  */
6362548a605dSAdrian Chadd static int
6363548a605dSAdrian Chadd ath_node_set_tim(struct ieee80211_node *ni, int enable)
6364548a605dSAdrian Chadd {
6365bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
6366548a605dSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
63673797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
6368548a605dSAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
6369548a605dSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6370548a605dSAdrian Chadd 	int changed = 0;
6371548a605dSAdrian Chadd 
63724bed2b67SAdrian Chadd 	ATH_TX_LOCK(sc);
6373548a605dSAdrian Chadd 	an->an_stack_psq = enable;
6374548a605dSAdrian Chadd 
6375548a605dSAdrian Chadd 	/*
6376548a605dSAdrian Chadd 	 * This will get called for all operating modes,
6377548a605dSAdrian Chadd 	 * even if avp->av_set_tim is unset.
6378548a605dSAdrian Chadd 	 * It's currently set for hostap/ibss modes; but
6379548a605dSAdrian Chadd 	 * the same infrastructure is used for both STA
6380548a605dSAdrian Chadd 	 * and AP/IBSS node power save.
6381548a605dSAdrian Chadd 	 */
6382548a605dSAdrian Chadd 	if (avp->av_set_tim == NULL) {
63834bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6384548a605dSAdrian Chadd 		return (0);
6385548a605dSAdrian Chadd 	}
6386548a605dSAdrian Chadd 
6387548a605dSAdrian Chadd 	/*
6388548a605dSAdrian Chadd 	 * If setting the bit, always set it here.
6389548a605dSAdrian Chadd 	 * If clearing the bit, only clear it if the
6390548a605dSAdrian Chadd 	 * software queue is also empty.
6391548a605dSAdrian Chadd 	 *
6392548a605dSAdrian Chadd 	 * If the node has left power save, just clear the TIM
6393548a605dSAdrian Chadd 	 * bit regardless of the state of the power save queue.
6394548a605dSAdrian Chadd 	 *
6395548a605dSAdrian Chadd 	 * XXX TODO: although atomics are used, it's quite possible
6396548a605dSAdrian Chadd 	 * that a race will occur between this and setting/clearing
6397548a605dSAdrian Chadd 	 * in another thread.  TX completion will occur always in
6398548a605dSAdrian Chadd 	 * one thread, however setting/clearing the TIM bit can come
6399548a605dSAdrian Chadd 	 * from a variety of different process contexts!
6400548a605dSAdrian Chadd 	 */
6401548a605dSAdrian Chadd 	if (enable && an->an_tim_set == 1) {
6402548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
64039b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, tim_set=1, ignoring\n",
64049b48fb4bSAdrian Chadd 		    __func__,
64059b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
64069b48fb4bSAdrian Chadd 		    ":",
64079b48fb4bSAdrian Chadd 		    enable);
64084bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6409548a605dSAdrian Chadd 	} else if (enable) {
6410548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
64119b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, enabling TIM\n",
64129b48fb4bSAdrian Chadd 		    __func__,
64139b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
64149b48fb4bSAdrian Chadd 		    ":",
64159b48fb4bSAdrian Chadd 		    enable);
6416548a605dSAdrian Chadd 		an->an_tim_set = 1;
64174bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6418548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6419ba83edd4SAdrian Chadd 	} else if (an->an_swq_depth == 0) {
6420548a605dSAdrian Chadd 		/* disable */
6421548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
64229b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_swq_depth == 0, disabling\n",
64239b48fb4bSAdrian Chadd 		    __func__,
64249b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
64259b48fb4bSAdrian Chadd 		    ":",
64269b48fb4bSAdrian Chadd 		    enable);
6427548a605dSAdrian Chadd 		an->an_tim_set = 0;
64284bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6429548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6430548a605dSAdrian Chadd 	} else if (! an->an_is_powersave) {
6431548a605dSAdrian Chadd 		/*
6432548a605dSAdrian Chadd 		 * disable regardless; the node isn't in powersave now
6433548a605dSAdrian Chadd 		 */
6434548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
64359b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_pwrsave=0, disabling\n",
64369b48fb4bSAdrian Chadd 		    __func__,
64379b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
64389b48fb4bSAdrian Chadd 		    ":",
64399b48fb4bSAdrian Chadd 		    enable);
6440548a605dSAdrian Chadd 		an->an_tim_set = 0;
64414bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6442548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6443548a605dSAdrian Chadd 	} else {
6444548a605dSAdrian Chadd 		/*
6445548a605dSAdrian Chadd 		 * psq disable, node is currently in powersave, node
6446548a605dSAdrian Chadd 		 * software queue isn't empty, so don't clear the TIM bit
6447548a605dSAdrian Chadd 		 * for now.
6448548a605dSAdrian Chadd 		 */
64494bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6450548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
64519b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_swq_depth > 0, ignoring\n",
64529b48fb4bSAdrian Chadd 		    __func__,
64539b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
64549b48fb4bSAdrian Chadd 		    ":",
64559b48fb4bSAdrian Chadd 		    enable);
6456548a605dSAdrian Chadd 		changed = 0;
6457548a605dSAdrian Chadd 	}
6458548a605dSAdrian Chadd 
6459548a605dSAdrian Chadd 	return (changed);
6460bdbb6e5bSAdrian Chadd #else
6461bdbb6e5bSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6462bdbb6e5bSAdrian Chadd 
646360328038SAdrian Chadd 	/*
6464661c81c3SBaptiste Daroussin 	 * Some operating modes don't set av_set_tim(), so don't
646560328038SAdrian Chadd 	 * update it here.
646660328038SAdrian Chadd 	 */
646760328038SAdrian Chadd 	if (avp->av_set_tim == NULL)
646860328038SAdrian Chadd 		return (0);
646960328038SAdrian Chadd 
6470bdbb6e5bSAdrian Chadd 	return (avp->av_set_tim(ni, enable));
6471bdbb6e5bSAdrian Chadd #endif /* ATH_SW_PSQ */
6472548a605dSAdrian Chadd }
6473548a605dSAdrian Chadd 
6474548a605dSAdrian Chadd /*
6475548a605dSAdrian Chadd  * Set or update the TIM from the software queue.
6476548a605dSAdrian Chadd  *
6477548a605dSAdrian Chadd  * Check the software queue depth before attempting to do lock
6478548a605dSAdrian Chadd  * anything; that avoids trying to obtain the lock.  Then,
6479548a605dSAdrian Chadd  * re-check afterwards to ensure nothing has changed in the
6480548a605dSAdrian Chadd  * meantime.
6481548a605dSAdrian Chadd  *
6482548a605dSAdrian Chadd  * set:   This is designed to be called from the TX path, after
6483548a605dSAdrian Chadd  *        a frame has been queued; to see if the swq > 0.
6484548a605dSAdrian Chadd  *
6485548a605dSAdrian Chadd  * clear: This is designed to be called from the buffer completion point
6486548a605dSAdrian Chadd  *        (right now it's ath_tx_default_comp()) where the state of
6487548a605dSAdrian Chadd  *        a software queue has changed.
6488548a605dSAdrian Chadd  *
6489548a605dSAdrian Chadd  * It makes sense to place it at buffer free / completion rather
6490548a605dSAdrian Chadd  * than after each software queue operation, as there's no real
6491548a605dSAdrian Chadd  * point in churning the TIM bit as the last frames in the software
6492548a605dSAdrian Chadd  * queue are transmitted.  If they fail and we retry them, we'd
6493548a605dSAdrian Chadd  * just be setting the TIM bit again anyway.
6494548a605dSAdrian Chadd  */
6495548a605dSAdrian Chadd void
6496548a605dSAdrian Chadd ath_tx_update_tim(struct ath_softc *sc, struct ieee80211_node *ni,
6497548a605dSAdrian Chadd      int enable)
6498548a605dSAdrian Chadd {
6499bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
6500548a605dSAdrian Chadd 	struct ath_node *an;
6501548a605dSAdrian Chadd 	struct ath_vap *avp;
6502548a605dSAdrian Chadd 
6503548a605dSAdrian Chadd 	/* Don't do this for broadcast/etc frames */
6504548a605dSAdrian Chadd 	if (ni == NULL)
6505548a605dSAdrian Chadd 		return;
6506548a605dSAdrian Chadd 
6507548a605dSAdrian Chadd 	an = ATH_NODE(ni);
6508548a605dSAdrian Chadd 	avp = ATH_VAP(ni->ni_vap);
6509548a605dSAdrian Chadd 
6510548a605dSAdrian Chadd 	/*
6511548a605dSAdrian Chadd 	 * And for operating modes without the TIM handler set, let's
6512548a605dSAdrian Chadd 	 * just skip those.
6513548a605dSAdrian Chadd 	 */
6514548a605dSAdrian Chadd 	if (avp->av_set_tim == NULL)
6515548a605dSAdrian Chadd 		return;
6516548a605dSAdrian Chadd 
65174bed2b67SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
6518548a605dSAdrian Chadd 
6519548a605dSAdrian Chadd 	if (enable) {
6520548a605dSAdrian Chadd 		if (an->an_is_powersave &&
6521548a605dSAdrian Chadd 		    an->an_tim_set == 0 &&
6522ba83edd4SAdrian Chadd 		    an->an_swq_depth != 0) {
6523548a605dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
65249b48fb4bSAdrian Chadd 			    "%s: %6D: swq_depth>0, tim_set=0, set!\n",
65259b48fb4bSAdrian Chadd 			    __func__,
65269b48fb4bSAdrian Chadd 			    ni->ni_macaddr,
65279b48fb4bSAdrian Chadd 			    ":");
6528548a605dSAdrian Chadd 			an->an_tim_set = 1;
6529548a605dSAdrian Chadd 			(void) avp->av_set_tim(ni, 1);
6530548a605dSAdrian Chadd 		}
6531548a605dSAdrian Chadd 	} else {
6532548a605dSAdrian Chadd 		/*
6533548a605dSAdrian Chadd 		 * Don't bother grabbing the lock unless the queue is empty.
6534548a605dSAdrian Chadd 		 */
65353b48f36eSAdrian Chadd 		if (an->an_swq_depth != 0)
6536548a605dSAdrian Chadd 			return;
6537548a605dSAdrian Chadd 
6538548a605dSAdrian Chadd 		if (an->an_is_powersave &&
6539548a605dSAdrian Chadd 		    an->an_stack_psq == 0 &&
6540548a605dSAdrian Chadd 		    an->an_tim_set == 1 &&
6541ba83edd4SAdrian Chadd 		    an->an_swq_depth == 0) {
6542548a605dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
654322a3aee6SAdrian Chadd 			    "%s: %6D: swq_depth=0, tim_set=1, psq_set=0,"
6544548a605dSAdrian Chadd 			    " clear!\n",
654522a3aee6SAdrian Chadd 			    __func__,
654622a3aee6SAdrian Chadd 			    ni->ni_macaddr,
654722a3aee6SAdrian Chadd 			    ":");
6548548a605dSAdrian Chadd 			an->an_tim_set = 0;
6549548a605dSAdrian Chadd 			(void) avp->av_set_tim(ni, 0);
6550548a605dSAdrian Chadd 		}
6551548a605dSAdrian Chadd 	}
6552bdbb6e5bSAdrian Chadd #else
6553bdbb6e5bSAdrian Chadd 	return;
6554bdbb6e5bSAdrian Chadd #endif	/* ATH_SW_PSQ */
6555548a605dSAdrian Chadd }
65560eb81626SAdrian Chadd 
655722a3aee6SAdrian Chadd /*
655822a3aee6SAdrian Chadd  * Received a ps-poll frame from net80211.
655922a3aee6SAdrian Chadd  *
656022a3aee6SAdrian Chadd  * Here we get a chance to serve out a software-queued frame ourselves
656122a3aee6SAdrian Chadd  * before we punt it to net80211 to transmit us one itself - either
656222a3aee6SAdrian Chadd  * because there's traffic in the net80211 psq, or a NULL frame to
656322a3aee6SAdrian Chadd  * indicate there's nothing else.
656422a3aee6SAdrian Chadd  */
656522a3aee6SAdrian Chadd static void
656622a3aee6SAdrian Chadd ath_node_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m)
656722a3aee6SAdrian Chadd {
656822a3aee6SAdrian Chadd #ifdef	ATH_SW_PSQ
656922a3aee6SAdrian Chadd 	struct ath_node *an;
657022a3aee6SAdrian Chadd 	struct ath_vap *avp;
657122a3aee6SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
65723797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
657322a3aee6SAdrian Chadd 	int tid;
657422a3aee6SAdrian Chadd 
657522a3aee6SAdrian Chadd 	/* Just paranoia */
657622a3aee6SAdrian Chadd 	if (ni == NULL)
657722a3aee6SAdrian Chadd 		return;
657822a3aee6SAdrian Chadd 
657922a3aee6SAdrian Chadd 	/*
658022a3aee6SAdrian Chadd 	 * Unassociated (temporary node) station.
658122a3aee6SAdrian Chadd 	 */
658222a3aee6SAdrian Chadd 	if (ni->ni_associd == 0)
658322a3aee6SAdrian Chadd 		return;
658422a3aee6SAdrian Chadd 
658522a3aee6SAdrian Chadd 	/*
658622a3aee6SAdrian Chadd 	 * We do have an active node, so let's begin looking into it.
658722a3aee6SAdrian Chadd 	 */
658822a3aee6SAdrian Chadd 	an = ATH_NODE(ni);
658922a3aee6SAdrian Chadd 	avp = ATH_VAP(ni->ni_vap);
659022a3aee6SAdrian Chadd 
659122a3aee6SAdrian Chadd 	/*
659222a3aee6SAdrian Chadd 	 * For now, we just call the original ps-poll method.
659322a3aee6SAdrian Chadd 	 * Once we're ready to flip this on:
659422a3aee6SAdrian Chadd 	 *
659522a3aee6SAdrian Chadd 	 * + Set leak to 1, as no matter what we're going to have
659622a3aee6SAdrian Chadd 	 *   to send a frame;
659722a3aee6SAdrian Chadd 	 * + Check the software queue and if there's something in it,
659822a3aee6SAdrian Chadd 	 *   schedule the highest TID thas has traffic from this node.
659922a3aee6SAdrian Chadd 	 *   Then make sure we schedule the software scheduler to
660022a3aee6SAdrian Chadd 	 *   run so it picks up said frame.
660122a3aee6SAdrian Chadd 	 *
660222a3aee6SAdrian Chadd 	 * That way whatever happens, we'll at least send _a_ frame
660322a3aee6SAdrian Chadd 	 * to the given node.
660422a3aee6SAdrian Chadd 	 *
660522a3aee6SAdrian Chadd 	 * Again, yes, it's crappy QoS if the node has multiple
660622a3aee6SAdrian Chadd 	 * TIDs worth of traffic - but let's get it working first
660722a3aee6SAdrian Chadd 	 * before we optimise it.
660822a3aee6SAdrian Chadd 	 *
660922a3aee6SAdrian Chadd 	 * Also yes, there's definitely latency here - we're not
661022a3aee6SAdrian Chadd 	 * direct dispatching to the hardware in this path (and
661122a3aee6SAdrian Chadd 	 * we're likely being called from the packet receive path,
661222a3aee6SAdrian Chadd 	 * so going back into TX may be a little hairy!) but again
661322a3aee6SAdrian Chadd 	 * I'd like to get this working first before optimising
661422a3aee6SAdrian Chadd 	 * turn-around time.
661522a3aee6SAdrian Chadd 	 */
661622a3aee6SAdrian Chadd 
661722a3aee6SAdrian Chadd 	ATH_TX_LOCK(sc);
661822a3aee6SAdrian Chadd 
661922a3aee6SAdrian Chadd 	/*
662022a3aee6SAdrian Chadd 	 * Legacy - we're called and the node isn't asleep.
662122a3aee6SAdrian Chadd 	 * Immediately punt.
662222a3aee6SAdrian Chadd 	 */
662322a3aee6SAdrian Chadd 	if (! an->an_is_powersave) {
662483bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
662522a3aee6SAdrian Chadd 		    "%s: %6D: not in powersave?\n",
662622a3aee6SAdrian Chadd 		    __func__,
662722a3aee6SAdrian Chadd 		    ni->ni_macaddr,
662822a3aee6SAdrian Chadd 		    ":");
662922a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
663022a3aee6SAdrian Chadd 		avp->av_recv_pspoll(ni, m);
663122a3aee6SAdrian Chadd 		return;
663222a3aee6SAdrian Chadd 	}
663322a3aee6SAdrian Chadd 
663422a3aee6SAdrian Chadd 	/*
663522a3aee6SAdrian Chadd 	 * We're in powersave.
663622a3aee6SAdrian Chadd 	 *
663722a3aee6SAdrian Chadd 	 * Leak a frame.
663822a3aee6SAdrian Chadd 	 */
663922a3aee6SAdrian Chadd 	an->an_leak_count = 1;
664022a3aee6SAdrian Chadd 
664122a3aee6SAdrian Chadd 	/*
664222a3aee6SAdrian Chadd 	 * Now, if there's no frames in the node, just punt to
664322a3aee6SAdrian Chadd 	 * recv_pspoll.
664422a3aee6SAdrian Chadd 	 *
664522a3aee6SAdrian Chadd 	 * Don't bother checking if the TIM bit is set, we really
664622a3aee6SAdrian Chadd 	 * only care if there are any frames here!
664722a3aee6SAdrian Chadd 	 */
664822a3aee6SAdrian Chadd 	if (an->an_swq_depth == 0) {
664922a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
665022a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
665122a3aee6SAdrian Chadd 		    "%s: %6D: SWQ empty; punting to net80211\n",
665222a3aee6SAdrian Chadd 		    __func__,
665322a3aee6SAdrian Chadd 		    ni->ni_macaddr,
665422a3aee6SAdrian Chadd 		    ":");
665522a3aee6SAdrian Chadd 		avp->av_recv_pspoll(ni, m);
665622a3aee6SAdrian Chadd 		return;
665722a3aee6SAdrian Chadd 	}
665822a3aee6SAdrian Chadd 
665922a3aee6SAdrian Chadd 	/*
666022a3aee6SAdrian Chadd 	 * Ok, let's schedule the highest TID that has traffic
666122a3aee6SAdrian Chadd 	 * and then schedule something.
666222a3aee6SAdrian Chadd 	 */
666322a3aee6SAdrian Chadd 	for (tid = IEEE80211_TID_SIZE - 1; tid >= 0; tid--) {
666422a3aee6SAdrian Chadd 		struct ath_tid *atid = &an->an_tid[tid];
666522a3aee6SAdrian Chadd 		/*
666622a3aee6SAdrian Chadd 		 * No frames? Skip.
666722a3aee6SAdrian Chadd 		 */
666822a3aee6SAdrian Chadd 		if (atid->axq_depth == 0)
666922a3aee6SAdrian Chadd 			continue;
667022a3aee6SAdrian Chadd 		ath_tx_tid_sched(sc, atid);
667122a3aee6SAdrian Chadd 		/*
667222a3aee6SAdrian Chadd 		 * XXX we could do a direct call to the TXQ
667322a3aee6SAdrian Chadd 		 * scheduler code here to optimise latency
667422a3aee6SAdrian Chadd 		 * at the expense of a REALLY deep callstack.
667522a3aee6SAdrian Chadd 		 */
667622a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
667722a3aee6SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_txqtask);
667822a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
667922a3aee6SAdrian Chadd 		    "%s: %6D: leaking frame to TID %d\n",
668022a3aee6SAdrian Chadd 		    __func__,
668122a3aee6SAdrian Chadd 		    ni->ni_macaddr,
668222a3aee6SAdrian Chadd 		    ":",
668322a3aee6SAdrian Chadd 		    tid);
668422a3aee6SAdrian Chadd 		return;
668522a3aee6SAdrian Chadd 	}
668622a3aee6SAdrian Chadd 
668722a3aee6SAdrian Chadd 	ATH_TX_UNLOCK(sc);
668822a3aee6SAdrian Chadd 
668922a3aee6SAdrian Chadd 	/*
669022a3aee6SAdrian Chadd 	 * XXX nothing in the TIDs at this point? Eek.
669122a3aee6SAdrian Chadd 	 */
669283bbd5ebSRui Paulo 	DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
669383bbd5ebSRui Paulo 	    "%s: %6D: TIDs empty, but ath_node showed traffic?!\n",
669422a3aee6SAdrian Chadd 	    __func__,
669522a3aee6SAdrian Chadd 	    ni->ni_macaddr,
669622a3aee6SAdrian Chadd 	    ":");
669722a3aee6SAdrian Chadd 	avp->av_recv_pspoll(ni, m);
669822a3aee6SAdrian Chadd #else
669922a3aee6SAdrian Chadd 	avp->av_recv_pspoll(ni, m);
670022a3aee6SAdrian Chadd #endif	/* ATH_SW_PSQ */
670122a3aee6SAdrian Chadd }
670222a3aee6SAdrian Chadd 
6703dba9c859SAdrian Chadd MODULE_VERSION(if_ath, 1);
6704dba9c859SAdrian Chadd MODULE_DEPEND(if_ath, wlan, 1, 1, 1);          /* 802.11 media layer */
67059389d5a9SAdrian Chadd #if	defined(IEEE80211_ALQ) || defined(AH_DEBUG_ALQ) || defined(ATH_DEBUG_ALQ)
670658816f3fSAdrian Chadd MODULE_DEPEND(if_ath, alq, 1, 1, 1);
670758816f3fSAdrian Chadd #endif
6708