xref: /freebsd/sys/dev/ath/if_ath.c (revision 82506f26c03aa312b91e01a797f31e061749a76d)
15591b213SSam Leffler /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
410ad9a77SSam Leffler  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
55591b213SSam Leffler  * All rights reserved.
65591b213SSam Leffler  *
75591b213SSam Leffler  * Redistribution and use in source and binary forms, with or without
85591b213SSam Leffler  * modification, are permitted provided that the following conditions
95591b213SSam Leffler  * are met:
105591b213SSam Leffler  * 1. Redistributions of source code must retain the above copyright
115591b213SSam Leffler  *    notice, this list of conditions and the following disclaimer,
125591b213SSam Leffler  *    without modification.
135591b213SSam Leffler  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
145591b213SSam Leffler  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
155591b213SSam Leffler  *    redistribution must be conditioned upon including a substantially
165591b213SSam Leffler  *    similar Disclaimer requirement for further binary redistribution.
175591b213SSam Leffler  *
185591b213SSam Leffler  * NO WARRANTY
195591b213SSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
205591b213SSam Leffler  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
215591b213SSam Leffler  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
225591b213SSam Leffler  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
235591b213SSam Leffler  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
245591b213SSam Leffler  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
255591b213SSam Leffler  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
265591b213SSam Leffler  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
275591b213SSam Leffler  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
285591b213SSam Leffler  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
295591b213SSam Leffler  * THE POSSIBILITY OF SUCH DAMAGES.
305591b213SSam Leffler  */
315591b213SSam Leffler 
325591b213SSam Leffler #include <sys/cdefs.h>
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 *);
1612e986da5SSam Leffler static void	ath_watchdog(void *);
1627a79cebfSGleb Smirnoff static void	ath_parent(struct ieee80211com *);
1635591b213SSam Leffler static void	ath_fatal_proc(void *, int);
164b032f27cSSam Leffler static void	ath_bmiss_vap(struct ieee80211vap *);
1655591b213SSam Leffler static void	ath_bmiss_proc(void *, int);
166fb3edd4fSAdrian Chadd static void	ath_tsfoor_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 */
2021410ca56SAdrian Chadd static int	ath_set_quiet_ie(struct ieee80211_node *, uint8_t *);
2035591b213SSam Leffler static void	ath_calibrate(void *);
204b032f27cSSam Leffler static int	ath_newstate(struct ieee80211vap *, enum ieee80211_state, int);
205e8fd88a3SSam Leffler static void	ath_setup_stationkey(struct ieee80211_node *);
206e9962332SSam Leffler static void	ath_newassoc(struct ieee80211_node *, int);
207b032f27cSSam Leffler static int	ath_setregdomain(struct ieee80211com *,
208b032f27cSSam Leffler 		    struct ieee80211_regdomain *, int,
209b032f27cSSam Leffler 		    struct ieee80211_channel []);
2105fe9f044SSam Leffler static void	ath_getradiocaps(struct ieee80211com *, int, int *,
211b032f27cSSam Leffler 		    struct ieee80211_channel []);
212b032f27cSSam Leffler static int	ath_getchannels(struct ath_softc *);
2135591b213SSam Leffler 
214c42a7b7eSSam Leffler static int	ath_rate_setup(struct ath_softc *, u_int mode);
2155591b213SSam Leffler static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
216c42a7b7eSSam Leffler 
217c42a7b7eSSam Leffler static void	ath_announce(struct ath_softc *);
2185591b213SSam Leffler 
21948237774SAdrian Chadd static void	ath_dfs_tasklet(void *, int);
2200eb81626SAdrian Chadd static void	ath_node_powersave(struct ieee80211_node *, int);
221548a605dSAdrian Chadd static int	ath_node_set_tim(struct ieee80211_node *, int);
22222a3aee6SAdrian Chadd static void	ath_node_recv_pspoll(struct ieee80211_node *, struct mbuf *);
22348237774SAdrian Chadd 
224584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
225a35dae8dSAdrian Chadd #include <dev/ath/if_ath_tdma.h>
226a35dae8dSAdrian Chadd #endif
22710ad9a77SSam Leffler 
2285591b213SSam Leffler SYSCTL_DECL(_hw_ath);
2295591b213SSam Leffler 
2305591b213SSam Leffler /* XXX validate sysctl values */
2312dc7fcc4SSam Leffler static	int ath_longcalinterval = 30;		/* long cals every 30 secs */
2322dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, longcal, CTLFLAG_RW, &ath_longcalinterval,
2332dc7fcc4SSam Leffler 	    0, "long chip calibration interval (secs)");
2342dc7fcc4SSam Leffler static	int ath_shortcalinterval = 100;		/* short cals every 100 ms */
2352dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, shortcal, CTLFLAG_RW, &ath_shortcalinterval,
2362dc7fcc4SSam Leffler 	    0, "short chip calibration interval (msecs)");
2372dc7fcc4SSam Leffler static	int ath_resetcalinterval = 20*60;	/* reset cal state 20 mins */
2382dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, resetcal, CTLFLAG_RW, &ath_resetcalinterval,
2392dc7fcc4SSam Leffler 	    0, "reset chip calibration results (secs)");
240a108ab63SAdrian Chadd static	int ath_anicalinterval = 100;		/* ANI calibration - 100 msec */
241a108ab63SAdrian Chadd SYSCTL_INT(_hw_ath, OID_AUTO, anical, CTLFLAG_RW, &ath_anicalinterval,
242a108ab63SAdrian Chadd 	    0, "ANI calibration (msecs)");
2435591b213SSam Leffler 
2443d184db2SAdrian Chadd int ath_rxbuf = ATH_RXBUF;		/* # rx buffers to allocate */
245af3b2549SHans Petter Selasky SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RWTUN, &ath_rxbuf,
246e2d787faSSam Leffler 	    0, "rx buffers allocated");
2473d184db2SAdrian Chadd int ath_txbuf = ATH_TXBUF;		/* # tx buffers to allocate */
248af3b2549SHans Petter Selasky SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RWTUN, &ath_txbuf,
249e2d787faSSam Leffler 	    0, "tx buffers allocated");
2503d184db2SAdrian Chadd int ath_txbuf_mgmt = ATH_MGMT_TXBUF;	/* # mgmt tx buffers to allocate */
251af3b2549SHans Petter Selasky SYSCTL_INT(_hw_ath, OID_AUTO, txbuf_mgmt, CTLFLAG_RWTUN, &ath_txbuf_mgmt,
252af33d486SAdrian Chadd 	    0, "tx (mgmt) buffers allocated");
253e2d787faSSam Leffler 
254a35dae8dSAdrian Chadd int ath_bstuck_threshold = 4;		/* max missed beacons */
255a32ac9d3SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, bstuck, CTLFLAG_RW, &ath_bstuck_threshold,
256a32ac9d3SSam Leffler 	    0, "max missed beacon xmits before chip reset");
257a32ac9d3SSam Leffler 
2586b349e5aSAdrian Chadd MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
259c42a7b7eSSam Leffler 
260f8418db5SAdrian Chadd void
ath_legacy_attach_comp_func(struct ath_softc * sc)261f8418db5SAdrian Chadd ath_legacy_attach_comp_func(struct ath_softc *sc)
262f8418db5SAdrian Chadd {
263f8418db5SAdrian Chadd 
264f8418db5SAdrian Chadd 	/*
265f8418db5SAdrian Chadd 	 * Special case certain configurations.  Note the
266f8418db5SAdrian Chadd 	 * CAB queue is handled by these specially so don't
267f8418db5SAdrian Chadd 	 * include them when checking the txq setup mask.
268f8418db5SAdrian Chadd 	 */
269f8418db5SAdrian Chadd 	switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
270f8418db5SAdrian Chadd 	case 0x01:
271f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
272f8418db5SAdrian Chadd 		break;
273f8418db5SAdrian Chadd 	case 0x0f:
274f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
275f8418db5SAdrian Chadd 		break;
276f8418db5SAdrian Chadd 	default:
277f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
278f8418db5SAdrian Chadd 		break;
279f8418db5SAdrian Chadd 	}
280f8418db5SAdrian Chadd }
281f8418db5SAdrian Chadd 
282f5c30c4eSAdrian Chadd /*
283f5c30c4eSAdrian Chadd  * Set the target power mode.
284f5c30c4eSAdrian Chadd  *
285f5c30c4eSAdrian Chadd  * If this is called during a point in time where
286f5c30c4eSAdrian Chadd  * the hardware is being programmed elsewhere, it will
287f5c30c4eSAdrian Chadd  * simply store it away and update it when all current
288f5c30c4eSAdrian Chadd  * uses of the hardware are completed.
2898c03e55dSAdrian Chadd  *
2908c03e55dSAdrian Chadd  * If the chip is going into network sleep or power off, then
2918c03e55dSAdrian Chadd  * we will wait until all uses of the chip are done before
2928c03e55dSAdrian Chadd  * going into network sleep or power off.
2938c03e55dSAdrian Chadd  *
2948c03e55dSAdrian Chadd  * If the chip is being programmed full-awake, then immediately
2958c03e55dSAdrian Chadd  * program it full-awake so we can actually stay awake rather than
2968c03e55dSAdrian Chadd  * the chip potentially going to sleep underneath us.
297f5c30c4eSAdrian Chadd  */
298f5c30c4eSAdrian Chadd void
_ath_power_setpower(struct ath_softc * sc,int power_state,int selfgen,const char * file,int line)2998c03e55dSAdrian Chadd _ath_power_setpower(struct ath_softc *sc, int power_state, int selfgen,
3008c03e55dSAdrian Chadd     const char *file, int line)
301f5c30c4eSAdrian Chadd {
302f5c30c4eSAdrian Chadd 	ATH_LOCK_ASSERT(sc);
303f5c30c4eSAdrian Chadd 
3048c03e55dSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) state=%d, refcnt=%d, target=%d, cur=%d\n",
305f5c30c4eSAdrian Chadd 	    __func__,
306f5c30c4eSAdrian Chadd 	    file,
307f5c30c4eSAdrian Chadd 	    line,
308f5c30c4eSAdrian Chadd 	    power_state,
3098c03e55dSAdrian Chadd 	    sc->sc_powersave_refcnt,
3108c03e55dSAdrian Chadd 	    sc->sc_target_powerstate,
3118c03e55dSAdrian Chadd 	    sc->sc_cur_powerstate);
312f5c30c4eSAdrian Chadd 
3138c03e55dSAdrian Chadd 	sc->sc_target_powerstate = power_state;
3148c03e55dSAdrian Chadd 
3158c03e55dSAdrian Chadd 	/*
3168c03e55dSAdrian Chadd 	 * Don't program the chip into network sleep if the chip
3178c03e55dSAdrian Chadd 	 * is being programmed elsewhere.
3188c03e55dSAdrian Chadd 	 *
3198c03e55dSAdrian Chadd 	 * However, if the chip is being programmed /awake/, force
3208c03e55dSAdrian Chadd 	 * the chip awake so we stay awake.
3218c03e55dSAdrian Chadd 	 */
3228c03e55dSAdrian Chadd 	if ((sc->sc_powersave_refcnt == 0 || power_state == HAL_PM_AWAKE) &&
323f5c30c4eSAdrian Chadd 	    power_state != sc->sc_cur_powerstate) {
324f5c30c4eSAdrian Chadd 		sc->sc_cur_powerstate = power_state;
325f5c30c4eSAdrian Chadd 		ath_hal_setpower(sc->sc_ah, power_state);
3267d567ed6SAdrian Chadd 
3277d567ed6SAdrian Chadd 		/*
3287d567ed6SAdrian Chadd 		 * If the NIC is force-awake, then set the
3297d567ed6SAdrian Chadd 		 * self-gen frame state appropriately.
3307d567ed6SAdrian Chadd 		 *
3317d567ed6SAdrian Chadd 		 * If the nic is in network sleep or full-sleep,
3327d567ed6SAdrian Chadd 		 * we let the above call leave the self-gen
3337d567ed6SAdrian Chadd 		 * state as "sleep".
3347d567ed6SAdrian Chadd 		 */
3358c03e55dSAdrian Chadd 		if (selfgen &&
3368c03e55dSAdrian Chadd 		    sc->sc_cur_powerstate == HAL_PM_AWAKE &&
3377d567ed6SAdrian Chadd 		    sc->sc_target_selfgen_state != HAL_PM_AWAKE) {
3387d567ed6SAdrian Chadd 			ath_hal_setselfgenpower(sc->sc_ah,
3397d567ed6SAdrian Chadd 			    sc->sc_target_selfgen_state);
3407d567ed6SAdrian Chadd 		}
3417d567ed6SAdrian Chadd 	}
3427d567ed6SAdrian Chadd }
3437d567ed6SAdrian Chadd 
3447d567ed6SAdrian Chadd /*
3457d567ed6SAdrian Chadd  * Set the current self-generated frames state.
3467d567ed6SAdrian Chadd  *
3477d567ed6SAdrian Chadd  * This is separate from the target power mode.  The chip may be
3487d567ed6SAdrian Chadd  * awake but the desired state is "sleep", so frames sent to the
3497d567ed6SAdrian Chadd  * destination has PWRMGT=1 in the 802.11 header.  The NIC also
3507d567ed6SAdrian Chadd  * needs to know to set PWRMGT=1 in self-generated frames.
3517d567ed6SAdrian Chadd  */
3527d567ed6SAdrian Chadd void
_ath_power_set_selfgen(struct ath_softc * sc,int power_state,const char * file,int line)3537d567ed6SAdrian Chadd _ath_power_set_selfgen(struct ath_softc *sc, int power_state, const char *file, int line)
3547d567ed6SAdrian Chadd {
3557d567ed6SAdrian Chadd 
3567d567ed6SAdrian Chadd 	ATH_LOCK_ASSERT(sc);
3577d567ed6SAdrian Chadd 
3587d567ed6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) state=%d, refcnt=%d\n",
3597d567ed6SAdrian Chadd 	    __func__,
3607d567ed6SAdrian Chadd 	    file,
3617d567ed6SAdrian Chadd 	    line,
3627d567ed6SAdrian Chadd 	    power_state,
3637d567ed6SAdrian Chadd 	    sc->sc_target_selfgen_state);
3647d567ed6SAdrian Chadd 
3657d567ed6SAdrian Chadd 	sc->sc_target_selfgen_state = power_state;
3667d567ed6SAdrian Chadd 
3677d567ed6SAdrian Chadd 	/*
3687d567ed6SAdrian Chadd 	 * If the NIC is force-awake, then set the power state.
3697d567ed6SAdrian Chadd 	 * Network-state and full-sleep will already transition it to
3707d567ed6SAdrian Chadd 	 * mark self-gen frames as sleeping - and we can't
3717d567ed6SAdrian Chadd 	 * guarantee the NIC is awake to program the self-gen frame
3727d567ed6SAdrian Chadd 	 * setting anyway.
3737d567ed6SAdrian Chadd 	 */
3747d567ed6SAdrian Chadd 	if (sc->sc_cur_powerstate == HAL_PM_AWAKE) {
3757d567ed6SAdrian Chadd 		ath_hal_setselfgenpower(sc->sc_ah, power_state);
376f5c30c4eSAdrian Chadd 	}
377f5c30c4eSAdrian Chadd }
378f5c30c4eSAdrian Chadd 
379f5c30c4eSAdrian Chadd /*
380f5c30c4eSAdrian Chadd  * Set the hardware power mode and take a reference.
381f5c30c4eSAdrian Chadd  *
382f5c30c4eSAdrian Chadd  * This doesn't update the target power mode in the driver;
383f5c30c4eSAdrian Chadd  * it just updates the hardware power state.
384f5c30c4eSAdrian Chadd  *
385f5c30c4eSAdrian Chadd  * XXX it should only ever force the hardware awake; it should
386f5c30c4eSAdrian Chadd  * never be called to set it asleep.
387f5c30c4eSAdrian Chadd  */
388f5c30c4eSAdrian Chadd void
_ath_power_set_power_state(struct ath_softc * sc,int power_state,const char * file,int line)389f5c30c4eSAdrian Chadd _ath_power_set_power_state(struct ath_softc *sc, int power_state, const char *file, int line)
390f5c30c4eSAdrian Chadd {
391f5c30c4eSAdrian Chadd 	ATH_LOCK_ASSERT(sc);
392f5c30c4eSAdrian Chadd 
393f5c30c4eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) state=%d, refcnt=%d\n",
394f5c30c4eSAdrian Chadd 	    __func__,
395f5c30c4eSAdrian Chadd 	    file,
396f5c30c4eSAdrian Chadd 	    line,
397f5c30c4eSAdrian Chadd 	    power_state,
398f5c30c4eSAdrian Chadd 	    sc->sc_powersave_refcnt);
399f5c30c4eSAdrian Chadd 
400f5c30c4eSAdrian Chadd 	sc->sc_powersave_refcnt++;
401f5c30c4eSAdrian Chadd 
4028c03e55dSAdrian Chadd 	/*
4038c03e55dSAdrian Chadd 	 * Only do the power state change if we're not programming
4048c03e55dSAdrian Chadd 	 * it elsewhere.
4058c03e55dSAdrian Chadd 	 */
406f5c30c4eSAdrian Chadd 	if (power_state != sc->sc_cur_powerstate) {
407f5c30c4eSAdrian Chadd 		ath_hal_setpower(sc->sc_ah, power_state);
408f5c30c4eSAdrian Chadd 		sc->sc_cur_powerstate = power_state;
4097d567ed6SAdrian Chadd 		/*
4107d567ed6SAdrian Chadd 		 * Adjust the self-gen powerstate if appropriate.
4117d567ed6SAdrian Chadd 		 */
4127d567ed6SAdrian Chadd 		if (sc->sc_cur_powerstate == HAL_PM_AWAKE &&
4137d567ed6SAdrian Chadd 		    sc->sc_target_selfgen_state != HAL_PM_AWAKE) {
4147d567ed6SAdrian Chadd 			ath_hal_setselfgenpower(sc->sc_ah,
4157d567ed6SAdrian Chadd 			    sc->sc_target_selfgen_state);
4167d567ed6SAdrian Chadd 		}
417f5c30c4eSAdrian Chadd 	}
418f5c30c4eSAdrian Chadd }
419f5c30c4eSAdrian Chadd 
420f5c30c4eSAdrian Chadd /*
421f5c30c4eSAdrian Chadd  * Restore the power save mode to what it once was.
422f5c30c4eSAdrian Chadd  *
423f5c30c4eSAdrian Chadd  * This will decrement the reference counter and once it hits
424f5c30c4eSAdrian Chadd  * zero, it'll restore the powersave state.
425f5c30c4eSAdrian Chadd  */
426f5c30c4eSAdrian Chadd void
_ath_power_restore_power_state(struct ath_softc * sc,const char * file,int line)427f5c30c4eSAdrian Chadd _ath_power_restore_power_state(struct ath_softc *sc, const char *file, int line)
428f5c30c4eSAdrian Chadd {
429f5c30c4eSAdrian Chadd 
430f5c30c4eSAdrian Chadd 	ATH_LOCK_ASSERT(sc);
431f5c30c4eSAdrian Chadd 
432f5c30c4eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) refcnt=%d, target state=%d\n",
433f5c30c4eSAdrian Chadd 	    __func__,
434f5c30c4eSAdrian Chadd 	    file,
435f5c30c4eSAdrian Chadd 	    line,
436f5c30c4eSAdrian Chadd 	    sc->sc_powersave_refcnt,
437f5c30c4eSAdrian Chadd 	    sc->sc_target_powerstate);
438f5c30c4eSAdrian Chadd 
439f5c30c4eSAdrian Chadd 	if (sc->sc_powersave_refcnt == 0)
440f5c30c4eSAdrian Chadd 		device_printf(sc->sc_dev, "%s: refcnt=0?\n", __func__);
441f5c30c4eSAdrian Chadd 	else
442f5c30c4eSAdrian Chadd 		sc->sc_powersave_refcnt--;
443f5c30c4eSAdrian Chadd 
444f5c30c4eSAdrian Chadd 	if (sc->sc_powersave_refcnt == 0 &&
445f5c30c4eSAdrian Chadd 	    sc->sc_target_powerstate != sc->sc_cur_powerstate) {
446f5c30c4eSAdrian Chadd 		sc->sc_cur_powerstate = sc->sc_target_powerstate;
447f5c30c4eSAdrian Chadd 		ath_hal_setpower(sc->sc_ah, sc->sc_target_powerstate);
448f5c30c4eSAdrian Chadd 	}
4497d567ed6SAdrian Chadd 
4507d567ed6SAdrian Chadd 	/*
4517d567ed6SAdrian Chadd 	 * Adjust the self-gen powerstate if appropriate.
4527d567ed6SAdrian Chadd 	 */
4537d567ed6SAdrian Chadd 	if (sc->sc_cur_powerstate == HAL_PM_AWAKE &&
4547d567ed6SAdrian Chadd 	    sc->sc_target_selfgen_state != HAL_PM_AWAKE) {
4557d567ed6SAdrian Chadd 		ath_hal_setselfgenpower(sc->sc_ah,
4567d567ed6SAdrian Chadd 		    sc->sc_target_selfgen_state);
4577d567ed6SAdrian Chadd 	}
4587d567ed6SAdrian Chadd 
459f5c30c4eSAdrian Chadd }
460f5c30c4eSAdrian Chadd 
4619389d5a9SAdrian Chadd /*
4629389d5a9SAdrian Chadd  * Configure the initial HAL configuration values based on bus
4639389d5a9SAdrian Chadd  * specific parameters.
4649389d5a9SAdrian Chadd  *
4659389d5a9SAdrian Chadd  * Some PCI IDs and other information may need tweaking.
4669389d5a9SAdrian Chadd  *
4679389d5a9SAdrian Chadd  * XXX TODO: ath9k and the Atheros HAL only program comm2g_switch_enable
4689389d5a9SAdrian Chadd  * if BT antenna diversity isn't enabled.
4699389d5a9SAdrian Chadd  *
4709389d5a9SAdrian Chadd  * So, let's also figure out how to enable BT diversity for AR9485.
4719389d5a9SAdrian Chadd  */
4729389d5a9SAdrian Chadd static void
ath_setup_hal_config(struct ath_softc * sc,HAL_OPS_CONFIG * ah_config)4739389d5a9SAdrian Chadd ath_setup_hal_config(struct ath_softc *sc, HAL_OPS_CONFIG *ah_config)
4749389d5a9SAdrian Chadd {
4759389d5a9SAdrian Chadd 	/* XXX TODO: only for PCI devices? */
4769389d5a9SAdrian Chadd 
4779389d5a9SAdrian Chadd 	if (sc->sc_pci_devinfo & (ATH_PCI_CUS198 | ATH_PCI_CUS230)) {
4789389d5a9SAdrian Chadd 		ah_config->ath_hal_ext_lna_ctl_gpio = 0x200; /* bit 9 */
4799389d5a9SAdrian Chadd 		ah_config->ath_hal_ext_atten_margin_cfg = AH_TRUE;
4809389d5a9SAdrian Chadd 		ah_config->ath_hal_min_gainidx = AH_TRUE;
4819389d5a9SAdrian Chadd 		ah_config->ath_hal_ant_ctrl_comm2g_switch_enable = 0x000bbb88;
4829389d5a9SAdrian Chadd 		/* XXX low_rssi_thresh */
4839389d5a9SAdrian Chadd 		/* XXX fast_div_bias */
4849389d5a9SAdrian Chadd 		device_printf(sc->sc_dev, "configuring for %s\n",
4859389d5a9SAdrian Chadd 		    (sc->sc_pci_devinfo & ATH_PCI_CUS198) ?
4869389d5a9SAdrian Chadd 		    "CUS198" : "CUS230");
4879389d5a9SAdrian Chadd 	}
4889389d5a9SAdrian Chadd 
4899389d5a9SAdrian Chadd 	if (sc->sc_pci_devinfo & ATH_PCI_CUS217)
4909389d5a9SAdrian Chadd 		device_printf(sc->sc_dev, "CUS217 card detected\n");
4919389d5a9SAdrian Chadd 
4929389d5a9SAdrian Chadd 	if (sc->sc_pci_devinfo & ATH_PCI_CUS252)
4939389d5a9SAdrian Chadd 		device_printf(sc->sc_dev, "CUS252 card detected\n");
4949389d5a9SAdrian Chadd 
4959389d5a9SAdrian Chadd 	if (sc->sc_pci_devinfo & ATH_PCI_AR9565_1ANT)
4969389d5a9SAdrian Chadd 		device_printf(sc->sc_dev, "WB335 1-ANT card detected\n");
4979389d5a9SAdrian Chadd 
4989389d5a9SAdrian Chadd 	if (sc->sc_pci_devinfo & ATH_PCI_AR9565_2ANT)
4999389d5a9SAdrian Chadd 		device_printf(sc->sc_dev, "WB335 2-ANT card detected\n");
5009389d5a9SAdrian Chadd 
501bcf5fc49SAdrian Chadd 	if (sc->sc_pci_devinfo & ATH_PCI_BT_ANT_DIV)
502bcf5fc49SAdrian Chadd 		device_printf(sc->sc_dev,
503bcf5fc49SAdrian Chadd 		    "Bluetooth Antenna Diversity card detected\n");
504bcf5fc49SAdrian Chadd 
5059389d5a9SAdrian Chadd 	if (sc->sc_pci_devinfo & ATH_PCI_KILLER)
5069389d5a9SAdrian Chadd 		device_printf(sc->sc_dev, "Killer Wireless card detected\n");
5079389d5a9SAdrian Chadd 
5089389d5a9SAdrian Chadd #if 0
5099389d5a9SAdrian Chadd         /*
5109389d5a9SAdrian Chadd          * Some WB335 cards do not support antenna diversity. Since
5119389d5a9SAdrian Chadd          * we use a hardcoded value for AR9565 instead of using the
5129389d5a9SAdrian Chadd          * EEPROM/OTP data, remove the combining feature from
5139389d5a9SAdrian Chadd          * the HW capabilities bitmap.
5149389d5a9SAdrian Chadd          */
5159389d5a9SAdrian Chadd         if (sc->sc_pci_devinfo & (ATH9K_PCI_AR9565_1ANT | ATH9K_PCI_AR9565_2ANT)) {
5169389d5a9SAdrian Chadd                 if (!(sc->sc_pci_devinfo & ATH9K_PCI_BT_ANT_DIV))
5179389d5a9SAdrian Chadd                         pCap->hw_caps &= ~ATH9K_HW_CAP_ANT_DIV_COMB;
5189389d5a9SAdrian Chadd         }
5199389d5a9SAdrian Chadd 
5209389d5a9SAdrian Chadd         if (sc->sc_pci_devinfo & ATH9K_PCI_BT_ANT_DIV) {
5219389d5a9SAdrian Chadd                 pCap->hw_caps |= ATH9K_HW_CAP_BT_ANT_DIV;
5229389d5a9SAdrian Chadd                 device_printf(sc->sc_dev, "Set BT/WLAN RX diversity capability\n");
5239389d5a9SAdrian Chadd         }
5249389d5a9SAdrian Chadd #endif
5259389d5a9SAdrian Chadd 
5269389d5a9SAdrian Chadd         if (sc->sc_pci_devinfo & ATH_PCI_D3_L1_WAR) {
5279389d5a9SAdrian Chadd                 ah_config->ath_hal_pcie_waen = 0x0040473b;
5289389d5a9SAdrian Chadd                 device_printf(sc->sc_dev, "Enable WAR for ASPM D3/L1\n");
5299389d5a9SAdrian Chadd         }
5309389d5a9SAdrian Chadd 
5319389d5a9SAdrian Chadd #if 0
5329389d5a9SAdrian Chadd         if (sc->sc_pci_devinfo & ATH9K_PCI_NO_PLL_PWRSAVE) {
5339389d5a9SAdrian Chadd                 ah->config.no_pll_pwrsave = true;
5349389d5a9SAdrian Chadd                 device_printf(sc->sc_dev, "Disable PLL PowerSave\n");
5359389d5a9SAdrian Chadd         }
5369389d5a9SAdrian Chadd #endif
5379389d5a9SAdrian Chadd 
5389389d5a9SAdrian Chadd }
5399389d5a9SAdrian Chadd 
540240b1f1dSAdrian Chadd /*
541240b1f1dSAdrian Chadd  * Attempt to fetch the MAC address from the kernel environment.
542240b1f1dSAdrian Chadd  *
543240b1f1dSAdrian Chadd  * Returns 0, macaddr in macaddr if successful; -1 otherwise.
544240b1f1dSAdrian Chadd  */
545240b1f1dSAdrian Chadd static int
ath_fetch_mac_kenv(struct ath_softc * sc,uint8_t * macaddr)546240b1f1dSAdrian Chadd ath_fetch_mac_kenv(struct ath_softc *sc, uint8_t *macaddr)
547240b1f1dSAdrian Chadd {
548240b1f1dSAdrian Chadd 	char devid_str[32];
549240b1f1dSAdrian Chadd 	int local_mac = 0;
550240b1f1dSAdrian Chadd 	char *local_macstr;
551240b1f1dSAdrian Chadd 
552240b1f1dSAdrian Chadd 	/*
553240b1f1dSAdrian Chadd 	 * Fetch from the kenv rather than using hints.
554240b1f1dSAdrian Chadd 	 *
555240b1f1dSAdrian Chadd 	 * Hints would be nice but the transition to dynamic
556240b1f1dSAdrian Chadd 	 * hints/kenv doesn't happen early enough for this
557240b1f1dSAdrian Chadd 	 * to work reliably (eg on anything embedded.)
558240b1f1dSAdrian Chadd 	 */
559240b1f1dSAdrian Chadd 	snprintf(devid_str, 32, "hint.%s.%d.macaddr",
560240b1f1dSAdrian Chadd 	    device_get_name(sc->sc_dev),
561240b1f1dSAdrian Chadd 	    device_get_unit(sc->sc_dev));
562240b1f1dSAdrian Chadd 
563240b1f1dSAdrian Chadd 	if ((local_macstr = kern_getenv(devid_str)) != NULL) {
564240b1f1dSAdrian Chadd 		uint32_t tmpmac[ETHER_ADDR_LEN];
565240b1f1dSAdrian Chadd 		int count;
566240b1f1dSAdrian Chadd 		int i;
567240b1f1dSAdrian Chadd 
568240b1f1dSAdrian Chadd 		/* Have a MAC address; should use it */
569240b1f1dSAdrian Chadd 		device_printf(sc->sc_dev,
570240b1f1dSAdrian Chadd 		    "Overriding MAC address from environment: '%s'\n",
571240b1f1dSAdrian Chadd 		    local_macstr);
572240b1f1dSAdrian Chadd 
573240b1f1dSAdrian Chadd 		/* Extract out the MAC address */
574240b1f1dSAdrian Chadd 		count = sscanf(local_macstr, "%x%*c%x%*c%x%*c%x%*c%x%*c%x",
575240b1f1dSAdrian Chadd 		    &tmpmac[0], &tmpmac[1],
576240b1f1dSAdrian Chadd 		    &tmpmac[2], &tmpmac[3],
577240b1f1dSAdrian Chadd 		    &tmpmac[4], &tmpmac[5]);
578240b1f1dSAdrian Chadd 		if (count == 6) {
579240b1f1dSAdrian Chadd 			/* Valid! */
580240b1f1dSAdrian Chadd 			local_mac = 1;
581240b1f1dSAdrian Chadd 			for (i = 0; i < ETHER_ADDR_LEN; i++)
582240b1f1dSAdrian Chadd 				macaddr[i] = tmpmac[i];
583240b1f1dSAdrian Chadd 		}
584240b1f1dSAdrian Chadd 		/* Done! */
585240b1f1dSAdrian Chadd 		freeenv(local_macstr);
586240b1f1dSAdrian Chadd 		local_macstr = NULL;
587240b1f1dSAdrian Chadd 	}
588240b1f1dSAdrian Chadd 
589240b1f1dSAdrian Chadd 	if (local_mac)
590240b1f1dSAdrian Chadd 		return (0);
591240b1f1dSAdrian Chadd 	return (-1);
592240b1f1dSAdrian Chadd }
593240b1f1dSAdrian Chadd 
59467397d39SAdrian Chadd #define	HAL_MODE_HT20 (HAL_MODE_11NG_HT20 | HAL_MODE_11NA_HT20)
59567397d39SAdrian Chadd #define	HAL_MODE_HT40 \
59667397d39SAdrian Chadd 	(HAL_MODE_11NG_HT40PLUS | HAL_MODE_11NG_HT40MINUS | \
59767397d39SAdrian Chadd 	HAL_MODE_11NA_HT40PLUS | HAL_MODE_11NA_HT40MINUS)
5985591b213SSam Leffler int
ath_attach(u_int16_t devid,struct ath_softc * sc)5995591b213SSam Leffler ath_attach(u_int16_t devid, struct ath_softc *sc)
6005591b213SSam Leffler {
6017a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
602fc74a9f9SBrooks Davis 	struct ath_hal *ah = NULL;
6035591b213SSam Leffler 	HAL_STATUS status;
604c42a7b7eSSam Leffler 	int error = 0, i;
605411373ebSSam Leffler 	u_int wmodes;
606a865860dSAdrian Chadd 	int rx_chainmask, tx_chainmask;
6079389d5a9SAdrian Chadd 	HAL_OPS_CONFIG ah_config;
6085591b213SSam Leffler 
609c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
6105591b213SSam Leffler 
61159686fe9SGleb Smirnoff 	ic->ic_softc = sc;
612c8550c02SGleb Smirnoff 	ic->ic_name = device_get_nameunit(sc->sc_dev);
613fc74a9f9SBrooks Davis 
6149389d5a9SAdrian Chadd 	/*
6159389d5a9SAdrian Chadd 	 * Configure the initial configuration data.
6169389d5a9SAdrian Chadd 	 *
6179389d5a9SAdrian Chadd 	 * This is stuff that may be needed early during attach
6189389d5a9SAdrian Chadd 	 * rather than done via configuration calls later.
6199389d5a9SAdrian Chadd 	 */
6209389d5a9SAdrian Chadd 	bzero(&ah_config, sizeof(ah_config));
6219389d5a9SAdrian Chadd 	ath_setup_hal_config(sc, &ah_config);
6229389d5a9SAdrian Chadd 
6237e97436bSAdrian Chadd 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh,
6249389d5a9SAdrian Chadd 	    sc->sc_eepromdata, &ah_config, &status);
6255591b213SSam Leffler 	if (ah == NULL) {
62676e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
62776e6fd5dSGleb Smirnoff 		    "unable to attach hardware; HAL status %u\n", status);
6285591b213SSam Leffler 		error = ENXIO;
6295591b213SSam Leffler 		goto bad;
6305591b213SSam Leffler 	}
6315591b213SSam Leffler 	sc->sc_ah = ah;
632b58b3803SSam Leffler 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
6333297be13SSam Leffler #ifdef	ATH_DEBUG
6343297be13SSam Leffler 	sc->sc_debug = ath_debug;
6353297be13SSam Leffler #endif
6365591b213SSam Leffler 
6375591b213SSam Leffler 	/*
63846be12aeSAdrian Chadd 	 * Force the chip awake during setup, just to keep
63946be12aeSAdrian Chadd 	 * the HAL/driver power tracking happy.
64046be12aeSAdrian Chadd 	 *
64146be12aeSAdrian Chadd 	 * There are some methods (eg ath_hal_setmac())
64246be12aeSAdrian Chadd 	 * that poke the hardware.
64346be12aeSAdrian Chadd 	 */
64446be12aeSAdrian Chadd 	ATH_LOCK(sc);
64546be12aeSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_AWAKE, 1);
64646be12aeSAdrian Chadd 	ATH_UNLOCK(sc);
64746be12aeSAdrian Chadd 
64846be12aeSAdrian Chadd 	/*
649f8cc9b09SAdrian Chadd 	 * Setup the DMA/EDMA functions based on the current
650f8cc9b09SAdrian Chadd 	 * hardware support.
651f8cc9b09SAdrian Chadd 	 *
652f8cc9b09SAdrian Chadd 	 * This is required before the descriptors are allocated.
653f8cc9b09SAdrian Chadd 	 */
6543d184db2SAdrian Chadd 	if (ath_hal_hasedma(sc->sc_ah)) {
6553d184db2SAdrian Chadd 		sc->sc_isedma = 1;
656f8cc9b09SAdrian Chadd 		ath_recv_setup_edma(sc);
6573fdfc330SAdrian Chadd 		ath_xmit_setup_edma(sc);
6583fdfc330SAdrian Chadd 	} else {
659f8cc9b09SAdrian Chadd 		ath_recv_setup_legacy(sc);
6603fdfc330SAdrian Chadd 		ath_xmit_setup_legacy(sc);
6613fdfc330SAdrian Chadd 	}
662f8cc9b09SAdrian Chadd 
663f5c30c4eSAdrian Chadd 	if (ath_hal_hasmybeacon(sc->sc_ah)) {
664f5c30c4eSAdrian Chadd 		sc->sc_do_mybeacon = 1;
665f5c30c4eSAdrian Chadd 	}
666f5c30c4eSAdrian Chadd 
667f8cc9b09SAdrian Chadd 	/*
668c42a7b7eSSam Leffler 	 * Check if the MAC has multi-rate retry support.
669c42a7b7eSSam Leffler 	 * We do this by trying to setup a fake extended
670c42a7b7eSSam Leffler 	 * descriptor.  MAC's that don't have support will
671c42a7b7eSSam Leffler 	 * return false w/o doing anything.  MAC's that do
672c42a7b7eSSam Leffler 	 * support it will return true w/o doing anything.
673c42a7b7eSSam Leffler 	 */
674c42a7b7eSSam Leffler 	sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
675c42a7b7eSSam Leffler 
676c42a7b7eSSam Leffler 	/*
677c42a7b7eSSam Leffler 	 * Check if the device has hardware counters for PHY
678c42a7b7eSSam Leffler 	 * errors.  If so we need to enable the MIB interrupt
679c42a7b7eSSam Leffler 	 * so we can act on stat triggers.
680c42a7b7eSSam Leffler 	 */
681c42a7b7eSSam Leffler 	if (ath_hal_hwphycounters(ah))
682c42a7b7eSSam Leffler 		sc->sc_needmib = 1;
683c42a7b7eSSam Leffler 
684c42a7b7eSSam Leffler 	/*
685c42a7b7eSSam Leffler 	 * Get the hardware key cache size.
686c42a7b7eSSam Leffler 	 */
687c42a7b7eSSam Leffler 	sc->sc_keymax = ath_hal_keycachesize(ah);
688e8fd88a3SSam Leffler 	if (sc->sc_keymax > ATH_KEYMAX) {
68976e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
69076e6fd5dSGleb Smirnoff 		    "Warning, using only %u of %u key cache slots\n",
691e8fd88a3SSam Leffler 		    ATH_KEYMAX, sc->sc_keymax);
692e8fd88a3SSam Leffler 		sc->sc_keymax = ATH_KEYMAX;
693c42a7b7eSSam Leffler 	}
694c42a7b7eSSam Leffler 	/*
695c42a7b7eSSam Leffler 	 * Reset the key cache since some parts do not
696c42a7b7eSSam Leffler 	 * reset the contents on initial power up.
697c42a7b7eSSam Leffler 	 */
698c42a7b7eSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
699c42a7b7eSSam Leffler 		ath_hal_keyreset(ah, i);
700c42a7b7eSSam Leffler 
701c42a7b7eSSam Leffler 	/*
702b032f27cSSam Leffler 	 * Collect the default channel list.
7035591b213SSam Leffler 	 */
704b032f27cSSam Leffler 	error = ath_getchannels(sc);
7055591b213SSam Leffler 	if (error != 0)
7065591b213SSam Leffler 		goto bad;
7075591b213SSam Leffler 
7085591b213SSam Leffler 	/*
7095591b213SSam Leffler 	 * Setup rate tables for all potential media types.
7105591b213SSam Leffler 	 */
7115591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11A);
7125591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11B);
7135591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11G);
714c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
715c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
71668e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_STURBO_A);
71768e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NA);
71868e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NG);
719724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_HALF);
720724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_QUARTER);
721aaa70f2fSSam Leffler 
722c42a7b7eSSam Leffler 	/* NB: setup here so ath_rate_update is happy */
723c42a7b7eSSam Leffler 	ath_setcurmode(sc, IEEE80211_MODE_11A);
7245591b213SSam Leffler 
725c42a7b7eSSam Leffler 	/*
7263fdfc330SAdrian Chadd 	 * Allocate TX descriptors and populate the lists.
727c42a7b7eSSam Leffler 	 */
7285591b213SSam Leffler 	error = ath_desc_alloc(sc);
7295591b213SSam Leffler 	if (error != 0) {
73076e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
73176e6fd5dSGleb Smirnoff 		    "failed to allocate TX descriptors: %d\n", error);
7323fdfc330SAdrian Chadd 		goto bad;
7333fdfc330SAdrian Chadd 	}
7343fdfc330SAdrian Chadd 	error = ath_txdma_setup(sc);
7353fdfc330SAdrian Chadd 	if (error != 0) {
73676e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
73776e6fd5dSGleb Smirnoff 		    "failed to allocate TX descriptors: %d\n", error);
7385591b213SSam Leffler 		goto bad;
7395591b213SSam Leffler 	}
7403d184db2SAdrian Chadd 
7413fdfc330SAdrian Chadd 	/*
7423fdfc330SAdrian Chadd 	 * Allocate RX descriptors and populate the lists.
7433fdfc330SAdrian Chadd 	 */
7443d184db2SAdrian Chadd 	error = ath_rxdma_setup(sc);
7453d184db2SAdrian Chadd 	if (error != 0) {
74676e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
74776e6fd5dSGleb Smirnoff 		     "failed to allocate RX descriptors: %d\n", error);
7483d184db2SAdrian Chadd 		goto bad;
7493d184db2SAdrian Chadd 	}
7503d184db2SAdrian Chadd 
751adcdc8f2SAdrian Chadd 	callout_init_mtx(&sc->sc_cal_ch, &sc->sc_mtx, 0);
752adcdc8f2SAdrian Chadd 	callout_init_mtx(&sc->sc_wd_ch, &sc->sc_mtx, 0);
7535591b213SSam Leffler 
754f0b2a0beSSam Leffler 	ATH_TXBUF_LOCK_INIT(sc);
7555591b213SSam Leffler 
7560bbf5441SSam Leffler 	sc->sc_tq = taskqueue_create("ath_taskq", M_NOWAIT,
7570bbf5441SSam Leffler 		taskqueue_thread_enqueue, &sc->sc_tq);
7587a79cebfSGleb Smirnoff 	taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, "%s taskq",
7597a79cebfSGleb Smirnoff 	    device_get_nameunit(sc->sc_dev));
7600bbf5441SSam Leffler 
761*82506f26SBjoern A. Zeeb 	TASK_INIT(&sc->sc_rxtask, 0, sc->sc_rx.recv_tasklet, sc);
7625591b213SSam Leffler 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
763fb3edd4fSAdrian Chadd 	TASK_INIT(&sc->sc_tsfoortask, 0, ath_tsfoor_proc, sc);
764c42a7b7eSSam Leffler 	TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc);
765d52f7132SAdrian Chadd 	TASK_INIT(&sc->sc_resettask,0, ath_reset_proc, sc);
76603e9308fSAdrian Chadd 	TASK_INIT(&sc->sc_txqtask, 0, ath_txq_sched_tasklet, sc);
767f846cf42SAdrian Chadd 	TASK_INIT(&sc->sc_fataltask, 0, ath_fatal_proc, sc);
7685591b213SSam Leffler 
7695591b213SSam Leffler 	/*
770c42a7b7eSSam Leffler 	 * Allocate hardware transmit queues: one queue for
771c42a7b7eSSam Leffler 	 * beacon frames and one data queue for each QoS
7724fa8d4efSDaniel Eischen 	 * priority.  Note that the hal handles resetting
773c42a7b7eSSam Leffler 	 * these queues at the needed time.
774c42a7b7eSSam Leffler 	 *
775c42a7b7eSSam Leffler 	 * XXX PS-Poll
7765591b213SSam Leffler 	 */
777e1252ce1SAdrian Chadd 	sc->sc_bhalq = ath_beaconq_setup(sc);
7785591b213SSam Leffler 	if (sc->sc_bhalq == (u_int) -1) {
77976e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
78076e6fd5dSGleb Smirnoff 		    "unable to setup a beacon xmit queue!\n");
781c42a7b7eSSam Leffler 		error = EIO;
782b28b4653SSam Leffler 		goto bad2;
7835591b213SSam Leffler 	}
784c42a7b7eSSam Leffler 	sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
785c42a7b7eSSam Leffler 	if (sc->sc_cabq == NULL) {
78676e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "unable to setup CAB xmit queue!\n");
787c42a7b7eSSam Leffler 		error = EIO;
788c42a7b7eSSam Leffler 		goto bad2;
789c42a7b7eSSam Leffler 	}
790c42a7b7eSSam Leffler 	/* NB: insure BK queue is the lowest priority h/w queue */
791c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
79276e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
79376e6fd5dSGleb Smirnoff 		    "unable to setup xmit queue for %s traffic!\n",
794c42a7b7eSSam Leffler 		    ieee80211_wme_acnames[WME_AC_BK]);
795c42a7b7eSSam Leffler 		error = EIO;
796c42a7b7eSSam Leffler 		goto bad2;
797c42a7b7eSSam Leffler 	}
798c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
799c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
800c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
801c42a7b7eSSam Leffler 		/*
802c42a7b7eSSam Leffler 		 * Not enough hardware tx queues to properly do WME;
803c42a7b7eSSam Leffler 		 * just punt and assign them all to the same h/w queue.
804c42a7b7eSSam Leffler 		 * We could do a better job of this if, for example,
805c42a7b7eSSam Leffler 		 * we allocate queues when we switch from station to
806c42a7b7eSSam Leffler 		 * AP mode.
807c42a7b7eSSam Leffler 		 */
808c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_VI] != NULL)
809c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
810c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_BE] != NULL)
811c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
812c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
813c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
814c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
815c42a7b7eSSam Leffler 	}
816c42a7b7eSSam Leffler 
817c42a7b7eSSam Leffler 	/*
818f8418db5SAdrian Chadd 	 * Attach the TX completion function.
819f8418db5SAdrian Chadd 	 *
820f8418db5SAdrian Chadd 	 * The non-EDMA chips may have some special case optimisations;
821f8418db5SAdrian Chadd 	 * this method gives everyone a chance to attach cleanly.
822c42a7b7eSSam Leffler 	 */
823f8418db5SAdrian Chadd 	sc->sc_tx.xmit_attach_comp_func(sc);
824c42a7b7eSSam Leffler 
825c42a7b7eSSam Leffler 	/*
826c42a7b7eSSam Leffler 	 * Setup rate control.  Some rate control modules
827c42a7b7eSSam Leffler 	 * call back to change the anntena state so expose
828c42a7b7eSSam Leffler 	 * the necessary entry points.
829c42a7b7eSSam Leffler 	 * XXX maybe belongs in struct ath_ratectrl?
830c42a7b7eSSam Leffler 	 */
831c42a7b7eSSam Leffler 	sc->sc_setdefantenna = ath_setdefantenna;
832c42a7b7eSSam Leffler 	sc->sc_rc = ath_rate_attach(sc);
833c42a7b7eSSam Leffler 	if (sc->sc_rc == NULL) {
834c42a7b7eSSam Leffler 		error = EIO;
835c42a7b7eSSam Leffler 		goto bad2;
836c42a7b7eSSam Leffler 	}
837c42a7b7eSSam Leffler 
83848237774SAdrian Chadd 	/* Attach DFS module */
83948237774SAdrian Chadd 	if (! ath_dfs_attach(sc)) {
8407e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
8417e97436bSAdrian Chadd 		    "%s: unable to attach DFS\n", __func__);
84248237774SAdrian Chadd 		error = EIO;
84348237774SAdrian Chadd 		goto bad2;
84448237774SAdrian Chadd 	}
84548237774SAdrian Chadd 
8469af351f9SAdrian Chadd 	/* Attach spectral module */
8479af351f9SAdrian Chadd 	if (ath_spectral_attach(sc) < 0) {
8489af351f9SAdrian Chadd 		device_printf(sc->sc_dev,
8499af351f9SAdrian Chadd 		    "%s: unable to attach spectral\n", __func__);
8509af351f9SAdrian Chadd 		error = EIO;
8519af351f9SAdrian Chadd 		goto bad2;
8529af351f9SAdrian Chadd 	}
8539af351f9SAdrian Chadd 
854b70f530bSAdrian Chadd 	/* Attach bluetooth coexistence module */
855b70f530bSAdrian Chadd 	if (ath_btcoex_attach(sc) < 0) {
856b70f530bSAdrian Chadd 		device_printf(sc->sc_dev,
857b70f530bSAdrian Chadd 		    "%s: unable to attach bluetooth coexistence\n", __func__);
858b70f530bSAdrian Chadd 		error = EIO;
859b70f530bSAdrian Chadd 		goto bad2;
860b70f530bSAdrian Chadd 	}
861b70f530bSAdrian Chadd 
862216ca234SAdrian Chadd 	/* Attach LNA diversity module */
863216ca234SAdrian Chadd 	if (ath_lna_div_attach(sc) < 0) {
864216ca234SAdrian Chadd 		device_printf(sc->sc_dev,
865216ca234SAdrian Chadd 		    "%s: unable to attach LNA diversity\n", __func__);
866216ca234SAdrian Chadd 		error = EIO;
867216ca234SAdrian Chadd 		goto bad2;
868216ca234SAdrian Chadd 	}
869216ca234SAdrian Chadd 
87048237774SAdrian Chadd 	/* Start DFS processing tasklet */
87148237774SAdrian Chadd 	TASK_INIT(&sc->sc_dfstask, 0, ath_dfs_tasklet, sc);
87248237774SAdrian Chadd 
8733440495aSAdrian Chadd 	/* Configure LED state */
8743e50ec2cSSam Leffler 	sc->sc_blinking = 0;
875c42a7b7eSSam Leffler 	sc->sc_ledstate = 1;
8763e50ec2cSSam Leffler 	sc->sc_ledon = 0;			/* low true */
8773e50ec2cSSam Leffler 	sc->sc_ledidle = (2700*hz)/1000;	/* 2.7sec */
878fd90e2edSJung-uk Kim 	callout_init(&sc->sc_ledtimer, 1);
8793440495aSAdrian Chadd 
8803440495aSAdrian Chadd 	/*
8813440495aSAdrian Chadd 	 * Don't setup hardware-based blinking.
8823440495aSAdrian Chadd 	 *
8833440495aSAdrian Chadd 	 * Although some NICs may have this configured in the
8843440495aSAdrian Chadd 	 * default reset register values, the user may wish
8853440495aSAdrian Chadd 	 * to alter which pins have which function.
8863440495aSAdrian Chadd 	 *
8873440495aSAdrian Chadd 	 * The reference driver attaches the MAC network LED to GPIO1 and
8883440495aSAdrian Chadd 	 * the MAC power LED to GPIO2.  However, the DWA-552 cardbus
8893440495aSAdrian Chadd 	 * NIC has these reversed.
8903440495aSAdrian Chadd 	 */
8913440495aSAdrian Chadd 	sc->sc_hardled = (1 == 0);
8923440495aSAdrian Chadd 	sc->sc_led_net_pin = -1;
8933440495aSAdrian Chadd 	sc->sc_led_pwr_pin = -1;
894c42a7b7eSSam Leffler 	/*
895c42a7b7eSSam Leffler 	 * Auto-enable soft led processing for IBM cards and for
896c42a7b7eSSam Leffler 	 * 5211 minipci cards.  Users can also manually enable/disable
897c42a7b7eSSam Leffler 	 * support with a sysctl.
898c42a7b7eSSam Leffler 	 */
899c42a7b7eSSam Leffler 	sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
9006558ffd9SAdrian Chadd 	ath_led_config(sc);
901a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
9025591b213SSam Leffler 
9035591b213SSam Leffler 	/* XXX not right but it's not used anywhere important */
9045591b213SSam Leffler 	ic->ic_phytype = IEEE80211_T_OFDM;
9055591b213SSam Leffler 	ic->ic_opmode = IEEE80211_M_STA;
906c42a7b7eSSam Leffler 	ic->ic_caps =
907c43feedeSSam Leffler 		  IEEE80211_C_STA		/* station mode */
908c43feedeSSam Leffler 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
909fe32c3efSSam Leffler 		| IEEE80211_C_HOSTAP		/* hostap mode */
910fe32c3efSSam Leffler 		| IEEE80211_C_MONITOR		/* monitor mode */
9117a04dc27SSam Leffler 		| IEEE80211_C_AHDEMO		/* adhoc demo mode */
912b032f27cSSam Leffler 		| IEEE80211_C_WDS		/* 4-address traffic works */
91359aa14a9SRui Paulo 		| IEEE80211_C_MBSS		/* mesh point link mode */
914fe32c3efSSam Leffler 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
915c42a7b7eSSam Leffler 		| IEEE80211_C_SHSLOT		/* short slot time supported */
916c42a7b7eSSam Leffler 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
9173b324f57SAdrian Chadd #ifndef	ATH_ENABLE_11N
91868e8e04eSSam Leffler 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
9193b324f57SAdrian Chadd #endif
92068e8e04eSSam Leffler 		| IEEE80211_C_TXFRAG		/* handle tx frags */
92110dc8de4SAdrian Chadd #ifdef	ATH_ENABLE_DFS
9227e97436bSAdrian Chadd 		| IEEE80211_C_DFS		/* Enable radar detection */
92310dc8de4SAdrian Chadd #endif
924f5c30c4eSAdrian Chadd 		| IEEE80211_C_PMGT		/* Station side power mgmt */
925f5c30c4eSAdrian Chadd 		| IEEE80211_C_SWSLEEP
92601e7e035SSam Leffler 		;
927c42a7b7eSSam Leffler 	/*
928c42a7b7eSSam Leffler 	 * Query the hal to figure out h/w crypto support.
929c42a7b7eSSam Leffler 	 */
930c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
931b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
932c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
933b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_OCB;
934c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
935b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_CCM;
936c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
937b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_CKIP;
938c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
939b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIP;
940c42a7b7eSSam Leffler 		/*
941c42a7b7eSSam Leffler 		 * Check if h/w does the MIC and/or whether the
942c42a7b7eSSam Leffler 		 * separate key cache entries are required to
943c42a7b7eSSam Leffler 		 * handle both tx+rx MIC keys.
944c42a7b7eSSam Leffler 		 */
945c42a7b7eSSam Leffler 		if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
946b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
9475901d2d3SSam Leffler 		/*
9485901d2d3SSam Leffler 		 * If the h/w supports storing tx+rx MIC keys
9495901d2d3SSam Leffler 		 * in one cache slot automatically enable use.
9505901d2d3SSam Leffler 		 */
9515901d2d3SSam Leffler 		if (ath_hal_hastkipsplit(ah) ||
9525901d2d3SSam Leffler 		    !ath_hal_settkipsplit(ah, AH_FALSE))
953c42a7b7eSSam Leffler 			sc->sc_splitmic = 1;
954b032f27cSSam Leffler 		/*
955b032f27cSSam Leffler 		 * If the h/w can do TKIP MIC together with WME then
956b032f27cSSam Leffler 		 * we use it; otherwise we force the MIC to be done
957b032f27cSSam Leffler 		 * in software by the net80211 layer.
958b032f27cSSam Leffler 		 */
959b032f27cSSam Leffler 		if (ath_hal_haswmetkipmic(ah))
960b032f27cSSam Leffler 			sc->sc_wmetkipmic = 1;
961c42a7b7eSSam Leffler 	}
962e8fd88a3SSam Leffler 	sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR);
9639ac01d39SRui Paulo 	/*
9641ac5dac2SRui Paulo 	 * Check for multicast key search support.
9659ac01d39SRui Paulo 	 */
9669ac01d39SRui Paulo 	if (ath_hal_hasmcastkeysearch(sc->sc_ah) &&
9679ac01d39SRui Paulo 	    !ath_hal_getmcastkeysearch(sc->sc_ah)) {
9689ac01d39SRui Paulo 		ath_hal_setmcastkeysearch(sc->sc_ah, 1);
9699ac01d39SRui Paulo 	}
970e8fd88a3SSam Leffler 	sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah);
971c42a7b7eSSam Leffler 	/*
9725901d2d3SSam Leffler 	 * Mark key cache slots associated with global keys
9735901d2d3SSam Leffler 	 * as in use.  If we knew TKIP was not to be used we
9745901d2d3SSam Leffler 	 * could leave the +32, +64, and +32+64 slots free.
9755901d2d3SSam Leffler 	 */
9765901d2d3SSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
9775901d2d3SSam Leffler 		setbit(sc->sc_keymap, i);
9785901d2d3SSam Leffler 		setbit(sc->sc_keymap, i+64);
9795901d2d3SSam Leffler 		if (sc->sc_splitmic) {
9805901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32);
9815901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32+64);
9825901d2d3SSam Leffler 		}
9835901d2d3SSam Leffler 	}
9845901d2d3SSam Leffler 	/*
985c42a7b7eSSam Leffler 	 * TPC support can be done either with a global cap or
986c42a7b7eSSam Leffler 	 * per-packet support.  The latter is not available on
987c42a7b7eSSam Leffler 	 * all parts.  We're a bit pedantic here as all parts
988c42a7b7eSSam Leffler 	 * support a global cap.
989c42a7b7eSSam Leffler 	 */
990c59005e9SSam Leffler 	if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah))
991c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TXPMGT;
992c42a7b7eSSam Leffler 
993c42a7b7eSSam Leffler 	/*
994c42a7b7eSSam Leffler 	 * Mark WME capability only if we have sufficient
995c42a7b7eSSam Leffler 	 * hardware queues to do proper priority scheduling.
996c42a7b7eSSam Leffler 	 */
997c42a7b7eSSam Leffler 	if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
998c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_WME;
999c42a7b7eSSam Leffler 	/*
1000e8fd88a3SSam Leffler 	 * Check for misc other capabilities.
1001c42a7b7eSSam Leffler 	 */
1002c42a7b7eSSam Leffler 	if (ath_hal_hasbursting(ah))
1003c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_BURST;
1004b032f27cSSam Leffler 	sc->sc_hasbmask = ath_hal_hasbssidmask(ah);
100559aa14a9SRui Paulo 	sc->sc_hasbmatch = ath_hal_hasbssidmatch(ah);
1006b032f27cSSam Leffler 	sc->sc_hastsfadd = ath_hal_hastsfadjust(ah);
10078a2a6beeSAdrian Chadd 	sc->sc_rxslink = ath_hal_self_linked_final_rxdesc(ah);
100851558243SAdrian Chadd 
100951558243SAdrian Chadd 	/* XXX TODO: just make this a "store tx/rx timestamp length" operation */
101051558243SAdrian Chadd 	if (ath_hal_get_rx_tsf_prec(ah, &i)) {
101151558243SAdrian Chadd 		if (i == 32) {
101251558243SAdrian Chadd 			sc->sc_rxtsf32 = 1;
101351558243SAdrian Chadd 		}
101451558243SAdrian Chadd 		if (bootverbose)
101551558243SAdrian Chadd 			device_printf(sc->sc_dev, "RX timestamp: %d bits\n", i);
101651558243SAdrian Chadd 	}
101751558243SAdrian Chadd 	if (ath_hal_get_tx_tsf_prec(ah, &i)) {
101851558243SAdrian Chadd 		if (bootverbose)
101951558243SAdrian Chadd 			device_printf(sc->sc_dev, "TX timestamp: %d bits\n", i);
102051558243SAdrian Chadd 	}
102151558243SAdrian Chadd 
1022dd6a574eSAdrian Chadd 	sc->sc_hasenforcetxop = ath_hal_hasenforcetxop(ah);
10233df7a8abSAdrian Chadd 	sc->sc_rx_lnamixer = ath_hal_hasrxlnamixer(ah);
1024216ca234SAdrian Chadd 	sc->sc_hasdivcomb = ath_hal_hasdivantcomb(ah);
1025216ca234SAdrian Chadd 
102646be12aeSAdrian Chadd 	/*
102746be12aeSAdrian Chadd 	 * Some WB335 cards do not support antenna diversity. Since
102846be12aeSAdrian Chadd 	 * we use a hardcoded value for AR9565 instead of using the
102946be12aeSAdrian Chadd 	 * EEPROM/OTP data, remove the combining feature from
103046be12aeSAdrian Chadd 	 * the HW capabilities bitmap.
103146be12aeSAdrian Chadd 	 */
103246be12aeSAdrian Chadd 	/*
103346be12aeSAdrian Chadd 	 * XXX TODO: check reference driver and ath9k for what to do
103446be12aeSAdrian Chadd 	 * here for WB335.  I think we have to actually disable the
103546be12aeSAdrian Chadd 	 * LNA div processing in the HAL and instead use the hard
103646be12aeSAdrian Chadd 	 * coded values; and then use BT diversity.
103746be12aeSAdrian Chadd 	 *
103846be12aeSAdrian Chadd 	 * .. but also need to setup MCI too for WB335..
103946be12aeSAdrian Chadd 	 */
104046be12aeSAdrian Chadd #if 0
104146be12aeSAdrian Chadd 	if (sc->sc_pci_devinfo & (ATH9K_PCI_AR9565_1ANT | ATH9K_PCI_AR9565_2ANT)) {
104246be12aeSAdrian Chadd 		device_printf(sc->sc_dev, "%s: WB335: disabling LNA mixer diversity\n",
104346be12aeSAdrian Chadd 		    __func__);
104446be12aeSAdrian Chadd 		sc->sc_dolnadiv = 0;
104546be12aeSAdrian Chadd 	}
104646be12aeSAdrian Chadd #endif
104746be12aeSAdrian Chadd 
104868e8e04eSSam Leffler 	if (ath_hal_hasfastframes(ah))
104968e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_FF;
105059efa8b5SSam Leffler 	wmodes = ath_hal_getwirelessmodes(ah);
1051411373ebSSam Leffler 	if (wmodes & (HAL_MODE_108G|HAL_MODE_TURBO))
105268e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TURBOP;
1053584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
105410ad9a77SSam Leffler 	if (ath_hal_macversion(ah) > 0x78) {
105510ad9a77SSam Leffler 		ic->ic_caps |= IEEE80211_C_TDMA; /* capable of TDMA */
105610ad9a77SSam Leffler 		ic->ic_tdma_update = ath_tdma_update;
105710ad9a77SSam Leffler 	}
105810ad9a77SSam Leffler #endif
105967397d39SAdrian Chadd 
106067397d39SAdrian Chadd 	/*
10619c85ff91SAdrian Chadd 	 * TODO: enforce that at least this many frames are available
10629c85ff91SAdrian Chadd 	 * in the txbuf list before allowing data frames (raw or
10639c85ff91SAdrian Chadd 	 * otherwise) to be transmitted.
10649c85ff91SAdrian Chadd 	 */
10659c85ff91SAdrian Chadd 	sc->sc_txq_data_minfree = 10;
10665e2c0d2dSAdrian Chadd 
10679c85ff91SAdrian Chadd 	/*
10685e2c0d2dSAdrian Chadd 	 * Shorten this to 64 packets, or 1/4 ath_txbuf, whichever
10695e2c0d2dSAdrian Chadd 	 * is smaller.
10705e2c0d2dSAdrian Chadd 	 *
10715e2c0d2dSAdrian Chadd 	 * Anything bigger can potentially see the cabq consume
10725e2c0d2dSAdrian Chadd 	 * almost all buffers, starving everything else, only to
10735e2c0d2dSAdrian Chadd 	 * see most fail to transmit in the given beacon interval.
10749c85ff91SAdrian Chadd 	 */
10755e2c0d2dSAdrian Chadd 	sc->sc_txq_mcastq_maxdepth = MIN(64, ath_txbuf / 4);
10769c85ff91SAdrian Chadd 
10777dcb2beaSAdrian Chadd 	/*
107822a3aee6SAdrian Chadd 	 * How deep can the node software TX queue get whilst it's asleep.
107922a3aee6SAdrian Chadd 	 */
108022a3aee6SAdrian Chadd 	sc->sc_txq_node_psq_maxdepth = 16;
108122a3aee6SAdrian Chadd 
108222a3aee6SAdrian Chadd 	/*
1083caa7e52fSEitan Adler 	 * Default the maximum queue to 1/4'th the TX buffers, or
1084c50346bcSAdrian Chadd 	 * 128, whichever is smaller.
1085c50346bcSAdrian Chadd 	 *
1086c50346bcSAdrian Chadd 	 * Set it to 128 instead of the previous default (64) because
1087c50346bcSAdrian Chadd 	 * at 64, two full A-MPDU subframes of 32 frames each is
1088c50346bcSAdrian Chadd 	 * enough to treat this node queue as full and all subsequent
1089c50346bcSAdrian Chadd 	 * traffic is dropped. Setting it to 128 means there'll
1090c50346bcSAdrian Chadd 	 * hopefully be another 64 frames in the software queue
1091c50346bcSAdrian Chadd 	 * to begin making A-MPDU frames out of.
10927dcb2beaSAdrian Chadd 	 */
1093c50346bcSAdrian Chadd 	sc->sc_txq_node_maxdepth = MIN(128, ath_txbuf / 4);
10947dcb2beaSAdrian Chadd 
1095b837332dSAdrian Chadd 	/* Enable CABQ by default */
1096b837332dSAdrian Chadd 	sc->sc_cabq_enable = 1;
1097b837332dSAdrian Chadd 
10989c85ff91SAdrian Chadd 	/*
1099a865860dSAdrian Chadd 	 * Allow the TX and RX chainmasks to be overridden by
1100a865860dSAdrian Chadd 	 * environment variables and/or device.hints.
1101a865860dSAdrian Chadd 	 *
1102a865860dSAdrian Chadd 	 * This must be done early - before the hardware is
1103a865860dSAdrian Chadd 	 * calibrated or before the 802.11n stream calculation
1104a865860dSAdrian Chadd 	 * is done.
1105a865860dSAdrian Chadd 	 */
1106a865860dSAdrian Chadd 	if (resource_int_value(device_get_name(sc->sc_dev),
1107a865860dSAdrian Chadd 	    device_get_unit(sc->sc_dev), "rx_chainmask",
1108a865860dSAdrian Chadd 	    &rx_chainmask) == 0) {
1109a865860dSAdrian Chadd 		device_printf(sc->sc_dev, "Setting RX chainmask to 0x%x\n",
1110a865860dSAdrian Chadd 		    rx_chainmask);
1111a865860dSAdrian Chadd 		(void) ath_hal_setrxchainmask(sc->sc_ah, rx_chainmask);
1112a865860dSAdrian Chadd 	}
1113a865860dSAdrian Chadd 	if (resource_int_value(device_get_name(sc->sc_dev),
1114a865860dSAdrian Chadd 	    device_get_unit(sc->sc_dev), "tx_chainmask",
1115a865860dSAdrian Chadd 	    &tx_chainmask) == 0) {
1116a865860dSAdrian Chadd 		device_printf(sc->sc_dev, "Setting TX chainmask to 0x%x\n",
1117a865860dSAdrian Chadd 		    tx_chainmask);
1118dc8552d5SAdrian Chadd 		(void) ath_hal_settxchainmask(sc->sc_ah, tx_chainmask);
1119a865860dSAdrian Chadd 	}
1120a865860dSAdrian Chadd 
1121af017101SAdrian Chadd 	/*
1122ff5b5634SAdrian Chadd 	 * Query the TX/RX chainmask configuration.
1123ff5b5634SAdrian Chadd 	 *
1124ff5b5634SAdrian Chadd 	 * This is only relevant for 11n devices.
1125ff5b5634SAdrian Chadd 	 */
1126ff5b5634SAdrian Chadd 	ath_hal_getrxchainmask(ah, &sc->sc_rxchainmask);
1127ff5b5634SAdrian Chadd 	ath_hal_gettxchainmask(ah, &sc->sc_txchainmask);
1128ff5b5634SAdrian Chadd 
1129ff5b5634SAdrian Chadd 	/*
1130af017101SAdrian Chadd 	 * Disable MRR with protected frames by default.
1131af017101SAdrian Chadd 	 * Only 802.11n series NICs can handle this.
1132af017101SAdrian Chadd 	 */
1133af017101SAdrian Chadd 	sc->sc_mrrprot = 0;	/* XXX should be a capability */
1134af017101SAdrian Chadd 
11355540369bSAdrian Chadd 	/*
11365540369bSAdrian Chadd 	 * Query the enterprise mode information the HAL.
11375540369bSAdrian Chadd 	 */
11385540369bSAdrian Chadd 	if (ath_hal_getcapability(ah, HAL_CAP_ENTERPRISE_MODE, 0,
11395540369bSAdrian Chadd 	    &sc->sc_ent_cfg) == HAL_OK)
11405540369bSAdrian Chadd 		sc->sc_use_ent = 1;
11415540369bSAdrian Chadd 
11428fd67f92SAdrian Chadd #ifdef	ATH_ENABLE_11N
114367397d39SAdrian Chadd 	/*
114467397d39SAdrian Chadd 	 * Query HT capabilities
114567397d39SAdrian Chadd 	 */
114667397d39SAdrian Chadd 	if (ath_hal_getcapability(ah, HAL_CAP_HT, 0, NULL) == HAL_OK &&
114767397d39SAdrian Chadd 	    (wmodes & (HAL_MODE_HT20 | HAL_MODE_HT40))) {
11486f4fb2d8SAdrian Chadd 		uint32_t rxs, txs;
11499f3a9150SAdrian Chadd 		uint32_t ldpc;
115067397d39SAdrian Chadd 
115167397d39SAdrian Chadd 		device_printf(sc->sc_dev, "[HT] enabling HT modes\n");
1152af017101SAdrian Chadd 
1153af017101SAdrian Chadd 		sc->sc_mrrprot = 1;	/* XXX should be a capability */
1154af017101SAdrian Chadd 
115567397d39SAdrian Chadd 		ic->ic_htcaps = IEEE80211_HTC_HT	/* HT operation */
115667397d39SAdrian Chadd 			    | IEEE80211_HTC_AMPDU	/* A-MPDU tx/rx */
115767397d39SAdrian Chadd 			    | IEEE80211_HTC_AMSDU	/* A-MSDU tx/rx */
11587e97436bSAdrian Chadd 			    | IEEE80211_HTCAP_MAXAMSDU_3839
11597e97436bSAdrian Chadd 			    				/* max A-MSDU length */
116067397d39SAdrian Chadd 			    | IEEE80211_HTCAP_SMPS_OFF;	/* SM power save off */
116167397d39SAdrian Chadd 
116276355edbSAdrian Chadd 		/*
116376355edbSAdrian Chadd 		 * Enable short-GI for HT20 only if the hardware
116476355edbSAdrian Chadd 		 * advertises support.
116576355edbSAdrian Chadd 		 * Notably, anything earlier than the AR9287 doesn't.
116676355edbSAdrian Chadd 		 */
116776355edbSAdrian Chadd 		if ((ath_hal_getcapability(ah,
116876355edbSAdrian Chadd 		    HAL_CAP_HT20_SGI, 0, NULL) == HAL_OK) &&
116976355edbSAdrian Chadd 		    (wmodes & HAL_MODE_HT20)) {
117076355edbSAdrian Chadd 			device_printf(sc->sc_dev,
117176355edbSAdrian Chadd 			    "[HT] enabling short-GI in 20MHz mode\n");
117276355edbSAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
117376355edbSAdrian Chadd 		}
117476355edbSAdrian Chadd 
117567397d39SAdrian Chadd 		if (wmodes & HAL_MODE_HT40)
117667397d39SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40
117767397d39SAdrian Chadd 			    |  IEEE80211_HTCAP_SHORTGI40;
117867397d39SAdrian Chadd 
117967397d39SAdrian Chadd 		/*
11807e97436bSAdrian Chadd 		 * TX/RX streams need to be taken into account when
11817e97436bSAdrian Chadd 		 * negotiating which MCS rates it'll receive and
118267397d39SAdrian Chadd 		 * what MCS rates are available for TX.
118367397d39SAdrian Chadd 		 */
118454517070SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 0, &txs);
118554517070SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 1, &rxs);
118667397d39SAdrian Chadd 		ic->ic_txstream = txs;
118767397d39SAdrian Chadd 		ic->ic_rxstream = rxs;
118867397d39SAdrian Chadd 
11896606ba81SAdrian Chadd 		/*
11906606ba81SAdrian Chadd 		 * Setup TX and RX STBC based on what the HAL allows and
11916606ba81SAdrian Chadd 		 * the currently configured chainmask set.
11926606ba81SAdrian Chadd 		 * Ie - don't enable STBC TX if only one chain is enabled.
11936606ba81SAdrian Chadd 		 * STBC RX is fine on a single RX chain; it just won't
11946606ba81SAdrian Chadd 		 * provide any real benefit.
11956606ba81SAdrian Chadd 		 */
11966606ba81SAdrian Chadd 		if (ath_hal_getcapability(ah, HAL_CAP_RX_STBC, 0,
11976606ba81SAdrian Chadd 		    NULL) == HAL_OK) {
11986606ba81SAdrian Chadd 			sc->sc_rx_stbc = 1;
11996606ba81SAdrian Chadd 			device_printf(sc->sc_dev,
12006606ba81SAdrian Chadd 			    "[HT] 1 stream STBC receive enabled\n");
12016606ba81SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_1STREAM;
12026606ba81SAdrian Chadd 		}
12036606ba81SAdrian Chadd 		if (txs > 1 && ath_hal_getcapability(ah, HAL_CAP_TX_STBC, 0,
12046606ba81SAdrian Chadd 		    NULL) == HAL_OK) {
12056606ba81SAdrian Chadd 			sc->sc_tx_stbc = 1;
12066606ba81SAdrian Chadd 			device_printf(sc->sc_dev,
12076606ba81SAdrian Chadd 			    "[HT] 1 stream STBC transmit enabled\n");
12086606ba81SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
12096606ba81SAdrian Chadd 		}
12106606ba81SAdrian Chadd 
1211ce656facSAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_RTS_AGGR_LIMIT, 1,
1212ce656facSAdrian Chadd 		    &sc->sc_rts_aggr_limit);
1213ce656facSAdrian Chadd 		if (sc->sc_rts_aggr_limit != (64 * 1024))
1214ce656facSAdrian Chadd 			device_printf(sc->sc_dev,
1215ce656facSAdrian Chadd 			    "[HT] RTS aggregates limited to %d KiB\n",
1216ce656facSAdrian Chadd 			    sc->sc_rts_aggr_limit / 1024);
1217ce656facSAdrian Chadd 
12189f3a9150SAdrian Chadd 		/*
12199f3a9150SAdrian Chadd 		 * LDPC
12209f3a9150SAdrian Chadd 		 */
12219f3a9150SAdrian Chadd 		if ((ath_hal_getcapability(ah, HAL_CAP_LDPC, 0, &ldpc))
12229f3a9150SAdrian Chadd 		    == HAL_OK && (ldpc == 1)) {
12239f3a9150SAdrian Chadd 			sc->sc_has_ldpc = 1;
12249f3a9150SAdrian Chadd 			device_printf(sc->sc_dev,
12259f3a9150SAdrian Chadd 			    "[HT] LDPC transmit/receive enabled\n");
12268f1e1139SAndriy Voskoboinyk 			ic->ic_htcaps |= IEEE80211_HTCAP_LDPC |
12278f1e1139SAndriy Voskoboinyk 					 IEEE80211_HTC_TXLDPC;
12289f3a9150SAdrian Chadd 		}
12299f3a9150SAdrian Chadd 
12307e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
12317e97436bSAdrian Chadd 		    "[HT] %d RX streams; %d TX streams\n", rxs, txs);
123267397d39SAdrian Chadd 	}
123367397d39SAdrian Chadd #endif
123467397d39SAdrian Chadd 
1235c42a7b7eSSam Leffler 	/*
1236f8aa9fd5SAdrian Chadd 	 * Initial aggregation settings.
1237f8aa9fd5SAdrian Chadd 	 */
123872910f03SAdrian Chadd 	sc->sc_hwq_limit_aggr = ATH_AGGR_MIN_QDEPTH;
123972910f03SAdrian Chadd 	sc->sc_hwq_limit_nonaggr = ATH_NONAGGR_MIN_QDEPTH;
1240f8aa9fd5SAdrian Chadd 	sc->sc_tid_hwq_lo = ATH_AGGR_SCHED_LOW;
1241f8aa9fd5SAdrian Chadd 	sc->sc_tid_hwq_hi = ATH_AGGR_SCHED_HIGH;
12424a502c33SAdrian Chadd 	sc->sc_aggr_limit = ATH_AGGR_MAXSIZE;
1243a54ecf78SAdrian Chadd 	sc->sc_delim_min_pad = 0;
1244f8aa9fd5SAdrian Chadd 
1245f8aa9fd5SAdrian Chadd 	/*
1246ddbe3036SAdrian Chadd 	 * Check if the hardware requires PCI register serialisation.
1247ddbe3036SAdrian Chadd 	 * Some of the Owl based MACs require this.
1248ddbe3036SAdrian Chadd 	 */
1249ddbe3036SAdrian Chadd 	if (mp_ncpus > 1 &&
1250ddbe3036SAdrian Chadd 	    ath_hal_getcapability(ah, HAL_CAP_SERIALISE_WAR,
1251ddbe3036SAdrian Chadd 	     0, NULL) == HAL_OK) {
1252ddbe3036SAdrian Chadd 		sc->sc_ah->ah_config.ah_serialise_reg_war = 1;
12537e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
12547e97436bSAdrian Chadd 		    "Enabling register serialisation\n");
1255ddbe3036SAdrian Chadd 	}
1256ddbe3036SAdrian Chadd 
1257ddbe3036SAdrian Chadd 	/*
1258f0db652cSAdrian Chadd 	 * Initialise the deferred completed RX buffer list.
1259f0db652cSAdrian Chadd 	 */
12605d4dedadSAdrian Chadd 	TAILQ_INIT(&sc->sc_rx_rxlist[HAL_RX_QUEUE_HP]);
12615d4dedadSAdrian Chadd 	TAILQ_INIT(&sc->sc_rx_rxlist[HAL_RX_QUEUE_LP]);
1262f0db652cSAdrian Chadd 
1263f0db652cSAdrian Chadd 	/*
1264c42a7b7eSSam Leffler 	 * Indicate we need the 802.11 header padded to a
1265c42a7b7eSSam Leffler 	 * 32-bit boundary for 4-address and QoS frames.
1266c42a7b7eSSam Leffler 	 */
1267c42a7b7eSSam Leffler 	ic->ic_flags |= IEEE80211_F_DATAPAD;
1268c42a7b7eSSam Leffler 
1269c42a7b7eSSam Leffler 	/*
1270c42a7b7eSSam Leffler 	 * Query the hal about antenna support.
1271c42a7b7eSSam Leffler 	 */
1272c42a7b7eSSam Leffler 	sc->sc_defant = ath_hal_getdefantenna(ah);
1273c42a7b7eSSam Leffler 
1274c42a7b7eSSam Leffler 	/*
1275c42a7b7eSSam Leffler 	 * Not all chips have the VEOL support we want to
1276c42a7b7eSSam Leffler 	 * use with IBSS beacons; check here for it.
1277c42a7b7eSSam Leffler 	 */
1278c42a7b7eSSam Leffler 	sc->sc_hasveol = ath_hal_hasveol(ah);
12795591b213SSam Leffler 
1280240b1f1dSAdrian Chadd 	/* get mac address from kenv first, then hardware */
12817a79cebfSGleb Smirnoff 	if (ath_fetch_mac_kenv(sc, ic->ic_macaddr) == 0) {
12829cecaef7SAdrian Chadd 		/* Tell the HAL now about the new MAC */
12837a79cebfSGleb Smirnoff 		ath_hal_setmac(ah, ic->ic_macaddr);
12849cecaef7SAdrian Chadd 	} else {
12857a79cebfSGleb Smirnoff 		ath_hal_getmac(ah, ic->ic_macaddr);
12869cecaef7SAdrian Chadd 	}
1287240b1f1dSAdrian Chadd 
1288b032f27cSSam Leffler 	if (sc->sc_hasbmask)
1289b032f27cSSam Leffler 		ath_hal_getbssidmask(ah, sc->sc_hwbssidmask);
12905591b213SSam Leffler 
1291b032f27cSSam Leffler 	/* NB: used to size node table key mapping array */
1292b032f27cSSam Leffler 	ic->ic_max_keyix = sc->sc_keymax;
12935591b213SSam Leffler 	/* call MI attach routine. */
12947a79cebfSGleb Smirnoff 	ieee80211_ifattach(ic);
1295b032f27cSSam Leffler 	ic->ic_setregdomain = ath_setregdomain;
1296b032f27cSSam Leffler 	ic->ic_getradiocaps = ath_getradiocaps;
1297b032f27cSSam Leffler 	sc->sc_opmode = HAL_M_STA;
1298b032f27cSSam Leffler 
12995591b213SSam Leffler 	/* override default methods */
13007a79cebfSGleb Smirnoff 	ic->ic_ioctl = ath_ioctl;
13017a79cebfSGleb Smirnoff 	ic->ic_parent = ath_parent;
13027a79cebfSGleb Smirnoff 	ic->ic_transmit = ath_transmit;
1303b032f27cSSam Leffler 	ic->ic_newassoc = ath_newassoc;
1304b032f27cSSam Leffler 	ic->ic_updateslot = ath_updateslot;
1305b032f27cSSam Leffler 	ic->ic_wme.wme_update = ath_wme_update;
1306b032f27cSSam Leffler 	ic->ic_vap_create = ath_vap_create;
1307b032f27cSSam Leffler 	ic->ic_vap_delete = ath_vap_delete;
1308b032f27cSSam Leffler 	ic->ic_raw_xmit = ath_raw_xmit;
1309b032f27cSSam Leffler 	ic->ic_update_mcast = ath_update_mcast;
1310b032f27cSSam Leffler 	ic->ic_update_promisc = ath_update_promisc;
13115591b213SSam Leffler 	ic->ic_node_alloc = ath_node_alloc;
13121e774079SSam Leffler 	sc->sc_node_free = ic->ic_node_free;
13135591b213SSam Leffler 	ic->ic_node_free = ath_node_free;
13144afa805eSAdrian Chadd 	sc->sc_node_cleanup = ic->ic_node_cleanup;
13154afa805eSAdrian Chadd 	ic->ic_node_cleanup = ath_node_cleanup;
131668e8e04eSSam Leffler 	ic->ic_node_getsignal = ath_node_getsignal;
131768e8e04eSSam Leffler 	ic->ic_scan_start = ath_scan_start;
131868e8e04eSSam Leffler 	ic->ic_scan_end = ath_scan_end;
131968e8e04eSSam Leffler 	ic->ic_set_channel = ath_set_channel;
1320fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
1321eb6f0de0SAdrian Chadd 	/* 802.11n specific - but just override anyway */
1322eb6f0de0SAdrian Chadd 	sc->sc_addba_request = ic->ic_addba_request;
1323eb6f0de0SAdrian Chadd 	sc->sc_addba_response = ic->ic_addba_response;
1324eb6f0de0SAdrian Chadd 	sc->sc_addba_stop = ic->ic_addba_stop;
1325eb6f0de0SAdrian Chadd 	sc->sc_bar_response = ic->ic_bar_response;
1326eb6f0de0SAdrian Chadd 	sc->sc_addba_response_timeout = ic->ic_addba_response_timeout;
1327eb6f0de0SAdrian Chadd 
1328eb6f0de0SAdrian Chadd 	ic->ic_addba_request = ath_addba_request;
1329eb6f0de0SAdrian Chadd 	ic->ic_addba_response = ath_addba_response;
1330eb6f0de0SAdrian Chadd 	ic->ic_addba_response_timeout = ath_addba_response_timeout;
1331eb6f0de0SAdrian Chadd 	ic->ic_addba_stop = ath_addba_stop;
1332eb6f0de0SAdrian Chadd 	ic->ic_bar_response = ath_bar_response;
1333eb6f0de0SAdrian Chadd 
1334fdd72b4aSAdrian Chadd 	ic->ic_update_chw = ath_update_chw;
1335fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
13361410ca56SAdrian Chadd 	ic->ic_set_quiet = ath_set_quiet_ie;
1337fdd72b4aSAdrian Chadd 
1338e1b5ab97SAdrian Chadd #ifdef	ATH_ENABLE_RADIOTAP_VENDOR_EXT
1339e1b5ab97SAdrian Chadd 	/*
1340e1b5ab97SAdrian Chadd 	 * There's one vendor bitmap entry in the RX radiotap
1341e1b5ab97SAdrian Chadd 	 * header; make sure that's taken into account.
1342e1b5ab97SAdrian Chadd 	 */
1343e1b5ab97SAdrian Chadd 	ieee80211_radiotap_attachv(ic,
1344e1b5ab97SAdrian Chadd 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th), 0,
1345e1b5ab97SAdrian Chadd 		ATH_TX_RADIOTAP_PRESENT,
1346e1b5ab97SAdrian Chadd 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th), 1,
1347e1b5ab97SAdrian Chadd 		ATH_RX_RADIOTAP_PRESENT);
1348e1b5ab97SAdrian Chadd #else
1349e1b5ab97SAdrian Chadd 	/*
1350e1b5ab97SAdrian Chadd 	 * No vendor bitmap/extensions are present.
1351e1b5ab97SAdrian Chadd 	 */
13525463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
13535463c4a4SSam Leffler 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
13545463c4a4SSam Leffler 		ATH_TX_RADIOTAP_PRESENT,
13555463c4a4SSam Leffler 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
13565463c4a4SSam Leffler 		ATH_RX_RADIOTAP_PRESENT);
1357e1b5ab97SAdrian Chadd #endif	/* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
13585463c4a4SSam Leffler 
13594866e6c2SSam Leffler 	/*
1360bdbb6e5bSAdrian Chadd 	 * Setup the ALQ logging if required
1361bdbb6e5bSAdrian Chadd 	 */
136289d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
1363bdbb6e5bSAdrian Chadd 	if_ath_alq_init(&sc->sc_alq, device_get_nameunit(sc->sc_dev));
1364bb327d28SAdrian Chadd 	if_ath_alq_setcfg(&sc->sc_alq,
1365bb327d28SAdrian Chadd 	    sc->sc_ah->ah_macVersion,
1366bb327d28SAdrian Chadd 	    sc->sc_ah->ah_macRev,
1367bb327d28SAdrian Chadd 	    sc->sc_ah->ah_phyRev,
1368bb327d28SAdrian Chadd 	    sc->sc_ah->ah_magic);
1369bdbb6e5bSAdrian Chadd #endif
1370bdbb6e5bSAdrian Chadd 
1371bdbb6e5bSAdrian Chadd 	/*
13724866e6c2SSam Leffler 	 * Setup dynamic sysctl's now that country code and
13734866e6c2SSam Leffler 	 * regdomain are available from the hal.
13744866e6c2SSam Leffler 	 */
13754866e6c2SSam Leffler 	ath_sysctlattach(sc);
1376e8dabfbeSAdrian Chadd 	ath_sysctl_stats_attach(sc);
137737931a35SAdrian Chadd 	ath_sysctl_hal_attach(sc);
137873454c73SSam Leffler 
1379c42a7b7eSSam Leffler 	if (bootverbose)
1380c42a7b7eSSam Leffler 		ieee80211_announce(ic);
1381c42a7b7eSSam Leffler 	ath_announce(sc);
1382f5c30c4eSAdrian Chadd 
1383f5c30c4eSAdrian Chadd 	/*
1384f5c30c4eSAdrian Chadd 	 * Put it to sleep for now.
1385f5c30c4eSAdrian Chadd 	 */
1386f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
13878c03e55dSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_FULL_SLEEP, 1);
1388f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1389f5c30c4eSAdrian Chadd 
13905591b213SSam Leffler 	return 0;
1391b28b4653SSam Leffler bad2:
1392c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
1393b28b4653SSam Leffler 	ath_desc_free(sc);
13943fdfc330SAdrian Chadd 	ath_txdma_teardown(sc);
13953d184db2SAdrian Chadd 	ath_rxdma_teardown(sc);
139646be12aeSAdrian Chadd 
13975591b213SSam Leffler bad:
13985591b213SSam Leffler 	if (ah)
13995591b213SSam Leffler 		ath_hal_detach(ah);
14005591b213SSam Leffler 	sc->sc_invalid = 1;
14015591b213SSam Leffler 	return error;
14025591b213SSam Leffler }
14035591b213SSam Leffler 
14045591b213SSam Leffler int
ath_detach(struct ath_softc * sc)14055591b213SSam Leffler ath_detach(struct ath_softc *sc)
14065591b213SSam Leffler {
14075591b213SSam Leffler 
1408c42a7b7eSSam Leffler 	/*
1409c42a7b7eSSam Leffler 	 * NB: the order of these is important:
141071b85077SSam Leffler 	 * o stop the chip so no more interrupts will fire
1411c42a7b7eSSam Leffler 	 * o call the 802.11 layer before detaching the hal to
1412c42a7b7eSSam Leffler 	 *   insure callbacks into the driver to delete global
1413c42a7b7eSSam Leffler 	 *   key cache entries can be handled
141471b85077SSam Leffler 	 * o free the taskqueue which drains any pending tasks
1415c42a7b7eSSam Leffler 	 * o reclaim the tx queue data structures after calling
1416c42a7b7eSSam Leffler 	 *   the 802.11 layer as we'll get called back to reclaim
1417c42a7b7eSSam Leffler 	 *   node state and potentially want to use them
1418c42a7b7eSSam Leffler 	 * o to cleanup the tx queues the hal is called, so detach
1419c42a7b7eSSam Leffler 	 *   it last
1420c42a7b7eSSam Leffler 	 * Other than that, it's straightforward...
1421c42a7b7eSSam Leffler 	 */
1422f5c30c4eSAdrian Chadd 
1423f5c30c4eSAdrian Chadd 	/*
1424f5c30c4eSAdrian Chadd 	 * XXX Wake the hardware up first.  ath_stop() will still
1425f5c30c4eSAdrian Chadd 	 * wake it up first, but I'd rather do it here just to
1426f5c30c4eSAdrian Chadd 	 * ensure it's awake.
1427f5c30c4eSAdrian Chadd 	 */
1428f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1429f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
14308c03e55dSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_AWAKE, 1);
1431f5c30c4eSAdrian Chadd 
1432f5c30c4eSAdrian Chadd 	/*
1433f5c30c4eSAdrian Chadd 	 * Stop things cleanly.
1434f5c30c4eSAdrian Chadd 	 */
14357a79cebfSGleb Smirnoff 	ath_stop(sc);
14367a79cebfSGleb Smirnoff 	ATH_UNLOCK(sc);
1437f5c30c4eSAdrian Chadd 
14387a79cebfSGleb Smirnoff 	ieee80211_ifdetach(&sc->sc_ic);
143971b85077SSam Leffler 	taskqueue_free(sc->sc_tq);
144086e07743SSam Leffler #ifdef ATH_TX99_DIAG
144186e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
144286e07743SSam Leffler 		sc->sc_tx99->detach(sc->sc_tx99);
144386e07743SSam Leffler #endif
1444c42a7b7eSSam Leffler 	ath_rate_detach(sc->sc_rc);
144589d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
1446bdbb6e5bSAdrian Chadd 	if_ath_alq_tidyup(&sc->sc_alq);
1447bdbb6e5bSAdrian Chadd #endif
1448216ca234SAdrian Chadd 	ath_lna_div_detach(sc);
1449b70f530bSAdrian Chadd 	ath_btcoex_detach(sc);
14509af351f9SAdrian Chadd 	ath_spectral_detach(sc);
145148237774SAdrian Chadd 	ath_dfs_detach(sc);
14525591b213SSam Leffler 	ath_desc_free(sc);
14534bf404eaSAdrian Chadd 	ath_txdma_teardown(sc);
14543d184db2SAdrian Chadd 	ath_rxdma_teardown(sc);
1455c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
145671b85077SSam Leffler 	ath_hal_detach(sc->sc_ah);	/* NB: sets chip in full sleep */
1457a93c5097SAdrian Chadd 
14585591b213SSam Leffler 	return 0;
14595591b213SSam Leffler }
14605591b213SSam Leffler 
1461b032f27cSSam Leffler /*
1462b032f27cSSam Leffler  * MAC address handling for multiple BSS on the same radio.
1463b032f27cSSam Leffler  * The first vap uses the MAC address from the EEPROM.  For
1464b032f27cSSam Leffler  * subsequent vap's we set the U/L bit (bit 1) in the MAC
1465b032f27cSSam Leffler  * address and use the next six bits as an index.
1466b032f27cSSam Leffler  */
1467b032f27cSSam Leffler static void
assign_address(struct ath_softc * sc,uint8_t mac[IEEE80211_ADDR_LEN],int clone)1468b032f27cSSam Leffler assign_address(struct ath_softc *sc, uint8_t mac[IEEE80211_ADDR_LEN], int clone)
1469b032f27cSSam Leffler {
1470b032f27cSSam Leffler 	int i;
1471b032f27cSSam Leffler 
1472b032f27cSSam Leffler 	if (clone && sc->sc_hasbmask) {
1473b032f27cSSam Leffler 		/* NB: we only do this if h/w supports multiple bssid */
1474b032f27cSSam Leffler 		for (i = 0; i < 8; i++)
1475b032f27cSSam Leffler 			if ((sc->sc_bssidmask & (1<<i)) == 0)
1476b032f27cSSam Leffler 				break;
1477b032f27cSSam Leffler 		if (i != 0)
1478b032f27cSSam Leffler 			mac[0] |= (i << 2)|0x2;
1479b032f27cSSam Leffler 	} else
1480b032f27cSSam Leffler 		i = 0;
1481b032f27cSSam Leffler 	sc->sc_bssidmask |= 1<<i;
1482b032f27cSSam Leffler 	sc->sc_hwbssidmask[0] &= ~mac[0];
1483b032f27cSSam Leffler 	if (i == 0)
1484b032f27cSSam Leffler 		sc->sc_nbssid0++;
1485b032f27cSSam Leffler }
1486b032f27cSSam Leffler 
1487b032f27cSSam Leffler static void
reclaim_address(struct ath_softc * sc,const uint8_t mac[IEEE80211_ADDR_LEN])1488b032f27cSSam Leffler reclaim_address(struct ath_softc *sc, const uint8_t mac[IEEE80211_ADDR_LEN])
1489b032f27cSSam Leffler {
1490b032f27cSSam Leffler 	int i = mac[0] >> 2;
1491b032f27cSSam Leffler 	uint8_t mask;
1492b032f27cSSam Leffler 
1493b032f27cSSam Leffler 	if (i != 0 || --sc->sc_nbssid0 == 0) {
1494b032f27cSSam Leffler 		sc->sc_bssidmask &= ~(1<<i);
1495b032f27cSSam Leffler 		/* recalculate bssid mask from remaining addresses */
1496b032f27cSSam Leffler 		mask = 0xff;
1497b032f27cSSam Leffler 		for (i = 1; i < 8; i++)
1498b032f27cSSam Leffler 			if (sc->sc_bssidmask & (1<<i))
1499b032f27cSSam Leffler 				mask &= ~((i<<2)|0x2);
1500b032f27cSSam Leffler 		sc->sc_hwbssidmask[0] |= mask;
1501b032f27cSSam Leffler 	}
1502b032f27cSSam Leffler }
1503b032f27cSSam Leffler 
1504b032f27cSSam Leffler /*
1505b032f27cSSam Leffler  * Assign a beacon xmit slot.  We try to space out
1506b032f27cSSam Leffler  * assignments so when beacons are staggered the
1507b032f27cSSam Leffler  * traffic coming out of the cab q has maximal time
1508b032f27cSSam Leffler  * to go out before the next beacon is scheduled.
1509b032f27cSSam Leffler  */
1510b032f27cSSam Leffler static int
assign_bslot(struct ath_softc * sc)1511b032f27cSSam Leffler assign_bslot(struct ath_softc *sc)
1512b032f27cSSam Leffler {
1513b032f27cSSam Leffler 	u_int slot, free;
1514b032f27cSSam Leffler 
1515b032f27cSSam Leffler 	free = 0;
1516b032f27cSSam Leffler 	for (slot = 0; slot < ATH_BCBUF; slot++)
1517b032f27cSSam Leffler 		if (sc->sc_bslot[slot] == NULL) {
1518b032f27cSSam Leffler 			if (sc->sc_bslot[(slot+1)%ATH_BCBUF] == NULL &&
1519b032f27cSSam Leffler 			    sc->sc_bslot[(slot-1)%ATH_BCBUF] == NULL)
1520b032f27cSSam Leffler 				return slot;
1521b032f27cSSam Leffler 			free = slot;
1522b032f27cSSam Leffler 			/* NB: keep looking for a double slot */
1523b032f27cSSam Leffler 		}
1524b032f27cSSam Leffler 	return free;
1525b032f27cSSam Leffler }
1526b032f27cSSam Leffler 
1527b032f27cSSam Leffler static struct ieee80211vap *
ath_vap_create(struct ieee80211com * ic,const char name[IFNAMSIZ],int unit,enum ieee80211_opmode opmode,int flags,const uint8_t bssid[IEEE80211_ADDR_LEN],const uint8_t mac0[IEEE80211_ADDR_LEN])1528fcd9500fSBernhard Schmidt ath_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
1529fcd9500fSBernhard Schmidt     enum ieee80211_opmode opmode, int flags,
1530b032f27cSSam Leffler     const uint8_t bssid[IEEE80211_ADDR_LEN],
1531b032f27cSSam Leffler     const uint8_t mac0[IEEE80211_ADDR_LEN])
1532b032f27cSSam Leffler {
15333797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
1534b032f27cSSam Leffler 	struct ath_vap *avp;
1535b032f27cSSam Leffler 	struct ieee80211vap *vap;
1536b032f27cSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN];
1537fcd9500fSBernhard Schmidt 	int needbeacon, error;
1538fcd9500fSBernhard Schmidt 	enum ieee80211_opmode ic_opmode;
1539b032f27cSSam Leffler 
15408aabf601SKevin Lo 	avp = malloc(sizeof(struct ath_vap), M_80211_VAP, M_WAITOK | M_ZERO);
1541b032f27cSSam Leffler 	needbeacon = 0;
1542b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(mac, mac0);
1543b032f27cSSam Leffler 
1544b032f27cSSam Leffler 	ATH_LOCK(sc);
1545a8962181SSam Leffler 	ic_opmode = opmode;		/* default to opmode of new vap */
1546b032f27cSSam Leffler 	switch (opmode) {
1547b032f27cSSam Leffler 	case IEEE80211_M_STA:
1548a8962181SSam Leffler 		if (sc->sc_nstavaps != 0) {	/* XXX only 1 for now */
1549b032f27cSSam Leffler 			device_printf(sc->sc_dev, "only 1 sta vap supported\n");
1550b032f27cSSam Leffler 			goto bad;
1551b032f27cSSam Leffler 		}
1552b032f27cSSam Leffler 		if (sc->sc_nvaps) {
1553b032f27cSSam Leffler 			/*
1554a8962181SSam Leffler 			 * With multiple vaps we must fall back
1555a8962181SSam Leffler 			 * to s/w beacon miss handling.
1556b032f27cSSam Leffler 			 */
1557b032f27cSSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
1558b032f27cSSam Leffler 		}
1559a8962181SSam Leffler 		if (flags & IEEE80211_CLONE_NOBEACONS) {
1560a8962181SSam Leffler 			/*
1561a8962181SSam Leffler 			 * Station mode w/o beacons are implemented w/ AP mode.
1562a8962181SSam Leffler 			 */
1563b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
1564a8962181SSam Leffler 		}
1565b032f27cSSam Leffler 		break;
1566b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1567b032f27cSSam Leffler 		if (sc->sc_nvaps != 0) {	/* XXX only 1 for now */
1568b032f27cSSam Leffler 			device_printf(sc->sc_dev,
1569b032f27cSSam Leffler 			    "only 1 ibss vap supported\n");
1570b032f27cSSam Leffler 			goto bad;
1571b032f27cSSam Leffler 		}
1572b032f27cSSam Leffler 		needbeacon = 1;
1573b032f27cSSam Leffler 		break;
1574b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1575584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
157610ad9a77SSam Leffler 		if (flags & IEEE80211_CLONE_TDMA) {
1577a8962181SSam Leffler 			if (sc->sc_nvaps != 0) {
1578a8962181SSam Leffler 				device_printf(sc->sc_dev,
1579a8962181SSam Leffler 				    "only 1 tdma vap supported\n");
1580a8962181SSam Leffler 				goto bad;
1581a8962181SSam Leffler 			}
158210ad9a77SSam Leffler 			needbeacon = 1;
158310ad9a77SSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
158410ad9a77SSam Leffler 		}
1585b032f27cSSam Leffler 		/* fall thru... */
158610ad9a77SSam Leffler #endif
1587b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1588b032f27cSSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode != opmode) {
1589a8962181SSam Leffler 			/*
1590a8962181SSam Leffler 			 * Adopt existing mode.  Adding a monitor or ahdemo
1591a8962181SSam Leffler 			 * vap to an existing configuration is of dubious
1592a8962181SSam Leffler 			 * value but should be ok.
1593a8962181SSam Leffler 			 */
1594b032f27cSSam Leffler 			/* XXX not right for monitor mode */
1595b032f27cSSam Leffler 			ic_opmode = ic->ic_opmode;
1596a8962181SSam Leffler 		}
1597b032f27cSSam Leffler 		break;
1598b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
159959aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1600b032f27cSSam Leffler 		needbeacon = 1;
1601a8962181SSam Leffler 		break;
1602b032f27cSSam Leffler 	case IEEE80211_M_WDS:
1603a8962181SSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode == IEEE80211_M_STA) {
1604b032f27cSSam Leffler 			device_printf(sc->sc_dev,
1605b032f27cSSam Leffler 			    "wds not supported in sta mode\n");
1606b032f27cSSam Leffler 			goto bad;
1607b032f27cSSam Leffler 		}
1608b032f27cSSam Leffler 		/*
1609b032f27cSSam Leffler 		 * Silently remove any request for a unique
1610b032f27cSSam Leffler 		 * bssid; WDS vap's always share the local
1611b032f27cSSam Leffler 		 * mac address.
1612b032f27cSSam Leffler 		 */
1613b032f27cSSam Leffler 		flags &= ~IEEE80211_CLONE_BSSID;
1614a8962181SSam Leffler 		if (sc->sc_nvaps == 0)
1615b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
1616a8962181SSam Leffler 		else
1617a8962181SSam Leffler 			ic_opmode = ic->ic_opmode;
16187d261891SRui Paulo 		break;
1619b032f27cSSam Leffler 	default:
1620b032f27cSSam Leffler 		device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
1621b032f27cSSam Leffler 		goto bad;
1622b032f27cSSam Leffler 	}
1623b032f27cSSam Leffler 	/*
1624b032f27cSSam Leffler 	 * Check that a beacon buffer is available; the code below assumes it.
1625b032f27cSSam Leffler 	 */
16266b349e5aSAdrian Chadd 	if (needbeacon & TAILQ_EMPTY(&sc->sc_bbuf)) {
1627b032f27cSSam Leffler 		device_printf(sc->sc_dev, "no beacon buffer available\n");
1628b032f27cSSam Leffler 		goto bad;
1629b032f27cSSam Leffler 	}
1630b032f27cSSam Leffler 
1631b032f27cSSam Leffler 	/* STA, AHDEMO? */
1632f6ede630SAdrian Chadd 	if (opmode == IEEE80211_M_HOSTAP || opmode == IEEE80211_M_MBSS || opmode == IEEE80211_M_STA) {
1633b032f27cSSam Leffler 		assign_address(sc, mac, flags & IEEE80211_CLONE_BSSID);
1634b032f27cSSam Leffler 		ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1635b032f27cSSam Leffler 	}
1636b032f27cSSam Leffler 
1637b032f27cSSam Leffler 	vap = &avp->av_vap;
1638b032f27cSSam Leffler 	/* XXX can't hold mutex across if_alloc */
1639b032f27cSSam Leffler 	ATH_UNLOCK(sc);
16407a79cebfSGleb Smirnoff 	error = ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
1641b032f27cSSam Leffler 	ATH_LOCK(sc);
1642b032f27cSSam Leffler 	if (error != 0) {
1643b032f27cSSam Leffler 		device_printf(sc->sc_dev, "%s: error %d creating vap\n",
1644b032f27cSSam Leffler 		    __func__, error);
1645b032f27cSSam Leffler 		goto bad2;
1646b032f27cSSam Leffler 	}
1647b032f27cSSam Leffler 
1648b032f27cSSam Leffler 	/* h/w crypto support */
1649b032f27cSSam Leffler 	vap->iv_key_alloc = ath_key_alloc;
1650b032f27cSSam Leffler 	vap->iv_key_delete = ath_key_delete;
1651b032f27cSSam Leffler 	vap->iv_key_set = ath_key_set;
1652b032f27cSSam Leffler 	vap->iv_key_update_begin = ath_key_update_begin;
1653b032f27cSSam Leffler 	vap->iv_key_update_end = ath_key_update_end;
1654b032f27cSSam Leffler 
1655b032f27cSSam Leffler 	/* override various methods */
1656b032f27cSSam Leffler 	avp->av_recv_mgmt = vap->iv_recv_mgmt;
1657b032f27cSSam Leffler 	vap->iv_recv_mgmt = ath_recv_mgmt;
1658b032f27cSSam Leffler 	vap->iv_reset = ath_reset_vap;
1659b032f27cSSam Leffler 	vap->iv_update_beacon = ath_beacon_update;
1660b032f27cSSam Leffler 	avp->av_newstate = vap->iv_newstate;
1661b032f27cSSam Leffler 	vap->iv_newstate = ath_newstate;
1662b032f27cSSam Leffler 	avp->av_bmiss = vap->iv_bmiss;
1663b032f27cSSam Leffler 	vap->iv_bmiss = ath_bmiss_vap;
1664b032f27cSSam Leffler 
16650eb81626SAdrian Chadd 	avp->av_node_ps = vap->iv_node_ps;
16660eb81626SAdrian Chadd 	vap->iv_node_ps = ath_node_powersave;
16670eb81626SAdrian Chadd 
1668548a605dSAdrian Chadd 	avp->av_set_tim = vap->iv_set_tim;
1669548a605dSAdrian Chadd 	vap->iv_set_tim = ath_node_set_tim;
1670548a605dSAdrian Chadd 
167122a3aee6SAdrian Chadd 	avp->av_recv_pspoll = vap->iv_recv_pspoll;
167222a3aee6SAdrian Chadd 	vap->iv_recv_pspoll = ath_node_recv_pspoll;
167322a3aee6SAdrian Chadd 
16749be25f4aSAdrian Chadd 	/* Set default parameters */
16759be25f4aSAdrian Chadd 
16769be25f4aSAdrian Chadd 	/*
16779be25f4aSAdrian Chadd 	 * Anything earlier than some AR9300 series MACs don't
16789be25f4aSAdrian Chadd 	 * support a smaller MPDU density.
16799be25f4aSAdrian Chadd 	 */
16809be25f4aSAdrian Chadd 	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_8;
16819be25f4aSAdrian Chadd 	/*
16829be25f4aSAdrian Chadd 	 * All NICs can handle the maximum size, however
16839be25f4aSAdrian Chadd 	 * AR5416 based MACs can only TX aggregates w/ RTS
16849be25f4aSAdrian Chadd 	 * protection when the total aggregate size is <= 8k.
16859be25f4aSAdrian Chadd 	 * However, for now that's enforced by the TX path.
16869be25f4aSAdrian Chadd 	 */
16879be25f4aSAdrian Chadd 	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K;
16880149d3d4SAdrian Chadd 	vap->iv_ampdu_limit = IEEE80211_HTCAP_MAXRXAMPDU_64K;
16899be25f4aSAdrian Chadd 
1690b032f27cSSam Leffler 	avp->av_bslot = -1;
1691b032f27cSSam Leffler 	if (needbeacon) {
1692b032f27cSSam Leffler 		/*
1693b032f27cSSam Leffler 		 * Allocate beacon state and setup the q for buffered
1694b032f27cSSam Leffler 		 * multicast frames.  We know a beacon buffer is
1695b032f27cSSam Leffler 		 * available because we checked above.
1696b032f27cSSam Leffler 		 */
16976b349e5aSAdrian Chadd 		avp->av_bcbuf = TAILQ_FIRST(&sc->sc_bbuf);
16986b349e5aSAdrian Chadd 		TAILQ_REMOVE(&sc->sc_bbuf, avp->av_bcbuf, bf_list);
1699b032f27cSSam Leffler 		if (opmode != IEEE80211_M_IBSS || !sc->sc_hasveol) {
1700b032f27cSSam Leffler 			/*
1701b032f27cSSam Leffler 			 * Assign the vap to a beacon xmit slot.  As above
1702b032f27cSSam Leffler 			 * this cannot fail to find a free one.
1703b032f27cSSam Leffler 			 */
1704b032f27cSSam Leffler 			avp->av_bslot = assign_bslot(sc);
1705b032f27cSSam Leffler 			KASSERT(sc->sc_bslot[avp->av_bslot] == NULL,
1706b032f27cSSam Leffler 			    ("beacon slot %u not empty", avp->av_bslot));
1707b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = vap;
1708b032f27cSSam Leffler 			sc->sc_nbcnvaps++;
1709b032f27cSSam Leffler 		}
1710b032f27cSSam Leffler 		if (sc->sc_hastsfadd && sc->sc_nbcnvaps > 0) {
1711b032f27cSSam Leffler 			/*
1712b032f27cSSam Leffler 			 * Multple vaps are to transmit beacons and we
1713b032f27cSSam Leffler 			 * have h/w support for TSF adjusting; enable
1714b032f27cSSam Leffler 			 * use of staggered beacons.
1715b032f27cSSam Leffler 			 */
1716b032f27cSSam Leffler 			sc->sc_stagbeacons = 1;
1717b032f27cSSam Leffler 		}
1718b032f27cSSam Leffler 		ath_txq_init(sc, &avp->av_mcastq, ATH_TXQ_SWQ);
1719b032f27cSSam Leffler 	}
1720b032f27cSSam Leffler 
1721b032f27cSSam Leffler 	ic->ic_opmode = ic_opmode;
1722b032f27cSSam Leffler 	if (opmode != IEEE80211_M_WDS) {
1723b032f27cSSam Leffler 		sc->sc_nvaps++;
1724b032f27cSSam Leffler 		if (opmode == IEEE80211_M_STA)
1725b032f27cSSam Leffler 			sc->sc_nstavaps++;
1726fe0dd789SSam Leffler 		if (opmode == IEEE80211_M_MBSS)
1727fe0dd789SSam Leffler 			sc->sc_nmeshvaps++;
1728b032f27cSSam Leffler 	}
1729b032f27cSSam Leffler 	switch (ic_opmode) {
1730b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1731b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_IBSS;
1732b032f27cSSam Leffler 		break;
1733b032f27cSSam Leffler 	case IEEE80211_M_STA:
1734b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_STA;
1735b032f27cSSam Leffler 		break;
1736b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1737584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
173810ad9a77SSam Leffler 		if (vap->iv_caps & IEEE80211_C_TDMA) {
173910ad9a77SSam Leffler 			sc->sc_tdma = 1;
174010ad9a77SSam Leffler 			/* NB: disable tsf adjust */
174110ad9a77SSam Leffler 			sc->sc_stagbeacons = 0;
174210ad9a77SSam Leffler 		}
174310ad9a77SSam Leffler 		/*
174410ad9a77SSam Leffler 		 * NB: adhoc demo mode is a pseudo mode; to the hal it's
174510ad9a77SSam Leffler 		 * just ap mode.
174610ad9a77SSam Leffler 		 */
174710ad9a77SSam Leffler 		/* fall thru... */
174810ad9a77SSam Leffler #endif
1749b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
175059aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1751b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_HOSTAP;
1752b032f27cSSam Leffler 		break;
1753b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1754b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_MONITOR;
1755b032f27cSSam Leffler 		break;
1756b032f27cSSam Leffler 	default:
1757b032f27cSSam Leffler 		/* XXX should not happen */
1758b032f27cSSam Leffler 		break;
1759b032f27cSSam Leffler 	}
1760b032f27cSSam Leffler 	if (sc->sc_hastsfadd) {
1761b032f27cSSam Leffler 		/*
1762b032f27cSSam Leffler 		 * Configure whether or not TSF adjust should be done.
1763b032f27cSSam Leffler 		 */
1764b032f27cSSam Leffler 		ath_hal_settsfadjust(sc->sc_ah, sc->sc_stagbeacons);
1765b032f27cSSam Leffler 	}
176610ad9a77SSam Leffler 	if (flags & IEEE80211_CLONE_NOBEACONS) {
176710ad9a77SSam Leffler 		/*
176810ad9a77SSam Leffler 		 * Enable s/w beacon miss handling.
176910ad9a77SSam Leffler 		 */
177010ad9a77SSam Leffler 		sc->sc_swbmiss = 1;
177110ad9a77SSam Leffler 	}
1772b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1773b032f27cSSam Leffler 
1774b032f27cSSam Leffler 	/* complete setup */
1775c6167b4bSBjoern A. Zeeb 	ieee80211_vap_attach(vap, ieee80211_media_change,
1776c6167b4bSBjoern A. Zeeb 	    ieee80211_media_status, mac);
1777b032f27cSSam Leffler 	return vap;
1778b032f27cSSam Leffler bad2:
1779b032f27cSSam Leffler 	reclaim_address(sc, mac);
1780b032f27cSSam Leffler 	ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1781b032f27cSSam Leffler bad:
1782b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1783b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1784b032f27cSSam Leffler 	return NULL;
1785b032f27cSSam Leffler }
1786b032f27cSSam Leffler 
1787b032f27cSSam Leffler static void
ath_vap_delete(struct ieee80211vap * vap)1788b032f27cSSam Leffler ath_vap_delete(struct ieee80211vap *vap)
1789b032f27cSSam Leffler {
1790b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
17913797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
1792b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1793b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
1794b032f27cSSam Leffler 
1795f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1796f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1797f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1798f5c30c4eSAdrian Chadd 
1799f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
18007a79cebfSGleb Smirnoff 	if (sc->sc_running) {
1801b032f27cSSam Leffler 		/*
1802b032f27cSSam Leffler 		 * Quiesce the hardware while we remove the vap.  In
1803b032f27cSSam Leffler 		 * particular we need to reclaim all references to
1804b032f27cSSam Leffler 		 * the vap state by any frames pending on the tx queues.
1805b032f27cSSam Leffler 		 */
1806b032f27cSSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
1807517526efSAdrian Chadd 		/* XXX Do all frames from all vaps/nodes need draining here? */
18089a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* stop recv side */
1809a100c050SAdrian Chadd 		ath_rx_flush(sc);
1810062cf7d9SAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);		/* stop hw xmit side */
1811b032f27cSSam Leffler 	}
1812b032f27cSSam Leffler 
1813f5c30c4eSAdrian Chadd 	/* .. leave the hardware awake for now. */
1814f5c30c4eSAdrian Chadd 
1815b032f27cSSam Leffler 	ieee80211_vap_detach(vap);
181616d4de92SAdrian Chadd 
181716d4de92SAdrian Chadd 	/*
181816d4de92SAdrian Chadd 	 * XXX Danger Will Robinson! Danger!
181916d4de92SAdrian Chadd 	 *
182016d4de92SAdrian Chadd 	 * Because ieee80211_vap_detach() can queue a frame (the station
182116d4de92SAdrian Chadd 	 * diassociate message?) after we've drained the TXQ and
182216d4de92SAdrian Chadd 	 * flushed the software TXQ, we will end up with a frame queued
182316d4de92SAdrian Chadd 	 * to a node whose vap is about to be freed.
182416d4de92SAdrian Chadd 	 *
182516d4de92SAdrian Chadd 	 * To work around this, flush the hardware/software again.
182616d4de92SAdrian Chadd 	 * This may be racy - the ath task may be running and the packet
182716d4de92SAdrian Chadd 	 * may be being scheduled between sw->hw txq. Tsk.
182816d4de92SAdrian Chadd 	 *
182916d4de92SAdrian Chadd 	 * TODO: figure out why a new node gets allocated somewhere around
18307a79cebfSGleb Smirnoff 	 * here (after the ath_tx_swq() call; and after an ath_stop()
183116d4de92SAdrian Chadd 	 * call!)
183216d4de92SAdrian Chadd 	 */
183316d4de92SAdrian Chadd 
183416d4de92SAdrian Chadd 	ath_draintxq(sc, ATH_RESET_DEFAULT);
183516d4de92SAdrian Chadd 
1836b032f27cSSam Leffler 	ATH_LOCK(sc);
1837b032f27cSSam Leffler 	/*
1838b032f27cSSam Leffler 	 * Reclaim beacon state.  Note this must be done before
1839b032f27cSSam Leffler 	 * the vap instance is reclaimed as we may have a reference
1840b032f27cSSam Leffler 	 * to it in the buffer for the beacon frame.
1841b032f27cSSam Leffler 	 */
1842b032f27cSSam Leffler 	if (avp->av_bcbuf != NULL) {
1843b032f27cSSam Leffler 		if (avp->av_bslot != -1) {
1844b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = NULL;
1845b032f27cSSam Leffler 			sc->sc_nbcnvaps--;
1846b032f27cSSam Leffler 		}
1847b032f27cSSam Leffler 		ath_beacon_return(sc, avp->av_bcbuf);
1848b032f27cSSam Leffler 		avp->av_bcbuf = NULL;
1849b032f27cSSam Leffler 		if (sc->sc_nbcnvaps == 0) {
1850b032f27cSSam Leffler 			sc->sc_stagbeacons = 0;
1851b032f27cSSam Leffler 			if (sc->sc_hastsfadd)
1852b032f27cSSam Leffler 				ath_hal_settsfadjust(sc->sc_ah, 0);
1853b032f27cSSam Leffler 		}
1854b032f27cSSam Leffler 		/*
1855b032f27cSSam Leffler 		 * Reclaim any pending mcast frames for the vap.
1856b032f27cSSam Leffler 		 */
1857b032f27cSSam Leffler 		ath_tx_draintxq(sc, &avp->av_mcastq);
1858b032f27cSSam Leffler 	}
1859b032f27cSSam Leffler 	/*
1860b032f27cSSam Leffler 	 * Update bookkeeping.
1861b032f27cSSam Leffler 	 */
1862b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
1863b032f27cSSam Leffler 		sc->sc_nstavaps--;
1864b032f27cSSam Leffler 		if (sc->sc_nstavaps == 0 && sc->sc_swbmiss)
1865b032f27cSSam Leffler 			sc->sc_swbmiss = 0;
186659aa14a9SRui Paulo 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1867f6ede630SAdrian Chadd 	    vap->iv_opmode == IEEE80211_M_STA ||
186859aa14a9SRui Paulo 	    vap->iv_opmode == IEEE80211_M_MBSS) {
1869b032f27cSSam Leffler 		reclaim_address(sc, vap->iv_myaddr);
1870b032f27cSSam Leffler 		ath_hal_setbssidmask(ah, sc->sc_hwbssidmask);
1871fe0dd789SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_MBSS)
1872fe0dd789SSam Leffler 			sc->sc_nmeshvaps--;
1873b032f27cSSam Leffler 	}
1874b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_WDS)
1875b032f27cSSam Leffler 		sc->sc_nvaps--;
1876584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
187710ad9a77SSam Leffler 	/* TDMA operation ceases when the last vap is destroyed */
187810ad9a77SSam Leffler 	if (sc->sc_tdma && sc->sc_nvaps == 0) {
187910ad9a77SSam Leffler 		sc->sc_tdma = 0;
188010ad9a77SSam Leffler 		sc->sc_swbmiss = 0;
188110ad9a77SSam Leffler 	}
188210ad9a77SSam Leffler #endif
1883b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1884b032f27cSSam Leffler 
18857a79cebfSGleb Smirnoff 	if (sc->sc_running) {
1886b032f27cSSam Leffler 		/*
1887b032f27cSSam Leffler 		 * Restart rx+tx machines if still running (RUNNING will
1888b032f27cSSam Leffler 		 * be reset if we just destroyed the last vap).
1889b032f27cSSam Leffler 		 */
1890b032f27cSSam Leffler 		if (ath_startrecv(sc) != 0)
189176e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev,
189276e6fd5dSGleb Smirnoff 			    "%s: unable to restart recv logic\n", __func__);
1893c89b957aSSam Leffler 		if (sc->sc_beacons) {		/* restart beacons */
1894c89b957aSSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
1895c89b957aSSam Leffler 			if (sc->sc_tdma)
1896c89b957aSSam Leffler 				ath_tdma_config(sc, NULL);
1897c89b957aSSam Leffler 			else
1898c89b957aSSam Leffler #endif
1899b032f27cSSam Leffler 				ath_beacon_config(sc, NULL);
1900c89b957aSSam Leffler 		}
1901b032f27cSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
1902b032f27cSSam Leffler 	}
1903f5c30c4eSAdrian Chadd 
1904f5c30c4eSAdrian Chadd 	/* Ok, let the hardware asleep. */
1905f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
190616d4de92SAdrian Chadd 	ATH_UNLOCK(sc);
1907b032f27cSSam Leffler }
1908b032f27cSSam Leffler 
19095591b213SSam Leffler void
ath_suspend(struct ath_softc * sc)19105591b213SSam Leffler ath_suspend(struct ath_softc *sc)
19115591b213SSam Leffler {
19127a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
19135591b213SSam Leffler 
19147a79cebfSGleb Smirnoff 	sc->sc_resume_up = ic->ic_nrunning != 0;
1915d1328898SAdrian Chadd 
1916d3ac945bSSam Leffler 	ieee80211_suspend_all(ic);
1917d3ac945bSSam Leffler 	/*
1918d3ac945bSSam Leffler 	 * NB: don't worry about putting the chip in low power
1919d3ac945bSSam Leffler 	 * mode; pci will power off our socket on suspend and
1920f29b8b7fSWarner Losh 	 * CardBus detaches the device.
192117bb5fd1SAdrian Chadd 	 *
192217bb5fd1SAdrian Chadd 	 * XXX TODO: well, that's great, except for non-cardbus
192317bb5fd1SAdrian Chadd 	 * devices!
1924d3ac945bSSam Leffler 	 */
1925d73df6d5SAdrian Chadd 
1926ae2a0aa4SAdrian Chadd 	/*
192717bb5fd1SAdrian Chadd 	 * XXX This doesn't wait until all pending taskqueue
192817bb5fd1SAdrian Chadd 	 * items and parallel transmit/receive/other threads
192917bb5fd1SAdrian Chadd 	 * are running!
193017bb5fd1SAdrian Chadd 	 */
193117bb5fd1SAdrian Chadd 	ath_hal_intrset(sc->sc_ah, 0);
193217bb5fd1SAdrian Chadd 	taskqueue_block(sc->sc_tq);
19337707f31dSAdrian Chadd 
19347707f31dSAdrian Chadd 	ATH_LOCK(sc);
19357707f31dSAdrian Chadd 	callout_stop(&sc->sc_cal_ch);
19367707f31dSAdrian Chadd 	ATH_UNLOCK(sc);
193717bb5fd1SAdrian Chadd 
193817bb5fd1SAdrian Chadd 	/*
1939ae2a0aa4SAdrian Chadd 	 * XXX ensure sc_invalid is 1
1940ae2a0aa4SAdrian Chadd 	 */
1941ae2a0aa4SAdrian Chadd 
1942ae2a0aa4SAdrian Chadd 	/* Disable the PCIe PHY, complete with workarounds */
1943ae2a0aa4SAdrian Chadd 	ath_hal_enablepcie(sc->sc_ah, 1, 1);
1944d3ac945bSSam Leffler }
1945d3ac945bSSam Leffler 
1946d3ac945bSSam Leffler /*
1947d3ac945bSSam Leffler  * Reset the key cache since some parts do not reset the
1948d3ac945bSSam Leffler  * contents on resume.  First we clear all entries, then
1949d3ac945bSSam Leffler  * re-load keys that the 802.11 layer assumes are setup
1950d3ac945bSSam Leffler  * in h/w.
1951d3ac945bSSam Leffler  */
1952d3ac945bSSam Leffler static void
ath_reset_keycache(struct ath_softc * sc)1953d3ac945bSSam Leffler ath_reset_keycache(struct ath_softc *sc)
1954d3ac945bSSam Leffler {
19557a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
1956d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1957d3ac945bSSam Leffler 	int i;
1958d3ac945bSSam Leffler 
1959f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1960f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1961d3ac945bSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
1962d3ac945bSSam Leffler 		ath_hal_keyreset(ah, i);
1963f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
1964f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1965d3ac945bSSam Leffler 	ieee80211_crypto_reload_keys(ic);
19665591b213SSam Leffler }
19675591b213SSam Leffler 
19686322256bSAdrian Chadd /*
19696322256bSAdrian Chadd  * Fetch the current chainmask configuration based on the current
19706322256bSAdrian Chadd  * operating channel and options.
19716322256bSAdrian Chadd  */
19726322256bSAdrian Chadd static void
ath_update_chainmasks(struct ath_softc * sc,struct ieee80211_channel * chan)19736322256bSAdrian Chadd ath_update_chainmasks(struct ath_softc *sc, struct ieee80211_channel *chan)
19746322256bSAdrian Chadd {
19756322256bSAdrian Chadd 
19766322256bSAdrian Chadd 	/*
19776322256bSAdrian Chadd 	 * Set TX chainmask to the currently configured chainmask;
19786322256bSAdrian Chadd 	 * the TX chainmask depends upon the current operating mode.
19796322256bSAdrian Chadd 	 */
19806322256bSAdrian Chadd 	sc->sc_cur_rxchainmask = sc->sc_rxchainmask;
19816322256bSAdrian Chadd 	if (IEEE80211_IS_CHAN_HT(chan)) {
19826322256bSAdrian Chadd 		sc->sc_cur_txchainmask = sc->sc_txchainmask;
19836322256bSAdrian Chadd 	} else {
19846322256bSAdrian Chadd 		sc->sc_cur_txchainmask = 1;
19856322256bSAdrian Chadd 	}
19867904f516SAdrian Chadd 
19877904f516SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
19887904f516SAdrian Chadd 	    "%s: TX chainmask is now 0x%x, RX is now 0x%x\n",
19897904f516SAdrian Chadd 	    __func__,
19907904f516SAdrian Chadd 	    sc->sc_cur_txchainmask,
19917904f516SAdrian Chadd 	    sc->sc_cur_rxchainmask);
19926322256bSAdrian Chadd }
19936322256bSAdrian Chadd 
19945591b213SSam Leffler void
ath_resume(struct ath_softc * sc)19955591b213SSam Leffler ath_resume(struct ath_softc *sc)
19965591b213SSam Leffler {
19977a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
1998d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1999d3ac945bSSam Leffler 	HAL_STATUS status;
20005591b213SSam Leffler 
2001ae2a0aa4SAdrian Chadd 	ath_hal_enablepcie(ah, 0, 0);
2002d73df6d5SAdrian Chadd 
2003d3ac945bSSam Leffler 	/*
2004d3ac945bSSam Leffler 	 * Must reset the chip before we reload the
2005d3ac945bSSam Leffler 	 * keycache as we were powered down on suspend.
2006d3ac945bSSam Leffler 	 */
20076322256bSAdrian Chadd 	ath_update_chainmasks(sc,
20086322256bSAdrian Chadd 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan);
20096322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
20106322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
2011f5c30c4eSAdrian Chadd 
2012f5c30c4eSAdrian Chadd 	/* Ensure we set the current power state to on */
2013f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
20147d567ed6SAdrian Chadd 	ath_power_setselfgen(sc, HAL_PM_AWAKE);
2015f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
20168c03e55dSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_AWAKE, 1);
2017f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2018f5c30c4eSAdrian Chadd 
2019054d7b69SSam Leffler 	ath_hal_reset(ah, sc->sc_opmode,
2020054d7b69SSam Leffler 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan,
2021f50e4ebfSAdrian Chadd 	    AH_FALSE, HAL_RESET_NORMAL, &status);
2022d3ac945bSSam Leffler 	ath_reset_keycache(sc);
20237e5eb44dSAdrian Chadd 
202417bb5fd1SAdrian Chadd 	ATH_RX_LOCK(sc);
202517bb5fd1SAdrian Chadd 	sc->sc_rx_stopped = 1;
202617bb5fd1SAdrian Chadd 	sc->sc_rx_resetted = 1;
202717bb5fd1SAdrian Chadd 	ATH_RX_UNLOCK(sc);
202817bb5fd1SAdrian Chadd 
20297e5eb44dSAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
20307e5eb44dSAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
20317e5eb44dSAdrian Chadd 
20329af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
20339af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
20349af351f9SAdrian Chadd 
2035dd6a574eSAdrian Chadd 	/*
2036b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
2037b70f530bSAdrian Chadd 	 */
2038b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
2039b70f530bSAdrian Chadd 
2040b70f530bSAdrian Chadd 	/*
2041dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
2042dd6a574eSAdrian Chadd 	 * support it.
2043dd6a574eSAdrian Chadd 	 */
2044dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
2045dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
2046dd6a574eSAdrian Chadd 	else
2047dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
2048dd6a574eSAdrian Chadd 
2049a497cd88SAdrian Chadd 	/* Restore the LED configuration */
2050a497cd88SAdrian Chadd 	ath_led_config(sc);
2051a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
2052a497cd88SAdrian Chadd 
2053d1328898SAdrian Chadd 	if (sc->sc_resume_up)
2054021a0db5SAdrian Chadd 		ieee80211_resume_all(ic);
20552fd9aabbSAdrian Chadd 
2056f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2057f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2058f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2059f5c30c4eSAdrian Chadd 
20602fd9aabbSAdrian Chadd 	/* XXX beacons ? */
20616b59f5e3SSam Leffler }
20625591b213SSam Leffler 
20635591b213SSam Leffler void
ath_shutdown(struct ath_softc * sc)20645591b213SSam Leffler ath_shutdown(struct ath_softc *sc)
20655591b213SSam Leffler {
20665591b213SSam Leffler 
20677a79cebfSGleb Smirnoff 	ATH_LOCK(sc);
20687a79cebfSGleb Smirnoff 	ath_stop(sc);
20697a79cebfSGleb Smirnoff 	ATH_UNLOCK(sc);
2070d3ac945bSSam Leffler 	/* NB: no point powering down chip as we're about to reboot */
20715591b213SSam Leffler }
20725591b213SSam Leffler 
2073c42a7b7eSSam Leffler /*
2074c42a7b7eSSam Leffler  * Interrupt handler.  Most of the actual processing is deferred.
2075c42a7b7eSSam Leffler  */
20765591b213SSam Leffler void
ath_intr(void * arg)20775591b213SSam Leffler ath_intr(void *arg)
20785591b213SSam Leffler {
20795591b213SSam Leffler 	struct ath_softc *sc = arg;
20805591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
20816f5fe81eSAdrian Chadd 	HAL_INT status = 0;
20828f939e79SAdrian Chadd 	uint32_t txqs;
20835591b213SSam Leffler 
2084ef27340cSAdrian Chadd 	/*
2085ef27340cSAdrian Chadd 	 * If we're inside a reset path, just print a warning and
2086ef27340cSAdrian Chadd 	 * clear the ISR. The reset routine will finish it for us.
2087ef27340cSAdrian Chadd 	 */
2088ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2089ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt) {
2090ef27340cSAdrian Chadd 		HAL_INT status;
2091ef27340cSAdrian Chadd 		ath_hal_getisr(ah, &status);	/* clear ISR */
2092ef27340cSAdrian Chadd 		ath_hal_intrset(ah, 0);		/* disable further intr's */
2093ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_ANY,
2094ef27340cSAdrian Chadd 		    "%s: in reset, ignoring: status=0x%x\n",
2095ef27340cSAdrian Chadd 		    __func__, status);
2096ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2097ef27340cSAdrian Chadd 		return;
2098ef27340cSAdrian Chadd 	}
2099ef27340cSAdrian Chadd 
21005591b213SSam Leffler 	if (sc->sc_invalid) {
21015591b213SSam Leffler 		/*
2102b58b3803SSam Leffler 		 * The hardware is not ready/present, don't touch anything.
2103b58b3803SSam Leffler 		 * Note this can happen early on if the IRQ is shared.
21045591b213SSam Leffler 		 */
2105c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
2106ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
21075591b213SSam Leffler 		return;
21085591b213SSam Leffler 	}
2109ef27340cSAdrian Chadd 	if (!ath_hal_intrpend(ah)) {		/* shared irq, not for us */
2110ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2111fdd758d4SSam Leffler 		return;
2112ef27340cSAdrian Chadd 	}
2113ef27340cSAdrian Chadd 
2114f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2115f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2116f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2117f5c30c4eSAdrian Chadd 
21187a79cebfSGleb Smirnoff 	if (sc->sc_ic.ic_nrunning == 0 && sc->sc_running == 0) {
211968e8e04eSSam Leffler 		HAL_INT status;
212068e8e04eSSam Leffler 
21217a79cebfSGleb Smirnoff 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: ic_nrunning %d sc_running %d\n",
21227a79cebfSGleb Smirnoff 		    __func__, sc->sc_ic.ic_nrunning, sc->sc_running);
21235591b213SSam Leffler 		ath_hal_getisr(ah, &status);	/* clear ISR */
21245591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable further intr's */
2125ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2126f5c30c4eSAdrian Chadd 
2127f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
2128f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
2129f5c30c4eSAdrian Chadd 		ATH_UNLOCK(sc);
21305591b213SSam Leffler 		return;
21315591b213SSam Leffler 	}
2132ef27340cSAdrian Chadd 
2133c42a7b7eSSam Leffler 	/*
2134c42a7b7eSSam Leffler 	 * Figure out the reason(s) for the interrupt.  Note
2135c42a7b7eSSam Leffler 	 * that the hal returns a pseudo-ISR that may include
2136c42a7b7eSSam Leffler 	 * bits we haven't explicitly enabled so we mask the
2137c42a7b7eSSam Leffler 	 * value to insure we only process bits we requested.
2138c42a7b7eSSam Leffler 	 */
21395591b213SSam Leffler 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
2140c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
214103682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 1, "ath_intr: mask=0x%.8x", status);
2142a26f3327SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
2143a26f3327SAdrian Chadd 	if_ath_alq_post_intr(&sc->sc_alq, status, ah->ah_intrstate,
2144a26f3327SAdrian Chadd 	    ah->ah_syncstate);
2145a26f3327SAdrian Chadd #endif	/* ATH_DEBUG_ALQ */
214631fdf3d6SAdrian Chadd #ifdef	ATH_KTR_INTR_DEBUG
214703682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 5,
2148f52d3452SAdrian Chadd 	    "ath_intr: ISR=0x%.8x, ISR_S0=0x%.8x, ISR_S1=0x%.8x, ISR_S2=0x%.8x, ISR_S5=0x%.8x",
2149f52d3452SAdrian Chadd 	    ah->ah_intrstate[0],
2150f52d3452SAdrian Chadd 	    ah->ah_intrstate[1],
2151f52d3452SAdrian Chadd 	    ah->ah_intrstate[2],
2152f52d3452SAdrian Chadd 	    ah->ah_intrstate[3],
2153f52d3452SAdrian Chadd 	    ah->ah_intrstate[6]);
215431fdf3d6SAdrian Chadd #endif
21559467e3f3SAdrian Chadd 
21569467e3f3SAdrian Chadd 	/* Squirrel away SYNC interrupt debugging */
21579467e3f3SAdrian Chadd 	if (ah->ah_syncstate != 0) {
21589467e3f3SAdrian Chadd 		int i;
21599467e3f3SAdrian Chadd 		for (i = 0; i < 32; i++)
21603655135dSConrad Meyer 			if (ah->ah_syncstate & (1 << i))
21619467e3f3SAdrian Chadd 				sc->sc_intr_stats.sync_intr[i]++;
21629467e3f3SAdrian Chadd 	}
21639467e3f3SAdrian Chadd 
2164ecddff40SSam Leffler 	status &= sc->sc_imask;			/* discard unasked for bits */
21656f5fe81eSAdrian Chadd 
21666f5fe81eSAdrian Chadd 	/* Short-circuit un-handled interrupts */
2167ef27340cSAdrian Chadd 	if (status == 0x0) {
2168ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2169f5c30c4eSAdrian Chadd 
2170f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
2171f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
2172f5c30c4eSAdrian Chadd 		ATH_UNLOCK(sc);
2173f5c30c4eSAdrian Chadd 
21746f5fe81eSAdrian Chadd 		return;
2175ef27340cSAdrian Chadd 	}
21766f5fe81eSAdrian Chadd 
2177ef27340cSAdrian Chadd 	/*
2178ef27340cSAdrian Chadd 	 * Take a note that we're inside the interrupt handler, so
2179ef27340cSAdrian Chadd 	 * the reset routines know to wait.
2180ef27340cSAdrian Chadd 	 */
2181ef27340cSAdrian Chadd 	sc->sc_intr_cnt++;
2182ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2183ef27340cSAdrian Chadd 
2184ef27340cSAdrian Chadd 	/*
2185ef27340cSAdrian Chadd 	 * Handle the interrupt. We won't run concurrent with the reset
2186ef27340cSAdrian Chadd 	 * or channel change routines as they'll wait for sc_intr_cnt
2187ef27340cSAdrian Chadd 	 * to be 0 before continuing.
2188ef27340cSAdrian Chadd 	 */
21895591b213SSam Leffler 	if (status & HAL_INT_FATAL) {
21905591b213SSam Leffler 		sc->sc_stats.ast_hardware++;
21915591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
2192f846cf42SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_fataltask);
21935591b213SSam Leffler 	} else {
2194c42a7b7eSSam Leffler 		if (status & HAL_INT_SWBA) {
2195c42a7b7eSSam Leffler 			/*
2196c42a7b7eSSam Leffler 			 * Software beacon alert--time to send a beacon.
2197c42a7b7eSSam Leffler 			 * Handle beacon transmission directly; deferring
2198c42a7b7eSSam Leffler 			 * this is too slow to meet timing constraints
2199c42a7b7eSSam Leffler 			 * under load.
2200c42a7b7eSSam Leffler 			 */
2201584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
220210ad9a77SSam Leffler 			if (sc->sc_tdma) {
220310ad9a77SSam Leffler 				if (sc->sc_tdmaswba == 0) {
22047a79cebfSGleb Smirnoff 					struct ieee80211com *ic = &sc->sc_ic;
220510ad9a77SSam Leffler 					struct ieee80211vap *vap =
220610ad9a77SSam Leffler 					    TAILQ_FIRST(&ic->ic_vaps);
220710ad9a77SSam Leffler 					ath_tdma_beacon_send(sc, vap);
220810ad9a77SSam Leffler 					sc->sc_tdmaswba =
220910ad9a77SSam Leffler 					    vap->iv_tdma->tdma_bintval;
221010ad9a77SSam Leffler 				} else
221110ad9a77SSam Leffler 					sc->sc_tdmaswba--;
221210ad9a77SSam Leffler 			} else
221310ad9a77SSam Leffler #endif
2214339ccfb3SSam Leffler 			{
2215c42a7b7eSSam Leffler 				ath_beacon_proc(sc, 0);
2216339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
2217339ccfb3SSam Leffler 				/*
2218339ccfb3SSam Leffler 				 * Schedule the rx taskq in case there's no
2219339ccfb3SSam Leffler 				 * traffic so any frames held on the staging
2220339ccfb3SSam Leffler 				 * queue are aged and potentially flushed.
2221339ccfb3SSam Leffler 				 */
2222f0db652cSAdrian Chadd 				sc->sc_rx.recv_sched(sc, 1);
2223339ccfb3SSam Leffler #endif
2224339ccfb3SSam Leffler 			}
2225c42a7b7eSSam Leffler 		}
22265591b213SSam Leffler 		if (status & HAL_INT_RXEOL) {
22278f939e79SAdrian Chadd 			int imask;
222803682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXEOL");
222917bb5fd1SAdrian Chadd 			if (! sc->sc_isedma) {
2230ef27340cSAdrian Chadd 				ATH_PCU_LOCK(sc);
22315591b213SSam Leffler 				/*
22325591b213SSam Leffler 				 * NB: the hardware should re-read the link when
22335591b213SSam Leffler 				 *     RXE bit is written, but it doesn't work at
22345591b213SSam Leffler 				 *     least on older hardware revs.
22355591b213SSam Leffler 				 */
22365591b213SSam Leffler 				sc->sc_stats.ast_rxeol++;
223773f895fcSAdrian Chadd 				/*
223873f895fcSAdrian Chadd 				 * Disable RXEOL/RXORN - prevent an interrupt
223973f895fcSAdrian Chadd 				 * storm until the PCU logic can be reset.
22401fdadc0fSAdrian Chadd 				 * In case the interface is reset some other
22411fdadc0fSAdrian Chadd 				 * way before "sc_kickpcu" is called, don't
22421fdadc0fSAdrian Chadd 				 * modify sc_imask - that way if it is reset
22431fdadc0fSAdrian Chadd 				 * by a call to ath_reset() somehow, the
22441fdadc0fSAdrian Chadd 				 * interrupt mask will be correctly reprogrammed.
224573f895fcSAdrian Chadd 				 */
22468f939e79SAdrian Chadd 				imask = sc->sc_imask;
22471fdadc0fSAdrian Chadd 				imask &= ~(HAL_INT_RXEOL | HAL_INT_RXORN);
22481fdadc0fSAdrian Chadd 				ath_hal_intrset(ah, imask);
22491fdadc0fSAdrian Chadd 				/*
22508f939e79SAdrian Chadd 				 * Only blank sc_rxlink if we've not yet kicked
22518f939e79SAdrian Chadd 				 * the PCU.
22528f939e79SAdrian Chadd 				 *
22538f939e79SAdrian Chadd 				 * This isn't entirely correct - the correct solution
22548f939e79SAdrian Chadd 				 * would be to have a PCU lock and engage that for
22558f939e79SAdrian Chadd 				 * the duration of the PCU fiddling; which would include
22568f939e79SAdrian Chadd 				 * running the RX process. Otherwise we could end up
22578f939e79SAdrian Chadd 				 * messing up the RX descriptor chain and making the
22588f939e79SAdrian Chadd 				 * RX desc list much shorter.
22598f939e79SAdrian Chadd 				 */
22608f939e79SAdrian Chadd 				if (! sc->sc_kickpcu)
22618f939e79SAdrian Chadd 					sc->sc_rxlink = NULL;
22628f939e79SAdrian Chadd 				sc->sc_kickpcu = 1;
2263f0db652cSAdrian Chadd 				ATH_PCU_UNLOCK(sc);
226417bb5fd1SAdrian Chadd 			}
22658f939e79SAdrian Chadd 			/*
226617bb5fd1SAdrian Chadd 			 * Enqueue an RX proc to handle whatever
22671fdadc0fSAdrian Chadd 			 * is in the RX queue.
226817bb5fd1SAdrian Chadd 			 * This will then kick the PCU if required.
22691fdadc0fSAdrian Chadd 			 */
2270f0db652cSAdrian Chadd 			sc->sc_rx.recv_sched(sc, 1);
22715591b213SSam Leffler 		}
22725591b213SSam Leffler 		if (status & HAL_INT_TXURN) {
22735591b213SSam Leffler 			sc->sc_stats.ast_txurn++;
22745591b213SSam Leffler 			/* bump tx trigger level */
22755591b213SSam Leffler 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
22765591b213SSam Leffler 		}
2277bcbb08ceSAdrian Chadd 		/*
2278bcbb08ceSAdrian Chadd 		 * Handle both the legacy and RX EDMA interrupt bits.
2279bcbb08ceSAdrian Chadd 		 * Note that HAL_INT_RXLP is also HAL_INT_RXDESC.
2280bcbb08ceSAdrian Chadd 		 */
2281bcbb08ceSAdrian Chadd 		if (status & (HAL_INT_RX | HAL_INT_RXHP | HAL_INT_RXLP)) {
22828f939e79SAdrian Chadd 			sc->sc_stats.ast_rx_intr++;
2283f0db652cSAdrian Chadd 			sc->sc_rx.recv_sched(sc, 1);
22848f939e79SAdrian Chadd 		}
22858f939e79SAdrian Chadd 		if (status & HAL_INT_TX) {
22868f939e79SAdrian Chadd 			sc->sc_stats.ast_tx_intr++;
22878f939e79SAdrian Chadd 			/*
22888f939e79SAdrian Chadd 			 * Grab all the currently set bits in the HAL txq bitmap
22898f939e79SAdrian Chadd 			 * and blank them. This is the only place we should be
22908f939e79SAdrian Chadd 			 * doing this.
22918f939e79SAdrian Chadd 			 */
2292bad98824SAdrian Chadd 			if (! sc->sc_isedma) {
2293ef27340cSAdrian Chadd 				ATH_PCU_LOCK(sc);
22948f939e79SAdrian Chadd 				txqs = 0xffffffff;
22958f939e79SAdrian Chadd 				ath_hal_gettxintrtxqs(sc->sc_ah, &txqs);
229603682514SAdrian Chadd 				ATH_KTR(sc, ATH_KTR_INTERRUPTS, 3,
229703682514SAdrian Chadd 				    "ath_intr: TX; txqs=0x%08x, txq_active was 0x%08x, now 0x%08x",
229803682514SAdrian Chadd 				    txqs,
229903682514SAdrian Chadd 				    sc->sc_txq_active,
230003682514SAdrian Chadd 				    sc->sc_txq_active | txqs);
23018f939e79SAdrian Chadd 				sc->sc_txq_active |= txqs;
2302ef27340cSAdrian Chadd 				ATH_PCU_UNLOCK(sc);
23038f939e79SAdrian Chadd 			}
2304bad98824SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask);
2305bad98824SAdrian Chadd 		}
23065591b213SSam Leffler 		if (status & HAL_INT_BMISS) {
23075591b213SSam Leffler 			sc->sc_stats.ast_bmiss++;
23080bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask);
23095591b213SSam Leffler 		}
23106ad02dbaSAdrian Chadd 		if (status & HAL_INT_GTT)
23116ad02dbaSAdrian Chadd 			sc->sc_stats.ast_tx_timeout++;
23125594f5c0SAdrian Chadd 		if (status & HAL_INT_CST)
23135594f5c0SAdrian Chadd 			sc->sc_stats.ast_tx_cst++;
2314c42a7b7eSSam Leffler 		if (status & HAL_INT_MIB) {
2315c42a7b7eSSam Leffler 			sc->sc_stats.ast_mib++;
2316ef27340cSAdrian Chadd 			ATH_PCU_LOCK(sc);
2317c42a7b7eSSam Leffler 			/*
2318c42a7b7eSSam Leffler 			 * Disable interrupts until we service the MIB
2319c42a7b7eSSam Leffler 			 * interrupt; otherwise it will continue to fire.
2320c42a7b7eSSam Leffler 			 */
2321c42a7b7eSSam Leffler 			ath_hal_intrset(ah, 0);
2322c42a7b7eSSam Leffler 			/*
2323c42a7b7eSSam Leffler 			 * Let the hal handle the event.  We assume it will
2324c42a7b7eSSam Leffler 			 * clear whatever condition caused the interrupt.
2325c42a7b7eSSam Leffler 			 */
2326ffa2cab6SSam Leffler 			ath_hal_mibevent(ah, &sc->sc_halstats);
23278f939e79SAdrian Chadd 			/*
23288f939e79SAdrian Chadd 			 * Don't reset the interrupt if we've just
23298f939e79SAdrian Chadd 			 * kicked the PCU, or we may get a nested
23308f939e79SAdrian Chadd 			 * RXEOL before the rxproc has had a chance
23318f939e79SAdrian Chadd 			 * to run.
23328f939e79SAdrian Chadd 			 */
23338f939e79SAdrian Chadd 			if (sc->sc_kickpcu == 0)
2334c42a7b7eSSam Leffler 				ath_hal_intrset(ah, sc->sc_imask);
2335ef27340cSAdrian Chadd 			ATH_PCU_UNLOCK(sc);
2336c42a7b7eSSam Leffler 		}
23379c4fc1e8SSam Leffler 		if (status & HAL_INT_RXORN) {
23389c4fc1e8SSam Leffler 			/* NB: hal marks HAL_INT_FATAL when RXORN is fatal */
233903682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXORN");
23409c4fc1e8SSam Leffler 			sc->sc_stats.ast_rxorn++;
23419c4fc1e8SSam Leffler 		}
2342f5c30c4eSAdrian Chadd 		if (status & HAL_INT_TSFOOR) {
2343fb3edd4fSAdrian Chadd 			/*
2344fb3edd4fSAdrian Chadd 			 * out of range beacon - wake the chip up,
2345fb3edd4fSAdrian Chadd 			 * but don't modify self-gen frame config.
2346fb3edd4fSAdrian Chadd 			 * Do a full reset to clear any potential stuck
2347fb3edd4fSAdrian Chadd 			 * PHY/MAC that generated this condition.
2348fb3edd4fSAdrian Chadd 			 */
2349fb3edd4fSAdrian Chadd 			sc->sc_stats.ast_tsfoor++;
23508c03e55dSAdrian Chadd 			ATH_LOCK(sc);
23518c03e55dSAdrian Chadd 			ath_power_setpower(sc, HAL_PM_AWAKE, 0);
23528c03e55dSAdrian Chadd 			ATH_UNLOCK(sc);
2353fb3edd4fSAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_tsfoortask);
2354fb3edd4fSAdrian Chadd 			device_printf(sc->sc_dev, "%s: TSFOOR\n", __func__);
2355f5c30c4eSAdrian Chadd 		}
2356bcf5fc49SAdrian Chadd 		if (status & HAL_INT_MCI) {
2357bcf5fc49SAdrian Chadd 			ath_btcoex_mci_intr(sc);
2358bcf5fc49SAdrian Chadd 		}
23595591b213SSam Leffler 	}
2360ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2361ef27340cSAdrian Chadd 	sc->sc_intr_cnt--;
2362ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2363f5c30c4eSAdrian Chadd 
2364f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2365f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2366f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
23675591b213SSam Leffler }
23685591b213SSam Leffler 
23695591b213SSam Leffler static void
ath_fatal_proc(void * arg,int pending)23705591b213SSam Leffler ath_fatal_proc(void *arg, int pending)
23715591b213SSam Leffler {
23725591b213SSam Leffler 	struct ath_softc *sc = arg;
237316c8acaaSSam Leffler 	u_int32_t *state;
237416c8acaaSSam Leffler 	u_int32_t len;
237568e8e04eSSam Leffler 	void *sp;
23765591b213SSam Leffler 
237770c81b20SAdrian Chadd 	if (sc->sc_invalid)
237870c81b20SAdrian Chadd 		return;
237970c81b20SAdrian Chadd 
238076e6fd5dSGleb Smirnoff 	device_printf(sc->sc_dev, "hardware error; resetting\n");
238116c8acaaSSam Leffler 	/*
238216c8acaaSSam Leffler 	 * Fatal errors are unrecoverable.  Typically these
238316c8acaaSSam Leffler 	 * are caused by DMA errors.  Collect h/w state from
238416c8acaaSSam Leffler 	 * the hal so we can diagnose what's going on.
238516c8acaaSSam Leffler 	 */
238668e8e04eSSam Leffler 	if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) {
238716c8acaaSSam Leffler 		KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len));
238868e8e04eSSam Leffler 		state = sp;
238976e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
239076e6fd5dSGleb Smirnoff 		    "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n", state[0],
239176e6fd5dSGleb Smirnoff 		    state[1] , state[2], state[3], state[4], state[5]);
239216c8acaaSSam Leffler 	}
23938c01c3dcSAdrian Chadd 	ath_reset(sc, ATH_RESET_NOLOSS, HAL_RESET_FORCE_COLD);
23945591b213SSam Leffler }
23955591b213SSam Leffler 
23965591b213SSam Leffler static void
ath_bmiss_vap(struct ieee80211vap * vap)2397b032f27cSSam Leffler ath_bmiss_vap(struct ieee80211vap *vap)
23985591b213SSam Leffler {
23993797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
2400f5c30c4eSAdrian Chadd 
240159fbb257SSam Leffler 	/*
240259fbb257SSam Leffler 	 * Workaround phantom bmiss interrupts by sanity-checking
240359fbb257SSam Leffler 	 * the time of our last rx'd frame.  If it is within the
240459fbb257SSam Leffler 	 * beacon miss interval then ignore the interrupt.  If it's
240559fbb257SSam Leffler 	 * truly a bmiss we'll get another interrupt soon and that'll
240659fbb257SSam Leffler 	 * be dispatched up for processing.  Note this applies only
240759fbb257SSam Leffler 	 * for h/w beacon miss events.
240859fbb257SSam Leffler 	 */
2409f5c30c4eSAdrian Chadd 
2410f5c30c4eSAdrian Chadd 	/*
2411f5c30c4eSAdrian Chadd 	 * XXX TODO: Just read the TSF during the interrupt path;
2412f5c30c4eSAdrian Chadd 	 * that way we don't have to wake up again just to read it
2413f5c30c4eSAdrian Chadd 	 * again.
2414f5c30c4eSAdrian Chadd 	 */
2415f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2416f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2417f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2418f5c30c4eSAdrian Chadd 
241959fbb257SSam Leffler 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
2420d7736e13SSam Leffler 		u_int64_t lastrx = sc->sc_lastrx;
2421d7736e13SSam Leffler 		u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah);
242280767531SAdrian Chadd 		/* XXX should take a locked ref to iv_bss */
2423d7736e13SSam Leffler 		u_int bmisstimeout =
2424b032f27cSSam Leffler 			vap->iv_bmissthreshold * vap->iv_bss->ni_intval * 1024;
2425d7736e13SSam Leffler 
2426d7736e13SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2427d7736e13SSam Leffler 		    "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n",
2428d7736e13SSam Leffler 		    __func__, (unsigned long long) tsf,
2429d7736e13SSam Leffler 		    (unsigned long long)(tsf - lastrx),
2430d7736e13SSam Leffler 		    (unsigned long long) lastrx, bmisstimeout);
243159fbb257SSam Leffler 
243259fbb257SSam Leffler 		if (tsf - lastrx <= bmisstimeout) {
2433d7736e13SSam Leffler 			sc->sc_stats.ast_bmiss_phantom++;
2434f5c30c4eSAdrian Chadd 
2435f5c30c4eSAdrian Chadd 			ATH_LOCK(sc);
2436f5c30c4eSAdrian Chadd 			ath_power_restore_power_state(sc);
2437f5c30c4eSAdrian Chadd 			ATH_UNLOCK(sc);
2438f5c30c4eSAdrian Chadd 
243959fbb257SSam Leffler 			return;
244059fbb257SSam Leffler 		}
244159fbb257SSam Leffler 	}
2442f5c30c4eSAdrian Chadd 
2443f5c30c4eSAdrian Chadd 	/*
24448c03e55dSAdrian Chadd 	 * Keep the hardware awake if it's asleep (and leave self-gen
24458c03e55dSAdrian Chadd 	 * frame config alone) until the next beacon, so we can resync
24468c03e55dSAdrian Chadd 	 * against the next beacon.
24478c03e55dSAdrian Chadd 	 *
24488c03e55dSAdrian Chadd 	 * This handles three common beacon miss cases in STA powersave mode -
24498c03e55dSAdrian Chadd 	 * (a) the beacon TBTT isnt a multiple of bintval;
24508c03e55dSAdrian Chadd 	 * (b) the beacon was missed; and
24518c03e55dSAdrian Chadd 	 * (c) the beacons are being delayed because the AP is busy and
24528c03e55dSAdrian Chadd 	 *     isn't reliably able to meet its TBTT.
2453f5c30c4eSAdrian Chadd 	 */
2454f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
24558c03e55dSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_AWAKE, 0);
2456f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2457f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2458f858e928SAdrian Chadd 
24598c03e55dSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_BEACON,
24608c03e55dSAdrian Chadd 	    "%s: forced awake; force syncbeacon=1\n", __func__);
2461f858e928SAdrian Chadd 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
2462f5c30c4eSAdrian Chadd 		/*
2463f5c30c4eSAdrian Chadd 		 * Attempt to force a beacon resync.
2464f5c30c4eSAdrian Chadd 		 */
2465f5c30c4eSAdrian Chadd 		sc->sc_syncbeacon = 1;
2466f858e928SAdrian Chadd 	}
2467f5c30c4eSAdrian Chadd 
246859fbb257SSam Leffler 	ATH_VAP(vap)->av_bmiss(vap);
2469e585d188SSam Leffler }
2470b032f27cSSam Leffler 
2471f5c30c4eSAdrian Chadd /* XXX this needs a force wakeup! */
2472b837332dSAdrian Chadd int
ath_hal_gethangstate(struct ath_hal * ah,uint32_t mask,uint32_t * hangs)2473459bc4f0SSam Leffler ath_hal_gethangstate(struct ath_hal *ah, uint32_t mask, uint32_t *hangs)
2474459bc4f0SSam Leffler {
2475459bc4f0SSam Leffler 	uint32_t rsize;
2476459bc4f0SSam Leffler 	void *sp;
2477459bc4f0SSam Leffler 
247825c96056SAdrian Chadd 	if (!ath_hal_getdiagstate(ah, HAL_DIAG_CHECK_HANGS, &mask, sizeof(mask), &sp, &rsize))
2479459bc4f0SSam Leffler 		return 0;
2480459bc4f0SSam Leffler 	KASSERT(rsize == sizeof(uint32_t), ("resultsize %u", rsize));
2481459bc4f0SSam Leffler 	*hangs = *(uint32_t *)sp;
2482459bc4f0SSam Leffler 	return 1;
2483459bc4f0SSam Leffler }
2484459bc4f0SSam Leffler 
2485b032f27cSSam Leffler static void
ath_bmiss_proc(void * arg,int pending)2486b032f27cSSam Leffler ath_bmiss_proc(void *arg, int pending)
2487b032f27cSSam Leffler {
2488b032f27cSSam Leffler 	struct ath_softc *sc = arg;
2489459bc4f0SSam Leffler 	uint32_t hangs;
2490b032f27cSSam Leffler 
2491b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
2492459bc4f0SSam Leffler 
2493f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2494f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2495f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2496f5c30c4eSAdrian Chadd 
2497f5c30c4eSAdrian Chadd 	ath_beacon_miss(sc);
2498f5c30c4eSAdrian Chadd 
2499a74ebfe5SAdrian Chadd 	/*
2500fb3edd4fSAdrian Chadd 	 * Do a reset upon any beacon miss event.
2501a74ebfe5SAdrian Chadd 	 *
2502a74ebfe5SAdrian Chadd 	 * It may be a non-recognised RX clear hang which needs a reset
2503a74ebfe5SAdrian Chadd 	 * to clear.
2504a74ebfe5SAdrian Chadd 	 */
2505459bc4f0SSam Leffler 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) {
25068c01c3dcSAdrian Chadd 		ath_reset(sc, ATH_RESET_NOLOSS, HAL_RESET_BBPANIC);
250776e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
250876e6fd5dSGleb Smirnoff 		    "bb hang detected (0x%x), resetting\n", hangs);
2509a74ebfe5SAdrian Chadd 	} else {
25108c01c3dcSAdrian Chadd 		ath_reset(sc, ATH_RESET_NOLOSS, HAL_RESET_FORCE_COLD);
25117a79cebfSGleb Smirnoff 		ieee80211_beacon_miss(&sc->sc_ic);
25125591b213SSam Leffler 	}
2513f5c30c4eSAdrian Chadd 
2514f5c30c4eSAdrian Chadd 	/* Force a beacon resync, in case they've drifted */
2515f5c30c4eSAdrian Chadd 	sc->sc_syncbeacon = 1;
2516f5c30c4eSAdrian Chadd 
2517f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2518f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2519f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2520a74ebfe5SAdrian Chadd }
25215591b213SSam Leffler 
2522724c193aSSam Leffler /*
2523fb3edd4fSAdrian Chadd  * Handle a TSF out of range interrupt in STA mode.
2524fb3edd4fSAdrian Chadd  *
2525fb3edd4fSAdrian Chadd  * This may be due to a partially deaf looking radio, so
2526fb3edd4fSAdrian Chadd  * do a full reset just in case it is indeed deaf and
2527fb3edd4fSAdrian Chadd  * resync the beacon.
2528fb3edd4fSAdrian Chadd  */
2529fb3edd4fSAdrian Chadd static void
ath_tsfoor_proc(void * arg,int pending)2530fb3edd4fSAdrian Chadd ath_tsfoor_proc(void *arg, int pending)
2531fb3edd4fSAdrian Chadd {
2532fb3edd4fSAdrian Chadd 	struct ath_softc *sc = arg;
2533fb3edd4fSAdrian Chadd 
2534fb3edd4fSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
2535fb3edd4fSAdrian Chadd 
2536fb3edd4fSAdrian Chadd 	ATH_LOCK(sc);
2537fb3edd4fSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2538fb3edd4fSAdrian Chadd 	ATH_UNLOCK(sc);
2539fb3edd4fSAdrian Chadd 
2540fb3edd4fSAdrian Chadd 	/*
2541fb3edd4fSAdrian Chadd 	 * Do a full reset after any TSFOOR.  It's possible that
2542fb3edd4fSAdrian Chadd 	 * we've gone deaf or partially deaf (eg due to calibration
2543fb3edd4fSAdrian Chadd 	 * failures) and this should clean things up a bit.
2544fb3edd4fSAdrian Chadd 	 */
2545fb3edd4fSAdrian Chadd 	ath_reset(sc, ATH_RESET_NOLOSS, HAL_RESET_FORCE_COLD);
2546fb3edd4fSAdrian Chadd 
2547fb3edd4fSAdrian Chadd 	/* Force a beacon resync, in case they've drifted */
2548fb3edd4fSAdrian Chadd 	sc->sc_syncbeacon = 1;
2549fb3edd4fSAdrian Chadd 
2550fb3edd4fSAdrian Chadd 	ATH_LOCK(sc);
2551fb3edd4fSAdrian Chadd 	ath_power_restore_power_state(sc);
2552fb3edd4fSAdrian Chadd 	ATH_UNLOCK(sc);
2553fb3edd4fSAdrian Chadd }
2554fb3edd4fSAdrian Chadd 
2555fb3edd4fSAdrian Chadd /*
2556b032f27cSSam Leffler  * Handle TKIP MIC setup to deal hardware that doesn't do MIC
2557b032f27cSSam Leffler  * calcs together with WME.  If necessary disable the crypto
2558b032f27cSSam Leffler  * hardware and mark the 802.11 state so keys will be setup
2559b032f27cSSam Leffler  * with the MIC work done in software.
2560b032f27cSSam Leffler  */
2561b032f27cSSam Leffler static void
ath_settkipmic(struct ath_softc * sc)2562b032f27cSSam Leffler ath_settkipmic(struct ath_softc *sc)
2563b032f27cSSam Leffler {
25647a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
2565b032f27cSSam Leffler 
2566b032f27cSSam Leffler 	if ((ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIP) && !sc->sc_wmetkipmic) {
2567b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WME) {
2568b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_FALSE);
2569b032f27cSSam Leffler 			ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC;
2570b032f27cSSam Leffler 		} else {
2571b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_TRUE);
2572b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
2573b032f27cSSam Leffler 		}
2574b032f27cSSam Leffler 	}
2575b032f27cSSam Leffler }
2576b032f27cSSam Leffler 
25771410ca56SAdrian Chadd static void
ath_vap_clear_quiet_ie(struct ath_softc * sc)25781410ca56SAdrian Chadd ath_vap_clear_quiet_ie(struct ath_softc *sc)
25791410ca56SAdrian Chadd {
25801410ca56SAdrian Chadd 	struct ieee80211com *ic = &sc->sc_ic;
25811410ca56SAdrian Chadd 	struct ieee80211vap *vap;
25821410ca56SAdrian Chadd 	struct ath_vap *avp;
25831410ca56SAdrian Chadd 
25841410ca56SAdrian Chadd 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
25851410ca56SAdrian Chadd 		avp = ATH_VAP(vap);
25861410ca56SAdrian Chadd 		/* Quiet time handling - ensure we resync */
25871410ca56SAdrian Chadd 		memset(&avp->quiet_ie, 0, sizeof(avp->quiet_ie));
25881410ca56SAdrian Chadd 	}
25891410ca56SAdrian Chadd }
25901410ca56SAdrian Chadd 
25917a79cebfSGleb Smirnoff static int
ath_init(struct ath_softc * sc)25927a79cebfSGleb Smirnoff ath_init(struct ath_softc *sc)
25935591b213SSam Leffler {
25947a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
25955591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
25965591b213SSam Leffler 	HAL_STATUS status;
25975591b213SSam Leffler 
25987a79cebfSGleb Smirnoff 	ATH_LOCK_ASSERT(sc);
25995591b213SSam Leffler 
26005591b213SSam Leffler 	/*
2601f5c30c4eSAdrian Chadd 	 * Force the sleep state awake.
2602f5c30c4eSAdrian Chadd 	 */
26037d567ed6SAdrian Chadd 	ath_power_setselfgen(sc, HAL_PM_AWAKE);
2604f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
26058c03e55dSAdrian Chadd 	ath_power_setpower(sc, HAL_PM_AWAKE, 1);
2606f5c30c4eSAdrian Chadd 
2607f5c30c4eSAdrian Chadd 	/*
26085591b213SSam Leffler 	 * Stop anything previously setup.  This is safe
26095591b213SSam Leffler 	 * whether this is the first time through or not.
26105591b213SSam Leffler 	 */
26117a79cebfSGleb Smirnoff 	ath_stop(sc);
26125591b213SSam Leffler 
26135591b213SSam Leffler 	/*
26145591b213SSam Leffler 	 * The basic interface to setting the hardware in a good
26155591b213SSam Leffler 	 * state is ``reset''.  On return the hardware is known to
26165591b213SSam Leffler 	 * be powered up and with interrupts disabled.  This must
26175591b213SSam Leffler 	 * be followed by initialization of the appropriate bits
26185591b213SSam Leffler 	 * and then setup of the interrupt mask.
26195591b213SSam Leffler 	 */
2620b032f27cSSam Leffler 	ath_settkipmic(sc);
26216322256bSAdrian Chadd 	ath_update_chainmasks(sc, ic->ic_curchan);
26226322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
26236322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
2624f5c30c4eSAdrian Chadd 
262576e6fd5dSGleb Smirnoff 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_FALSE,
2626f50e4ebfSAdrian Chadd 	    HAL_RESET_NORMAL, &status)) {
262776e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
262876e6fd5dSGleb Smirnoff 		    "unable to reset hardware; hal status %u\n", status);
26297a79cebfSGleb Smirnoff 		return (ENODEV);
26305591b213SSam Leffler 	}
263117bb5fd1SAdrian Chadd 
263217bb5fd1SAdrian Chadd 	ATH_RX_LOCK(sc);
263317bb5fd1SAdrian Chadd 	sc->sc_rx_stopped = 1;
263417bb5fd1SAdrian Chadd 	sc->sc_rx_resetted = 1;
263517bb5fd1SAdrian Chadd 	ATH_RX_UNLOCK(sc);
263617bb5fd1SAdrian Chadd 
26371410ca56SAdrian Chadd 	/* Clear quiet IE state for each VAP */
26381410ca56SAdrian Chadd 	ath_vap_clear_quiet_ie(sc);
26391410ca56SAdrian Chadd 
2640b032f27cSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
26415591b213SSam Leffler 
264248237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
264348237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
264448237774SAdrian Chadd 
26459af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
26469af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
26479af351f9SAdrian Chadd 
26485591b213SSam Leffler 	/*
2649b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
2650b70f530bSAdrian Chadd 	 */
2651b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
2652b70f530bSAdrian Chadd 
2653b70f530bSAdrian Chadd 	/*
2654dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
2655dd6a574eSAdrian Chadd 	 * support it.
2656dd6a574eSAdrian Chadd 	 */
2657dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
2658dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
2659dd6a574eSAdrian Chadd 	else
2660dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
2661dd6a574eSAdrian Chadd 
2662dd6a574eSAdrian Chadd 	/*
2663c59005e9SSam Leffler 	 * Likewise this is set during reset so update
2664c59005e9SSam Leffler 	 * state cached in the driver.
2665c59005e9SSam Leffler 	 */
2666c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
26679bbfde1eSAdrian Chadd 	sc->sc_lastlongcal = ticks;
26682dc7fcc4SSam Leffler 	sc->sc_resetcal = 1;
26692dc7fcc4SSam Leffler 	sc->sc_lastcalreset = 0;
26709bbfde1eSAdrian Chadd 	sc->sc_lastani = ticks;
26719bbfde1eSAdrian Chadd 	sc->sc_lastshortcal = ticks;
2672a108ab63SAdrian Chadd 	sc->sc_doresetcal = AH_FALSE;
26732fd9aabbSAdrian Chadd 	/*
26742fd9aabbSAdrian Chadd 	 * Beacon timers were cleared here; give ath_newstate()
26752fd9aabbSAdrian Chadd 	 * a hint that the beacon timers should be poked when
26762fd9aabbSAdrian Chadd 	 * things transition to the RUN state.
26772fd9aabbSAdrian Chadd 	 */
26782fd9aabbSAdrian Chadd 	sc->sc_beacons = 0;
2679c42a7b7eSSam Leffler 
2680c42a7b7eSSam Leffler 	/*
26815591b213SSam Leffler 	 * Setup the hardware after reset: the key cache
26825591b213SSam Leffler 	 * is filled as needed and the receive engine is
26835591b213SSam Leffler 	 * set going.  Frame transmit is handled entirely
26845591b213SSam Leffler 	 * in the frame output path; there's nothing to do
26855591b213SSam Leffler 	 * here except setup the interrupt mask.
26865591b213SSam Leffler 	 */
26875591b213SSam Leffler 	if (ath_startrecv(sc) != 0) {
268876e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "unable to start recv logic\n");
2689f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
26907a79cebfSGleb Smirnoff 		return (ENODEV);
26915591b213SSam Leffler 	}
26925591b213SSam Leffler 
26935591b213SSam Leffler 	/*
26945591b213SSam Leffler 	 * Enable interrupts.
26955591b213SSam Leffler 	 */
26965591b213SSam Leffler 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
269717bb5fd1SAdrian Chadd 		  | HAL_INT_RXORN | HAL_INT_TXURN
26985591b213SSam Leffler 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
2699bcbb08ceSAdrian Chadd 
2700bcbb08ceSAdrian Chadd 	/*
2701bcbb08ceSAdrian Chadd 	 * Enable RX EDMA bits.  Note these overlap with
2702bcbb08ceSAdrian Chadd 	 * HAL_INT_RX and HAL_INT_RXDESC respectively.
2703bcbb08ceSAdrian Chadd 	 */
2704bcbb08ceSAdrian Chadd 	if (sc->sc_isedma)
2705bcbb08ceSAdrian Chadd 		sc->sc_imask |= (HAL_INT_RXHP | HAL_INT_RXLP);
2706bcbb08ceSAdrian Chadd 
2707c42a7b7eSSam Leffler 	/*
270817bb5fd1SAdrian Chadd 	 * If we're an EDMA NIC, we don't care about RXEOL.
270917bb5fd1SAdrian Chadd 	 * Writing a new descriptor in will simply restart
271017bb5fd1SAdrian Chadd 	 * RX DMA.
271117bb5fd1SAdrian Chadd 	 */
271217bb5fd1SAdrian Chadd 	if (! sc->sc_isedma)
271317bb5fd1SAdrian Chadd 		sc->sc_imask |= HAL_INT_RXEOL;
271417bb5fd1SAdrian Chadd 
271517bb5fd1SAdrian Chadd 	/*
2716bcf5fc49SAdrian Chadd 	 * Enable MCI interrupt for MCI devices.
2717bcf5fc49SAdrian Chadd 	 */
2718bcf5fc49SAdrian Chadd 	if (sc->sc_btcoex_mci)
2719bcf5fc49SAdrian Chadd 		sc->sc_imask |= HAL_INT_MCI;
2720bcf5fc49SAdrian Chadd 
2721bcf5fc49SAdrian Chadd 	/*
2722c42a7b7eSSam Leffler 	 * Enable MIB interrupts when there are hardware phy counters.
2723c42a7b7eSSam Leffler 	 * Note we only do this (at the moment) for station mode.
2724c42a7b7eSSam Leffler 	 */
2725c42a7b7eSSam Leffler 	if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
2726c42a7b7eSSam Leffler 		sc->sc_imask |= HAL_INT_MIB;
27275591b213SSam Leffler 
2728f5c30c4eSAdrian Chadd 	/*
2729f5c30c4eSAdrian Chadd 	 * XXX add capability for this.
2730f5c30c4eSAdrian Chadd 	 *
2731f5c30c4eSAdrian Chadd 	 * If we're in STA mode (and maybe IBSS?) then register for
2732f5c30c4eSAdrian Chadd 	 * TSFOOR interrupts.
2733f5c30c4eSAdrian Chadd 	 */
2734f5c30c4eSAdrian Chadd 	if (ic->ic_opmode == IEEE80211_M_STA)
2735f5c30c4eSAdrian Chadd 		sc->sc_imask |= HAL_INT_TSFOOR;
2736f5c30c4eSAdrian Chadd 
27375594f5c0SAdrian Chadd 	/* Enable global TX timeout and carrier sense timeout if available */
27386ad02dbaSAdrian Chadd 	if (ath_hal_gtxto_supported(ah))
27393788ebedSAdrian Chadd 		sc->sc_imask |= HAL_INT_GTT;
2740d0a0ebc6SAdrian Chadd 
2741d0a0ebc6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: imask=0x%x\n",
2742d0a0ebc6SAdrian Chadd 		__func__, sc->sc_imask);
27436ad02dbaSAdrian Chadd 
27447a79cebfSGleb Smirnoff 	sc->sc_running = 1;
27452e986da5SSam Leffler 	callout_reset(&sc->sc_wd_ch, hz, ath_watchdog, sc);
2746b032f27cSSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
27475591b213SSam Leffler 
2748f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2749b032f27cSSam Leffler 
27507a79cebfSGleb Smirnoff 	return (0);
27515591b213SSam Leffler }
27525591b213SSam Leffler 
27535591b213SSam Leffler static void
ath_stop(struct ath_softc * sc)27547a79cebfSGleb Smirnoff ath_stop(struct ath_softc *sc)
27555591b213SSam Leffler {
27565591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
27575591b213SSam Leffler 
2758c42a7b7eSSam Leffler 	ATH_LOCK_ASSERT(sc);
2759f5c30c4eSAdrian Chadd 
2760f5c30c4eSAdrian Chadd 	/*
2761f5c30c4eSAdrian Chadd 	 * Wake the hardware up before fiddling with it.
2762f5c30c4eSAdrian Chadd 	 */
2763f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2764f5c30c4eSAdrian Chadd 
27657a79cebfSGleb Smirnoff 	if (sc->sc_running) {
27665591b213SSam Leffler 		/*
27675591b213SSam Leffler 		 * Shutdown the hardware and driver:
2768c42a7b7eSSam Leffler 		 *    reset 802.11 state machine
27695591b213SSam Leffler 		 *    turn off timers
2770c42a7b7eSSam Leffler 		 *    disable interrupts
2771c42a7b7eSSam Leffler 		 *    turn off the radio
27725591b213SSam Leffler 		 *    clear transmit machinery
27735591b213SSam Leffler 		 *    clear receive machinery
27745591b213SSam Leffler 		 *    drain and release tx queues
27755591b213SSam Leffler 		 *    reclaim beacon resources
27765591b213SSam Leffler 		 *    power down hardware
27775591b213SSam Leffler 		 *
27785591b213SSam Leffler 		 * Note that some of this work is not possible if the
27795591b213SSam Leffler 		 * hardware is gone (invalid).
27805591b213SSam Leffler 		 */
278186e07743SSam Leffler #ifdef ATH_TX99_DIAG
278286e07743SSam Leffler 		if (sc->sc_tx99 != NULL)
278386e07743SSam Leffler 			sc->sc_tx99->stop(sc->sc_tx99);
278486e07743SSam Leffler #endif
27852e986da5SSam Leffler 		callout_stop(&sc->sc_wd_ch);
27862e986da5SSam Leffler 		sc->sc_wd_timer = 0;
27877a79cebfSGleb Smirnoff 		sc->sc_running = 0;
2788c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
27893e50ec2cSSam Leffler 			if (sc->sc_softled) {
27903e50ec2cSSam Leffler 				callout_stop(&sc->sc_ledtimer);
27913e50ec2cSSam Leffler 				ath_hal_gpioset(ah, sc->sc_ledpin,
27923e50ec2cSSam Leffler 					!sc->sc_ledon);
27933e50ec2cSSam Leffler 				sc->sc_blinking = 0;
27943e50ec2cSSam Leffler 			}
27955591b213SSam Leffler 			ath_hal_intrset(ah, 0);
2796c42a7b7eSSam Leffler 		}
2797062cf7d9SAdrian Chadd 		/* XXX we should stop RX regardless of whether it's valid */
2798c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
27999a842e8bSAdrian Chadd 			ath_stoprecv(sc, 1);
2800c42a7b7eSSam Leffler 			ath_hal_phydisable(ah);
2801c42a7b7eSSam Leffler 		} else
28025591b213SSam Leffler 			sc->sc_rxlink = NULL;
2803062cf7d9SAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);
2804b032f27cSSam Leffler 		ath_beacon_free(sc);	/* XXX not needed */
2805c42a7b7eSSam Leffler 	}
2806f5c30c4eSAdrian Chadd 
2807f5c30c4eSAdrian Chadd 	/* And now, restore the current power state */
2808f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2809c42a7b7eSSam Leffler }
2810c42a7b7eSSam Leffler 
2811f5c30c4eSAdrian Chadd /*
2812f5c30c4eSAdrian Chadd  * Wait until all pending TX/RX has completed.
2813f5c30c4eSAdrian Chadd  *
2814f5c30c4eSAdrian Chadd  * This waits until all existing transmit, receive and interrupts
2815f5c30c4eSAdrian Chadd  * have completed.  It's assumed that the caller has first
2816f5c30c4eSAdrian Chadd  * grabbed the reset lock so it doesn't try to do overlapping
2817f5c30c4eSAdrian Chadd  * chip resets.
2818f5c30c4eSAdrian Chadd  */
2819f5c30c4eSAdrian Chadd #define	MAX_TXRX_ITERATIONS	100
2820ef27340cSAdrian Chadd static void
ath_txrx_stop_locked(struct ath_softc * sc)282121008bf1SAdrian Chadd ath_txrx_stop_locked(struct ath_softc *sc)
2822ef27340cSAdrian Chadd {
2823ef27340cSAdrian Chadd 	int i = MAX_TXRX_ITERATIONS;
2824ef27340cSAdrian Chadd 
2825ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
282621008bf1SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
282721008bf1SAdrian Chadd 
2828ef27340cSAdrian Chadd 	/*
2829ef27340cSAdrian Chadd 	 * Sleep until all the pending operations have completed.
2830ef27340cSAdrian Chadd 	 *
2831ef27340cSAdrian Chadd 	 * The caller must ensure that reset has been incremented
2832ef27340cSAdrian Chadd 	 * or the pending operations may continue being queued.
2833ef27340cSAdrian Chadd 	 */
2834ef27340cSAdrian Chadd 	while (sc->sc_rxproc_cnt || sc->sc_txproc_cnt ||
2835ef27340cSAdrian Chadd 	    sc->sc_txstart_cnt || sc->sc_intr_cnt) {
2836ef27340cSAdrian Chadd 		if (i <= 0)
2837ef27340cSAdrian Chadd 			break;
2838f5c30c4eSAdrian Chadd 		msleep(sc, &sc->sc_pcu_mtx, 0, "ath_txrx_stop",
2839f5c30c4eSAdrian Chadd 		    msecs_to_ticks(10));
2840ef27340cSAdrian Chadd 		i--;
2841ef27340cSAdrian Chadd 	}
2842ef27340cSAdrian Chadd 
2843ef27340cSAdrian Chadd 	if (i <= 0)
2844ef27340cSAdrian Chadd 		device_printf(sc->sc_dev,
2845ef27340cSAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2846ef27340cSAdrian Chadd 		    __func__, MAX_TXRX_ITERATIONS);
2847ef27340cSAdrian Chadd }
2848ef27340cSAdrian Chadd #undef	MAX_TXRX_ITERATIONS
2849ef27340cSAdrian Chadd 
2850e78719adSAdrian Chadd #if 0
2851ef27340cSAdrian Chadd static void
285221008bf1SAdrian Chadd ath_txrx_stop(struct ath_softc *sc)
285321008bf1SAdrian Chadd {
285421008bf1SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
285521008bf1SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
285621008bf1SAdrian Chadd 
285721008bf1SAdrian Chadd 	ATH_PCU_LOCK(sc);
285821008bf1SAdrian Chadd 	ath_txrx_stop_locked(sc);
285921008bf1SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
286021008bf1SAdrian Chadd }
2861e78719adSAdrian Chadd #endif
286221008bf1SAdrian Chadd 
286321008bf1SAdrian Chadd static void
ath_txrx_start(struct ath_softc * sc)2864ef27340cSAdrian Chadd ath_txrx_start(struct ath_softc *sc)
2865ef27340cSAdrian Chadd {
2866ef27340cSAdrian Chadd 
2867ef27340cSAdrian Chadd 	taskqueue_unblock(sc->sc_tq);
2868ef27340cSAdrian Chadd }
2869ef27340cSAdrian Chadd 
2870ee321975SAdrian Chadd /*
2871ee321975SAdrian Chadd  * Grab the reset lock, and wait around until no one else
2872ee321975SAdrian Chadd  * is trying to do anything with it.
2873ee321975SAdrian Chadd  *
2874ee321975SAdrian Chadd  * This is totally horrible but we can't hold this lock for
2875ee321975SAdrian Chadd  * long enough to do TX/RX or we end up with net80211/ip stack
2876ee321975SAdrian Chadd  * LORs and eventual deadlock.
2877ee321975SAdrian Chadd  *
2878ee321975SAdrian Chadd  * "dowait" signals whether to spin, waiting for the reset
2879ee321975SAdrian Chadd  * lock count to reach 0. This should (for now) only be used
2880ee321975SAdrian Chadd  * during the reset path, as the rest of the code may not
2881ee321975SAdrian Chadd  * be locking-reentrant enough to behave correctly.
2882ee321975SAdrian Chadd  *
2883ee321975SAdrian Chadd  * Another, cleaner way should be found to serialise all of
2884ee321975SAdrian Chadd  * these operations.
2885ee321975SAdrian Chadd  */
2886f5c30c4eSAdrian Chadd #define	MAX_RESET_ITERATIONS	25
2887ee321975SAdrian Chadd static int
ath_reset_grablock(struct ath_softc * sc,int dowait)2888ee321975SAdrian Chadd ath_reset_grablock(struct ath_softc *sc, int dowait)
2889ee321975SAdrian Chadd {
2890ee321975SAdrian Chadd 	int w = 0;
2891ee321975SAdrian Chadd 	int i = MAX_RESET_ITERATIONS;
2892ee321975SAdrian Chadd 
2893ee321975SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
2894ee321975SAdrian Chadd 	do {
2895ee321975SAdrian Chadd 		if (sc->sc_inreset_cnt == 0) {
2896ee321975SAdrian Chadd 			w = 1;
2897ee321975SAdrian Chadd 			break;
2898ee321975SAdrian Chadd 		}
2899ee321975SAdrian Chadd 		if (dowait == 0) {
2900ee321975SAdrian Chadd 			w = 0;
2901ee321975SAdrian Chadd 			break;
2902ee321975SAdrian Chadd 		}
2903ee321975SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2904f5c30c4eSAdrian Chadd 		/*
2905f5c30c4eSAdrian Chadd 		 * 1 tick is likely not enough time for long calibrations
2906f5c30c4eSAdrian Chadd 		 * to complete.  So we should wait quite a while.
2907f5c30c4eSAdrian Chadd 		 */
2908f5c30c4eSAdrian Chadd 		pause("ath_reset_grablock", msecs_to_ticks(100));
2909ee321975SAdrian Chadd 		i--;
2910ee321975SAdrian Chadd 		ATH_PCU_LOCK(sc);
2911ee321975SAdrian Chadd 	} while (i > 0);
2912ee321975SAdrian Chadd 
2913ee321975SAdrian Chadd 	/*
2914ee321975SAdrian Chadd 	 * We always increment the refcounter, regardless
2915ee321975SAdrian Chadd 	 * of whether we succeeded to get it in an exclusive
2916ee321975SAdrian Chadd 	 * way.
2917ee321975SAdrian Chadd 	 */
2918ee321975SAdrian Chadd 	sc->sc_inreset_cnt++;
2919ee321975SAdrian Chadd 
2920ee321975SAdrian Chadd 	if (i <= 0)
2921ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2922ee321975SAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2923ee321975SAdrian Chadd 		    __func__, MAX_RESET_ITERATIONS);
2924ee321975SAdrian Chadd 
2925ee321975SAdrian Chadd 	if (w == 0)
2926ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2927ee321975SAdrian Chadd 		    "%s: warning, recursive reset path!\n",
2928ee321975SAdrian Chadd 		    __func__);
2929ee321975SAdrian Chadd 
2930ee321975SAdrian Chadd 	return w;
2931ee321975SAdrian Chadd }
2932ee321975SAdrian Chadd #undef MAX_RESET_ITERATIONS
2933ee321975SAdrian Chadd 
2934ee321975SAdrian Chadd /*
29355591b213SSam Leffler  * Reset the hardware w/o losing operational state.  This is
29365591b213SSam Leffler  * basically a more efficient way of doing ath_stop, ath_init,
29375591b213SSam Leffler  * followed by state transitions to the current 802.11
2938c42a7b7eSSam Leffler  * operational state.  Used to recover from various errors and
2939c42a7b7eSSam Leffler  * to reset or reload hardware state.
29405591b213SSam Leffler  */
29416079fdbeSAdrian Chadd int
ath_reset(struct ath_softc * sc,ATH_RESET_TYPE reset_type,HAL_RESET_TYPE ah_reset_type)29428c01c3dcSAdrian Chadd ath_reset(struct ath_softc *sc, ATH_RESET_TYPE reset_type,
29438c01c3dcSAdrian Chadd     HAL_RESET_TYPE ah_reset_type)
29445591b213SSam Leffler {
29457a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
29465591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
29475591b213SSam Leffler 	HAL_STATUS status;
2948ef27340cSAdrian Chadd 	int i;
29495591b213SSam Leffler 
2950f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
295116d4de92SAdrian Chadd 
2952ee321975SAdrian Chadd 	/* Ensure ATH_LOCK isn't held; ath_rx_proc can't be locked */
2953ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
2954ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
2955ef27340cSAdrian Chadd 
2956f6b6084bSPedro F. Giffuni 	/* Try to (stop any further TX/RX from occurring */
2957d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
2958d52f7132SAdrian Chadd 
2959f5c30c4eSAdrian Chadd 	/*
2960f5c30c4eSAdrian Chadd 	 * Wake the hardware up.
2961f5c30c4eSAdrian Chadd 	 */
2962f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2963f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2964f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2965f5c30c4eSAdrian Chadd 
2966ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2967904e385eSAdrian Chadd 
2968904e385eSAdrian Chadd 	/*
2969904e385eSAdrian Chadd 	 * Grab the reset lock before TX/RX is stopped.
2970904e385eSAdrian Chadd 	 *
2971904e385eSAdrian Chadd 	 * This is needed to ensure that when the TX/RX actually does finish,
2972904e385eSAdrian Chadd 	 * no further TX/RX/reset runs in parallel with this.
2973904e385eSAdrian Chadd 	 */
2974ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
2975ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
2976ef27340cSAdrian Chadd 		    __func__);
2977ef27340cSAdrian Chadd 	}
2978904e385eSAdrian Chadd 
2979904e385eSAdrian Chadd 	/* disable interrupts */
2980904e385eSAdrian Chadd 	ath_hal_intrset(ah, 0);
2981904e385eSAdrian Chadd 
2982904e385eSAdrian Chadd 	/*
2983904e385eSAdrian Chadd 	 * Now, ensure that any in progress TX/RX completes before we
2984904e385eSAdrian Chadd 	 * continue.
2985904e385eSAdrian Chadd 	 */
2986904e385eSAdrian Chadd 	ath_txrx_stop_locked(sc);
2987904e385eSAdrian Chadd 
2988ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2989ef27340cSAdrian Chadd 
2990f52d3452SAdrian Chadd 	/*
2991ef27340cSAdrian Chadd 	 * Regardless of whether we're doing a no-loss flush or
2992ef27340cSAdrian Chadd 	 * not, stop the PCU and handle what's in the RX queue.
2993ef27340cSAdrian Chadd 	 * That way frames aren't dropped which shouldn't be.
2994ef27340cSAdrian Chadd 	 */
29959a842e8bSAdrian Chadd 	ath_stoprecv(sc, (reset_type != ATH_RESET_NOLOSS));
2996f8cc9b09SAdrian Chadd 	ath_rx_flush(sc);
2997ef27340cSAdrian Chadd 
2998062cf7d9SAdrian Chadd 	/*
2999062cf7d9SAdrian Chadd 	 * Should now wait for pending TX/RX to complete
3000f6b6084bSPedro F. Giffuni 	 * and block future ones from occurring. This needs to be
3001062cf7d9SAdrian Chadd 	 * done before the TX queue is drained.
3002062cf7d9SAdrian Chadd 	 */
3003062cf7d9SAdrian Chadd 	ath_draintxq(sc, reset_type);	/* stop xmit side */
3004062cf7d9SAdrian Chadd 
3005b032f27cSSam Leffler 	ath_settkipmic(sc);		/* configure TKIP MIC handling */
30065591b213SSam Leffler 	/* NB: indicate channel change so we do a full reset */
30076322256bSAdrian Chadd 	ath_update_chainmasks(sc, ic->ic_curchan);
30086322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
30096322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
3010f50e4ebfSAdrian Chadd 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_TRUE,
30118c01c3dcSAdrian Chadd 	    ah_reset_type, &status))
301276e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
301376e6fd5dSGleb Smirnoff 		    "%s: unable to reset hardware; hal status %u\n",
30145591b213SSam Leffler 		    __func__, status);
3015c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
301648237774SAdrian Chadd 
301717bb5fd1SAdrian Chadd 	ATH_RX_LOCK(sc);
301817bb5fd1SAdrian Chadd 	sc->sc_rx_stopped = 1;
301917bb5fd1SAdrian Chadd 	sc->sc_rx_resetted = 1;
302017bb5fd1SAdrian Chadd 	ATH_RX_UNLOCK(sc);
302117bb5fd1SAdrian Chadd 
30221410ca56SAdrian Chadd 	/* Quiet time handling - ensure we resync */
30231410ca56SAdrian Chadd 	ath_vap_clear_quiet_ie(sc);
30241410ca56SAdrian Chadd 
302548237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
302648237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
302748237774SAdrian Chadd 
30289af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
30299af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
30309af351f9SAdrian Chadd 
3031dd6a574eSAdrian Chadd 	/*
3032b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
3033b70f530bSAdrian Chadd 	 */
3034b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
3035b70f530bSAdrian Chadd 
3036b70f530bSAdrian Chadd 	/*
3037dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
3038dd6a574eSAdrian Chadd 	 * support it.
3039dd6a574eSAdrian Chadd 	 */
3040dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
3041dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
3042dd6a574eSAdrian Chadd 	else
3043dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
3044dd6a574eSAdrian Chadd 
304568e8e04eSSam Leffler 	if (ath_startrecv(sc) != 0)	/* restart recv */
304676e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
304776e6fd5dSGleb Smirnoff 		    "%s: unable to start recv logic\n", __func__);
3048c42a7b7eSSam Leffler 	/*
3049c42a7b7eSSam Leffler 	 * We may be doing a reset in response to an ioctl
3050c42a7b7eSSam Leffler 	 * that changes the channel so update any state that
3051c42a7b7eSSam Leffler 	 * might change as a result.
3052c42a7b7eSSam Leffler 	 */
3053724c193aSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
3054c89b957aSSam Leffler 	if (sc->sc_beacons) {		/* restart beacons */
3055584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
305610ad9a77SSam Leffler 		if (sc->sc_tdma)
305710ad9a77SSam Leffler 			ath_tdma_config(sc, NULL);
305810ad9a77SSam Leffler 		else
305910ad9a77SSam Leffler #endif
3060c89b957aSSam Leffler 			ath_beacon_config(sc, NULL);
306110ad9a77SSam Leffler 	}
3062c42a7b7eSSam Leffler 
3063ef27340cSAdrian Chadd 	/*
3064ef27340cSAdrian Chadd 	 * Release the reset lock and re-enable interrupts here.
3065ef27340cSAdrian Chadd 	 * If an interrupt was being processed in ath_intr(),
3066ef27340cSAdrian Chadd 	 * it would disable interrupts at this point. So we have
3067ef27340cSAdrian Chadd 	 * to atomically enable interrupts and decrement the
3068ef27340cSAdrian Chadd 	 * reset counter - this way ath_intr() doesn't end up
3069ef27340cSAdrian Chadd 	 * disabling interrupts without a corresponding enable
3070ef27340cSAdrian Chadd 	 * in the rest or channel change path.
3071f5c30c4eSAdrian Chadd 	 *
3072f5c30c4eSAdrian Chadd 	 * Grab the TX reference in case we need to transmit.
3073f5c30c4eSAdrian Chadd 	 * That way a parallel transmit doesn't.
3074ef27340cSAdrian Chadd 	 */
3075ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
3076ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
3077f5c30c4eSAdrian Chadd 	sc->sc_txstart_cnt++;
3078ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
3079ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
3080ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3081ef27340cSAdrian Chadd 
3082ef27340cSAdrian Chadd 	/*
3083ef27340cSAdrian Chadd 	 * TX and RX can be started here. If it were started with
3084ef27340cSAdrian Chadd 	 * sc_inreset_cnt > 0, the TX and RX path would abort.
3085ef27340cSAdrian Chadd 	 * Thus if this is a nested call through the reset or
3086ef27340cSAdrian Chadd 	 * channel change code, TX completion will occur but
3087ef27340cSAdrian Chadd 	 * RX completion and ath_start / ath_tx_start will not
3088ef27340cSAdrian Chadd 	 * run.
3089ef27340cSAdrian Chadd 	 */
3090ef27340cSAdrian Chadd 
3091ef27340cSAdrian Chadd 	/* Restart TX/RX as needed */
3092ef27340cSAdrian Chadd 	ath_txrx_start(sc);
3093ef27340cSAdrian Chadd 
3094f5c30c4eSAdrian Chadd 	/* XXX TODO: we need to hold the tx refcount here! */
3095f5c30c4eSAdrian Chadd 
3096375307d4SAdrian Chadd 	/* Restart TX completion and pending TX */
3097ef27340cSAdrian Chadd 	if (reset_type == ATH_RESET_NOLOSS) {
3098ef27340cSAdrian Chadd 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
3099ef27340cSAdrian Chadd 			if (ATH_TXQ_SETUP(sc, i)) {
3100b837332dSAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
3101ef27340cSAdrian Chadd 				ath_txq_restart_dma(sc, &sc->sc_txq[i]);
3102b837332dSAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
3103b837332dSAdrian Chadd 
3104b837332dSAdrian Chadd 				ATH_TX_LOCK(sc);
3105ef27340cSAdrian Chadd 				ath_txq_sched(sc, &sc->sc_txq[i]);
3106375307d4SAdrian Chadd 				ATH_TX_UNLOCK(sc);
3107ef27340cSAdrian Chadd 			}
3108b837332dSAdrian Chadd 		}
3109b837332dSAdrian Chadd 	}
3110ef27340cSAdrian Chadd 
3111f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3112f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3113f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3114f5c30c4eSAdrian Chadd 
3115f5c30c4eSAdrian Chadd 	ATH_PCU_LOCK(sc);
3116f5c30c4eSAdrian Chadd 	sc->sc_txstart_cnt--;
3117f5c30c4eSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3118f5c30c4eSAdrian Chadd 
3119ef27340cSAdrian Chadd 	/* Handle any frames in the TX queue */
3120ef27340cSAdrian Chadd 	/*
3121ef27340cSAdrian Chadd 	 * XXX should this be done by the caller, rather than
3122ef27340cSAdrian Chadd 	 * ath_reset() ?
3123ef27340cSAdrian Chadd 	 */
31248e739394SAdrian Chadd 	ath_tx_kick(sc);		/* restart xmit */
3125c42a7b7eSSam Leffler 	return 0;
31265591b213SSam Leffler }
31275591b213SSam Leffler 
312868e8e04eSSam Leffler static int
ath_reset_vap(struct ieee80211vap * vap,u_long cmd)3129b032f27cSSam Leffler ath_reset_vap(struct ieee80211vap *vap, u_long cmd)
3130b032f27cSSam Leffler {
31314b54a231SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
31323797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
31334b54a231SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
31344b54a231SSam Leffler 
31354b54a231SSam Leffler 	switch (cmd) {
31364b54a231SSam Leffler 	case IEEE80211_IOC_TXPOWER:
31374b54a231SSam Leffler 		/*
31384b54a231SSam Leffler 		 * If per-packet TPC is enabled, then we have nothing
31394b54a231SSam Leffler 		 * to do; otherwise we need to force the global limit.
31404b54a231SSam Leffler 		 * All this can happen directly; no need to reset.
31414b54a231SSam Leffler 		 */
31424b54a231SSam Leffler 		if (!ath_hal_gettpc(ah))
31434b54a231SSam Leffler 			ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
31444b54a231SSam Leffler 		return 0;
31454b54a231SSam Leffler 	}
3146517526efSAdrian Chadd 	/* XXX? Full or NOLOSS? */
31478c01c3dcSAdrian Chadd 	return ath_reset(sc, ATH_RESET_FULL, HAL_RESET_NORMAL);
3148b032f27cSSam Leffler }
3149b032f27cSSam Leffler 
3150b8e788a5SAdrian Chadd struct ath_buf *
_ath_getbuf_locked(struct ath_softc * sc,ath_buf_type_t btype)3151af33d486SAdrian Chadd _ath_getbuf_locked(struct ath_softc *sc, ath_buf_type_t btype)
315210ad9a77SSam Leffler {
315310ad9a77SSam Leffler 	struct ath_buf *bf;
315410ad9a77SSam Leffler 
315510ad9a77SSam Leffler 	ATH_TXBUF_LOCK_ASSERT(sc);
315610ad9a77SSam Leffler 
3157af33d486SAdrian Chadd 	if (btype == ATH_BUFTYPE_MGMT)
3158af33d486SAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_txbuf_mgmt);
3159af33d486SAdrian Chadd 	else
31606b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_txbuf);
3161af33d486SAdrian Chadd 
3162e346b073SAdrian Chadd 	if (bf == NULL) {
3163e346b073SAdrian Chadd 		sc->sc_stats.ast_tx_getnobuf++;
3164e346b073SAdrian Chadd 	} else {
3165e346b073SAdrian Chadd 		if (bf->bf_flags & ATH_BUF_BUSY) {
3166e346b073SAdrian Chadd 			sc->sc_stats.ast_tx_getbusybuf++;
3167e346b073SAdrian Chadd 			bf = NULL;
3168e346b073SAdrian Chadd 		}
3169e346b073SAdrian Chadd 	}
3170e346b073SAdrian Chadd 
3171af33d486SAdrian Chadd 	if (bf != NULL && (bf->bf_flags & ATH_BUF_BUSY) == 0) {
3172af33d486SAdrian Chadd 		if (btype == ATH_BUFTYPE_MGMT)
3173af33d486SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_txbuf_mgmt, bf, bf_list);
317423ced6c1SAdrian Chadd 		else {
3175af33d486SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
317623ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt--;
317723ced6c1SAdrian Chadd 
317823ced6c1SAdrian Chadd 			/*
317923ced6c1SAdrian Chadd 			 * This shuldn't happen; however just to be
318023ced6c1SAdrian Chadd 			 * safe print a warning and fudge the txbuf
318123ced6c1SAdrian Chadd 			 * count.
318223ced6c1SAdrian Chadd 			 */
318323ced6c1SAdrian Chadd 			if (sc->sc_txbuf_cnt < 0) {
318423ced6c1SAdrian Chadd 				device_printf(sc->sc_dev,
318523ced6c1SAdrian Chadd 				    "%s: sc_txbuf_cnt < 0?\n",
318623ced6c1SAdrian Chadd 				    __func__);
318723ced6c1SAdrian Chadd 				sc->sc_txbuf_cnt = 0;
318823ced6c1SAdrian Chadd 			}
318923ced6c1SAdrian Chadd 		}
3190af33d486SAdrian Chadd 	} else
319110ad9a77SSam Leffler 		bf = NULL;
3192e346b073SAdrian Chadd 
319310ad9a77SSam Leffler 	if (bf == NULL) {
3194af33d486SAdrian Chadd 		/* XXX should check which list, mgmt or otherwise */
319510ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: %s\n", __func__,
31966b349e5aSAdrian Chadd 		    TAILQ_FIRST(&sc->sc_txbuf) == NULL ?
319710ad9a77SSam Leffler 			"out of xmit buffers" : "xmit buffer busy");
3198e346b073SAdrian Chadd 		return NULL;
319910ad9a77SSam Leffler 	}
3200e346b073SAdrian Chadd 
3201af33d486SAdrian Chadd 	/* XXX TODO: should do this at buffer list initialisation */
3202af33d486SAdrian Chadd 	/* XXX (then, ensure the buffer has the right flag set) */
32033feffbd7SAdrian Chadd 	bf->bf_flags = 0;
3204af33d486SAdrian Chadd 	if (btype == ATH_BUFTYPE_MGMT)
3205af33d486SAdrian Chadd 		bf->bf_flags |= ATH_BUF_MGMT;
3206af33d486SAdrian Chadd 	else
3207af33d486SAdrian Chadd 		bf->bf_flags &= (~ATH_BUF_MGMT);
3208af33d486SAdrian Chadd 
3209e346b073SAdrian Chadd 	/* Valid bf here; clear some basic fields */
3210e346b073SAdrian Chadd 	bf->bf_next = NULL;	/* XXX just to be sure */
3211e346b073SAdrian Chadd 	bf->bf_last = NULL;	/* XXX again, just to be sure */
3212e346b073SAdrian Chadd 	bf->bf_comp = NULL;	/* XXX again, just to be sure */
3213e346b073SAdrian Chadd 	bzero(&bf->bf_state, sizeof(bf->bf_state));
3214e346b073SAdrian Chadd 
321585bf9bc3SAdrian Chadd 	/*
321685bf9bc3SAdrian Chadd 	 * Track the descriptor ID only if doing EDMA
321785bf9bc3SAdrian Chadd 	 */
321885bf9bc3SAdrian Chadd 	if (sc->sc_isedma) {
321985bf9bc3SAdrian Chadd 		bf->bf_descid = sc->sc_txbuf_descid;
322085bf9bc3SAdrian Chadd 		sc->sc_txbuf_descid++;
322185bf9bc3SAdrian Chadd 	}
322285bf9bc3SAdrian Chadd 
322310ad9a77SSam Leffler 	return bf;
322410ad9a77SSam Leffler }
322510ad9a77SSam Leffler 
3226e346b073SAdrian Chadd /*
3227e346b073SAdrian Chadd  * When retrying a software frame, buffers marked ATH_BUF_BUSY
3228e346b073SAdrian Chadd  * can't be thrown back on the queue as they could still be
3229e346b073SAdrian Chadd  * in use by the hardware.
3230e346b073SAdrian Chadd  *
3231e346b073SAdrian Chadd  * This duplicates the buffer, or returns NULL.
3232e346b073SAdrian Chadd  *
3233e346b073SAdrian Chadd  * The descriptor is also copied but the link pointers and
3234e346b073SAdrian Chadd  * the DMA segments aren't copied; this frame should thus
3235e346b073SAdrian Chadd  * be again passed through the descriptor setup/chain routines
3236e346b073SAdrian Chadd  * so the link is correct.
3237e346b073SAdrian Chadd  *
3238e346b073SAdrian Chadd  * The caller must free the buffer using ath_freebuf().
3239e346b073SAdrian Chadd  */
3240e346b073SAdrian Chadd struct ath_buf *
ath_buf_clone(struct ath_softc * sc,struct ath_buf * bf)32413f3a5dbdSAdrian Chadd ath_buf_clone(struct ath_softc *sc, struct ath_buf *bf)
3242e346b073SAdrian Chadd {
3243e346b073SAdrian Chadd 	struct ath_buf *tbf;
3244e346b073SAdrian Chadd 
3245af33d486SAdrian Chadd 	tbf = ath_getbuf(sc,
3246af33d486SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_MGMT) ?
3247af33d486SAdrian Chadd 	     ATH_BUFTYPE_MGMT : ATH_BUFTYPE_NORMAL);
3248e346b073SAdrian Chadd 	if (tbf == NULL)
3249e346b073SAdrian Chadd 		return NULL;	/* XXX failure? Why? */
3250e346b073SAdrian Chadd 
3251e346b073SAdrian Chadd 	/* Copy basics */
3252e346b073SAdrian Chadd 	tbf->bf_next = NULL;
3253e346b073SAdrian Chadd 	tbf->bf_nseg = bf->bf_nseg;
32543feffbd7SAdrian Chadd 	tbf->bf_flags = bf->bf_flags & ATH_BUF_FLAGS_CLONE;
3255e346b073SAdrian Chadd 	tbf->bf_status = bf->bf_status;
3256e346b073SAdrian Chadd 	tbf->bf_m = bf->bf_m;
3257e346b073SAdrian Chadd 	tbf->bf_node = bf->bf_node;
3258f5c30c4eSAdrian Chadd 	KASSERT((bf->bf_node != NULL), ("%s: bf_node=NULL!", __func__));
3259e346b073SAdrian Chadd 	/* will be setup by the chain/setup function */
3260e346b073SAdrian Chadd 	tbf->bf_lastds = NULL;
3261e346b073SAdrian Chadd 	/* for now, last == self */
3262e346b073SAdrian Chadd 	tbf->bf_last = tbf;
3263e346b073SAdrian Chadd 	tbf->bf_comp = bf->bf_comp;
3264e346b073SAdrian Chadd 
3265e346b073SAdrian Chadd 	/* NOTE: DMA segments will be setup by the setup/chain functions */
3266e346b073SAdrian Chadd 
3267e346b073SAdrian Chadd 	/* The caller has to re-init the descriptor + links */
3268e346b073SAdrian Chadd 
32693f3a5dbdSAdrian Chadd 	/*
32703f3a5dbdSAdrian Chadd 	 * Free the DMA mapping here, before we NULL the mbuf.
32713f3a5dbdSAdrian Chadd 	 * We must only call bus_dmamap_unload() once per mbuf chain
32723f3a5dbdSAdrian Chadd 	 * or behaviour is undefined.
32733f3a5dbdSAdrian Chadd 	 */
32743f3a5dbdSAdrian Chadd 	if (bf->bf_m != NULL) {
32753f3a5dbdSAdrian Chadd 		/*
32763f3a5dbdSAdrian Chadd 		 * XXX is this POSTWRITE call required?
32773f3a5dbdSAdrian Chadd 		 */
32783f3a5dbdSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
32793f3a5dbdSAdrian Chadd 		    BUS_DMASYNC_POSTWRITE);
32803f3a5dbdSAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
32813f3a5dbdSAdrian Chadd 	}
32823f3a5dbdSAdrian Chadd 
32833f3a5dbdSAdrian Chadd 	bf->bf_m = NULL;
32843f3a5dbdSAdrian Chadd 	bf->bf_node = NULL;
32853f3a5dbdSAdrian Chadd 
3286e346b073SAdrian Chadd 	/* Copy state */
3287e346b073SAdrian Chadd 	memcpy(&tbf->bf_state, &bf->bf_state, sizeof(bf->bf_state));
3288e346b073SAdrian Chadd 
3289e346b073SAdrian Chadd 	return tbf;
3290e346b073SAdrian Chadd }
3291e346b073SAdrian Chadd 
3292b8e788a5SAdrian Chadd struct ath_buf *
ath_getbuf(struct ath_softc * sc,ath_buf_type_t btype)3293af33d486SAdrian Chadd ath_getbuf(struct ath_softc *sc, ath_buf_type_t btype)
329410ad9a77SSam Leffler {
329510ad9a77SSam Leffler 	struct ath_buf *bf;
329610ad9a77SSam Leffler 
329710ad9a77SSam Leffler 	ATH_TXBUF_LOCK(sc);
3298af33d486SAdrian Chadd 	bf = _ath_getbuf_locked(sc, btype);
3299af33d486SAdrian Chadd 	/*
3300af33d486SAdrian Chadd 	 * If a mgmt buffer was requested but we're out of those,
3301af33d486SAdrian Chadd 	 * try requesting a normal one.
3302af33d486SAdrian Chadd 	 */
3303af33d486SAdrian Chadd 	if (bf == NULL && btype == ATH_BUFTYPE_MGMT)
3304af33d486SAdrian Chadd 		bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL);
3305e4e7938aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
330610ad9a77SSam Leffler 	if (bf == NULL) {
330710ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: stop queue\n", __func__);
330810ad9a77SSam Leffler 		sc->sc_stats.ast_tx_qstop++;
330910ad9a77SSam Leffler 	}
331010ad9a77SSam Leffler 	return bf;
331110ad9a77SSam Leffler }
331210ad9a77SSam Leffler 
33137dcb2beaSAdrian Chadd /*
3314cd7dffd0SAdrian Chadd  * Transmit a single frame.
3315cd7dffd0SAdrian Chadd  *
3316cd7dffd0SAdrian Chadd  * net80211 will free the node reference if the transmit
3317cd7dffd0SAdrian Chadd  * fails, so don't free the node reference here.
33187dcb2beaSAdrian Chadd  */
3319cd7dffd0SAdrian Chadd static int
ath_transmit(struct ieee80211com * ic,struct mbuf * m)33207a79cebfSGleb Smirnoff ath_transmit(struct ieee80211com *ic, struct mbuf *m)
3321cd7dffd0SAdrian Chadd {
33223797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
3323cd7dffd0SAdrian Chadd 	struct ieee80211_node *ni;
3324cd7dffd0SAdrian Chadd 	struct mbuf *next;
3325cd7dffd0SAdrian Chadd 	struct ath_buf *bf;
3326cd7dffd0SAdrian Chadd 	ath_bufhead frags;
3327cd7dffd0SAdrian Chadd 	int retval = 0;
3328cd7dffd0SAdrian Chadd 
3329cd7dffd0SAdrian Chadd 	/*
3330cd7dffd0SAdrian Chadd 	 * Tell the reset path that we're currently transmitting.
3331cd7dffd0SAdrian Chadd 	 */
3332cd7dffd0SAdrian Chadd 	ATH_PCU_LOCK(sc);
3333cd7dffd0SAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
333483bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
3335cd7dffd0SAdrian Chadd 		    "%s: sc_inreset_cnt > 0; bailing\n", __func__);
3336cd7dffd0SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
3337cd7dffd0SAdrian Chadd 		sc->sc_stats.ast_tx_qstop++;
3338cd7dffd0SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TX, 0, "ath_start_task: OACTIVE, finish");
3339cd7dffd0SAdrian Chadd 		return (ENOBUFS);	/* XXX should be EINVAL or? */
3340cd7dffd0SAdrian Chadd 	}
3341cd7dffd0SAdrian Chadd 	sc->sc_txstart_cnt++;
3342cd7dffd0SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3343cd7dffd0SAdrian Chadd 
3344f5c30c4eSAdrian Chadd 	/* Wake the hardware up already */
3345f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3346f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3347f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3348f5c30c4eSAdrian Chadd 
3349cd7dffd0SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_transmit: start");
3350cd7dffd0SAdrian Chadd 	/*
3351cd7dffd0SAdrian Chadd 	 * Grab the TX lock - it's ok to do this here; we haven't
3352cd7dffd0SAdrian Chadd 	 * yet started transmitting.
3353cd7dffd0SAdrian Chadd 	 */
3354cd7dffd0SAdrian Chadd 	ATH_TX_LOCK(sc);
3355cd7dffd0SAdrian Chadd 
3356cd7dffd0SAdrian Chadd 	/*
3357cd7dffd0SAdrian Chadd 	 * Node reference, if there's one.
3358cd7dffd0SAdrian Chadd 	 */
33597dcb2beaSAdrian Chadd 	ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
33607dcb2beaSAdrian Chadd 
33617dcb2beaSAdrian Chadd 	/*
33627dcb2beaSAdrian Chadd 	 * Enforce how deep a node queue can get.
33637dcb2beaSAdrian Chadd 	 *
33647dcb2beaSAdrian Chadd 	 * XXX it would be nicer if we kept an mbuf queue per
33657dcb2beaSAdrian Chadd 	 * node and only whacked them into ath_bufs when we
33667dcb2beaSAdrian Chadd 	 * are ready to schedule some traffic from them.
33677dcb2beaSAdrian Chadd 	 * .. that may come later.
33687dcb2beaSAdrian Chadd 	 *
33697dcb2beaSAdrian Chadd 	 * XXX we should also track the per-node hardware queue
33707dcb2beaSAdrian Chadd 	 * depth so it is easy to limit the _SUM_ of the swq and
33717dcb2beaSAdrian Chadd 	 * hwq frames.  Since we only schedule two HWQ frames
33727dcb2beaSAdrian Chadd 	 * at a time, this should be OK for now.
33737dcb2beaSAdrian Chadd 	 */
33747dcb2beaSAdrian Chadd 	if ((!(m->m_flags & M_EAPOL)) &&
33757dcb2beaSAdrian Chadd 	    (ATH_NODE(ni)->an_swq_depth > sc->sc_txq_node_maxdepth)) {
33767dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nodeq_overflow++;
3377cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
3378cd7dffd0SAdrian Chadd 		goto finish;
33797dcb2beaSAdrian Chadd 	}
33807dcb2beaSAdrian Chadd 
33817dcb2beaSAdrian Chadd 	/*
33827dcb2beaSAdrian Chadd 	 * Check how many TX buffers are available.
33837dcb2beaSAdrian Chadd 	 *
33847dcb2beaSAdrian Chadd 	 * If this is for non-EAPOL traffic, just leave some
33857dcb2beaSAdrian Chadd 	 * space free in order for buffer cloning and raw
33867dcb2beaSAdrian Chadd 	 * frame transmission to occur.
33877dcb2beaSAdrian Chadd 	 *
33887dcb2beaSAdrian Chadd 	 * If it's for EAPOL traffic, ignore this for now.
33897dcb2beaSAdrian Chadd 	 * Management traffic will be sent via the raw transmit
33907dcb2beaSAdrian Chadd 	 * method which bypasses this check.
33917dcb2beaSAdrian Chadd 	 *
33927dcb2beaSAdrian Chadd 	 * This is needed to ensure that EAPOL frames during
33937dcb2beaSAdrian Chadd 	 * (re) keying have a chance to go out.
33947dcb2beaSAdrian Chadd 	 *
33957dcb2beaSAdrian Chadd 	 * See kern/138379 for more information.
33967dcb2beaSAdrian Chadd 	 */
33977dcb2beaSAdrian Chadd 	if ((!(m->m_flags & M_EAPOL)) &&
33987dcb2beaSAdrian Chadd 	    (sc->sc_txbuf_cnt <= sc->sc_txq_data_minfree)) {
33997dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
3400cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
3401cd7dffd0SAdrian Chadd 		goto finish;
340223ced6c1SAdrian Chadd 	}
340323ced6c1SAdrian Chadd 
34045591b213SSam Leffler 	/*
34055591b213SSam Leffler 	 * Grab a TX buffer and associated resources.
34067dcb2beaSAdrian Chadd 	 *
34077dcb2beaSAdrian Chadd 	 * If it's an EAPOL frame, allocate a MGMT ath_buf.
34087dcb2beaSAdrian Chadd 	 * That way even with temporary buffer exhaustion due to
34097dcb2beaSAdrian Chadd 	 * the data path doesn't leave us without the ability
34107dcb2beaSAdrian Chadd 	 * to transmit management frames.
34117dcb2beaSAdrian Chadd 	 *
34127dcb2beaSAdrian Chadd 	 * Otherwise allocate a normal buffer.
34135591b213SSam Leffler 	 */
34147dcb2beaSAdrian Chadd 	if (m->m_flags & M_EAPOL)
34157dcb2beaSAdrian Chadd 		bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT);
34167dcb2beaSAdrian Chadd 	else
3417af33d486SAdrian Chadd 		bf = ath_getbuf(sc, ATH_BUFTYPE_NORMAL);
34181a85141aSAdrian Chadd 
34197dcb2beaSAdrian Chadd 	if (bf == NULL) {
34207dcb2beaSAdrian Chadd 		/*
3421cd7dffd0SAdrian Chadd 		 * If we failed to allocate a buffer, fail.
34227dcb2beaSAdrian Chadd 		 *
34237dcb2beaSAdrian Chadd 		 * We shouldn't fail normally, due to the check
34247dcb2beaSAdrian Chadd 		 * above.
34257dcb2beaSAdrian Chadd 		 */
34267dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
3427cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
3428cd7dffd0SAdrian Chadd 		goto finish;
3429b032f27cSSam Leffler 	}
34307dcb2beaSAdrian Chadd 
3431cd7dffd0SAdrian Chadd 	/*
3432cd7dffd0SAdrian Chadd 	 * At this point we have a buffer; so we need to free it
3433cd7dffd0SAdrian Chadd 	 * if we hit any error conditions.
3434cd7dffd0SAdrian Chadd 	 */
34357dcb2beaSAdrian Chadd 
343668e8e04eSSam Leffler 	/*
343768e8e04eSSam Leffler 	 * Check for fragmentation.  If this frame
343868e8e04eSSam Leffler 	 * has been broken up verify we have enough
343968e8e04eSSam Leffler 	 * buffers to send all the fragments so all
344068e8e04eSSam Leffler 	 * go out or none...
344168e8e04eSSam Leffler 	 */
34426b349e5aSAdrian Chadd 	TAILQ_INIT(&frags);
34431a85141aSAdrian Chadd 	if ((m->m_flags & M_FRAG) &&
34441a85141aSAdrian Chadd 	    !ath_txfrag_setup(sc, &frags, m, ni)) {
344568e8e04eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT,
344668e8e04eSSam Leffler 		    "%s: out of txfrag buffers\n", __func__);
344736c6be9aSSam Leffler 		sc->sc_stats.ast_tx_nofrag++;
34487a79cebfSGleb Smirnoff 		if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
34497a79cebfSGleb Smirnoff 		/*
34507a79cebfSGleb Smirnoff 		 * XXXGL: is mbuf valid after ath_txfrag_setup? If yes,
34517a79cebfSGleb Smirnoff 		 * we shouldn't free it but return back.
34527a79cebfSGleb Smirnoff 		 */
3453d07be335SAdrian Chadd 		ieee80211_free_mbuf(m);
34547a79cebfSGleb Smirnoff 		m = NULL;
345568e8e04eSSam Leffler 		goto bad;
345668e8e04eSSam Leffler 	}
3457cd7dffd0SAdrian Chadd 
3458cd7dffd0SAdrian Chadd 	/*
3459cd7dffd0SAdrian Chadd 	 * At this point if we have any TX fragments, then we will
3460cd7dffd0SAdrian Chadd 	 * have bumped the node reference once for each of those.
3461cd7dffd0SAdrian Chadd 	 */
3462cd7dffd0SAdrian Chadd 
3463cd7dffd0SAdrian Chadd 	/*
3464cd7dffd0SAdrian Chadd 	 * XXX Is there anything actually _enforcing_ that the
3465cd7dffd0SAdrian Chadd 	 * fragments are being transmitted in one hit, rather than
3466cd7dffd0SAdrian Chadd 	 * being interleaved with other transmissions on that
3467cd7dffd0SAdrian Chadd 	 * hardware queue?
3468cd7dffd0SAdrian Chadd 	 *
3469cd7dffd0SAdrian Chadd 	 * The ATH TX output lock is the only thing serialising this
3470cd7dffd0SAdrian Chadd 	 * right now.
3471cd7dffd0SAdrian Chadd 	 */
3472cd7dffd0SAdrian Chadd 
3473cd7dffd0SAdrian Chadd 	/*
3474cd7dffd0SAdrian Chadd 	 * Calculate the "next fragment" length field in ath_buf
3475cd7dffd0SAdrian Chadd 	 * in order to let the transmit path know enough about
3476cd7dffd0SAdrian Chadd 	 * what to next write to the hardware.
3477cd7dffd0SAdrian Chadd 	 */
3478cd7dffd0SAdrian Chadd 	if (m->m_flags & M_FRAG) {
3479cd7dffd0SAdrian Chadd 		struct ath_buf *fbf = bf;
3480cd7dffd0SAdrian Chadd 		struct ath_buf *n_fbf = NULL;
3481cd7dffd0SAdrian Chadd 		struct mbuf *fm = m->m_nextpkt;
3482cd7dffd0SAdrian Chadd 
3483cd7dffd0SAdrian Chadd 		/*
3484cd7dffd0SAdrian Chadd 		 * We need to walk the list of fragments and set
3485cd7dffd0SAdrian Chadd 		 * the next size to the following buffer.
3486cd7dffd0SAdrian Chadd 		 * However, the first buffer isn't in the frag
3487cd7dffd0SAdrian Chadd 		 * list, so we have to do some gymnastics here.
3488cd7dffd0SAdrian Chadd 		 */
3489cd7dffd0SAdrian Chadd 		TAILQ_FOREACH(n_fbf, &frags, bf_list) {
3490cd7dffd0SAdrian Chadd 			fbf->bf_nextfraglen = fm->m_pkthdr.len;
3491cd7dffd0SAdrian Chadd 			fbf = n_fbf;
3492cd7dffd0SAdrian Chadd 			fm = fm->m_nextpkt;
3493cd7dffd0SAdrian Chadd 		}
3494cd7dffd0SAdrian Chadd 	}
3495cd7dffd0SAdrian Chadd 
34961a85141aSAdrian Chadd nextfrag:
349768e8e04eSSam Leffler 	/*
34981a85141aSAdrian Chadd 	 * Pass the frame to the h/w for transmission.
34991a85141aSAdrian Chadd 	 * Fragmented frames have each frag chained together
35001a85141aSAdrian Chadd 	 * with m_nextpkt.  We know there are sufficient ath_buf's
35011a85141aSAdrian Chadd 	 * to send all the frags because of work done by
35021a85141aSAdrian Chadd 	 * ath_txfrag_setup.  We leave m_nextpkt set while
35031a85141aSAdrian Chadd 	 * calling ath_tx_start so it can use it to extend the
35041a85141aSAdrian Chadd 	 * the tx duration to cover the subsequent frag and
35051a85141aSAdrian Chadd 	 * so it can reclaim all the mbufs in case of an error;
35061a85141aSAdrian Chadd 	 * ath_tx_start clears m_nextpkt once it commits to
35071a85141aSAdrian Chadd 	 * handing the frame to the hardware.
3508cd7dffd0SAdrian Chadd 	 *
3509cd7dffd0SAdrian Chadd 	 * Note: if this fails, then the mbufs are freed but
3510cd7dffd0SAdrian Chadd 	 * not the node reference.
3511da4552abSAdrian Chadd 	 *
3512da4552abSAdrian Chadd 	 * So, we now have to free the node reference ourselves here
3513da4552abSAdrian Chadd 	 * and return OK up to the stack.
351468e8e04eSSam Leffler 	 */
35151a85141aSAdrian Chadd 	next = m->m_nextpkt;
35161a85141aSAdrian Chadd 	if (ath_tx_start(sc, ni, bf, m)) {
35175591b213SSam Leffler bad:
35187a79cebfSGleb Smirnoff 		if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
35191a85141aSAdrian Chadd reclaim:
352068e8e04eSSam Leffler 		bf->bf_m = NULL;
352168e8e04eSSam Leffler 		bf->bf_node = NULL;
3522c42a7b7eSSam Leffler 		ATH_TXBUF_LOCK(sc);
3523e1a50456SAdrian Chadd 		ath_returnbuf_head(sc, bf);
3524cd7dffd0SAdrian Chadd 		/*
3525cd7dffd0SAdrian Chadd 		 * Free the rest of the node references and
3526cd7dffd0SAdrian Chadd 		 * buffers for the fragment list.
3527cd7dffd0SAdrian Chadd 		 */
352868e8e04eSSam Leffler 		ath_txfrag_cleanup(sc, &frags, ni);
3529c42a7b7eSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
3530da4552abSAdrian Chadd 
3531da4552abSAdrian Chadd 		/*
3532da4552abSAdrian Chadd 		 * XXX: And free the node/return OK; ath_tx_start() may have
3533da4552abSAdrian Chadd 		 *      modified the buffer.  We currently have no way to
3534da4552abSAdrian Chadd 		 *      signify that the mbuf was freed but there was an error.
3535da4552abSAdrian Chadd 		 */
3536da4552abSAdrian Chadd 		ieee80211_free_node(ni);
3537da4552abSAdrian Chadd 		retval = 0;
3538cd7dffd0SAdrian Chadd 		goto finish;
35391a85141aSAdrian Chadd 	}
35401a85141aSAdrian Chadd 
3541548a605dSAdrian Chadd 	/*
3542548a605dSAdrian Chadd 	 * Check here if the node is in power save state.
3543548a605dSAdrian Chadd 	 */
3544548a605dSAdrian Chadd 	ath_tx_update_tim(sc, ni, 1);
3545548a605dSAdrian Chadd 
35461a85141aSAdrian Chadd 	if (next != NULL) {
354768e8e04eSSam Leffler 		/*
35481a85141aSAdrian Chadd 		 * Beware of state changing between frags.
35491a85141aSAdrian Chadd 		 * XXX check sta power-save state?
355068e8e04eSSam Leffler 		 */
35511a85141aSAdrian Chadd 		if (ni->ni_vap->iv_state != IEEE80211_S_RUN) {
3552c5239edbSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
35531a85141aSAdrian Chadd 			    "%s: flush fragmented packet, state %s\n",
35541a85141aSAdrian Chadd 			    __func__,
35551a85141aSAdrian Chadd 			    ieee80211_state_name[ni->ni_vap->iv_state]);
3556a91ab3c0SAdrian Chadd 			/* XXX dmamap */
3557d07be335SAdrian Chadd 			ieee80211_free_mbuf(next);
35581a85141aSAdrian Chadd 			goto reclaim;
3559c5239edbSAdrian Chadd 		}
35601a85141aSAdrian Chadd 		m = next;
35611a85141aSAdrian Chadd 		bf = TAILQ_FIRST(&frags);
35621a85141aSAdrian Chadd 		KASSERT(bf != NULL, ("no buf for txfrag"));
35631a85141aSAdrian Chadd 		TAILQ_REMOVE(&frags, bf, bf_list);
35641a85141aSAdrian Chadd 		goto nextfrag;
3565c5239edbSAdrian Chadd 	}
3566c5239edbSAdrian Chadd 
3567cd7dffd0SAdrian Chadd 	/*
3568cd7dffd0SAdrian Chadd 	 * Bump watchdog timer.
3569cd7dffd0SAdrian Chadd 	 */
35701a85141aSAdrian Chadd 	sc->sc_wd_timer = 5;
3571cd7dffd0SAdrian Chadd 
3572cd7dffd0SAdrian Chadd finish:
3573cd7dffd0SAdrian Chadd 	ATH_TX_UNLOCK(sc);
3574cd7dffd0SAdrian Chadd 
3575cd7dffd0SAdrian Chadd 	/*
3576cd7dffd0SAdrian Chadd 	 * Finished transmitting!
3577cd7dffd0SAdrian Chadd 	 */
3578cd7dffd0SAdrian Chadd 	ATH_PCU_LOCK(sc);
3579cd7dffd0SAdrian Chadd 	sc->sc_txstart_cnt--;
3580cd7dffd0SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3581cd7dffd0SAdrian Chadd 
3582f5c30c4eSAdrian Chadd 	/* Sleep the hardware if required */
3583f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3584f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3585f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3586f5c30c4eSAdrian Chadd 
3587cd7dffd0SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_transmit: finished");
3588cd7dffd0SAdrian Chadd 
3589cd7dffd0SAdrian Chadd 	return (retval);
35905591b213SSam Leffler }
3591cd7dffd0SAdrian Chadd 
3592c42a7b7eSSam Leffler /*
3593c42a7b7eSSam Leffler  * Block/unblock tx+rx processing while a key change is done.
3594c42a7b7eSSam Leffler  * We assume the caller serializes key management operations
3595c42a7b7eSSam Leffler  * so we only need to worry about synchronization with other
3596c42a7b7eSSam Leffler  * uses that originate in the driver.
3597c42a7b7eSSam Leffler  */
3598c42a7b7eSSam Leffler static void
ath_key_update_begin(struct ieee80211vap * vap)3599b032f27cSSam Leffler ath_key_update_begin(struct ieee80211vap *vap)
3600c42a7b7eSSam Leffler {
36013797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
3602c42a7b7eSSam Leffler 
3603c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
3604b032f27cSSam Leffler 	taskqueue_block(sc->sc_tq);
3605c42a7b7eSSam Leffler }
3606c42a7b7eSSam Leffler 
3607c42a7b7eSSam Leffler static void
ath_key_update_end(struct ieee80211vap * vap)3608b032f27cSSam Leffler ath_key_update_end(struct ieee80211vap *vap)
3609c42a7b7eSSam Leffler {
36103797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
3611c42a7b7eSSam Leffler 
3612c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
3613b032f27cSSam Leffler 	taskqueue_unblock(sc->sc_tq);
3614c42a7b7eSSam Leffler }
36155591b213SSam Leffler 
3616b032f27cSSam Leffler static void
ath_update_promisc(struct ieee80211com * ic)3617272f6adeSGleb Smirnoff ath_update_promisc(struct ieee80211com *ic)
3618b032f27cSSam Leffler {
3619272f6adeSGleb Smirnoff 	struct ath_softc *sc = ic->ic_softc;
3620b032f27cSSam Leffler 	u_int32_t rfilt;
3621b032f27cSSam Leffler 
3622b032f27cSSam Leffler 	/* configure rx filter */
3623f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3624f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3625b032f27cSSam Leffler 	rfilt = ath_calcrxfilter(sc);
3626b032f27cSSam Leffler 	ath_hal_setrxfilter(sc->sc_ah, rfilt);
3627f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3628f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3629b032f27cSSam Leffler 
3630b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x\n", __func__, rfilt);
3631b032f27cSSam Leffler }
3632b032f27cSSam Leffler 
363365451730SGleb Smirnoff static u_int
ath_hash_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)363465451730SGleb Smirnoff ath_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
363565451730SGleb Smirnoff {
363665451730SGleb Smirnoff 	uint32_t val, *mfilt = arg;
363765451730SGleb Smirnoff 	char *dl;
363865451730SGleb Smirnoff 	uint8_t pos;
363965451730SGleb Smirnoff 
364065451730SGleb Smirnoff 	/* calculate XOR of eight 6bit values */
364165451730SGleb Smirnoff 	dl = LLADDR(sdl);
364265451730SGleb Smirnoff 	val = le32dec(dl + 0);
364365451730SGleb Smirnoff 	pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
364465451730SGleb Smirnoff 	val = le32dec(dl + 3);
364565451730SGleb Smirnoff 	pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
364665451730SGleb Smirnoff 	pos &= 0x3f;
364765451730SGleb Smirnoff 	mfilt[pos / 32] |= (1 << (pos % 32));
364865451730SGleb Smirnoff 
364965451730SGleb Smirnoff 	return (1);
365065451730SGleb Smirnoff }
365165451730SGleb Smirnoff 
3652e5bd159eSAdrian Chadd /*
3653e5bd159eSAdrian Chadd  * Driver-internal mcast update call.
3654e5bd159eSAdrian Chadd  *
3655e5bd159eSAdrian Chadd  * Assumes the hardware is already awake.
3656e5bd159eSAdrian Chadd  */
3657b032f27cSSam Leffler static void
ath_update_mcast_hw(struct ath_softc * sc)3658e5bd159eSAdrian Chadd ath_update_mcast_hw(struct ath_softc *sc)
3659b032f27cSSam Leffler {
36607a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
3661b032f27cSSam Leffler 	u_int32_t mfilt[2];
3662b032f27cSSam Leffler 
3663b032f27cSSam Leffler 	/* calculate and install multicast filter */
36647a79cebfSGleb Smirnoff 	if (ic->ic_allmulti == 0) {
36657a79cebfSGleb Smirnoff 		struct ieee80211vap *vap;
36667a79cebfSGleb Smirnoff 
3667b032f27cSSam Leffler 		/*
3668b032f27cSSam Leffler 		 * Merge multicast addresses to form the hardware filter.
3669b032f27cSSam Leffler 		 */
3670b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = 0;
367165451730SGleb Smirnoff 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
367265451730SGleb Smirnoff 			if_foreach_llmaddr(vap->iv_ifp, ath_hash_maddr, &mfilt);
3673b032f27cSSam Leffler 	} else
3674b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = ~0;
3675e5bd159eSAdrian Chadd 
3676b032f27cSSam Leffler 	ath_hal_setmcastfilter(sc->sc_ah, mfilt[0], mfilt[1]);
3677e5bd159eSAdrian Chadd 
3678b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: MC filter %08x:%08x\n",
3679b032f27cSSam Leffler 		__func__, mfilt[0], mfilt[1]);
36804bc0e754SSam Leffler }
36814bc0e754SSam Leffler 
3682e5bd159eSAdrian Chadd /*
3683e5bd159eSAdrian Chadd  * Called from the net80211 layer - force the hardware
3684e5bd159eSAdrian Chadd  * awake before operating.
3685e5bd159eSAdrian Chadd  */
3686e5bd159eSAdrian Chadd static void
ath_update_mcast(struct ieee80211com * ic)3687272f6adeSGleb Smirnoff ath_update_mcast(struct ieee80211com *ic)
3688e5bd159eSAdrian Chadd {
3689272f6adeSGleb Smirnoff 	struct ath_softc *sc = ic->ic_softc;
3690e5bd159eSAdrian Chadd 
3691e5bd159eSAdrian Chadd 	ATH_LOCK(sc);
3692e5bd159eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3693e5bd159eSAdrian Chadd 	ATH_UNLOCK(sc);
3694e5bd159eSAdrian Chadd 
3695e5bd159eSAdrian Chadd 	ath_update_mcast_hw(sc);
3696e5bd159eSAdrian Chadd 
3697e5bd159eSAdrian Chadd 	ATH_LOCK(sc);
3698e5bd159eSAdrian Chadd 	ath_power_restore_power_state(sc);
3699e5bd159eSAdrian Chadd 	ATH_UNLOCK(sc);
3700e5bd159eSAdrian Chadd }
3701e5bd159eSAdrian Chadd 
3702e60c4fc2SAdrian Chadd void
ath_mode_init(struct ath_softc * sc)37035591b213SSam Leffler ath_mode_init(struct ath_softc *sc)
37045591b213SSam Leffler {
37057a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
3706b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3707b032f27cSSam Leffler 	u_int32_t rfilt;
37085591b213SSam Leffler 
370946be12aeSAdrian Chadd 	/* XXX power state? */
371046be12aeSAdrian Chadd 
37114bc0e754SSam Leffler 	/* configure rx filter */
371268e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
37134bc0e754SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
37144bc0e754SSam Leffler 
37155591b213SSam Leffler 	/* configure operational mode */
3716c42a7b7eSSam Leffler 	ath_hal_setopmode(ah);
3717c42a7b7eSSam Leffler 
371829aca940SSam Leffler 	/* handle any link-level address change */
37197a79cebfSGleb Smirnoff 	ath_hal_setmac(ah, ic->ic_macaddr);
37205591b213SSam Leffler 
37215591b213SSam Leffler 	/* calculate and install multicast filter */
3722e5bd159eSAdrian Chadd 	ath_update_mcast_hw(sc);
37235591b213SSam Leffler }
37245591b213SSam Leffler 
3725c42a7b7eSSam Leffler /*
3726c42a7b7eSSam Leffler  * Set the slot time based on the current setting.
3727c42a7b7eSSam Leffler  */
3728ba5c15d9SAdrian Chadd void
ath_setslottime(struct ath_softc * sc)3729c42a7b7eSSam Leffler ath_setslottime(struct ath_softc *sc)
3730c42a7b7eSSam Leffler {
37317a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
3732c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3733aaa70f2fSSam Leffler 	u_int usec;
3734c42a7b7eSSam Leffler 
3735aaa70f2fSSam Leffler 	if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan))
3736aaa70f2fSSam Leffler 		usec = 13;
3737aaa70f2fSSam Leffler 	else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan))
3738aaa70f2fSSam Leffler 		usec = 21;
3739724c193aSSam Leffler 	else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) {
3740724c193aSSam Leffler 		/* honor short/long slot time only in 11g */
3741724c193aSSam Leffler 		/* XXX shouldn't honor on pure g or turbo g channel */
3742724c193aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
3743aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_9;
3744aaa70f2fSSam Leffler 		else
3745aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_20;
3746724c193aSSam Leffler 	} else
3747724c193aSSam Leffler 		usec = HAL_SLOT_TIME_9;
3748aaa70f2fSSam Leffler 
3749aaa70f2fSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
3750aaa70f2fSSam Leffler 	    "%s: chan %u MHz flags 0x%x %s slot, %u usec\n",
3751aaa70f2fSSam Leffler 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
3752aaa70f2fSSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec);
3753aaa70f2fSSam Leffler 
3754f5c30c4eSAdrian Chadd 	/* Wake up the hardware first before updating the slot time */
3755f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
3756f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3757aaa70f2fSSam Leffler 	ath_hal_setslottime(ah, usec);
3758f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
3759c42a7b7eSSam Leffler 	sc->sc_updateslot = OK;
3760f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
3761c42a7b7eSSam Leffler }
3762c42a7b7eSSam Leffler 
3763c42a7b7eSSam Leffler /*
3764c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update the
3765c42a7b7eSSam Leffler  * slot time based on the current setting.
3766c42a7b7eSSam Leffler  */
3767c42a7b7eSSam Leffler static void
ath_updateslot(struct ieee80211com * ic)3768272f6adeSGleb Smirnoff ath_updateslot(struct ieee80211com *ic)
3769c42a7b7eSSam Leffler {
3770272f6adeSGleb Smirnoff 	struct ath_softc *sc = ic->ic_softc;
3771c42a7b7eSSam Leffler 
3772c42a7b7eSSam Leffler 	/*
3773c42a7b7eSSam Leffler 	 * When not coordinating the BSS, change the hardware
3774c42a7b7eSSam Leffler 	 * immediately.  For other operation we defer the change
3775c42a7b7eSSam Leffler 	 * until beacon updates have propagated to the stations.
3776f5c30c4eSAdrian Chadd 	 *
3777f5c30c4eSAdrian Chadd 	 * XXX sc_updateslot isn't changed behind a lock?
3778c42a7b7eSSam Leffler 	 */
377959aa14a9SRui Paulo 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
378059aa14a9SRui Paulo 	    ic->ic_opmode == IEEE80211_M_MBSS)
3781c42a7b7eSSam Leffler 		sc->sc_updateslot = UPDATE;
3782c42a7b7eSSam Leffler 	else
3783c42a7b7eSSam Leffler 		ath_setslottime(sc);
3784c42a7b7eSSam Leffler }
3785c42a7b7eSSam Leffler 
3786c42a7b7eSSam Leffler /*
3787622b3fd2SSam Leffler  * Append the contents of src to dst; both queues
3788622b3fd2SSam Leffler  * are assumed to be locked.
3789622b3fd2SSam Leffler  */
3790ba5c15d9SAdrian Chadd void
ath_txqmove(struct ath_txq * dst,struct ath_txq * src)3791622b3fd2SSam Leffler ath_txqmove(struct ath_txq *dst, struct ath_txq *src)
3792622b3fd2SSam Leffler {
3793e86fd7a7SAdrian Chadd 
3794b837332dSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(src);
3795b837332dSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(dst);
3796b837332dSAdrian Chadd 
37976b349e5aSAdrian Chadd 	TAILQ_CONCAT(&dst->axq_q, &src->axq_q, bf_list);
3798622b3fd2SSam Leffler 	dst->axq_link = src->axq_link;
3799622b3fd2SSam Leffler 	src->axq_link = NULL;
3800622b3fd2SSam Leffler 	dst->axq_depth += src->axq_depth;
38016edf1dc7SAdrian Chadd 	dst->axq_aggr_depth += src->axq_aggr_depth;
3802622b3fd2SSam Leffler 	src->axq_depth = 0;
38036edf1dc7SAdrian Chadd 	src->axq_aggr_depth = 0;
3804622b3fd2SSam Leffler }
3805622b3fd2SSam Leffler 
3806622b3fd2SSam Leffler /*
3807d52f7132SAdrian Chadd  * Reset the hardware, with no loss.
3808d52f7132SAdrian Chadd  *
3809d52f7132SAdrian Chadd  * This can't be used for a general case reset.
3810d52f7132SAdrian Chadd  */
3811d52f7132SAdrian Chadd static void
ath_reset_proc(void * arg,int pending)3812d52f7132SAdrian Chadd ath_reset_proc(void *arg, int pending)
3813d52f7132SAdrian Chadd {
3814d52f7132SAdrian Chadd 	struct ath_softc *sc = arg;
3815d52f7132SAdrian Chadd 
3816d52f7132SAdrian Chadd #if 0
381776e6fd5dSGleb Smirnoff 	device_printf(sc->sc_dev, "%s: resetting\n", __func__);
3818d52f7132SAdrian Chadd #endif
38198c01c3dcSAdrian Chadd 	ath_reset(sc, ATH_RESET_NOLOSS, HAL_RESET_FORCE_COLD);
3820d52f7132SAdrian Chadd }
3821d52f7132SAdrian Chadd 
3822d52f7132SAdrian Chadd /*
3823c42a7b7eSSam Leffler  * Reset the hardware after detecting beacons have stopped.
3824c42a7b7eSSam Leffler  */
3825c42a7b7eSSam Leffler static void
ath_bstuck_proc(void * arg,int pending)3826c42a7b7eSSam Leffler ath_bstuck_proc(void *arg, int pending)
3827c42a7b7eSSam Leffler {
3828c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
382916d4de92SAdrian Chadd 	uint32_t hangs = 0;
383016d4de92SAdrian Chadd 
383116d4de92SAdrian Chadd 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0)
383276e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "bb hang detected (0x%x)\n", hangs);
3833c42a7b7eSSam Leffler 
3834370f81faSAdrian Chadd #ifdef	ATH_DEBUG_ALQ
3835370f81faSAdrian Chadd 	if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_STUCK_BEACON))
3836370f81faSAdrian Chadd 		if_ath_alq_post(&sc->sc_alq, ATH_ALQ_STUCK_BEACON, 0, NULL);
3837370f81faSAdrian Chadd #endif
3838370f81faSAdrian Chadd 
383976e6fd5dSGleb Smirnoff 	device_printf(sc->sc_dev, "stuck beacon; resetting (bmiss count %u)\n",
3840c42a7b7eSSam Leffler 	    sc->sc_bmisscount);
3841c2e34459SSam Leffler 	sc->sc_stats.ast_bstuck++;
384216d4de92SAdrian Chadd 	/*
384316d4de92SAdrian Chadd 	 * This assumes that there's no simultaneous channel mode change
3844f6b6084bSPedro F. Giffuni 	 * occurring.
384516d4de92SAdrian Chadd 	 */
38468c01c3dcSAdrian Chadd 	ath_reset(sc, ATH_RESET_NOLOSS, HAL_RESET_FORCE_COLD);
3847c42a7b7eSSam Leffler }
3848c42a7b7eSSam Leffler 
3849c42a7b7eSSam Leffler static int
ath_desc_alloc(struct ath_softc * sc)38505591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc)
38515591b213SSam Leffler {
3852c42a7b7eSSam Leffler 	int error;
38535591b213SSam Leffler 
3854c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
385509067b6eSAdrian Chadd 		    "tx", sc->sc_tx_desclen, ath_txbuf, ATH_MAX_SCATTER);
3856c42a7b7eSSam Leffler 	if (error != 0) {
38575591b213SSam Leffler 		return error;
3858c42a7b7eSSam Leffler 	}
385923ced6c1SAdrian Chadd 	sc->sc_txbuf_cnt = ath_txbuf;
3860c42a7b7eSSam Leffler 
3861af33d486SAdrian Chadd 	error = ath_descdma_setup(sc, &sc->sc_txdma_mgmt, &sc->sc_txbuf_mgmt,
38621006fc0cSAdrian Chadd 		    "tx_mgmt", sc->sc_tx_desclen, ath_txbuf_mgmt,
38631006fc0cSAdrian Chadd 		    ATH_TXDESC);
3864af33d486SAdrian Chadd 	if (error != 0) {
3865af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3866af33d486SAdrian Chadd 		return error;
3867af33d486SAdrian Chadd 	}
3868af33d486SAdrian Chadd 
3869af33d486SAdrian Chadd 	/*
3870af33d486SAdrian Chadd 	 * XXX mark txbuf_mgmt frames with ATH_BUF_MGMT, so the
3871af33d486SAdrian Chadd 	 * flag doesn't have to be set in ath_getbuf_locked().
3872af33d486SAdrian Chadd 	 */
3873af33d486SAdrian Chadd 
3874c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
38751006fc0cSAdrian Chadd 			"beacon", sc->sc_tx_desclen, ATH_BCBUF, 1);
3876c42a7b7eSSam Leffler 	if (error != 0) {
3877af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3878af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
3879af33d486SAdrian Chadd 		    &sc->sc_txbuf_mgmt);
3880c42a7b7eSSam Leffler 		return error;
3881c42a7b7eSSam Leffler 	}
38825591b213SSam Leffler 	return 0;
38835591b213SSam Leffler }
38845591b213SSam Leffler 
38855591b213SSam Leffler static void
ath_desc_free(struct ath_softc * sc)38865591b213SSam Leffler ath_desc_free(struct ath_softc *sc)
38875591b213SSam Leffler {
38885591b213SSam Leffler 
3889c42a7b7eSSam Leffler 	if (sc->sc_bdma.dd_desc_len != 0)
3890c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
3891c42a7b7eSSam Leffler 	if (sc->sc_txdma.dd_desc_len != 0)
3892c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3893af33d486SAdrian Chadd 	if (sc->sc_txdma_mgmt.dd_desc_len != 0)
3894af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
3895af33d486SAdrian Chadd 		    &sc->sc_txbuf_mgmt);
38965591b213SSam Leffler }
38975591b213SSam Leffler 
38985591b213SSam Leffler static struct ieee80211_node *
ath_node_alloc(struct ieee80211vap * vap,const uint8_t mac[IEEE80211_ADDR_LEN])389938c208f8SSam Leffler ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
39005591b213SSam Leffler {
390138c208f8SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
39023797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
3903c42a7b7eSSam Leffler 	const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
3904c42a7b7eSSam Leffler 	struct ath_node *an;
3905c42a7b7eSSam Leffler 
3906c42a7b7eSSam Leffler 	an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
3907c42a7b7eSSam Leffler 	if (an == NULL) {
3908c42a7b7eSSam Leffler 		/* XXX stat+msg */
3909de5af704SSam Leffler 		return NULL;
39105591b213SSam Leffler 	}
3911c42a7b7eSSam Leffler 	ath_rate_node_init(sc, an);
39125591b213SSam Leffler 
39133dd85b26SAdrian Chadd 	/* Setup the mutex - there's no associd yet so set the name to NULL */
39143dd85b26SAdrian Chadd 	snprintf(an->an_name, sizeof(an->an_name), "%s: node %p",
39153dd85b26SAdrian Chadd 	    device_get_nameunit(sc->sc_dev), an);
39163dd85b26SAdrian Chadd 	mtx_init(&an->an_mtx, an->an_name, NULL, MTX_DEF);
39173dd85b26SAdrian Chadd 
3918eb6f0de0SAdrian Chadd 	/* XXX setup ath_tid */
3919eb6f0de0SAdrian Chadd 	ath_tx_tid_init(sc, an);
3920eb6f0de0SAdrian Chadd 
39217d450faaSAdrian Chadd 	an->an_node_stats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
39227d450faaSAdrian Chadd 	an->an_node_stats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
39237d450faaSAdrian Chadd 	an->an_node_stats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
39247d450faaSAdrian Chadd 
39259b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__, mac, ":", an);
3926c42a7b7eSSam Leffler 	return &an->an_node;
3927c42a7b7eSSam Leffler }
3928c42a7b7eSSam Leffler 
39295591b213SSam Leffler static void
ath_node_cleanup(struct ieee80211_node * ni)39304afa805eSAdrian Chadd ath_node_cleanup(struct ieee80211_node *ni)
39314afa805eSAdrian Chadd {
39324afa805eSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
39333797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
39344afa805eSAdrian Chadd 
39359b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__,
39369b48fb4bSAdrian Chadd 	    ni->ni_macaddr, ":", ATH_NODE(ni));
39379b48fb4bSAdrian Chadd 
39384afa805eSAdrian Chadd 	/* Cleanup ath_tid, free unused bufs, unlink bufs in TXQ */
3939eb6f0de0SAdrian Chadd 	ath_tx_node_flush(sc, ATH_NODE(ni));
39404afa805eSAdrian Chadd 	ath_rate_node_cleanup(sc, ATH_NODE(ni));
39414afa805eSAdrian Chadd 	sc->sc_node_cleanup(ni);
39424afa805eSAdrian Chadd }
39434afa805eSAdrian Chadd 
39444afa805eSAdrian Chadd static void
ath_node_free(struct ieee80211_node * ni)3945c42a7b7eSSam Leffler ath_node_free(struct ieee80211_node *ni)
39465591b213SSam Leffler {
3947c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
39483797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
39491e774079SSam Leffler 
39509b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__,
39519b48fb4bSAdrian Chadd 	    ni->ni_macaddr, ":", ATH_NODE(ni));
39523dd85b26SAdrian Chadd 	mtx_destroy(&ATH_NODE(ni)->an_mtx);
3953c42a7b7eSSam Leffler 	sc->sc_node_free(ni);
39545591b213SSam Leffler }
39555591b213SSam Leffler 
395668e8e04eSSam Leffler static void
ath_node_getsignal(const struct ieee80211_node * ni,int8_t * rssi,int8_t * noise)395768e8e04eSSam Leffler ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
395868e8e04eSSam Leffler {
395968e8e04eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
39603797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
396168e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
396268e8e04eSSam Leffler 
3963b032f27cSSam Leffler 	*rssi = ic->ic_node_getrssi(ni);
396459efa8b5SSam Leffler 	if (ni->ni_chan != IEEE80211_CHAN_ANYC)
396559efa8b5SSam Leffler 		*noise = ath_hal_getchannoise(ah, ni->ni_chan);
396659efa8b5SSam Leffler 	else
396768e8e04eSSam Leffler 		*noise = -95;		/* nominally correct */
396868e8e04eSSam Leffler }
396968e8e04eSSam Leffler 
3970c42a7b7eSSam Leffler /*
3971c42a7b7eSSam Leffler  * Set the default antenna.
3972c42a7b7eSSam Leffler  */
3973e60c4fc2SAdrian Chadd void
ath_setdefantenna(struct ath_softc * sc,u_int antenna)3974c42a7b7eSSam Leffler ath_setdefantenna(struct ath_softc *sc, u_int antenna)
3975c42a7b7eSSam Leffler {
3976c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3977c42a7b7eSSam Leffler 
3978c42a7b7eSSam Leffler 	/* XXX block beacon interrupts */
3979c42a7b7eSSam Leffler 	ath_hal_setdefantenna(ah, antenna);
3980c42a7b7eSSam Leffler 	if (sc->sc_defant != antenna)
3981c42a7b7eSSam Leffler 		sc->sc_stats.ast_ant_defswitch++;
3982c42a7b7eSSam Leffler 	sc->sc_defant = antenna;
3983c42a7b7eSSam Leffler 	sc->sc_rxotherant = 0;
3984c42a7b7eSSam Leffler }
3985c42a7b7eSSam Leffler 
39865463c4a4SSam Leffler static void
ath_txq_init(struct ath_softc * sc,struct ath_txq * txq,int qnum)3987622b3fd2SSam Leffler ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum)
3988622b3fd2SSam Leffler {
3989622b3fd2SSam Leffler 	txq->axq_qnum = qnum;
3990339ccfb3SSam Leffler 	txq->axq_ac = 0;
3991622b3fd2SSam Leffler 	txq->axq_depth = 0;
399216d4de92SAdrian Chadd 	txq->axq_aggr_depth = 0;
3993622b3fd2SSam Leffler 	txq->axq_intrcnt = 0;
3994622b3fd2SSam Leffler 	txq->axq_link = NULL;
39956b349e5aSAdrian Chadd 	txq->axq_softc = sc;
39966b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_q);
39976b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_tidq);
39983feffbd7SAdrian Chadd 	TAILQ_INIT(&txq->fifo.axq_q);
3999b837332dSAdrian Chadd 	ATH_TXQ_LOCK_INIT(sc, txq);
4000622b3fd2SSam Leffler }
4001622b3fd2SSam Leffler 
40025591b213SSam Leffler /*
4003c42a7b7eSSam Leffler  * Setup a h/w transmit queue.
40045591b213SSam Leffler  */
4005c42a7b7eSSam Leffler static struct ath_txq *
ath_txq_setup(struct ath_softc * sc,int qtype,int subtype)4006c42a7b7eSSam Leffler ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
4007c42a7b7eSSam Leffler {
4008c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4009c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
4010c42a7b7eSSam Leffler 	int qnum;
4011c42a7b7eSSam Leffler 
4012c42a7b7eSSam Leffler 	memset(&qi, 0, sizeof(qi));
4013c42a7b7eSSam Leffler 	qi.tqi_subtype = subtype;
4014c42a7b7eSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
4015c42a7b7eSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
4016c42a7b7eSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
4017c42a7b7eSSam Leffler 	/*
4018c42a7b7eSSam Leffler 	 * Enable interrupts only for EOL and DESC conditions.
4019c42a7b7eSSam Leffler 	 * We mark tx descriptors to receive a DESC interrupt
4020c42a7b7eSSam Leffler 	 * when a tx queue gets deep; otherwise waiting for the
4021c42a7b7eSSam Leffler 	 * EOL to reap descriptors.  Note that this is done to
4022c42a7b7eSSam Leffler 	 * reduce interrupt load and this only defers reaping
4023c42a7b7eSSam Leffler 	 * descriptors, never transmitting frames.  Aside from
4024c42a7b7eSSam Leffler 	 * reducing interrupts this also permits more concurrency.
4025c42a7b7eSSam Leffler 	 * The only potential downside is if the tx queue backs
4026c42a7b7eSSam Leffler 	 * up in which case the top half of the kernel may backup
4027c42a7b7eSSam Leffler 	 * due to a lack of tx descriptors.
4028c42a7b7eSSam Leffler 	 */
40296961e9edSAdrian Chadd 	if (sc->sc_isedma)
40306961e9edSAdrian Chadd 		qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE |
40316961e9edSAdrian Chadd 		    HAL_TXQ_TXOKINT_ENABLE;
40326961e9edSAdrian Chadd 	else
40336961e9edSAdrian Chadd 		qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE |
40346961e9edSAdrian Chadd 		    HAL_TXQ_TXDESCINT_ENABLE;
40356961e9edSAdrian Chadd 
4036c42a7b7eSSam Leffler 	qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
4037c42a7b7eSSam Leffler 	if (qnum == -1) {
4038c42a7b7eSSam Leffler 		/*
4039c42a7b7eSSam Leffler 		 * NB: don't print a message, this happens
4040a614e076SSam Leffler 		 * normally on parts with too few tx queues
4041c42a7b7eSSam Leffler 		 */
4042c42a7b7eSSam Leffler 		return NULL;
4043c42a7b7eSSam Leffler 	}
4044d6166defSAdrian Chadd 	if (qnum >= nitems(sc->sc_txq)) {
40456891c875SPeter Wemm 		device_printf(sc->sc_dev,
40466891c875SPeter Wemm 			"hal qnum %u out of range, max %zu!\n",
4047d6166defSAdrian Chadd 			qnum, nitems(sc->sc_txq));
4048c42a7b7eSSam Leffler 		ath_hal_releasetxqueue(ah, qnum);
4049c42a7b7eSSam Leffler 		return NULL;
4050c42a7b7eSSam Leffler 	}
4051c42a7b7eSSam Leffler 	if (!ATH_TXQ_SETUP(sc, qnum)) {
4052622b3fd2SSam Leffler 		ath_txq_init(sc, &sc->sc_txq[qnum], qnum);
4053c42a7b7eSSam Leffler 		sc->sc_txqsetup |= 1<<qnum;
4054c42a7b7eSSam Leffler 	}
4055c42a7b7eSSam Leffler 	return &sc->sc_txq[qnum];
4056c42a7b7eSSam Leffler }
4057c42a7b7eSSam Leffler 
4058c42a7b7eSSam Leffler /*
4059c42a7b7eSSam Leffler  * Setup a hardware data transmit queue for the specified
4060c42a7b7eSSam Leffler  * access control.  The hal may not support all requested
4061c42a7b7eSSam Leffler  * queues in which case it will return a reference to a
4062c42a7b7eSSam Leffler  * previously setup queue.  We record the mapping from ac's
4063c42a7b7eSSam Leffler  * to h/w queues for use by ath_tx_start and also track
4064c42a7b7eSSam Leffler  * the set of h/w queues being used to optimize work in the
4065c42a7b7eSSam Leffler  * transmit interrupt handler and related routines.
4066c42a7b7eSSam Leffler  */
4067c42a7b7eSSam Leffler static int
ath_tx_setup(struct ath_softc * sc,int ac,int haltype)4068c42a7b7eSSam Leffler ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
4069c42a7b7eSSam Leffler {
4070c42a7b7eSSam Leffler 	struct ath_txq *txq;
4071c42a7b7eSSam Leffler 
4072d6166defSAdrian Chadd 	if (ac >= nitems(sc->sc_ac2q)) {
40736891c875SPeter Wemm 		device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
4074d6166defSAdrian Chadd 			ac, nitems(sc->sc_ac2q));
4075c42a7b7eSSam Leffler 		return 0;
4076c42a7b7eSSam Leffler 	}
4077c42a7b7eSSam Leffler 	txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
4078c42a7b7eSSam Leffler 	if (txq != NULL) {
4079339ccfb3SSam Leffler 		txq->axq_ac = ac;
4080c42a7b7eSSam Leffler 		sc->sc_ac2q[ac] = txq;
4081c42a7b7eSSam Leffler 		return 1;
4082c42a7b7eSSam Leffler 	} else
4083c42a7b7eSSam Leffler 		return 0;
4084c42a7b7eSSam Leffler }
4085c42a7b7eSSam Leffler 
4086c42a7b7eSSam Leffler /*
4087c42a7b7eSSam Leffler  * Update WME parameters for a transmit queue.
4088c42a7b7eSSam Leffler  */
4089c42a7b7eSSam Leffler static int
ath_txq_update(struct ath_softc * sc,int ac)4090c42a7b7eSSam Leffler ath_txq_update(struct ath_softc *sc, int ac)
4091c42a7b7eSSam Leffler {
4092c42a7b7eSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<v)-1)
40937a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
4094c42a7b7eSSam Leffler 	struct ath_txq *txq = sc->sc_ac2q[ac];
4095565312d1SAdrian Chadd 	struct chanAccParams chp;
4096565312d1SAdrian Chadd 	struct wmeParams *wmep;
4097c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4098c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
4099c42a7b7eSSam Leffler 
4100565312d1SAdrian Chadd 	ieee80211_wme_ic_getparams(ic, &chp);
4101565312d1SAdrian Chadd 	wmep = &chp.cap_wmeParams[ac];
4102565312d1SAdrian Chadd 
4103c42a7b7eSSam Leffler 	ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
4104584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
410510ad9a77SSam Leffler 	if (sc->sc_tdma) {
410610ad9a77SSam Leffler 		/*
410710ad9a77SSam Leffler 		 * AIFS is zero so there's no pre-transmit wait.  The
410810ad9a77SSam Leffler 		 * burst time defines the slot duration and is configured
410909be6601SSam Leffler 		 * through net80211.  The QCU is setup to not do post-xmit
411010ad9a77SSam Leffler 		 * back off, lockout all lower-priority QCU's, and fire
411110ad9a77SSam Leffler 		 * off the DMA beacon alert timer which is setup based
411210ad9a77SSam Leffler 		 * on the slot configuration.
411310ad9a77SSam Leffler 		 */
411410ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
411510ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
411610ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
411710ad9a77SSam Leffler 			      | HAL_TXQ_TXEOLINT_ENABLE
411810ad9a77SSam Leffler 			      | HAL_TXQ_DBA_GATED
411910ad9a77SSam Leffler 			      | HAL_TXQ_BACKOFF_DISABLE
412010ad9a77SSam Leffler 			      | HAL_TXQ_ARB_LOCKOUT_GLOBAL
412110ad9a77SSam Leffler 			      ;
412210ad9a77SSam Leffler 		qi.tqi_aifs = 0;
412310ad9a77SSam Leffler 		/* XXX +dbaprep? */
412410ad9a77SSam Leffler 		qi.tqi_readyTime = sc->sc_tdmaslotlen;
412510ad9a77SSam Leffler 		qi.tqi_burstTime = qi.tqi_readyTime;
412610ad9a77SSam Leffler 	} else {
412710ad9a77SSam Leffler #endif
412816d4de92SAdrian Chadd 		/*
412916d4de92SAdrian Chadd 		 * XXX shouldn't this just use the default flags
413016d4de92SAdrian Chadd 		 * used in the previous queue setup?
413116d4de92SAdrian Chadd 		 */
413210ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
413310ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
413410ad9a77SSam Leffler 			      | HAL_TXQ_TXDESCINT_ENABLE
413510ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
41361f25c0f7SAdrian Chadd 			      | HAL_TXQ_TXEOLINT_ENABLE
413710ad9a77SSam Leffler 			      ;
4138c42a7b7eSSam Leffler 		qi.tqi_aifs = wmep->wmep_aifsn;
4139c42a7b7eSSam Leffler 		qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
4140c42a7b7eSSam Leffler 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
414110ad9a77SSam Leffler 		qi.tqi_readyTime = 0;
4142d6166defSAdrian Chadd 		qi.tqi_burstTime = IEEE80211_TXOP_TO_US(wmep->wmep_txopLimit);
4143584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
414410ad9a77SSam Leffler 	}
414510ad9a77SSam Leffler #endif
414610ad9a77SSam Leffler 
414710ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
414810ad9a77SSam Leffler 	    "%s: Q%u qflags 0x%x aifs %u cwmin %u cwmax %u burstTime %u\n",
414910ad9a77SSam Leffler 	    __func__, txq->axq_qnum, qi.tqi_qflags,
415010ad9a77SSam Leffler 	    qi.tqi_aifs, qi.tqi_cwmin, qi.tqi_cwmax, qi.tqi_burstTime);
4151c42a7b7eSSam Leffler 
4152c42a7b7eSSam Leffler 	if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
415376e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "unable to update hardware queue "
415476e6fd5dSGleb Smirnoff 		    "parameters for %s traffic!\n", ieee80211_wme_acnames[ac]);
4155c42a7b7eSSam Leffler 		return 0;
4156c42a7b7eSSam Leffler 	} else {
4157c42a7b7eSSam Leffler 		ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
4158c42a7b7eSSam Leffler 		return 1;
4159c42a7b7eSSam Leffler 	}
4160c42a7b7eSSam Leffler #undef ATH_EXPONENT_TO_VALUE
4161c42a7b7eSSam Leffler }
4162c42a7b7eSSam Leffler 
4163c42a7b7eSSam Leffler /*
4164c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update WME parameters.
4165c42a7b7eSSam Leffler  */
4166a35dae8dSAdrian Chadd int
ath_wme_update(struct ieee80211com * ic)4167c42a7b7eSSam Leffler ath_wme_update(struct ieee80211com *ic)
4168c42a7b7eSSam Leffler {
41693797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
4170c42a7b7eSSam Leffler 
4171c42a7b7eSSam Leffler 	return !ath_txq_update(sc, WME_AC_BE) ||
4172c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_BK) ||
4173c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VI) ||
4174c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
4175c42a7b7eSSam Leffler }
4176c42a7b7eSSam Leffler 
4177c42a7b7eSSam Leffler /*
4178c42a7b7eSSam Leffler  * Reclaim resources for a setup queue.
4179c42a7b7eSSam Leffler  */
4180c42a7b7eSSam Leffler static void
ath_tx_cleanupq(struct ath_softc * sc,struct ath_txq * txq)4181c42a7b7eSSam Leffler ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
4182c42a7b7eSSam Leffler {
4183c42a7b7eSSam Leffler 
4184c42a7b7eSSam Leffler 	ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
4185c42a7b7eSSam Leffler 	sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
4186b837332dSAdrian Chadd 	ATH_TXQ_LOCK_DESTROY(txq);
4187c42a7b7eSSam Leffler }
4188c42a7b7eSSam Leffler 
4189c42a7b7eSSam Leffler /*
4190c42a7b7eSSam Leffler  * Reclaim all tx queue resources.
4191c42a7b7eSSam Leffler  */
4192c42a7b7eSSam Leffler static void
ath_tx_cleanup(struct ath_softc * sc)4193c42a7b7eSSam Leffler ath_tx_cleanup(struct ath_softc *sc)
4194c42a7b7eSSam Leffler {
4195c42a7b7eSSam Leffler 	int i;
4196c42a7b7eSSam Leffler 
4197c42a7b7eSSam Leffler 	ATH_TXBUF_LOCK_DESTROY(sc);
4198c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4199c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
4200c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, &sc->sc_txq[i]);
4201c42a7b7eSSam Leffler }
42025591b213SSam Leffler 
420399d258fdSSam Leffler /*
4204ab06fdf2SSam Leffler  * Return h/w rate index for an IEEE rate (w/o basic rate bit)
4205ab06fdf2SSam Leffler  * using the current rates in sc_rixmap.
42068b5341deSSam Leffler  */
4207b8e788a5SAdrian Chadd int
ath_tx_findrix(const struct ath_softc * sc,uint8_t rate)4208ab06fdf2SSam Leffler ath_tx_findrix(const struct ath_softc *sc, uint8_t rate)
42098b5341deSSam Leffler {
4210ab06fdf2SSam Leffler 	int rix = sc->sc_rixmap[rate];
4211ab06fdf2SSam Leffler 	/* NB: return lowest rix for invalid rate */
4212ab06fdf2SSam Leffler 	return (rix == 0xff ? 0 : rix);
42138b5341deSSam Leffler }
42148b5341deSSam Leffler 
42159352fb7aSAdrian Chadd static void
ath_tx_update_stats(struct ath_softc * sc,struct ath_tx_status * ts,struct ath_buf * bf)42169352fb7aSAdrian Chadd ath_tx_update_stats(struct ath_softc *sc, struct ath_tx_status *ts,
42179352fb7aSAdrian Chadd     struct ath_buf *bf)
42189352fb7aSAdrian Chadd {
42199352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
42207a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
42219352fb7aSAdrian Chadd 	int sr, lr, pri;
42229352fb7aSAdrian Chadd 
42239352fb7aSAdrian Chadd 	if (ts->ts_status == 0) {
42249352fb7aSAdrian Chadd 		u_int8_t txant = ts->ts_antenna;
422551dfae38SAdrian Chadd 		/*
422651dfae38SAdrian Chadd 		 * Handle weird/corrupted tx antenna field
422751dfae38SAdrian Chadd 		 */
422851dfae38SAdrian Chadd 		if (txant >= ATH_IOCTL_STATS_NUM_TX_ANTENNA)
422951dfae38SAdrian Chadd 			txant = 0;
42309352fb7aSAdrian Chadd 		sc->sc_stats.ast_ant_tx[txant]++;
42319352fb7aSAdrian Chadd 		sc->sc_ant_tx[txant]++;
42329352fb7aSAdrian Chadd 		if (ts->ts_finaltsi != 0)
42339352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_altrate++;
42341410ca56SAdrian Chadd 
42351410ca56SAdrian Chadd 		/* XXX TODO: should do per-pri conuters */
42369352fb7aSAdrian Chadd 		pri = M_WME_GETAC(bf->bf_m);
42379352fb7aSAdrian Chadd 		if (pri >= WME_AC_VO)
42389352fb7aSAdrian Chadd 			ic->ic_wme.wme_hipri_traffic++;
42391410ca56SAdrian Chadd 
4240875a9451SAdrian Chadd 		if ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)
42419352fb7aSAdrian Chadd 			ni->ni_inact = ni->ni_inact_reload;
42429352fb7aSAdrian Chadd 	} else {
42439352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XRETRY)
42449352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xretries++;
42459352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FIFO)
42469352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_fifoerr++;
42479352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FILT)
42489352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_filtered++;
42499352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XTXOP)
42509352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xtxop++;
42519352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_TIMER_EXPIRED)
42529352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_timerexpired++;
42539352fb7aSAdrian Chadd 
42549352fb7aSAdrian Chadd 		if (bf->bf_m->m_flags & M_FF)
42559352fb7aSAdrian Chadd 			sc->sc_stats.ast_ff_txerr++;
42569352fb7aSAdrian Chadd 	}
42579352fb7aSAdrian Chadd 	/* XXX when is this valid? */
4258158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DESC_CFG_ERR)
42599352fb7aSAdrian Chadd 		sc->sc_stats.ast_tx_desccfgerr++;
4260158cb431SAdrian Chadd 	/*
4261158cb431SAdrian Chadd 	 * This can be valid for successful frame transmission!
4262158cb431SAdrian Chadd 	 * If there's a TX FIFO underrun during aggregate transmission,
4263158cb431SAdrian Chadd 	 * the MAC will pad the rest of the aggregate with delimiters.
4264158cb431SAdrian Chadd 	 * If a BA is returned, the frame is marked as "OK" and it's up
4265158cb431SAdrian Chadd 	 * to the TX completion code to notice which frames weren't
4266158cb431SAdrian Chadd 	 * successfully transmitted.
4267158cb431SAdrian Chadd 	 */
4268158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DATA_UNDERRUN)
4269158cb431SAdrian Chadd 		sc->sc_stats.ast_tx_data_underrun++;
4270158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DELIM_UNDERRUN)
4271158cb431SAdrian Chadd 		sc->sc_stats.ast_tx_delim_underrun++;
42729352fb7aSAdrian Chadd 
42739352fb7aSAdrian Chadd 	sr = ts->ts_shortretry;
42749352fb7aSAdrian Chadd 	lr = ts->ts_longretry;
42759352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_shortretry += sr;
42769352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_longretry += lr;
42779352fb7aSAdrian Chadd 
42789352fb7aSAdrian Chadd }
42799352fb7aSAdrian Chadd 
42809352fb7aSAdrian Chadd /*
42819352fb7aSAdrian Chadd  * The default completion. If fail is 1, this means
42829352fb7aSAdrian Chadd  * "please don't retry the frame, and just return -1 status
42839352fb7aSAdrian Chadd  * to the net80211 stack.
42849352fb7aSAdrian Chadd  */
42859352fb7aSAdrian Chadd void
ath_tx_default_comp(struct ath_softc * sc,struct ath_buf * bf,int fail)42869352fb7aSAdrian Chadd ath_tx_default_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
42879352fb7aSAdrian Chadd {
42889352fb7aSAdrian Chadd 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
42899352fb7aSAdrian Chadd 	int st;
42909352fb7aSAdrian Chadd 
42919352fb7aSAdrian Chadd 	if (fail == 1)
42929352fb7aSAdrian Chadd 		st = -1;
42939352fb7aSAdrian Chadd 	else
4294875a9451SAdrian Chadd 		st = ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) ?
42959352fb7aSAdrian Chadd 		    ts->ts_status : HAL_TXERR_XRETRY;
42969352fb7aSAdrian Chadd 
4297ce597531SAdrian Chadd #if 0
42989352fb7aSAdrian Chadd 	if (bf->bf_state.bfs_dobaw)
42999352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
4300a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: dobaw should've been cleared!\n",
4301a66d5089SAdrian Chadd 		    __func__,
4302a66d5089SAdrian Chadd 		    bf,
4303a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
4304ce597531SAdrian Chadd #endif
43059352fb7aSAdrian Chadd 	if (bf->bf_next != NULL)
43069352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
4307a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: bf_next not NULL!\n",
4308a66d5089SAdrian Chadd 		    __func__,
4309a66d5089SAdrian Chadd 		    bf,
4310a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
43119352fb7aSAdrian Chadd 
43129352fb7aSAdrian Chadd 	/*
4313548a605dSAdrian Chadd 	 * Check if the node software queue is empty; if so
4314548a605dSAdrian Chadd 	 * then clear the TIM.
4315548a605dSAdrian Chadd 	 *
4316548a605dSAdrian Chadd 	 * This needs to be done before the buffer is freed as
4317548a605dSAdrian Chadd 	 * otherwise the node reference will have been released
4318548a605dSAdrian Chadd 	 * and the node may not actually exist any longer.
4319548a605dSAdrian Chadd 	 *
4320548a605dSAdrian Chadd 	 * XXX I don't like this belonging here, but it's cleaner
4321548a605dSAdrian Chadd 	 * to do it here right now then all the other places
4322548a605dSAdrian Chadd 	 * where ath_tx_default_comp() is called.
4323548a605dSAdrian Chadd 	 *
4324548a605dSAdrian Chadd 	 * XXX TODO: during drain, ensure that the callback is
4325548a605dSAdrian Chadd 	 * being called so we get a chance to update the TIM.
4326548a605dSAdrian Chadd 	 */
43274bed2b67SAdrian Chadd 	if (bf->bf_node) {
43284bed2b67SAdrian Chadd 		ATH_TX_LOCK(sc);
4329548a605dSAdrian Chadd 		ath_tx_update_tim(sc, bf->bf_node, 0);
43304bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
43314bed2b67SAdrian Chadd 	}
4332548a605dSAdrian Chadd 
4333548a605dSAdrian Chadd 	/*
43349352fb7aSAdrian Chadd 	 * Do any tx complete callback.  Note this must
43359352fb7aSAdrian Chadd 	 * be done before releasing the node reference.
43369352fb7aSAdrian Chadd 	 * This will free the mbuf, release the net80211
43379352fb7aSAdrian Chadd 	 * node and recycle the ath_buf.
43389352fb7aSAdrian Chadd 	 */
43399352fb7aSAdrian Chadd 	ath_tx_freebuf(sc, bf, st);
43409352fb7aSAdrian Chadd }
43419352fb7aSAdrian Chadd 
43429352fb7aSAdrian Chadd /*
4343eb6f0de0SAdrian Chadd  * Update rate control with the given completion status.
4344eb6f0de0SAdrian Chadd  */
4345eb6f0de0SAdrian Chadd void
ath_tx_update_ratectrl(struct ath_softc * sc,struct ieee80211_node * ni,struct ath_rc_series * rc,struct ath_tx_status * ts,int frmlen,int rc_framelen,int nframes,int nbad)4346eb6f0de0SAdrian Chadd ath_tx_update_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
4347eb6f0de0SAdrian Chadd     struct ath_rc_series *rc, struct ath_tx_status *ts, int frmlen,
4348cce63444SAdrian Chadd     int rc_framelen, int nframes, int nbad)
4349eb6f0de0SAdrian Chadd {
4350eb6f0de0SAdrian Chadd 	struct ath_node *an;
4351eb6f0de0SAdrian Chadd 
4352eb6f0de0SAdrian Chadd 	/* Only for unicast frames */
4353eb6f0de0SAdrian Chadd 	if (ni == NULL)
4354eb6f0de0SAdrian Chadd 		return;
4355eb6f0de0SAdrian Chadd 
4356eb6f0de0SAdrian Chadd 	an = ATH_NODE(ni);
4357548a605dSAdrian Chadd 	ATH_NODE_UNLOCK_ASSERT(an);
4358eb6f0de0SAdrian Chadd 
435984f950a5SAdrian Chadd 	/*
436084f950a5SAdrian Chadd 	 * XXX TODO: teach the rate control about TXERR_FILT and
436184f950a5SAdrian Chadd 	 * see about handling it (eg see how many attempts were
436284f950a5SAdrian Chadd 	 * made before it got filtered and account for that.)
436384f950a5SAdrian Chadd 	 */
4364cce63444SAdrian Chadd 
4365eb6f0de0SAdrian Chadd 	if ((ts->ts_status & HAL_TXERR_FILT) == 0) {
4366eb6f0de0SAdrian Chadd 		ATH_NODE_LOCK(an);
4367cce63444SAdrian Chadd 		ath_rate_tx_complete(sc, an, rc, ts, frmlen, rc_framelen,
4368cce63444SAdrian Chadd 		    nframes, nbad);
4369eb6f0de0SAdrian Chadd 		ATH_NODE_UNLOCK(an);
4370eb6f0de0SAdrian Chadd 	}
4371eb6f0de0SAdrian Chadd }
4372eb6f0de0SAdrian Chadd 
4373eb6f0de0SAdrian Chadd /*
4374bad98824SAdrian Chadd  * Process the completion of the given buffer.
4375bad98824SAdrian Chadd  *
4376bad98824SAdrian Chadd  * This calls the rate control update and then the buffer completion.
4377bad98824SAdrian Chadd  * This will either free the buffer or requeue it.  In any case, the
4378bad98824SAdrian Chadd  * bf pointer should be treated as invalid after this function is called.
4379bad98824SAdrian Chadd  */
4380bad98824SAdrian Chadd void
ath_tx_process_buf_completion(struct ath_softc * sc,struct ath_txq * txq,struct ath_tx_status * ts,struct ath_buf * bf)4381bad98824SAdrian Chadd ath_tx_process_buf_completion(struct ath_softc *sc, struct ath_txq *txq,
4382bad98824SAdrian Chadd     struct ath_tx_status *ts, struct ath_buf *bf)
4383bad98824SAdrian Chadd {
4384bad98824SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
4385bad98824SAdrian Chadd 
4386375307d4SAdrian Chadd 	ATH_TX_UNLOCK_ASSERT(sc);
43875e018508SAdrian Chadd 	ATH_TXQ_UNLOCK_ASSERT(txq);
4388bad98824SAdrian Chadd 
4389bad98824SAdrian Chadd 	/* If unicast frame, update general statistics */
4390bad98824SAdrian Chadd 	if (ni != NULL) {
4391bad98824SAdrian Chadd 		/* update statistics */
4392bad98824SAdrian Chadd 		ath_tx_update_stats(sc, ts, bf);
4393bad98824SAdrian Chadd 	}
4394bad98824SAdrian Chadd 
4395bad98824SAdrian Chadd 	/*
4396bad98824SAdrian Chadd 	 * Call the completion handler.
4397bad98824SAdrian Chadd 	 * The completion handler is responsible for
4398bad98824SAdrian Chadd 	 * calling the rate control code.
4399bad98824SAdrian Chadd 	 *
4400bad98824SAdrian Chadd 	 * Frames with no completion handler get the
4401bad98824SAdrian Chadd 	 * rate control code called here.
4402bad98824SAdrian Chadd 	 */
4403bad98824SAdrian Chadd 	if (bf->bf_comp == NULL) {
4404bad98824SAdrian Chadd 		if ((ts->ts_status & HAL_TXERR_FILT) == 0 &&
4405bad98824SAdrian Chadd 		    (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) {
4406bad98824SAdrian Chadd 			/*
4407bad98824SAdrian Chadd 			 * XXX assume this isn't an aggregate
4408bad98824SAdrian Chadd 			 * frame.
440984f950a5SAdrian Chadd 			 *
441084f950a5SAdrian Chadd 			 * XXX TODO: also do this for filtered frames?
441184f950a5SAdrian Chadd 			 * Once rate control knows about them?
4412bad98824SAdrian Chadd 			 */
4413bad98824SAdrian Chadd 			ath_tx_update_ratectrl(sc, ni,
4414bad98824SAdrian Chadd 			     bf->bf_state.bfs_rc, ts,
4415cce63444SAdrian Chadd 			    bf->bf_state.bfs_pktlen,
4416cce63444SAdrian Chadd 			    bf->bf_state.bfs_pktlen,
4417cce63444SAdrian Chadd 			    1,
4418bad98824SAdrian Chadd 			    (ts->ts_status == 0 ? 0 : 1));
4419bad98824SAdrian Chadd 		}
4420bad98824SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4421bad98824SAdrian Chadd 	} else
4422bad98824SAdrian Chadd 		bf->bf_comp(sc, bf, 0);
4423bad98824SAdrian Chadd }
4424bad98824SAdrian Chadd 
4425bad98824SAdrian Chadd /*
4426c42a7b7eSSam Leffler  * Process completed xmit descriptors from the specified queue.
4427eb6f0de0SAdrian Chadd  * Kick the packet scheduler if needed. This can occur from this
4428eb6f0de0SAdrian Chadd  * particular task.
4429c42a7b7eSSam Leffler  */
4430788e6aa9SAdrian Chadd static int
ath_tx_processq(struct ath_softc * sc,struct ath_txq * txq,int dosched)4431788e6aa9SAdrian Chadd ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq, int dosched)
44325591b213SSam Leffler {
44335591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
44349352fb7aSAdrian Chadd 	struct ath_buf *bf;
44356edf1dc7SAdrian Chadd 	struct ath_desc *ds;
443665f9edeeSSam Leffler 	struct ath_tx_status *ts;
44375591b213SSam Leffler 	struct ieee80211_node *ni;
443853e98d5aSAdrian Chadd #ifdef	IEEE80211_SUPPORT_SUPERG
44397a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
444053e98d5aSAdrian Chadd #endif	/* IEEE80211_SUPPORT_SUPERG */
44419352fb7aSAdrian Chadd 	int nacked;
44425591b213SSam Leffler 	HAL_STATUS status;
44435591b213SSam Leffler 
4444c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
4445c42a7b7eSSam Leffler 		__func__, txq->axq_qnum,
4446c42a7b7eSSam Leffler 		(caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
4447c42a7b7eSSam Leffler 		txq->axq_link);
444803682514SAdrian Chadd 
444903682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 4,
445003682514SAdrian Chadd 	    "ath_tx_processq: txq=%u head %p link %p depth %p",
445103682514SAdrian Chadd 	    txq->axq_qnum,
445203682514SAdrian Chadd 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
445303682514SAdrian Chadd 	    txq->axq_link,
445403682514SAdrian Chadd 	    txq->axq_depth);
445503682514SAdrian Chadd 
4456d7736e13SSam Leffler 	nacked = 0;
44575591b213SSam Leffler 	for (;;) {
4458b837332dSAdrian Chadd 		ATH_TXQ_LOCK(txq);
4459c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;	/* reset periodic desc intr count */
44606b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&txq->axq_q);
44615591b213SSam Leffler 		if (bf == NULL) {
4462b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
44635591b213SSam Leffler 			break;
44645591b213SSam Leffler 		}
44656edf1dc7SAdrian Chadd 		ds = bf->bf_lastds;	/* XXX must be setup correctly! */
446665f9edeeSSam Leffler 		ts = &bf->bf_status.ds_txstat;
446703682514SAdrian Chadd 
446865f9edeeSSam Leffler 		status = ath_hal_txprocdesc(ah, ds, ts);
4469a585a9a1SSam Leffler #ifdef ATH_DEBUG
4470c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
44716902009eSSam Leffler 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
44726902009eSSam Leffler 			    status == HAL_OK);
447303682514SAdrian Chadd 		else if ((sc->sc_debug & ATH_DEBUG_RESET) && (dosched == 0))
4474d6b20023SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
4475d6b20023SAdrian Chadd 			    status == HAL_OK);
44765591b213SSam Leffler #endif
4477bb327d28SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
4478bb327d28SAdrian Chadd 		if (if_ath_alq_checkdebug(&sc->sc_alq,
4479bb327d28SAdrian Chadd 		    ATH_ALQ_EDMA_TXSTATUS)) {
4480bb327d28SAdrian Chadd 			if_ath_alq_post(&sc->sc_alq, ATH_ALQ_EDMA_TXSTATUS,
4481bb327d28SAdrian Chadd 			sc->sc_tx_statuslen,
4482bb327d28SAdrian Chadd 			(char *) ds);
4483bb327d28SAdrian Chadd 		}
4484bb327d28SAdrian Chadd #endif
448503682514SAdrian Chadd 
44865591b213SSam Leffler 		if (status == HAL_EINPROGRESS) {
448703682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_TXCOMP, 3,
448803682514SAdrian Chadd 			    "ath_tx_processq: txq=%u, bf=%p ds=%p, HAL_EINPROGRESS",
448903682514SAdrian Chadd 			    txq->axq_qnum, bf, ds);
4490b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
44915591b213SSam Leffler 			break;
44925591b213SSam Leffler 		}
44936b349e5aSAdrian Chadd 		ATH_TXQ_REMOVE(txq, bf, bf_list);
44945e018508SAdrian Chadd 
44955e018508SAdrian Chadd 		/*
44965e018508SAdrian Chadd 		 * Sanity check.
44975e018508SAdrian Chadd 		 */
44985e018508SAdrian Chadd 		if (txq->axq_qnum != bf->bf_state.bfs_tx_queue) {
44995e018508SAdrian Chadd 			device_printf(sc->sc_dev,
45005e018508SAdrian Chadd 			    "%s: TXQ=%d: bf=%p, bfs_tx_queue=%d\n",
45015e018508SAdrian Chadd 			    __func__,
45025e018508SAdrian Chadd 			    txq->axq_qnum,
45035e018508SAdrian Chadd 			    bf,
45045e018508SAdrian Chadd 			    bf->bf_state.bfs_tx_queue);
45055e018508SAdrian Chadd 		}
45065e018508SAdrian Chadd 		if (txq->axq_qnum != bf->bf_last->bf_state.bfs_tx_queue) {
45075e018508SAdrian Chadd 			device_printf(sc->sc_dev,
45085e018508SAdrian Chadd 			    "%s: TXQ=%d: bf_last=%p, bfs_tx_queue=%d\n",
45095e018508SAdrian Chadd 			    __func__,
45105e018508SAdrian Chadd 			    txq->axq_qnum,
45115e018508SAdrian Chadd 			    bf->bf_last,
45125e018508SAdrian Chadd 			    bf->bf_last->bf_state.bfs_tx_queue);
45135e018508SAdrian Chadd 		}
45145e018508SAdrian Chadd 
45155e018508SAdrian Chadd #if 0
4516d3731e4bSAdrian Chadd 		if (txq->axq_depth > 0) {
451710ad9a77SSam Leffler 			/*
4518d3731e4bSAdrian Chadd 			 * More frames follow.  Mark the buffer busy
4519d3731e4bSAdrian Chadd 			 * so it's not re-used while the hardware may
4520d3731e4bSAdrian Chadd 			 * still re-read the link field in the descriptor.
45216edf1dc7SAdrian Chadd 			 *
4522d3731e4bSAdrian Chadd 			 * Use the last buffer in an aggregate as that
4523d3731e4bSAdrian Chadd 			 * is where the hardware may be - intermediate
4524d3731e4bSAdrian Chadd 			 * descriptors won't be "busy".
452510ad9a77SSam Leffler 			 */
45266edf1dc7SAdrian Chadd 			bf->bf_last->bf_flags |= ATH_BUF_BUSY;
4527d3731e4bSAdrian Chadd 		} else
4528d3731e4bSAdrian Chadd 			txq->axq_link = NULL;
45295e018508SAdrian Chadd #else
45305e018508SAdrian Chadd 		bf->bf_last->bf_flags |= ATH_BUF_BUSY;
45315e018508SAdrian Chadd #endif
45326edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
45336edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
45345591b213SSam Leffler 
45355591b213SSam Leffler 		ni = bf->bf_node;
453603682514SAdrian Chadd 
453703682514SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TXCOMP, 5,
453803682514SAdrian Chadd 		    "ath_tx_processq: txq=%u, bf=%p, ds=%p, ni=%p, ts_status=0x%08x",
453903682514SAdrian Chadd 		    txq->axq_qnum, bf, ds, ni, ts->ts_status);
4540c42a7b7eSSam Leffler 		/*
45419352fb7aSAdrian Chadd 		 * If unicast frame was ack'd update RSSI,
454284784be1SSam Leffler 		 * including the last rx time used to
454384784be1SSam Leffler 		 * workaround phantom bmiss interrupts.
4544d7736e13SSam Leffler 		 */
45459352fb7aSAdrian Chadd 		if (ni != NULL && ts->ts_status == 0 &&
4546875a9451SAdrian Chadd 		    ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)) {
4547d7736e13SSam Leffler 			nacked++;
454884784be1SSam Leffler 			sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
454984784be1SSam Leffler 			ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi,
455084784be1SSam Leffler 				ts->ts_rssi);
45517d450faaSAdrian Chadd 			ATH_RSSI_LPF(ATH_NODE(ni)->an_node_stats.ns_avgtxrssi,
45527d450faaSAdrian Chadd 				ts->ts_rssi);
455384784be1SSam Leffler 		}
4554b837332dSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
45559352fb7aSAdrian Chadd 
4556bad98824SAdrian Chadd 		/*
4557bad98824SAdrian Chadd 		 * Update statistics and call completion
4558bad98824SAdrian Chadd 		 */
4559bad98824SAdrian Chadd 		ath_tx_process_buf_completion(sc, txq, ts, bf);
4560548a605dSAdrian Chadd 
4561548a605dSAdrian Chadd 		/* XXX at this point, bf and ni may be totally invalid */
45625591b213SSam Leffler 	}
4563339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
456468e8e04eSSam Leffler 	/*
456568e8e04eSSam Leffler 	 * Flush fast-frame staging queue when traffic slows.
456668e8e04eSSam Leffler 	 */
456768e8e04eSSam Leffler 	if (txq->axq_depth <= 1)
456804f19fd6SSam Leffler 		ieee80211_ff_flush(ic, txq->axq_ac);
4569339ccfb3SSam Leffler #endif
4570eb6f0de0SAdrian Chadd 
457121bca442SAdrian Chadd 	/* Kick the software TXQ scheduler */
4572eb6f0de0SAdrian Chadd 	if (dosched) {
4573a40880adSAdrian Chadd 		ATH_TX_LOCK(sc);
4574a40880adSAdrian Chadd 		ath_txq_sched(sc, txq);
4575a40880adSAdrian Chadd 		ATH_TX_UNLOCK(sc);
4576eb6f0de0SAdrian Chadd 	}
4577eb6f0de0SAdrian Chadd 
457803682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
457903682514SAdrian Chadd 	    "ath_tx_processq: txq=%u: done",
458003682514SAdrian Chadd 	    txq->axq_qnum);
458103682514SAdrian Chadd 
4582d7736e13SSam Leffler 	return nacked;
4583d7736e13SSam Leffler }
4584d7736e13SSam Leffler 
45858f939e79SAdrian Chadd #define	TXQACTIVE(t, q)		( (t) & (1 << (q)))
4586c42a7b7eSSam Leffler 
4587c42a7b7eSSam Leffler /*
4588c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4589c42a7b7eSSam Leffler  * for a single hardware transmit queue (e.g. 5210 and 5211).
4590c42a7b7eSSam Leffler  */
4591c42a7b7eSSam Leffler static void
ath_tx_proc_q0(void * arg,int npending)4592c42a7b7eSSam Leffler ath_tx_proc_q0(void *arg, int npending)
4593c42a7b7eSSam Leffler {
4594c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
45958f939e79SAdrian Chadd 	uint32_t txqs;
4596c42a7b7eSSam Leffler 
4597ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4598ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
45998f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
46008f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4601ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
46028f939e79SAdrian Chadd 
4603f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4604f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4605f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4606f5c30c4eSAdrian Chadd 
460703682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
460803682514SAdrian Chadd 	    "ath_tx_proc_q0: txqs=0x%08x", txqs);
460903682514SAdrian Chadd 
461096ff485dSAdrian Chadd 	if (TXQACTIVE(txqs, 0) && ath_tx_processq(sc, &sc->sc_txq[0], 1))
46118f939e79SAdrian Chadd 		/* XXX why is lastrx updated in tx code? */
4612d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
46138f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
461496ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
46152e986da5SSam Leffler 	sc->sc_wd_timer = 0;
46165591b213SSam Leffler 
46173e50ec2cSSam Leffler 	if (sc->sc_softled)
461846d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
46193e50ec2cSSam Leffler 
4620ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4621ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4622ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
46231a85141aSAdrian Chadd 
4624f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4625f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4626f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4627f5c30c4eSAdrian Chadd 
46281a85141aSAdrian Chadd 	ath_tx_kick(sc);
46295591b213SSam Leffler }
46305591b213SSam Leffler 
46315591b213SSam Leffler /*
4632c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4633c42a7b7eSSam Leffler  * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
46345591b213SSam Leffler  */
46355591b213SSam Leffler static void
ath_tx_proc_q0123(void * arg,int npending)4636c42a7b7eSSam Leffler ath_tx_proc_q0123(void *arg, int npending)
4637c42a7b7eSSam Leffler {
4638c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4639d7736e13SSam Leffler 	int nacked;
46408f939e79SAdrian Chadd 	uint32_t txqs;
46418f939e79SAdrian Chadd 
4642ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4643ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
46448f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
46458f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4646ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4647c42a7b7eSSam Leffler 
4648f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4649f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4650f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4651f5c30c4eSAdrian Chadd 
465203682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
465303682514SAdrian Chadd 	    "ath_tx_proc_q0123: txqs=0x%08x", txqs);
465403682514SAdrian Chadd 
4655c42a7b7eSSam Leffler 	/*
4656c42a7b7eSSam Leffler 	 * Process each active queue.
4657c42a7b7eSSam Leffler 	 */
4658d7736e13SSam Leffler 	nacked = 0;
46598f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 0))
466096ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[0], 1);
46618f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 1))
466296ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[1], 1);
46638f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 2))
466496ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[2], 1);
46658f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 3))
466696ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[3], 1);
46678f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
466896ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
4669d7736e13SSam Leffler 	if (nacked)
4670d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4671c42a7b7eSSam Leffler 
46722e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4673c42a7b7eSSam Leffler 
46743e50ec2cSSam Leffler 	if (sc->sc_softled)
467546d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
46763e50ec2cSSam Leffler 
4677ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4678ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4679ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
46801a85141aSAdrian Chadd 
4681f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4682f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4683f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4684f5c30c4eSAdrian Chadd 
46851a85141aSAdrian Chadd 	ath_tx_kick(sc);
4686c42a7b7eSSam Leffler }
4687c42a7b7eSSam Leffler 
4688c42a7b7eSSam Leffler /*
4689c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt.
4690c42a7b7eSSam Leffler  */
4691c42a7b7eSSam Leffler static void
ath_tx_proc(void * arg,int npending)4692c42a7b7eSSam Leffler ath_tx_proc(void *arg, int npending)
4693c42a7b7eSSam Leffler {
4694c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4695d7736e13SSam Leffler 	int i, nacked;
46968f939e79SAdrian Chadd 	uint32_t txqs;
46978f939e79SAdrian Chadd 
4698ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4699ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
47008f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
47018f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4702ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4703c42a7b7eSSam Leffler 
4704f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4705f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4706f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4707f5c30c4eSAdrian Chadd 
470803682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1, "ath_tx_proc: txqs=0x%08x", txqs);
470903682514SAdrian Chadd 
4710c42a7b7eSSam Leffler 	/*
4711c42a7b7eSSam Leffler 	 * Process each active queue.
4712c42a7b7eSSam Leffler 	 */
4713d7736e13SSam Leffler 	nacked = 0;
4714c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
47158f939e79SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i) && TXQACTIVE(txqs, i))
471696ff485dSAdrian Chadd 			nacked += ath_tx_processq(sc, &sc->sc_txq[i], 1);
4717d7736e13SSam Leffler 	if (nacked)
4718d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4719c42a7b7eSSam Leffler 
47202e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4721c42a7b7eSSam Leffler 
47223e50ec2cSSam Leffler 	if (sc->sc_softled)
472346d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
47243e50ec2cSSam Leffler 
4725ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4726ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4727ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
47281a85141aSAdrian Chadd 
4729f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4730f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4731f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4732f5c30c4eSAdrian Chadd 
47331a85141aSAdrian Chadd 	ath_tx_kick(sc);
4734c42a7b7eSSam Leffler }
473516d4de92SAdrian Chadd #undef	TXQACTIVE
4736c42a7b7eSSam Leffler 
47379352fb7aSAdrian Chadd /*
473803e9308fSAdrian Chadd  * Deferred processing of TXQ rescheduling.
473903e9308fSAdrian Chadd  */
474003e9308fSAdrian Chadd static void
ath_txq_sched_tasklet(void * arg,int npending)474103e9308fSAdrian Chadd ath_txq_sched_tasklet(void *arg, int npending)
474203e9308fSAdrian Chadd {
474303e9308fSAdrian Chadd 	struct ath_softc *sc = arg;
474403e9308fSAdrian Chadd 	int i;
474503e9308fSAdrian Chadd 
474603e9308fSAdrian Chadd 	/* XXX is skipping ok? */
474703e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
474803e9308fSAdrian Chadd #if 0
474903e9308fSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
475003e9308fSAdrian Chadd 		device_printf(sc->sc_dev,
475103e9308fSAdrian Chadd 		    "%s: sc_inreset_cnt > 0; skipping\n", __func__);
475203e9308fSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
475303e9308fSAdrian Chadd 		return;
475403e9308fSAdrian Chadd 	}
475503e9308fSAdrian Chadd #endif
475603e9308fSAdrian Chadd 	sc->sc_txproc_cnt++;
475703e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
475803e9308fSAdrian Chadd 
4759f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4760f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4761f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4762f5c30c4eSAdrian Chadd 
4763375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
476403e9308fSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
4765b5a9dfd5SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
476603e9308fSAdrian Chadd 			ath_txq_sched(sc, &sc->sc_txq[i]);
4767b5a9dfd5SAdrian Chadd 		}
476803e9308fSAdrian Chadd 	}
4769375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
477003e9308fSAdrian Chadd 
4771f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
4772f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
4773f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
4774f5c30c4eSAdrian Chadd 
477503e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
477603e9308fSAdrian Chadd 	sc->sc_txproc_cnt--;
477703e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
477803e9308fSAdrian Chadd }
477903e9308fSAdrian Chadd 
4780e1a50456SAdrian Chadd void
ath_returnbuf_tail(struct ath_softc * sc,struct ath_buf * bf)4781e1a50456SAdrian Chadd ath_returnbuf_tail(struct ath_softc *sc, struct ath_buf *bf)
4782e1a50456SAdrian Chadd {
4783e1a50456SAdrian Chadd 
4784e1a50456SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
4785e1a50456SAdrian Chadd 
4786af33d486SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_MGMT)
4787af33d486SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf_mgmt, bf, bf_list);
478823ced6c1SAdrian Chadd 	else {
4789e1a50456SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
479023ced6c1SAdrian Chadd 		sc->sc_txbuf_cnt++;
479123ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt > ath_txbuf) {
479223ced6c1SAdrian Chadd 			device_printf(sc->sc_dev,
479323ced6c1SAdrian Chadd 			    "%s: sc_txbuf_cnt > %d?\n",
479423ced6c1SAdrian Chadd 			    __func__,
479523ced6c1SAdrian Chadd 			    ath_txbuf);
479623ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt = ath_txbuf;
479723ced6c1SAdrian Chadd 		}
479823ced6c1SAdrian Chadd 	}
4799e1a50456SAdrian Chadd }
4800e1a50456SAdrian Chadd 
4801e1a50456SAdrian Chadd void
ath_returnbuf_head(struct ath_softc * sc,struct ath_buf * bf)4802e1a50456SAdrian Chadd ath_returnbuf_head(struct ath_softc *sc, struct ath_buf *bf)
4803e1a50456SAdrian Chadd {
4804e1a50456SAdrian Chadd 
4805e1a50456SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
4806e1a50456SAdrian Chadd 
4807af33d486SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_MGMT)
4808af33d486SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf_mgmt, bf, bf_list);
480923ced6c1SAdrian Chadd 	else {
4810e1a50456SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
481123ced6c1SAdrian Chadd 		sc->sc_txbuf_cnt++;
481223ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt > ATH_TXBUF) {
481323ced6c1SAdrian Chadd 			device_printf(sc->sc_dev,
481423ced6c1SAdrian Chadd 			    "%s: sc_txbuf_cnt > %d?\n",
481523ced6c1SAdrian Chadd 			    __func__,
481623ced6c1SAdrian Chadd 			    ATH_TXBUF);
481723ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt = ATH_TXBUF;
481823ced6c1SAdrian Chadd 		}
481923ced6c1SAdrian Chadd 	}
4820e1a50456SAdrian Chadd }
4821e1a50456SAdrian Chadd 
482203e9308fSAdrian Chadd /*
4823629ce218SAdrian Chadd  * Free the holding buffer if it exists
4824629ce218SAdrian Chadd  */
48253feffbd7SAdrian Chadd void
ath_txq_freeholdingbuf(struct ath_softc * sc,struct ath_txq * txq)4826629ce218SAdrian Chadd ath_txq_freeholdingbuf(struct ath_softc *sc, struct ath_txq *txq)
4827629ce218SAdrian Chadd {
48285e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK_ASSERT(sc);
48295e018508SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
4830629ce218SAdrian Chadd 
4831629ce218SAdrian Chadd 	if (txq->axq_holdingbf == NULL)
4832629ce218SAdrian Chadd 		return;
4833629ce218SAdrian Chadd 
4834629ce218SAdrian Chadd 	txq->axq_holdingbf->bf_flags &= ~ATH_BUF_BUSY;
48355e018508SAdrian Chadd 
48365e018508SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
4837629ce218SAdrian Chadd 	ath_returnbuf_tail(sc, txq->axq_holdingbf);
48385e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
48395e018508SAdrian Chadd 
4840629ce218SAdrian Chadd 	txq->axq_holdingbf = NULL;
4841629ce218SAdrian Chadd }
4842629ce218SAdrian Chadd 
4843629ce218SAdrian Chadd /*
4844629ce218SAdrian Chadd  * Add this buffer to the holding queue, freeing the previous
4845629ce218SAdrian Chadd  * one if it exists.
4846629ce218SAdrian Chadd  */
4847629ce218SAdrian Chadd static void
ath_txq_addholdingbuf(struct ath_softc * sc,struct ath_buf * bf)4848629ce218SAdrian Chadd ath_txq_addholdingbuf(struct ath_softc *sc, struct ath_buf *bf)
4849629ce218SAdrian Chadd {
4850629ce218SAdrian Chadd 	struct ath_txq *txq;
4851629ce218SAdrian Chadd 
48525e018508SAdrian Chadd 	txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue];
48535e018508SAdrian Chadd 
48545e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK_ASSERT(sc);
48555e018508SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
48565f2f0e61SAdrian Chadd 
4857629ce218SAdrian Chadd 	/* XXX assert ATH_BUF_BUSY is set */
4858629ce218SAdrian Chadd 
4859629ce218SAdrian Chadd 	/* XXX assert the tx queue is under the max number */
4860629ce218SAdrian Chadd 	if (bf->bf_state.bfs_tx_queue > HAL_NUM_TX_QUEUES) {
4861629ce218SAdrian Chadd 		device_printf(sc->sc_dev, "%s: bf=%p: invalid tx queue (%d)\n",
4862629ce218SAdrian Chadd 		    __func__,
4863629ce218SAdrian Chadd 		    bf,
4864629ce218SAdrian Chadd 		    bf->bf_state.bfs_tx_queue);
4865629ce218SAdrian Chadd 		bf->bf_flags &= ~ATH_BUF_BUSY;
4866629ce218SAdrian Chadd 		ath_returnbuf_tail(sc, bf);
4867629ce218SAdrian Chadd 		return;
4868629ce218SAdrian Chadd 	}
4869629ce218SAdrian Chadd 	ath_txq_freeholdingbuf(sc, txq);
4870629ce218SAdrian Chadd 	txq->axq_holdingbf = bf;
4871629ce218SAdrian Chadd }
4872629ce218SAdrian Chadd 
4873629ce218SAdrian Chadd /*
48749352fb7aSAdrian Chadd  * Return a buffer to the pool and update the 'busy' flag on the
48759352fb7aSAdrian Chadd  * previous 'tail' entry.
48769352fb7aSAdrian Chadd  *
48779352fb7aSAdrian Chadd  * This _must_ only be called when the buffer is involved in a completed
48789352fb7aSAdrian Chadd  * TX. The logic is that if it was part of an active TX, the previous
48799352fb7aSAdrian Chadd  * buffer on the list is now not involved in a halted TX DMA queue, waiting
48809352fb7aSAdrian Chadd  * for restart (eg for TDMA.)
48819352fb7aSAdrian Chadd  *
48829352fb7aSAdrian Chadd  * The caller must free the mbuf and recycle the node reference.
48835e018508SAdrian Chadd  *
48845e018508SAdrian Chadd  * XXX This method of handling busy / holding buffers is insanely stupid.
48855e018508SAdrian Chadd  * It requires bf_state.bfs_tx_queue to be correctly assigned.  It would
48865e018508SAdrian Chadd  * be much nicer if buffers in the processq() methods would instead be
48875e018508SAdrian Chadd  * always completed there (pushed onto a txq or ath_bufhead) so we knew
48885e018508SAdrian Chadd  * exactly what hardware queue they came from in the first place.
48899352fb7aSAdrian Chadd  */
48909352fb7aSAdrian Chadd void
ath_freebuf(struct ath_softc * sc,struct ath_buf * bf)48919352fb7aSAdrian Chadd ath_freebuf(struct ath_softc *sc, struct ath_buf *bf)
48929352fb7aSAdrian Chadd {
48935e018508SAdrian Chadd 	struct ath_txq *txq;
48945e018508SAdrian Chadd 
48955e018508SAdrian Chadd 	txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue];
48965e018508SAdrian Chadd 
48979352fb7aSAdrian Chadd 	KASSERT((bf->bf_node == NULL), ("%s: bf->bf_node != NULL\n", __func__));
48989352fb7aSAdrian Chadd 	KASSERT((bf->bf_m == NULL), ("%s: bf->bf_m != NULL\n", __func__));
48999352fb7aSAdrian Chadd 
4900629ce218SAdrian Chadd 	/*
49015e018508SAdrian Chadd 	 * If this buffer is busy, push it onto the holding queue.
4902629ce218SAdrian Chadd 	 */
4903629ce218SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_BUSY) {
49045e018508SAdrian Chadd 		ATH_TXQ_LOCK(txq);
4905629ce218SAdrian Chadd 		ath_txq_addholdingbuf(sc, bf);
49065e018508SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
4907629ce218SAdrian Chadd 		return;
4908629ce218SAdrian Chadd 	}
4909629ce218SAdrian Chadd 
4910629ce218SAdrian Chadd 	/*
4911629ce218SAdrian Chadd 	 * Not a busy buffer, so free normally
4912629ce218SAdrian Chadd 	 */
49139352fb7aSAdrian Chadd 	ATH_TXBUF_LOCK(sc);
4914e1a50456SAdrian Chadd 	ath_returnbuf_tail(sc, bf);
49159352fb7aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
49169352fb7aSAdrian Chadd }
49179352fb7aSAdrian Chadd 
49189352fb7aSAdrian Chadd /*
49199352fb7aSAdrian Chadd  * This is currently used by ath_tx_draintxq() and
49209352fb7aSAdrian Chadd  * ath_tx_tid_free_pkts().
49219352fb7aSAdrian Chadd  *
49229352fb7aSAdrian Chadd  * It recycles a single ath_buf.
49239352fb7aSAdrian Chadd  */
49249352fb7aSAdrian Chadd void
ath_tx_freebuf(struct ath_softc * sc,struct ath_buf * bf,int status)49259352fb7aSAdrian Chadd ath_tx_freebuf(struct ath_softc *sc, struct ath_buf *bf, int status)
49269352fb7aSAdrian Chadd {
49279352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
49289352fb7aSAdrian Chadd 	struct mbuf *m0 = bf->bf_m;
49299352fb7aSAdrian Chadd 
49303f3a5dbdSAdrian Chadd 	/*
49313f3a5dbdSAdrian Chadd 	 * Make sure that we only sync/unload if there's an mbuf.
49323f3a5dbdSAdrian Chadd 	 * If not (eg we cloned a buffer), the unload will have already
4933f6b6084bSPedro F. Giffuni 	 * occurred.
49343f3a5dbdSAdrian Chadd 	 */
49353f3a5dbdSAdrian Chadd 	if (bf->bf_m != NULL) {
49363f3a5dbdSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
49373f3a5dbdSAdrian Chadd 		    BUS_DMASYNC_POSTWRITE);
49383f3a5dbdSAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
49393f3a5dbdSAdrian Chadd 	}
49403f3a5dbdSAdrian Chadd 
49419352fb7aSAdrian Chadd 	bf->bf_node = NULL;
49429352fb7aSAdrian Chadd 	bf->bf_m = NULL;
49439352fb7aSAdrian Chadd 
49449352fb7aSAdrian Chadd 	/* Free the buffer, it's not needed any longer */
49459352fb7aSAdrian Chadd 	ath_freebuf(sc, bf);
49469352fb7aSAdrian Chadd 
4947e95f3424SAdrian Chadd 	/* Pass the buffer back to net80211 - completing it */
4948e95f3424SAdrian Chadd 	ieee80211_tx_complete(ni, m0, status);
49499352fb7aSAdrian Chadd }
49509352fb7aSAdrian Chadd 
49513feffbd7SAdrian Chadd static struct ath_buf *
ath_tx_draintxq_get_one(struct ath_softc * sc,struct ath_txq * txq)49523feffbd7SAdrian Chadd ath_tx_draintxq_get_one(struct ath_softc *sc, struct ath_txq *txq)
49533feffbd7SAdrian Chadd {
49543feffbd7SAdrian Chadd 	struct ath_buf *bf;
49553feffbd7SAdrian Chadd 
49563feffbd7SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
49573feffbd7SAdrian Chadd 
49583feffbd7SAdrian Chadd 	/*
49593feffbd7SAdrian Chadd 	 * Drain the FIFO queue first, then if it's
49603feffbd7SAdrian Chadd 	 * empty, move to the normal frame queue.
49613feffbd7SAdrian Chadd 	 */
49623feffbd7SAdrian Chadd 	bf = TAILQ_FIRST(&txq->fifo.axq_q);
49633feffbd7SAdrian Chadd 	if (bf != NULL) {
49643feffbd7SAdrian Chadd 		/*
49653feffbd7SAdrian Chadd 		 * Is it the last buffer in this set?
49663feffbd7SAdrian Chadd 		 * Decrement the FIFO counter.
49673feffbd7SAdrian Chadd 		 */
49683feffbd7SAdrian Chadd 		if (bf->bf_flags & ATH_BUF_FIFOEND) {
49693feffbd7SAdrian Chadd 			if (txq->axq_fifo_depth == 0) {
49703feffbd7SAdrian Chadd 				device_printf(sc->sc_dev,
49713feffbd7SAdrian Chadd 				    "%s: Q%d: fifo_depth=0, fifo.axq_depth=%d?\n",
49723feffbd7SAdrian Chadd 				    __func__,
49733feffbd7SAdrian Chadd 				    txq->axq_qnum,
49743feffbd7SAdrian Chadd 				    txq->fifo.axq_depth);
49753feffbd7SAdrian Chadd 			} else
49763feffbd7SAdrian Chadd 				txq->axq_fifo_depth--;
49773feffbd7SAdrian Chadd 		}
49783feffbd7SAdrian Chadd 		ATH_TXQ_REMOVE(&txq->fifo, bf, bf_list);
49793feffbd7SAdrian Chadd 		return (bf);
49803feffbd7SAdrian Chadd 	}
49813feffbd7SAdrian Chadd 
49823feffbd7SAdrian Chadd 	/*
49833feffbd7SAdrian Chadd 	 * Debugging!
49843feffbd7SAdrian Chadd 	 */
49853feffbd7SAdrian Chadd 	if (txq->axq_fifo_depth != 0 || txq->fifo.axq_depth != 0) {
49863feffbd7SAdrian Chadd 		device_printf(sc->sc_dev,
49873feffbd7SAdrian Chadd 		    "%s: Q%d: fifo_depth=%d, fifo.axq_depth=%d\n",
49883feffbd7SAdrian Chadd 		    __func__,
49893feffbd7SAdrian Chadd 		    txq->axq_qnum,
49903feffbd7SAdrian Chadd 		    txq->axq_fifo_depth,
49913feffbd7SAdrian Chadd 		    txq->fifo.axq_depth);
49923feffbd7SAdrian Chadd 	}
49933feffbd7SAdrian Chadd 
49943feffbd7SAdrian Chadd 	/*
49953feffbd7SAdrian Chadd 	 * Now drain the pending queue.
49963feffbd7SAdrian Chadd 	 */
49973feffbd7SAdrian Chadd 	bf = TAILQ_FIRST(&txq->axq_q);
49983feffbd7SAdrian Chadd 	if (bf == NULL) {
49993feffbd7SAdrian Chadd 		txq->axq_link = NULL;
50003feffbd7SAdrian Chadd 		return (NULL);
50013feffbd7SAdrian Chadd 	}
50023feffbd7SAdrian Chadd 	ATH_TXQ_REMOVE(txq, bf, bf_list);
50033feffbd7SAdrian Chadd 	return (bf);
50043feffbd7SAdrian Chadd }
50053feffbd7SAdrian Chadd 
50069352fb7aSAdrian Chadd void
ath_tx_draintxq(struct ath_softc * sc,struct ath_txq * txq)50071762ec94SAdrian Chadd ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
50085591b213SSam Leffler {
5009a585a9a1SSam Leffler #ifdef ATH_DEBUG
50105591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5011d2f6ed15SSam Leffler #endif
50125591b213SSam Leffler 	struct ath_buf *bf;
50137a4c5ed9SSam Leffler 	u_int ix;
50145591b213SSam Leffler 
5015c42a7b7eSSam Leffler 	/*
5016c42a7b7eSSam Leffler 	 * NB: this assumes output has been stopped and
50175d61b5e8SSam Leffler 	 *     we do not need to block ath_tx_proc
5018c42a7b7eSSam Leffler 	 */
50197a4c5ed9SSam Leffler 	for (ix = 0;; ix++) {
5020b837332dSAdrian Chadd 		ATH_TXQ_LOCK(txq);
50213feffbd7SAdrian Chadd 		bf = ath_tx_draintxq_get_one(sc, txq);
50225591b213SSam Leffler 		if (bf == NULL) {
5023b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
50245591b213SSam Leffler 			break;
50255591b213SSam Leffler 		}
50266edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
50276edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
5028a585a9a1SSam Leffler #ifdef ATH_DEBUG
50294a3ac3fcSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RESET) {
50307a79cebfSGleb Smirnoff 			struct ieee80211com *ic = &sc->sc_ic;
50311762ec94SAdrian Chadd 			int status = 0;
5032b032f27cSSam Leffler 
50331762ec94SAdrian Chadd 			/*
50341762ec94SAdrian Chadd 			 * EDMA operation has a TX completion FIFO
50351762ec94SAdrian Chadd 			 * separate from the TX descriptor, so this
50361762ec94SAdrian Chadd 			 * method of checking the "completion" status
50371762ec94SAdrian Chadd 			 * is wrong.
50381762ec94SAdrian Chadd 			 */
50391762ec94SAdrian Chadd 			if (! sc->sc_isedma) {
50401762ec94SAdrian Chadd 				status = (ath_hal_txprocdesc(ah,
50411762ec94SAdrian Chadd 				    bf->bf_lastds,
504265f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
50431762ec94SAdrian Chadd 			}
50441762ec94SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, ix, status);
5045e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ic, mtod(bf->bf_m, const uint8_t *),
50464a3ac3fcSSam Leffler 			    bf->bf_m->m_len, 0, -1);
50474a3ac3fcSSam Leffler 		}
5048a585a9a1SSam Leffler #endif /* ATH_DEBUG */
504923428eafSSam Leffler 		/*
50509352fb7aSAdrian Chadd 		 * Since we're now doing magic in the completion
50519352fb7aSAdrian Chadd 		 * functions, we -must- call it for aggregation
50529352fb7aSAdrian Chadd 		 * destinations or BAW tracking will get upset.
505323428eafSSam Leffler 		 */
50549352fb7aSAdrian Chadd 		/*
50559352fb7aSAdrian Chadd 		 * Clear ATH_BUF_BUSY; the completion handler
50569352fb7aSAdrian Chadd 		 * will free the buffer.
50579352fb7aSAdrian Chadd 		 */
5058b837332dSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
505910ad9a77SSam Leffler 		bf->bf_flags &= ~ATH_BUF_BUSY;
50609352fb7aSAdrian Chadd 		if (bf->bf_comp)
50619352fb7aSAdrian Chadd 			bf->bf_comp(sc, bf, 1);
50629352fb7aSAdrian Chadd 		else
50639352fb7aSAdrian Chadd 			ath_tx_default_comp(sc, bf, 1);
50645591b213SSam Leffler 	}
50659352fb7aSAdrian Chadd 
5066eb6f0de0SAdrian Chadd 	/*
5067629ce218SAdrian Chadd 	 * Free the holding buffer if it exists
5068629ce218SAdrian Chadd 	 */
50695e018508SAdrian Chadd 	ATH_TXQ_LOCK(txq);
5070629ce218SAdrian Chadd 	ath_txq_freeholdingbuf(sc, txq);
50715e018508SAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
5072629ce218SAdrian Chadd 
5073629ce218SAdrian Chadd 	/*
5074eb6f0de0SAdrian Chadd 	 * Drain software queued frames which are on
5075eb6f0de0SAdrian Chadd 	 * active TIDs.
5076eb6f0de0SAdrian Chadd 	 */
5077eb6f0de0SAdrian Chadd 	ath_tx_txq_drain(sc, txq);
5078c42a7b7eSSam Leffler }
5079c42a7b7eSSam Leffler 
5080c42a7b7eSSam Leffler static void
ath_tx_stopdma(struct ath_softc * sc,struct ath_txq * txq)5081c42a7b7eSSam Leffler ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
5082c42a7b7eSSam Leffler {
5083c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5084c42a7b7eSSam Leffler 
50859be82a42SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
50869be82a42SAdrian Chadd 
50879d2a962bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
5088dfaf8de9SAdrian Chadd 	    "%s: tx queue [%u] %p, active=%d, hwpending=%d, flags 0x%08x, "
5089dfaf8de9SAdrian Chadd 	    "link %p, holdingbf=%p\n",
50909d2a962bSAdrian Chadd 	    __func__,
50919d2a962bSAdrian Chadd 	    txq->axq_qnum,
50926891c875SPeter Wemm 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
50938d060542SAdrian Chadd 	    (int) (!! ath_hal_txqenabled(ah, txq->axq_qnum)),
50948d060542SAdrian Chadd 	    (int) ath_hal_numtxpending(ah, txq->axq_qnum),
50959d2a962bSAdrian Chadd 	    txq->axq_flags,
5096dfaf8de9SAdrian Chadd 	    txq->axq_link,
5097dfaf8de9SAdrian Chadd 	    txq->axq_holdingbf);
5098dfaf8de9SAdrian Chadd 
50994a3ac3fcSSam Leffler 	(void) ath_hal_stoptxdma(ah, txq->axq_qnum);
51009be82a42SAdrian Chadd 	/* We've stopped TX DMA, so mark this as stopped. */
51019be82a42SAdrian Chadd 	txq->axq_flags &= ~ATH_TXQ_PUTRUNNING;
5102dfaf8de9SAdrian Chadd 
5103dfaf8de9SAdrian Chadd #ifdef	ATH_DEBUG
5104dfaf8de9SAdrian Chadd 	if ((sc->sc_debug & ATH_DEBUG_RESET)
5105dfaf8de9SAdrian Chadd 	    && (txq->axq_holdingbf != NULL)) {
5106dfaf8de9SAdrian Chadd 		ath_printtxbuf(sc, txq->axq_holdingbf, txq->axq_qnum, 0, 0);
5107dfaf8de9SAdrian Chadd 	}
5108dfaf8de9SAdrian Chadd #endif
5109c42a7b7eSSam Leffler }
5110c42a7b7eSSam Leffler 
5111bad98824SAdrian Chadd int
ath_stoptxdma(struct ath_softc * sc)51122d433424SAdrian Chadd ath_stoptxdma(struct ath_softc *sc)
5113c42a7b7eSSam Leffler {
5114c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5115c42a7b7eSSam Leffler 	int i;
5116c42a7b7eSSam Leffler 
5117c42a7b7eSSam Leffler 	/* XXX return value */
51182d433424SAdrian Chadd 	if (sc->sc_invalid)
51192d433424SAdrian Chadd 		return 0;
51202d433424SAdrian Chadd 
5121c42a7b7eSSam Leffler 	if (!sc->sc_invalid) {
5122c42a7b7eSSam Leffler 		/* don't touch the hardware if marked invalid */
51234a3ac3fcSSam Leffler 		DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
51244a3ac3fcSSam Leffler 		    __func__, sc->sc_bhalq,
51254a3ac3fcSSam Leffler 		    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq),
51264a3ac3fcSSam Leffler 		    NULL);
51279be82a42SAdrian Chadd 
51289be82a42SAdrian Chadd 		/* stop the beacon queue */
5129c42a7b7eSSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
51309be82a42SAdrian Chadd 
51319be82a42SAdrian Chadd 		/* Stop the data queues */
51329be82a42SAdrian Chadd 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
51339be82a42SAdrian Chadd 			if (ATH_TXQ_SETUP(sc, i)) {
51349be82a42SAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
5135c42a7b7eSSam Leffler 				ath_tx_stopdma(sc, &sc->sc_txq[i]);
51369be82a42SAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
51379be82a42SAdrian Chadd 			}
51389be82a42SAdrian Chadd 		}
5139c42a7b7eSSam Leffler 	}
51402d433424SAdrian Chadd 
51412d433424SAdrian Chadd 	return 1;
51422d433424SAdrian Chadd }
51432d433424SAdrian Chadd 
514407187d11SAdrian Chadd #ifdef	ATH_DEBUG
51459be82a42SAdrian Chadd void
ath_tx_dump(struct ath_softc * sc,struct ath_txq * txq)5146ed261a61SAdrian Chadd ath_tx_dump(struct ath_softc *sc, struct ath_txq *txq)
5147ed261a61SAdrian Chadd {
5148ed261a61SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
5149ed261a61SAdrian Chadd 	struct ath_buf *bf;
5150ed261a61SAdrian Chadd 	int i = 0;
5151ed261a61SAdrian Chadd 
5152ed261a61SAdrian Chadd 	if (! (sc->sc_debug & ATH_DEBUG_RESET))
5153ed261a61SAdrian Chadd 		return;
5154ed261a61SAdrian Chadd 
5155ed261a61SAdrian Chadd 	device_printf(sc->sc_dev, "%s: Q%d: begin\n",
5156ed261a61SAdrian Chadd 	    __func__, txq->axq_qnum);
5157ed261a61SAdrian Chadd 	TAILQ_FOREACH(bf, &txq->axq_q, bf_list) {
5158ed261a61SAdrian Chadd 		ath_printtxbuf(sc, bf, txq->axq_qnum, i,
5159ed261a61SAdrian Chadd 			ath_hal_txprocdesc(ah, bf->bf_lastds,
5160ed261a61SAdrian Chadd 			    &bf->bf_status.ds_txstat) == HAL_OK);
5161ed261a61SAdrian Chadd 		i++;
5162ed261a61SAdrian Chadd 	}
5163ed261a61SAdrian Chadd 	device_printf(sc->sc_dev, "%s: Q%d: end\n",
5164ed261a61SAdrian Chadd 	    __func__, txq->axq_qnum);
5165ed261a61SAdrian Chadd }
516607187d11SAdrian Chadd #endif /* ATH_DEBUG */
5167ed261a61SAdrian Chadd 
51682d433424SAdrian Chadd /*
51692d433424SAdrian Chadd  * Drain the transmit queues and reclaim resources.
51702d433424SAdrian Chadd  */
5171788e6aa9SAdrian Chadd void
ath_legacy_tx_drain(struct ath_softc * sc,ATH_RESET_TYPE reset_type)5172788e6aa9SAdrian Chadd ath_legacy_tx_drain(struct ath_softc *sc, ATH_RESET_TYPE reset_type)
51732d433424SAdrian Chadd {
51742d433424SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
5175ba2c1fbcSAdrian Chadd 	struct ath_buf *bf_last;
51767a79cebfSGleb Smirnoff 	int i;
51772d433424SAdrian Chadd 
51782d433424SAdrian Chadd 	(void) ath_stoptxdma(sc);
51792d433424SAdrian Chadd 
5180ed261a61SAdrian Chadd 	/*
5181ed261a61SAdrian Chadd 	 * Dump the queue contents
5182ed261a61SAdrian Chadd 	 */
5183ef27340cSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
5184ef27340cSAdrian Chadd 		/*
5185ef27340cSAdrian Chadd 		 * XXX TODO: should we just handle the completed TX frames
5186ef27340cSAdrian Chadd 		 * here, whether or not the reset is a full one or not?
5187ef27340cSAdrian Chadd 		 */
5188ef27340cSAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
518907187d11SAdrian Chadd #ifdef	ATH_DEBUG
5190ed261a61SAdrian Chadd 			if (sc->sc_debug & ATH_DEBUG_RESET)
5191ed261a61SAdrian Chadd 				ath_tx_dump(sc, &sc->sc_txq[i]);
519207187d11SAdrian Chadd #endif	/* ATH_DEBUG */
51938328d6e4SAdrian Chadd 			if (reset_type == ATH_RESET_NOLOSS) {
5194ef27340cSAdrian Chadd 				ath_tx_processq(sc, &sc->sc_txq[i], 0);
51958328d6e4SAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
51968328d6e4SAdrian Chadd 				/*
51978328d6e4SAdrian Chadd 				 * Free the holding buffer; DMA is now
51988328d6e4SAdrian Chadd 				 * stopped.
51998328d6e4SAdrian Chadd 				 */
52008328d6e4SAdrian Chadd 				ath_txq_freeholdingbuf(sc, &sc->sc_txq[i]);
52018328d6e4SAdrian Chadd 				/*
52029be82a42SAdrian Chadd 				 * Setup the link pointer to be the
52039be82a42SAdrian Chadd 				 * _last_ buffer/descriptor in the list.
52049be82a42SAdrian Chadd 				 * If there's nothing in the list, set it
52059be82a42SAdrian Chadd 				 * to NULL.
52068328d6e4SAdrian Chadd 				 */
52079be82a42SAdrian Chadd 				bf_last = ATH_TXQ_LAST(&sc->sc_txq[i],
52089be82a42SAdrian Chadd 				    axq_q_s);
52099be82a42SAdrian Chadd 				if (bf_last != NULL) {
52109be82a42SAdrian Chadd 					ath_hal_gettxdesclinkptr(ah,
52119be82a42SAdrian Chadd 					    bf_last->bf_lastds,
52129be82a42SAdrian Chadd 					    &sc->sc_txq[i].axq_link);
52139be82a42SAdrian Chadd 				} else {
52148328d6e4SAdrian Chadd 					sc->sc_txq[i].axq_link = NULL;
52159be82a42SAdrian Chadd 				}
52168328d6e4SAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
52178328d6e4SAdrian Chadd 			} else
5218c42a7b7eSSam Leffler 				ath_tx_draintxq(sc, &sc->sc_txq[i]);
5219ef27340cSAdrian Chadd 		}
5220ef27340cSAdrian Chadd 	}
52214a3ac3fcSSam Leffler #ifdef ATH_DEBUG
52224a3ac3fcSSam Leffler 	if (sc->sc_debug & ATH_DEBUG_RESET) {
52236b349e5aSAdrian Chadd 		struct ath_buf *bf = TAILQ_FIRST(&sc->sc_bbuf);
52244a3ac3fcSSam Leffler 		if (bf != NULL && bf->bf_m != NULL) {
52256902009eSSam Leffler 			ath_printtxbuf(sc, bf, sc->sc_bhalq, 0,
52266edf1dc7SAdrian Chadd 				ath_hal_txprocdesc(ah, bf->bf_lastds,
522765f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
52287a79cebfSGleb Smirnoff 			ieee80211_dump_pkt(&sc->sc_ic,
5229e40b6ab1SSam Leffler 			    mtod(bf->bf_m, const uint8_t *), bf->bf_m->m_len,
5230e40b6ab1SSam Leffler 			    0, -1);
52314a3ac3fcSSam Leffler 		}
52324a3ac3fcSSam Leffler 	}
52334a3ac3fcSSam Leffler #endif /* ATH_DEBUG */
52342e986da5SSam Leffler 	sc->sc_wd_timer = 0;
52355591b213SSam Leffler }
52365591b213SSam Leffler 
52375591b213SSam Leffler /*
5238c42a7b7eSSam Leffler  * Update internal state after a channel change.
5239c42a7b7eSSam Leffler  */
5240c42a7b7eSSam Leffler static void
ath_chan_change(struct ath_softc * sc,struct ieee80211_channel * chan)5241c42a7b7eSSam Leffler ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
5242c42a7b7eSSam Leffler {
5243c42a7b7eSSam Leffler 	enum ieee80211_phymode mode;
5244c42a7b7eSSam Leffler 
5245c42a7b7eSSam Leffler 	/*
5246c42a7b7eSSam Leffler 	 * Change channels and update the h/w rate map
5247c42a7b7eSSam Leffler 	 * if we're switching; e.g. 11a to 11b/g.
5248c42a7b7eSSam Leffler 	 */
524968e8e04eSSam Leffler 	mode = ieee80211_chan2mode(chan);
5250c42a7b7eSSam Leffler 	if (mode != sc->sc_curmode)
5251c42a7b7eSSam Leffler 		ath_setcurmode(sc, mode);
525259efa8b5SSam Leffler 	sc->sc_curchan = chan;
5253c42a7b7eSSam Leffler }
5254c42a7b7eSSam Leffler 
5255c42a7b7eSSam Leffler /*
52565591b213SSam Leffler  * Set/change channels.  If the channel is really being changed,
52574fa8d4efSDaniel Eischen  * it's done by resetting the chip.  To accomplish this we must
52585591b213SSam Leffler  * first cleanup any pending DMA, then restart stuff after a la
52595591b213SSam Leffler  * ath_init.
52605591b213SSam Leffler  */
52615591b213SSam Leffler static int
ath_chan_set(struct ath_softc * sc,struct ieee80211_channel * chan)52625591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
52635591b213SSam Leffler {
52647a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
52655591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5266ef27340cSAdrian Chadd 	int ret = 0;
5267ef27340cSAdrian Chadd 
5268ef27340cSAdrian Chadd 	/* Treat this as an interface reset */
5269d52f7132SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
5270d52f7132SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
5271d52f7132SAdrian Chadd 
5272f6b6084bSPedro F. Giffuni 	/* (Try to) stop TX/RX from occurring */
5273d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
5274d52f7132SAdrian Chadd 
5275ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5276904e385eSAdrian Chadd 
527717bb5fd1SAdrian Chadd 	/* Disable interrupts */
527817bb5fd1SAdrian Chadd 	ath_hal_intrset(ah, 0);
527917bb5fd1SAdrian Chadd 
5280904e385eSAdrian Chadd 	/* Stop new RX/TX/interrupt completion */
5281ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
5282ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
5283ef27340cSAdrian Chadd 		    __func__);
5284ee321975SAdrian Chadd 	}
5285904e385eSAdrian Chadd 
5286904e385eSAdrian Chadd 	/* Stop pending RX/TX completion */
5287904e385eSAdrian Chadd 	ath_txrx_stop_locked(sc);
5288904e385eSAdrian Chadd 
5289ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5290c42a7b7eSSam Leffler 
529159efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz, flags 0x%x)\n",
529259efa8b5SSam Leffler 	    __func__, ieee80211_chan2ieee(ic, chan),
529359efa8b5SSam Leffler 	    chan->ic_freq, chan->ic_flags);
529459efa8b5SSam Leffler 	if (chan != sc->sc_curchan) {
5295c42a7b7eSSam Leffler 		HAL_STATUS status;
52965591b213SSam Leffler 		/*
52975591b213SSam Leffler 		 * To switch channels clear any pending DMA operations;
52985591b213SSam Leffler 		 * wait long enough for the RX fifo to drain, reset the
52995591b213SSam Leffler 		 * hardware at the new frequency, and then re-enable
53005591b213SSam Leffler 		 * the relevant bits of the h/w.
53015591b213SSam Leffler 		 */
5302ef27340cSAdrian Chadd #if 0
53035591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
5304ef27340cSAdrian Chadd #endif
53059a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* turn off frame recv */
53069a842e8bSAdrian Chadd 		/*
53079a842e8bSAdrian Chadd 		 * First, handle completed TX/RX frames.
53089a842e8bSAdrian Chadd 		 */
5309f8cc9b09SAdrian Chadd 		ath_rx_flush(sc);
53109a842e8bSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_NOLOSS);
53119a842e8bSAdrian Chadd 		/*
53129a842e8bSAdrian Chadd 		 * Next, flush the non-scheduled frames.
53139a842e8bSAdrian Chadd 		 */
5314517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_FULL);	/* clear pending tx frames */
53159a842e8bSAdrian Chadd 
53166322256bSAdrian Chadd 		ath_update_chainmasks(sc, chan);
53176322256bSAdrian Chadd 		ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
53186322256bSAdrian Chadd 		    sc->sc_cur_rxchainmask);
5319f50e4ebfSAdrian Chadd 		if (!ath_hal_reset(ah, sc->sc_opmode, chan, AH_TRUE,
5320f50e4ebfSAdrian Chadd 		    HAL_RESET_NORMAL, &status)) {
532176e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev, "%s: unable to reset "
532279649302SGavin Atkinson 			    "channel %u (%u MHz, flags 0x%x), hal status %u\n",
532359efa8b5SSam Leffler 			    __func__, ieee80211_chan2ieee(ic, chan),
532459efa8b5SSam Leffler 			    chan->ic_freq, chan->ic_flags, status);
5325ef27340cSAdrian Chadd 			ret = EIO;
5326ef27340cSAdrian Chadd 			goto finish;
53275591b213SSam Leffler 		}
5328c59005e9SSam Leffler 		sc->sc_diversity = ath_hal_getdiversity(ah);
5329c42a7b7eSSam Leffler 
533017bb5fd1SAdrian Chadd 		ATH_RX_LOCK(sc);
533117bb5fd1SAdrian Chadd 		sc->sc_rx_stopped = 1;
533217bb5fd1SAdrian Chadd 		sc->sc_rx_resetted = 1;
533317bb5fd1SAdrian Chadd 		ATH_RX_UNLOCK(sc);
533417bb5fd1SAdrian Chadd 
53351410ca56SAdrian Chadd 		/* Quiet time handling - ensure we resync */
53361410ca56SAdrian Chadd 		ath_vap_clear_quiet_ie(sc);
53371410ca56SAdrian Chadd 
533848237774SAdrian Chadd 		/* Let DFS at it in case it's a DFS channel */
5339398bca2eSAdrian Chadd 		ath_dfs_radar_enable(sc, chan);
534048237774SAdrian Chadd 
53419af351f9SAdrian Chadd 		/* Let spectral at in case spectral is enabled */
53429af351f9SAdrian Chadd 		ath_spectral_enable(sc, chan);
53439af351f9SAdrian Chadd 
53445591b213SSam Leffler 		/*
5345b70f530bSAdrian Chadd 		 * Let bluetooth coexistence at in case it's needed for this
5346b70f530bSAdrian Chadd 		 * channel
5347b70f530bSAdrian Chadd 		 */
5348b70f530bSAdrian Chadd 		ath_btcoex_enable(sc, ic->ic_curchan);
5349b70f530bSAdrian Chadd 
5350b70f530bSAdrian Chadd 		/*
5351dd6a574eSAdrian Chadd 		 * If we're doing TDMA, enforce the TXOP limitation for chips
5352dd6a574eSAdrian Chadd 		 * that support it.
5353dd6a574eSAdrian Chadd 		 */
5354dd6a574eSAdrian Chadd 		if (sc->sc_hasenforcetxop && sc->sc_tdma)
5355dd6a574eSAdrian Chadd 			ath_hal_setenforcetxop(sc->sc_ah, 1);
5356dd6a574eSAdrian Chadd 		else
5357dd6a574eSAdrian Chadd 			ath_hal_setenforcetxop(sc->sc_ah, 0);
5358dd6a574eSAdrian Chadd 
5359dd6a574eSAdrian Chadd 		/*
53605591b213SSam Leffler 		 * Re-enable rx framework.
53615591b213SSam Leffler 		 */
53625591b213SSam Leffler 		if (ath_startrecv(sc) != 0) {
536376e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev,
536476e6fd5dSGleb Smirnoff 			    "%s: unable to restart recv logic\n", __func__);
5365ef27340cSAdrian Chadd 			ret = EIO;
5366ef27340cSAdrian Chadd 			goto finish;
53675591b213SSam Leffler 		}
53685591b213SSam Leffler 
53695591b213SSam Leffler 		/*
53705591b213SSam Leffler 		 * Change channels and update the h/w rate map
53715591b213SSam Leffler 		 * if we're switching; e.g. 11a to 11b/g.
53725591b213SSam Leffler 		 */
5373c42a7b7eSSam Leffler 		ath_chan_change(sc, chan);
53740a915fadSSam Leffler 
53750a915fadSSam Leffler 		/*
53762fd9aabbSAdrian Chadd 		 * Reset clears the beacon timers; reset them
53772fd9aabbSAdrian Chadd 		 * here if needed.
53782fd9aabbSAdrian Chadd 		 */
53792fd9aabbSAdrian Chadd 		if (sc->sc_beacons) {		/* restart beacons */
53802fd9aabbSAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
53812fd9aabbSAdrian Chadd 			if (sc->sc_tdma)
53822fd9aabbSAdrian Chadd 				ath_tdma_config(sc, NULL);
53832fd9aabbSAdrian Chadd 			else
53842fd9aabbSAdrian Chadd #endif
53852fd9aabbSAdrian Chadd 			ath_beacon_config(sc, NULL);
53862fd9aabbSAdrian Chadd 		}
53872fd9aabbSAdrian Chadd 
53882fd9aabbSAdrian Chadd 		/*
53890a915fadSSam Leffler 		 * Re-enable interrupts.
53900a915fadSSam Leffler 		 */
5391e78719adSAdrian Chadd #if 0
53920a915fadSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
5393ef27340cSAdrian Chadd #endif
53945591b213SSam Leffler 	}
5395ef27340cSAdrian Chadd 
5396ef27340cSAdrian Chadd finish:
5397ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5398ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
5399ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
5400ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
5401ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5402ef27340cSAdrian Chadd 
5403ef27340cSAdrian Chadd 	ath_txrx_start(sc);
5404ef27340cSAdrian Chadd 	/* XXX ath_start? */
5405ef27340cSAdrian Chadd 
5406ef27340cSAdrian Chadd 	return ret;
54075591b213SSam Leffler }
54085591b213SSam Leffler 
54095591b213SSam Leffler /*
54105591b213SSam Leffler  * Periodically recalibrate the PHY to account
54115591b213SSam Leffler  * for temperature/environment changes.
54125591b213SSam Leffler  */
54135591b213SSam Leffler static void
ath_calibrate(void * arg)54145591b213SSam Leffler ath_calibrate(void *arg)
54155591b213SSam Leffler {
54165591b213SSam Leffler 	struct ath_softc *sc = arg;
54175591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
54187a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
5419943e37a1SAdrian Chadd 	HAL_BOOL longCal, isCalDone = AH_TRUE;
5420a108ab63SAdrian Chadd 	HAL_BOOL aniCal, shortCal = AH_FALSE;
54212dc7fcc4SSam Leffler 	int nextcal;
54225591b213SSam Leffler 
5423adcdc8f2SAdrian Chadd 	ATH_LOCK_ASSERT(sc);
54247707f31dSAdrian Chadd 
5425f5c30c4eSAdrian Chadd 	/*
5426f5c30c4eSAdrian Chadd 	 * Force the hardware awake for ANI work.
5427f5c30c4eSAdrian Chadd 	 */
5428f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
5429f5c30c4eSAdrian Chadd 
5430f5c30c4eSAdrian Chadd 	/* Skip trying to do this if we're in reset */
5431f5c30c4eSAdrian Chadd 	if (sc->sc_inreset_cnt)
5432f5c30c4eSAdrian Chadd 		goto restart;
5433f5c30c4eSAdrian Chadd 
54348d91de92SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SCAN)	/* defer, off channel */
54358d91de92SSam Leffler 		goto restart;
54362dc7fcc4SSam Leffler 	longCal = (ticks - sc->sc_lastlongcal >= ath_longcalinterval*hz);
5437a108ab63SAdrian Chadd 	aniCal = (ticks - sc->sc_lastani >= ath_anicalinterval*hz/1000);
5438a108ab63SAdrian Chadd 	if (sc->sc_doresetcal)
5439a108ab63SAdrian Chadd 		shortCal = (ticks - sc->sc_lastshortcal >= ath_shortcalinterval*hz/1000);
5440a108ab63SAdrian Chadd 
5441a108ab63SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: shortCal=%d; longCal=%d; aniCal=%d\n", __func__, shortCal, longCal, aniCal);
5442a108ab63SAdrian Chadd 	if (aniCal) {
5443a108ab63SAdrian Chadd 		sc->sc_stats.ast_ani_cal++;
5444a108ab63SAdrian Chadd 		sc->sc_lastani = ticks;
5445a108ab63SAdrian Chadd 		ath_hal_ani_poll(ah, sc->sc_curchan);
5446a108ab63SAdrian Chadd 	}
5447a108ab63SAdrian Chadd 
54482dc7fcc4SSam Leffler 	if (longCal) {
54495591b213SSam Leffler 		sc->sc_stats.ast_per_cal++;
54508197f57eSAdrian Chadd 		sc->sc_lastlongcal = ticks;
54515591b213SSam Leffler 		if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
54525591b213SSam Leffler 			/*
54535591b213SSam Leffler 			 * Rfgain is out of bounds, reset the chip
54545591b213SSam Leffler 			 * to load new gain values.
54555591b213SSam Leffler 			 */
5456370572d9SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
5457370572d9SSam Leffler 				"%s: rfgain change\n", __func__);
54585591b213SSam Leffler 			sc->sc_stats.ast_per_rfgain++;
5459ef27340cSAdrian Chadd 			sc->sc_resetcal = 0;
5460ef27340cSAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
5461d52f7132SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
5462d52f7132SAdrian Chadd 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
5463f5c30c4eSAdrian Chadd 			ath_power_restore_power_state(sc);
5464ef27340cSAdrian Chadd 			return;
54655591b213SSam Leffler 		}
54662dc7fcc4SSam Leffler 		/*
54672dc7fcc4SSam Leffler 		 * If this long cal is after an idle period, then
54682dc7fcc4SSam Leffler 		 * reset the data collection state so we start fresh.
54692dc7fcc4SSam Leffler 		 */
54702dc7fcc4SSam Leffler 		if (sc->sc_resetcal) {
547159efa8b5SSam Leffler 			(void) ath_hal_calreset(ah, sc->sc_curchan);
54722dc7fcc4SSam Leffler 			sc->sc_lastcalreset = ticks;
5473a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
54742dc7fcc4SSam Leffler 			sc->sc_resetcal = 0;
5475a108ab63SAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
54762dc7fcc4SSam Leffler 		}
54772dc7fcc4SSam Leffler 	}
5478a108ab63SAdrian Chadd 
5479a108ab63SAdrian Chadd 	/* Only call if we're doing a short/long cal, not for ANI calibration */
5480a108ab63SAdrian Chadd 	if (shortCal || longCal) {
5481943e37a1SAdrian Chadd 		isCalDone = AH_FALSE;
548259efa8b5SSam Leffler 		if (ath_hal_calibrateN(ah, sc->sc_curchan, longCal, &isCalDone)) {
54832dc7fcc4SSam Leffler 			if (longCal) {
54842dc7fcc4SSam Leffler 				/*
54852dc7fcc4SSam Leffler 				 * Calibrate noise floor data again in case of change.
54862dc7fcc4SSam Leffler 				 */
54872dc7fcc4SSam Leffler 				ath_hal_process_noisefloor(ah);
54882dc7fcc4SSam Leffler 			}
54892dc7fcc4SSam Leffler 		} else {
5490c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
5491c42a7b7eSSam Leffler 				"%s: calibration of channel %u failed\n",
549259efa8b5SSam Leffler 				__func__, sc->sc_curchan->ic_freq);
54935591b213SSam Leffler 			sc->sc_stats.ast_per_calfail++;
54945591b213SSam Leffler 		}
54957d450faaSAdrian Chadd 		/*
54967d450faaSAdrian Chadd 		 * XXX TODO: get the NF calibration results from the HAL.
54977d450faaSAdrian Chadd 		 * If we failed NF cal then schedule a hard reset to potentially
54987d450faaSAdrian Chadd 		 * un-freeze the PHY.
54997d450faaSAdrian Chadd 		 *
55007d450faaSAdrian Chadd 		 * Note we have to be careful here to not get stuck in an
55017d450faaSAdrian Chadd 		 * infinite NIC restart.  Ideally we'd not restart if we
55027d450faaSAdrian Chadd 		 * failed the first NF cal - that /can/ fail sometimes in
55037d450faaSAdrian Chadd 		 * a noisy environment.
55048c01c3dcSAdrian Chadd 		 *
55058c01c3dcSAdrian Chadd 		 * Instead, we should likely temporarily shorten the longCal
55068c01c3dcSAdrian Chadd 		 * period to happen pretty quickly and if a subsequent one
55078c01c3dcSAdrian Chadd 		 * fails, do a full reset.
55087d450faaSAdrian Chadd 		 */
5509a108ab63SAdrian Chadd 		if (shortCal)
5510a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
5511a108ab63SAdrian Chadd 	}
55122dc7fcc4SSam Leffler 	if (!isCalDone) {
55138d91de92SSam Leffler restart:
55147b0c77ecSSam Leffler 		/*
55152dc7fcc4SSam Leffler 		 * Use a shorter interval to potentially collect multiple
55162dc7fcc4SSam Leffler 		 * data samples required to complete calibration.  Once
55172dc7fcc4SSam Leffler 		 * we're told the work is done we drop back to a longer
55182dc7fcc4SSam Leffler 		 * interval between requests.  We're more aggressive doing
55192dc7fcc4SSam Leffler 		 * work when operating as an AP to improve operation right
55202dc7fcc4SSam Leffler 		 * after startup.
55217b0c77ecSSam Leffler 		 */
5522a108ab63SAdrian Chadd 		sc->sc_lastshortcal = ticks;
5523a108ab63SAdrian Chadd 		nextcal = ath_shortcalinterval*hz/1000;
55242dc7fcc4SSam Leffler 		if (sc->sc_opmode != HAL_M_HOSTAP)
55252dc7fcc4SSam Leffler 			nextcal *= 10;
5526a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_TRUE;
55272dc7fcc4SSam Leffler 	} else {
5528a108ab63SAdrian Chadd 		/* nextcal should be the shortest time for next event */
55292dc7fcc4SSam Leffler 		nextcal = ath_longcalinterval*hz;
55302dc7fcc4SSam Leffler 		if (sc->sc_lastcalreset == 0)
55312dc7fcc4SSam Leffler 			sc->sc_lastcalreset = sc->sc_lastlongcal;
55322dc7fcc4SSam Leffler 		else if (ticks - sc->sc_lastcalreset >= ath_resetcalinterval*hz)
55332dc7fcc4SSam Leffler 			sc->sc_resetcal = 1;	/* setup reset next trip */
5534a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_FALSE;
5535bd5a9920SSam Leffler 	}
5536a108ab63SAdrian Chadd 	/* ANI calibration may occur more often than short/long/resetcal */
5537a108ab63SAdrian Chadd 	if (ath_anicalinterval > 0)
5538a108ab63SAdrian Chadd 		nextcal = MIN(nextcal, ath_anicalinterval*hz/1000);
5539bd5a9920SSam Leffler 
55402dc7fcc4SSam Leffler 	if (nextcal != 0) {
55412dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: next +%u (%sisCalDone)\n",
55422dc7fcc4SSam Leffler 		    __func__, nextcal, isCalDone ? "" : "!");
55432dc7fcc4SSam Leffler 		callout_reset(&sc->sc_cal_ch, nextcal, ath_calibrate, sc);
55442dc7fcc4SSam Leffler 	} else {
55452dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: calibration disabled\n",
55462dc7fcc4SSam Leffler 		    __func__);
55472dc7fcc4SSam Leffler 		/* NB: don't rearm timer */
55482dc7fcc4SSam Leffler 	}
5549f5c30c4eSAdrian Chadd 	/*
5550f5c30c4eSAdrian Chadd 	 * Restore power state now that we're done.
5551f5c30c4eSAdrian Chadd 	 */
5552f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
55535591b213SSam Leffler }
55545591b213SSam Leffler 
555568e8e04eSSam Leffler static void
ath_scan_start(struct ieee80211com * ic)555668e8e04eSSam Leffler ath_scan_start(struct ieee80211com *ic)
555768e8e04eSSam Leffler {
55583797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
555968e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
556068e8e04eSSam Leffler 	u_int32_t rfilt;
556168e8e04eSSam Leffler 
556268e8e04eSSam Leffler 	/* XXX calibration timer? */
55637a79cebfSGleb Smirnoff 	/* XXXGL: is constant ieee80211broadcastaddr a correct choice? */
556468e8e04eSSam Leffler 
5565c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
556668e8e04eSSam Leffler 	sc->sc_scanning = 1;
556768e8e04eSSam Leffler 	sc->sc_syncbeacon = 0;
556868e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5569c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
5570c98cefc5SAdrian Chadd 
5571c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
557268e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
55737a79cebfSGleb Smirnoff 	ath_hal_setassocid(ah, ieee80211broadcastaddr, 0);
5574c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
557568e8e04eSSam Leffler 
557668e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0\n",
55777a79cebfSGleb Smirnoff 		 __func__, rfilt, ether_sprintf(ieee80211broadcastaddr));
557868e8e04eSSam Leffler }
557968e8e04eSSam Leffler 
558068e8e04eSSam Leffler static void
ath_scan_end(struct ieee80211com * ic)558168e8e04eSSam Leffler ath_scan_end(struct ieee80211com *ic)
558268e8e04eSSam Leffler {
55833797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
558468e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
558568e8e04eSSam Leffler 	u_int32_t rfilt;
558668e8e04eSSam Leffler 
5587c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
558868e8e04eSSam Leffler 	sc->sc_scanning = 0;
558968e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5590c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
5591c98cefc5SAdrian Chadd 
5592c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
559368e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
559468e8e04eSSam Leffler 	ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
559568e8e04eSSam Leffler 
559668e8e04eSSam Leffler 	ath_hal_process_noisefloor(ah);
5597c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
559868e8e04eSSam Leffler 
559968e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
560068e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(sc->sc_curbssid),
560168e8e04eSSam Leffler 		 sc->sc_curaid);
560268e8e04eSSam Leffler }
560368e8e04eSSam Leffler 
5604fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
5605e7200579SAdrian Chadd /*
5606e7200579SAdrian Chadd  * For now, just do a channel change.
5607e7200579SAdrian Chadd  *
5608e7200579SAdrian Chadd  * Later, we'll go through the hard slog of suspending tx/rx, changing rate
5609e7200579SAdrian Chadd  * control state and resetting the hardware without dropping frames out
5610e7200579SAdrian Chadd  * of the queue.
5611e7200579SAdrian Chadd  *
5612e7200579SAdrian Chadd  * The unfortunate trouble here is making absolutely sure that the
5613e7200579SAdrian Chadd  * channel width change has propagated enough so the hardware
5614e7200579SAdrian Chadd  * absolutely isn't handed bogus frames for it's current operating
5615e7200579SAdrian Chadd  * mode. (Eg, 40MHz frames in 20MHz mode.) Since TX and RX can and
5616e7200579SAdrian Chadd  * does occur in parallel, we need to make certain we've blocked
5617e7200579SAdrian Chadd  * any further ongoing TX (and RX, that can cause raw TX)
5618e7200579SAdrian Chadd  * before we do this.
5619e7200579SAdrian Chadd  */
5620e7200579SAdrian Chadd static void
ath_update_chw(struct ieee80211com * ic)5621e7200579SAdrian Chadd ath_update_chw(struct ieee80211com *ic)
5622e7200579SAdrian Chadd {
56233797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
5624e7200579SAdrian Chadd 
56251410ca56SAdrian Chadd 	//DPRINTF(sc, ATH_DEBUG_STATE, "%s: called\n", __func__);
56261410ca56SAdrian Chadd 	device_printf(sc->sc_dev, "%s: called\n", __func__);
56271410ca56SAdrian Chadd 
56281410ca56SAdrian Chadd 	/*
56291410ca56SAdrian Chadd 	 * XXX TODO: schedule a tasklet that stops things without freeing,
56301410ca56SAdrian Chadd 	 * walks the now stopped TX queue(s) looking for frames to retry
56311410ca56SAdrian Chadd 	 * as if we TX filtered them (whch may mean dropping non-ampdu frames!)
56321410ca56SAdrian Chadd 	 * but okay) then place them back on the software queue so they
56331410ca56SAdrian Chadd 	 * can have the rate control lookup done again.
56341410ca56SAdrian Chadd 	 */
5635e7200579SAdrian Chadd 	ath_set_channel(ic);
5636e7200579SAdrian Chadd }
5637fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
5638e7200579SAdrian Chadd 
56391410ca56SAdrian Chadd /*
56401410ca56SAdrian Chadd  * This is called by the beacon parsing routine in the receive
56411410ca56SAdrian Chadd  * path to update the current quiet time information provided by
56421410ca56SAdrian Chadd  * an AP.
56431410ca56SAdrian Chadd  *
56441410ca56SAdrian Chadd  * This is STA specific, it doesn't take the AP TBTT/beacon slot
56451410ca56SAdrian Chadd  * offset into account.
56461410ca56SAdrian Chadd  *
56471410ca56SAdrian Chadd  * The quiet IE doesn't control the /now/ beacon interval - it
56481410ca56SAdrian Chadd  * controls the upcoming beacon interval.  So, when tbtt=1,
56491410ca56SAdrian Chadd  * the quiet element programming shall be for the next beacon
56501410ca56SAdrian Chadd  * interval.  There's no tbtt=0 behaviour defined, so don't.
56511410ca56SAdrian Chadd  *
56521410ca56SAdrian Chadd  * Since we're programming the next quiet interval, we have
56531410ca56SAdrian Chadd  * to keep in mind what we will see when the next beacon
56541410ca56SAdrian Chadd  * is received with potentially a quiet IE.  For example, if
56551410ca56SAdrian Chadd  * quiet_period is 1, then we are always getting a quiet interval
56561410ca56SAdrian Chadd  * each TBTT - so if we just program it in upon each beacon received,
56571410ca56SAdrian Chadd  * it will constantly reflect the "next" TBTT and we will never
56581410ca56SAdrian Chadd  * let the counter stay programmed correctly.
56591410ca56SAdrian Chadd  *
56601410ca56SAdrian Chadd  * So:
56611410ca56SAdrian Chadd  * + the first time we see the quiet IE, program it and store
56621410ca56SAdrian Chadd  *   the details somewhere;
56631410ca56SAdrian Chadd  * + if the quiet parameters don't change (ie, period/duration/offset)
56641410ca56SAdrian Chadd  *   then just leave the programming enabled;
56651410ca56SAdrian Chadd  * + (we can "skip" beacons, so don't try to enforce tbttcount unless
56661410ca56SAdrian Chadd  *   you're willing to also do the skipped beacon math);
56671410ca56SAdrian Chadd  * + if the quiet IE is removed, then halt quiet time.
56681410ca56SAdrian Chadd  */
56691410ca56SAdrian Chadd static int
ath_set_quiet_ie(struct ieee80211_node * ni,uint8_t * ie)56701410ca56SAdrian Chadd ath_set_quiet_ie(struct ieee80211_node *ni, uint8_t *ie)
56711410ca56SAdrian Chadd {
56721410ca56SAdrian Chadd 	struct ieee80211_quiet_ie *q;
56731410ca56SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
56741410ca56SAdrian Chadd 	struct ath_vap *avp = ATH_VAP(vap);
56751410ca56SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
56761410ca56SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
56771410ca56SAdrian Chadd 
56781410ca56SAdrian Chadd 	if (vap->iv_opmode != IEEE80211_M_STA)
56791410ca56SAdrian Chadd 		return (0);
56801410ca56SAdrian Chadd 
56811410ca56SAdrian Chadd 	/* Verify we have a quiet time IE */
56821410ca56SAdrian Chadd 	if (ie == NULL) {
56831410ca56SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_QUIETIE,
56841410ca56SAdrian Chadd 		    "%s: called; NULL IE, disabling\n", __func__);
56851410ca56SAdrian Chadd 
56861410ca56SAdrian Chadd 		ath_hal_set_quiet(sc->sc_ah, 0, 0, 0, HAL_QUIET_DISABLE);
56871410ca56SAdrian Chadd 		memset(&avp->quiet_ie, 0, sizeof(avp->quiet_ie));
56881410ca56SAdrian Chadd 		return (0);
56891410ca56SAdrian Chadd 	}
56901410ca56SAdrian Chadd 
56911410ca56SAdrian Chadd 	/* If we do, verify it's actually legit */
56921410ca56SAdrian Chadd 	if (ie[0] != IEEE80211_ELEMID_QUIET)
56931410ca56SAdrian Chadd 		return 0;
56941410ca56SAdrian Chadd 	if (ie[1] != 6)
56951410ca56SAdrian Chadd 		return 0;
56961410ca56SAdrian Chadd 
56971410ca56SAdrian Chadd 	/* Note: this belongs in net80211, parsed out and everything */
56981410ca56SAdrian Chadd 	q = (void *) ie;
56991410ca56SAdrian Chadd 
57001410ca56SAdrian Chadd 	/*
57011410ca56SAdrian Chadd 	 * Compare what we have stored to what we last saw.
57021410ca56SAdrian Chadd 	 * If they're the same then don't program in anything.
57031410ca56SAdrian Chadd 	 */
57041410ca56SAdrian Chadd 	if ((q->period == avp->quiet_ie.period) &&
57051410ca56SAdrian Chadd 	    (le16dec(&q->duration) == le16dec(&avp->quiet_ie.duration)) &&
57061410ca56SAdrian Chadd 	    (le16dec(&q->offset) == le16dec(&avp->quiet_ie.offset)))
57071410ca56SAdrian Chadd 		return (0);
57081410ca56SAdrian Chadd 
57091410ca56SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_QUIETIE,
57101410ca56SAdrian Chadd 	    "%s: called; tbttcount=%d, period=%d, duration=%d, offset=%d\n",
57111410ca56SAdrian Chadd 	    __func__,
57121410ca56SAdrian Chadd 	    (int) q->tbttcount,
57131410ca56SAdrian Chadd 	    (int) q->period,
57141410ca56SAdrian Chadd 	    (int) le16dec(&q->duration),
57151410ca56SAdrian Chadd 	    (int) le16dec(&q->offset));
57161410ca56SAdrian Chadd 
57171410ca56SAdrian Chadd 	/*
57181410ca56SAdrian Chadd 	 * Don't program in garbage values.
57191410ca56SAdrian Chadd 	 */
57201410ca56SAdrian Chadd 	if ((le16dec(&q->duration) == 0) ||
57211410ca56SAdrian Chadd 	    (le16dec(&q->duration) >= ni->ni_intval)) {
57221410ca56SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_QUIETIE,
57231410ca56SAdrian Chadd 		    "%s: invalid duration (%d)\n", __func__,
57241410ca56SAdrian Chadd 		    le16dec(&q->duration));
57251410ca56SAdrian Chadd 		    return (0);
57261410ca56SAdrian Chadd 	}
57271410ca56SAdrian Chadd 	/*
57281410ca56SAdrian Chadd 	 * Can have a 0 offset, but not a duration - so just check
57291410ca56SAdrian Chadd 	 * they don't exceed the intval.
57301410ca56SAdrian Chadd 	 */
57311410ca56SAdrian Chadd 	if (le16dec(&q->duration) + le16dec(&q->offset) >= ni->ni_intval) {
57321410ca56SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_QUIETIE,
57331410ca56SAdrian Chadd 		    "%s: invalid duration + offset (%d+%d)\n", __func__,
57341410ca56SAdrian Chadd 		    le16dec(&q->duration),
57351410ca56SAdrian Chadd 		    le16dec(&q->offset));
57361410ca56SAdrian Chadd 		    return (0);
57371410ca56SAdrian Chadd 	}
57381410ca56SAdrian Chadd 	if (q->tbttcount == 0) {
57391410ca56SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_QUIETIE,
57401410ca56SAdrian Chadd 		    "%s: invalid tbttcount (0)\n", __func__);
57411410ca56SAdrian Chadd 		    return (0);
57421410ca56SAdrian Chadd 	}
57431410ca56SAdrian Chadd 	if (q->period == 0) {
57441410ca56SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_QUIETIE,
57451410ca56SAdrian Chadd 		    "%s: invalid period (0)\n", __func__);
57461410ca56SAdrian Chadd 		    return (0);
57471410ca56SAdrian Chadd 	}
57481410ca56SAdrian Chadd 
57491410ca56SAdrian Chadd 	/*
57501410ca56SAdrian Chadd 	 * This is a new quiet time IE config, so wait until tbttcount
57511410ca56SAdrian Chadd 	 * is equal to 1, and program it in.
57521410ca56SAdrian Chadd 	 */
57531410ca56SAdrian Chadd 	if (q->tbttcount == 1) {
57541410ca56SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_QUIETIE,
57551410ca56SAdrian Chadd 		    "%s: programming\n", __func__);
57561410ca56SAdrian Chadd 		ath_hal_set_quiet(sc->sc_ah,
57571410ca56SAdrian Chadd 		    q->period * ni->ni_intval,	/* convert to TU */
57581410ca56SAdrian Chadd 		    le16dec(&q->duration),	/* already in TU */
57591410ca56SAdrian Chadd 		    le16dec(&q->offset) + ni->ni_intval,
57601410ca56SAdrian Chadd 		    HAL_QUIET_ENABLE | HAL_QUIET_ADD_CURRENT_TSF);
57611410ca56SAdrian Chadd 		/*
57621410ca56SAdrian Chadd 		 * Note: no HAL_QUIET_ADD_SWBA_RESP_TIME; as this is for
57631410ca56SAdrian Chadd 		 * STA mode
57641410ca56SAdrian Chadd 		 */
57651410ca56SAdrian Chadd 
57661410ca56SAdrian Chadd 		/* Update local state */
57671410ca56SAdrian Chadd 		memcpy(&avp->quiet_ie, ie, sizeof(struct ieee80211_quiet_ie));
57681410ca56SAdrian Chadd 	}
57691410ca56SAdrian Chadd 
57701410ca56SAdrian Chadd 	return (0);
57711410ca56SAdrian Chadd }
57721410ca56SAdrian Chadd 
577368e8e04eSSam Leffler static void
ath_set_channel(struct ieee80211com * ic)577468e8e04eSSam Leffler ath_set_channel(struct ieee80211com *ic)
577568e8e04eSSam Leffler {
57763797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
577768e8e04eSSam Leffler 
5778f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
5779f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
5780f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
5781f5c30c4eSAdrian Chadd 
578268e8e04eSSam Leffler 	(void) ath_chan_set(sc, ic->ic_curchan);
578368e8e04eSSam Leffler 	/*
578468e8e04eSSam Leffler 	 * If we are returning to our bss channel then mark state
578568e8e04eSSam Leffler 	 * so the next recv'd beacon's tsf will be used to sync the
578668e8e04eSSam Leffler 	 * beacon timers.  Note that since we only hear beacons in
578768e8e04eSSam Leffler 	 * sta/ibss mode this has no effect in other operating modes.
578868e8e04eSSam Leffler 	 */
5789a887b1e3SAdrian Chadd 	ATH_LOCK(sc);
579068e8e04eSSam Leffler 	if (!sc->sc_scanning && ic->ic_curchan == ic->ic_bsschan)
579168e8e04eSSam Leffler 		sc->sc_syncbeacon = 1;
5792f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
5793a887b1e3SAdrian Chadd 	ATH_UNLOCK(sc);
579468e8e04eSSam Leffler }
579568e8e04eSSam Leffler 
5796b032f27cSSam Leffler /*
5797b032f27cSSam Leffler  * Walk the vap list and check if there any vap's in RUN state.
5798b032f27cSSam Leffler  */
57995591b213SSam Leffler static int
ath_isanyrunningvaps(struct ieee80211vap * this)5800b032f27cSSam Leffler ath_isanyrunningvaps(struct ieee80211vap *this)
58015591b213SSam Leffler {
5802b032f27cSSam Leffler 	struct ieee80211com *ic = this->iv_ic;
5803b032f27cSSam Leffler 	struct ieee80211vap *vap;
5804b032f27cSSam Leffler 
5805b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
5806b032f27cSSam Leffler 
5807b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
5808309a3e45SSam Leffler 		if (vap != this && vap->iv_state >= IEEE80211_S_RUN)
5809b032f27cSSam Leffler 			return 1;
5810b032f27cSSam Leffler 	}
5811b032f27cSSam Leffler 	return 0;
5812b032f27cSSam Leffler }
5813b032f27cSSam Leffler 
5814b032f27cSSam Leffler static int
ath_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)5815b032f27cSSam Leffler ath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
5816b032f27cSSam Leffler {
5817b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
58183797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
5819b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
582045bbf62fSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5821b032f27cSSam Leffler 	struct ieee80211_node *ni = NULL;
582268e8e04eSSam Leffler 	int i, error, stamode;
58235591b213SSam Leffler 	u_int32_t rfilt;
5824f52efb6dSAdrian Chadd 	int csa_run_transition = 0;
5825f5c30c4eSAdrian Chadd 	enum ieee80211_state ostate = vap->iv_state;
5826a74ebfe5SAdrian Chadd 
58275591b213SSam Leffler 	static const HAL_LED_STATE leds[] = {
58285591b213SSam Leffler 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
58295591b213SSam Leffler 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
58305591b213SSam Leffler 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
58315591b213SSam Leffler 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
583277d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CAC */
58335591b213SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
583477d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CSA */
583577d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_SLEEP */
58365591b213SSam Leffler 	};
58375591b213SSam Leffler 
5838c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
5839f5c30c4eSAdrian Chadd 		ieee80211_state_name[ostate],
5840c42a7b7eSSam Leffler 		ieee80211_state_name[nstate]);
58415591b213SSam Leffler 
5842107fdf96SAdrian Chadd 	/*
5843107fdf96SAdrian Chadd 	 * net80211 _should_ have the comlock asserted at this point.
5844107fdf96SAdrian Chadd 	 * There are some comments around the calls to vap->iv_newstate
5845107fdf96SAdrian Chadd 	 * which indicate that it (newstate) may end up dropping the
5846107fdf96SAdrian Chadd 	 * lock.  This and the subsequent lock assert check after newstate
5847107fdf96SAdrian Chadd 	 * are an attempt to catch these and figure out how/why.
5848107fdf96SAdrian Chadd 	 */
5849107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
5850107fdf96SAdrian Chadd 
5851f5c30c4eSAdrian Chadd 	/* Before we touch the hardware - wake it up */
5852f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
58537d567ed6SAdrian Chadd 	/*
58547d567ed6SAdrian Chadd 	 * If the NIC is in anything other than SLEEP state,
58557d567ed6SAdrian Chadd 	 * we need to ensure that self-generated frames are
58567d567ed6SAdrian Chadd 	 * set for PWRMGT=0.  Otherwise we may end up with
58577d567ed6SAdrian Chadd 	 * strange situations.
58587d567ed6SAdrian Chadd 	 *
58597d567ed6SAdrian Chadd 	 * XXX TODO: is this actually the case? :-)
58607d567ed6SAdrian Chadd 	 */
58617d567ed6SAdrian Chadd 	if (nstate != IEEE80211_S_SLEEP)
58627d567ed6SAdrian Chadd 		ath_power_setselfgen(sc, HAL_PM_AWAKE);
58637d567ed6SAdrian Chadd 
58647d567ed6SAdrian Chadd 	/*
58657d567ed6SAdrian Chadd 	 * Now, wake the thing up.
58667d567ed6SAdrian Chadd 	 */
5867f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
58687707f31dSAdrian Chadd 
58697707f31dSAdrian Chadd 	/*
58707707f31dSAdrian Chadd 	 * And stop the calibration callout whilst we have
58717707f31dSAdrian Chadd 	 * ATH_LOCK held.
58727707f31dSAdrian Chadd 	 */
58737707f31dSAdrian Chadd 	callout_stop(&sc->sc_cal_ch);
5874f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
5875f5c30c4eSAdrian Chadd 
5876f5c30c4eSAdrian Chadd 	if (ostate == IEEE80211_S_CSA && nstate == IEEE80211_S_RUN)
5877f52efb6dSAdrian Chadd 		csa_run_transition = 1;
5878f52efb6dSAdrian Chadd 
58795591b213SSam Leffler 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
58805591b213SSam Leffler 
5881b032f27cSSam Leffler 	if (nstate == IEEE80211_S_SCAN) {
588258769f58SSam Leffler 		/*
5883b032f27cSSam Leffler 		 * Scanning: turn off beacon miss and don't beacon.
5884b032f27cSSam Leffler 		 * Mark beacon state so when we reach RUN state we'll
5885b032f27cSSam Leffler 		 * [re]setup beacons.  Unblock the task q thread so
5886b032f27cSSam Leffler 		 * deferred interrupt processing is done.
588758769f58SSam Leffler 		 */
5888f5c30c4eSAdrian Chadd 
5889f5c30c4eSAdrian Chadd 		/* Ensure we stay awake during scan */
5890f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
58917d567ed6SAdrian Chadd 		ath_power_setselfgen(sc, HAL_PM_AWAKE);
58928c03e55dSAdrian Chadd 		ath_power_setpower(sc, HAL_PM_AWAKE, 1);
5893f5c30c4eSAdrian Chadd 		ATH_UNLOCK(sc);
5894f5c30c4eSAdrian Chadd 
5895b032f27cSSam Leffler 		ath_hal_intrset(ah,
5896b032f27cSSam Leffler 		    sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
58975591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5898b032f27cSSam Leffler 		sc->sc_beacons = 0;
5899b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
59005591b213SSam Leffler 	}
59015591b213SSam Leffler 
590280767531SAdrian Chadd 	ni = ieee80211_ref_node(vap->iv_bss);
590368e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5904b032f27cSSam Leffler 	stamode = (vap->iv_opmode == IEEE80211_M_STA ||
59057b916f89SSam Leffler 		   vap->iv_opmode == IEEE80211_M_AHDEMO ||
5906b032f27cSSam Leffler 		   vap->iv_opmode == IEEE80211_M_IBSS);
5907f5c30c4eSAdrian Chadd 
5908f5c30c4eSAdrian Chadd 	/*
5909f5c30c4eSAdrian Chadd 	 * XXX Dont need to do this (and others) if we've transitioned
5910f5c30c4eSAdrian Chadd 	 * from SLEEP->RUN.
5911f5c30c4eSAdrian Chadd 	 */
591268e8e04eSSam Leffler 	if (stamode && nstate == IEEE80211_S_RUN) {
591368e8e04eSSam Leffler 		sc->sc_curaid = ni->ni_associd;
591468e8e04eSSam Leffler 		IEEE80211_ADDR_COPY(sc->sc_curbssid, ni->ni_bssid);
5915b032f27cSSam Leffler 		ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
5916b032f27cSSam Leffler 	}
591768e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
5918b032f27cSSam Leffler 	   __func__, rfilt, ether_sprintf(sc->sc_curbssid), sc->sc_curaid);
591968e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
592068e8e04eSSam Leffler 
5921b032f27cSSam Leffler 	/* XXX is this to restore keycache on resume? */
5922b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_STA &&
5923b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY)) {
59245591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
59255591b213SSam Leffler 			if (ath_hal_keyisvalid(ah, i))
592668e8e04eSSam Leffler 				ath_hal_keysetmac(ah, i, ni->ni_bssid);
59275591b213SSam Leffler 	}
5928b032f27cSSam Leffler 
5929b032f27cSSam Leffler 	/*
5930b032f27cSSam Leffler 	 * Invoke the parent method to do net80211 work.
5931b032f27cSSam Leffler 	 */
5932b032f27cSSam Leffler 	error = avp->av_newstate(vap, nstate, arg);
5933b032f27cSSam Leffler 	if (error != 0)
5934b032f27cSSam Leffler 		goto bad;
5935c42a7b7eSSam Leffler 
5936107fdf96SAdrian Chadd 	/*
5937107fdf96SAdrian Chadd 	 * See above: ensure av_newstate() doesn't drop the lock
5938107fdf96SAdrian Chadd 	 * on us.
5939107fdf96SAdrian Chadd 	 */
5940107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
5941107fdf96SAdrian Chadd 
594246be12aeSAdrian Chadd 	/*
594346be12aeSAdrian Chadd 	 * XXX TODO: if nstate is _S_CAC, then we should disable
594446be12aeSAdrian Chadd 	 * ACK processing until CAC is completed.
594546be12aeSAdrian Chadd 	 */
594646be12aeSAdrian Chadd 
594746be12aeSAdrian Chadd 	/*
594846be12aeSAdrian Chadd 	 * XXX TODO: if we're on a passive channel, then we should
594946be12aeSAdrian Chadd 	 * not allow any ACKs or self-generated frames until we hear
595046be12aeSAdrian Chadd 	 * a beacon.  Unfortunately there isn't a notification from
595146be12aeSAdrian Chadd 	 * net80211 so perhaps we could slot that particular check
595246be12aeSAdrian Chadd 	 * into the mgmt receive path and just ensure that we clear
595346be12aeSAdrian Chadd 	 * it on RX of beacons in passive mode (and only clear it
595446be12aeSAdrian Chadd 	 * once, obviously.)
595546be12aeSAdrian Chadd 	 */
595646be12aeSAdrian Chadd 
595746be12aeSAdrian Chadd 	/*
595846be12aeSAdrian Chadd 	 * XXX TODO: net80211 should be tracking whether channels
595946be12aeSAdrian Chadd 	 * have heard beacons and are thus considered "OK" for
596046be12aeSAdrian Chadd 	 * transmitting - and then inform the driver about this
596146be12aeSAdrian Chadd 	 * state change.  That way if we hear an AP go quiet
596246be12aeSAdrian Chadd 	 * (and nothing else is beaconing on a channel) the
596346be12aeSAdrian Chadd 	 * channel can go back to being passive until another
596446be12aeSAdrian Chadd 	 * beacon is heard.
596546be12aeSAdrian Chadd 	 */
596646be12aeSAdrian Chadd 
596746be12aeSAdrian Chadd 	/*
596846be12aeSAdrian Chadd 	 * XXX TODO: if nstate is _S_CAC, then we should disable
596946be12aeSAdrian Chadd 	 * ACK processing until CAC is completed.
597046be12aeSAdrian Chadd 	 */
597146be12aeSAdrian Chadd 
597246be12aeSAdrian Chadd 	/*
597346be12aeSAdrian Chadd 	 * XXX TODO: if we're on a passive channel, then we should
597446be12aeSAdrian Chadd 	 * not allow any ACKs or self-generated frames until we hear
597546be12aeSAdrian Chadd 	 * a beacon.  Unfortunately there isn't a notification from
597646be12aeSAdrian Chadd 	 * net80211 so perhaps we could slot that particular check
597746be12aeSAdrian Chadd 	 * into the mgmt receive path and just ensure that we clear
597846be12aeSAdrian Chadd 	 * it on RX of beacons in passive mode (and only clear it
597946be12aeSAdrian Chadd 	 * once, obviously.)
598046be12aeSAdrian Chadd 	 */
598146be12aeSAdrian Chadd 
598246be12aeSAdrian Chadd 	/*
598346be12aeSAdrian Chadd 	 * XXX TODO: net80211 should be tracking whether channels
598446be12aeSAdrian Chadd 	 * have heard beacons and are thus considered "OK" for
598546be12aeSAdrian Chadd 	 * transmitting - and then inform the driver about this
598646be12aeSAdrian Chadd 	 * state change.  That way if we hear an AP go quiet
598746be12aeSAdrian Chadd 	 * (and nothing else is beaconing on a channel) the
598846be12aeSAdrian Chadd 	 * channel can go back to being passive until another
598946be12aeSAdrian Chadd 	 * beacon is heard.
599046be12aeSAdrian Chadd 	 */
599146be12aeSAdrian Chadd 
599268e8e04eSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
5993b032f27cSSam Leffler 		/* NB: collect bss node again, it may have changed */
599480767531SAdrian Chadd 		ieee80211_free_node(ni);
599580767531SAdrian Chadd 		ni = ieee80211_ref_node(vap->iv_bss);
59965591b213SSam Leffler 
5997b032f27cSSam Leffler 		DPRINTF(sc, ATH_DEBUG_STATE,
5998b032f27cSSam Leffler 		    "%s(RUN): iv_flags 0x%08x bintvl %d bssid %s "
5999b032f27cSSam Leffler 		    "capinfo 0x%04x chan %d\n", __func__,
6000b032f27cSSam Leffler 		    vap->iv_flags, ni->ni_intval, ether_sprintf(ni->ni_bssid),
6001b032f27cSSam Leffler 		    ni->ni_capinfo, ieee80211_chan2ieee(ic, ic->ic_curchan));
6002b032f27cSSam Leffler 
6003b032f27cSSam Leffler 		switch (vap->iv_opmode) {
6004584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
600510ad9a77SSam Leffler 		case IEEE80211_M_AHDEMO:
600610ad9a77SSam Leffler 			if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
600710ad9a77SSam Leffler 				break;
600810ad9a77SSam Leffler 			/* fall thru... */
600910ad9a77SSam Leffler #endif
6010e8fd88a3SSam Leffler 		case IEEE80211_M_HOSTAP:
6011e8fd88a3SSam Leffler 		case IEEE80211_M_IBSS:
601259aa14a9SRui Paulo 		case IEEE80211_M_MBSS:
601346be12aeSAdrian Chadd 
601446be12aeSAdrian Chadd 			/*
601546be12aeSAdrian Chadd 			 * TODO: Enable ACK processing (ie, clear AR_DIAG_ACK_DIS.)
601646be12aeSAdrian Chadd 			 * For channels that are in CAC, we may have disabled
601746be12aeSAdrian Chadd 			 * this during CAC to ensure we don't ACK frames
601846be12aeSAdrian Chadd 			 * sent to us.
601946be12aeSAdrian Chadd 			 */
602046be12aeSAdrian Chadd 
60215591b213SSam Leffler 			/*
6022e8fd88a3SSam Leffler 			 * Allocate and setup the beacon frame.
6023e8fd88a3SSam Leffler 			 *
6024f818612bSSam Leffler 			 * Stop any previous beacon DMA.  This may be
6025f818612bSSam Leffler 			 * necessary, for example, when an ibss merge
6026f818612bSSam Leffler 			 * causes reconfiguration; there will be a state
6027f818612bSSam Leffler 			 * transition from RUN->RUN that means we may
6028f818612bSSam Leffler 			 * be called with beacon transmission active.
6029f818612bSSam Leffler 			 */
6030f818612bSSam Leffler 			ath_hal_stoptxdma(ah, sc->sc_bhalq);
6031b032f27cSSam Leffler 
60325591b213SSam Leffler 			error = ath_beacon_alloc(sc, ni);
60335591b213SSam Leffler 			if (error != 0)
60345591b213SSam Leffler 				goto bad;
60357a04dc27SSam Leffler 			/*
603680d939bfSSam Leffler 			 * If joining an adhoc network defer beacon timer
603780d939bfSSam Leffler 			 * configuration to the next beacon frame so we
603880d939bfSSam Leffler 			 * have a current TSF to use.  Otherwise we're
6039b032f27cSSam Leffler 			 * starting an ibss/bss so there's no need to delay;
6040b032f27cSSam Leffler 			 * if this is the first vap moving to RUN state, then
6041b032f27cSSam Leffler 			 * beacon state needs to be [re]configured.
60427a04dc27SSam Leffler 			 */
6043b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_IBSS &&
6044b032f27cSSam Leffler 			    ni->ni_tstamp.tsf != 0) {
604580d939bfSSam Leffler 				sc->sc_syncbeacon = 1;
6046b032f27cSSam Leffler 			} else if (!sc->sc_beacons) {
6047584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
604810ad9a77SSam Leffler 				if (vap->iv_caps & IEEE80211_C_TDMA)
604910ad9a77SSam Leffler 					ath_tdma_config(sc, vap);
605010ad9a77SSam Leffler 				else
605110ad9a77SSam Leffler #endif
6052b032f27cSSam Leffler 					ath_beacon_config(sc, vap);
6053b032f27cSSam Leffler 				sc->sc_beacons = 1;
6054b032f27cSSam Leffler 			}
6055e8fd88a3SSam Leffler 			break;
6056e8fd88a3SSam Leffler 		case IEEE80211_M_STA:
6057e8fd88a3SSam Leffler 			/*
605880d939bfSSam Leffler 			 * Defer beacon timer configuration to the next
605980d939bfSSam Leffler 			 * beacon frame so we have a current TSF to use
606080d939bfSSam Leffler 			 * (any TSF collected when scanning is likely old).
6061f52efb6dSAdrian Chadd 			 * However if it's due to a CSA -> RUN transition,
6062f52efb6dSAdrian Chadd 			 * force a beacon update so we pick up a lack of
6063f52efb6dSAdrian Chadd 			 * beacons from an AP in CAC and thus force a
6064f52efb6dSAdrian Chadd 			 * scan.
6065a74ebfe5SAdrian Chadd 			 *
6066a74ebfe5SAdrian Chadd 			 * And, there's also corner cases here where
6067a74ebfe5SAdrian Chadd 			 * after a scan, the AP may have disappeared.
6068a74ebfe5SAdrian Chadd 			 * In that case, we may not receive an actual
6069a74ebfe5SAdrian Chadd 			 * beacon to update the beacon timer and thus we
6070a74ebfe5SAdrian Chadd 			 * won't get notified of the missing beacons.
6071f858e928SAdrian Chadd 			 *
6072f858e928SAdrian Chadd 			 * Also, don't do any of this if we're not running
6073f858e928SAdrian Chadd 			 * with hardware beacon support, as that'll interfere
6074f858e928SAdrian Chadd 			 * with an AP VAP.
60757a04dc27SSam Leffler 			 */
6076f5c30c4eSAdrian Chadd 			if (ostate != IEEE80211_S_RUN &&
6077f5c30c4eSAdrian Chadd 			    ostate != IEEE80211_S_SLEEP) {
6078f858e928SAdrian Chadd 
6079f858e928SAdrian Chadd 				if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
6080f5c30c4eSAdrian Chadd 					DPRINTF(sc, ATH_DEBUG_BEACON,
6081f5c30c4eSAdrian Chadd 					    "%s: STA; syncbeacon=1\n", __func__);
608280d939bfSSam Leffler 					sc->sc_syncbeacon = 1;
6083f858e928SAdrian Chadd 					if (csa_run_transition)
6084f858e928SAdrian Chadd 						ath_beacon_config(sc, vap);
6085f858e928SAdrian Chadd 				}
6086f5c30c4eSAdrian Chadd 
60871410ca56SAdrian Chadd 				/* Quiet time handling - ensure we resync */
60881410ca56SAdrian Chadd 				memset(&avp->quiet_ie, 0, sizeof(avp->quiet_ie));
60891410ca56SAdrian Chadd 
6090a74ebfe5SAdrian Chadd 			/*
6091a74ebfe5SAdrian Chadd 			 * PR: kern/175227
6092a74ebfe5SAdrian Chadd 			 *
6093a74ebfe5SAdrian Chadd 			 * Reconfigure beacons during reset; as otherwise
6094a74ebfe5SAdrian Chadd 			 * we won't get the beacon timers reprogrammed
6095a74ebfe5SAdrian Chadd 			 * after a reset and thus we won't pick up a
6096a74ebfe5SAdrian Chadd 			 * beacon miss interrupt.
6097a74ebfe5SAdrian Chadd 			 *
6098a74ebfe5SAdrian Chadd 			 * Hopefully we'll see a beacon before the BMISS
6099a74ebfe5SAdrian Chadd 			 * timer fires (too often), leading to a STA
6100a74ebfe5SAdrian Chadd 			 * disassociation.
6101a74ebfe5SAdrian Chadd 			 */
6102f858e928SAdrian Chadd 				if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
6103a74ebfe5SAdrian Chadd 					sc->sc_beacons = 1;
6104f5c30c4eSAdrian Chadd 				}
6105f858e928SAdrian Chadd 			}
6106e8fd88a3SSam Leffler 			break;
6107b032f27cSSam Leffler 		case IEEE80211_M_MONITOR:
6108b032f27cSSam Leffler 			/*
6109b032f27cSSam Leffler 			 * Monitor mode vaps have only INIT->RUN and RUN->RUN
6110b032f27cSSam Leffler 			 * transitions so we must re-enable interrupts here to
6111b032f27cSSam Leffler 			 * handle the case of a single monitor mode vap.
6112b032f27cSSam Leffler 			 */
6113b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask);
6114b032f27cSSam Leffler 			break;
6115b032f27cSSam Leffler 		case IEEE80211_M_WDS:
6116b032f27cSSam Leffler 			break;
6117e8fd88a3SSam Leffler 		default:
6118e8fd88a3SSam Leffler 			break;
61195591b213SSam Leffler 		}
61205591b213SSam Leffler 		/*
61217b0c77ecSSam Leffler 		 * Let the hal process statistics collected during a
61227b0c77ecSSam Leffler 		 * scan so it can provide calibrated noise floor data.
61237b0c77ecSSam Leffler 		 */
61247b0c77ecSSam Leffler 		ath_hal_process_noisefloor(ah);
61257b0c77ecSSam Leffler 		/*
6126ffa2cab6SSam Leffler 		 * Reset rssi stats; maybe not the best place...
6127ffa2cab6SSam Leffler 		 */
6128ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
6129ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
6130ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
6131f5c30c4eSAdrian Chadd 
6132f5c30c4eSAdrian Chadd 		/*
6133f5c30c4eSAdrian Chadd 		 * Force awake for RUN mode.
6134f5c30c4eSAdrian Chadd 		 */
6135f5c30c4eSAdrian Chadd 		ATH_LOCK(sc);
61367d567ed6SAdrian Chadd 		ath_power_setselfgen(sc, HAL_PM_AWAKE);
61378c03e55dSAdrian Chadd 		ath_power_setpower(sc, HAL_PM_AWAKE, 1);
6138f5c30c4eSAdrian Chadd 
613945bbf62fSSam Leffler 		/*
6140b032f27cSSam Leffler 		 * Finally, start any timers and the task q thread
6141b032f27cSSam Leffler 		 * (in case we didn't go through SCAN state).
614245bbf62fSSam Leffler 		 */
61432dc7fcc4SSam Leffler 		if (ath_longcalinterval != 0) {
6144c42a7b7eSSam Leffler 			/* start periodic recalibration timer */
61452dc7fcc4SSam Leffler 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
61462dc7fcc4SSam Leffler 		} else {
61472dc7fcc4SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
61482dc7fcc4SSam Leffler 			    "%s: calibration disabled\n", __func__);
6149c42a7b7eSSam Leffler 		}
61507707f31dSAdrian Chadd 		ATH_UNLOCK(sc);
6151f5c30c4eSAdrian Chadd 
6152b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
6153b032f27cSSam Leffler 	} else if (nstate == IEEE80211_S_INIT) {
61541410ca56SAdrian Chadd 		/* Quiet time handling - ensure we resync */
61551410ca56SAdrian Chadd 		memset(&avp->quiet_ie, 0, sizeof(avp->quiet_ie));
61561410ca56SAdrian Chadd 
6157b032f27cSSam Leffler 		/*
6158b032f27cSSam Leffler 		 * If there are no vaps left in RUN state then
6159b032f27cSSam Leffler 		 * shutdown host/driver operation:
6160b032f27cSSam Leffler 		 * o disable interrupts
6161b032f27cSSam Leffler 		 * o disable the task queue thread
6162b032f27cSSam Leffler 		 * o mark beacon processing as stopped
6163b032f27cSSam Leffler 		 */
6164b032f27cSSam Leffler 		if (!ath_isanyrunningvaps(vap)) {
6165b032f27cSSam Leffler 			sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
6166b032f27cSSam Leffler 			/* disable interrupts  */
6167b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL);
6168b032f27cSSam Leffler 			taskqueue_block(sc->sc_tq);
6169b032f27cSSam Leffler 			sc->sc_beacons = 0;
6170b032f27cSSam Leffler 		}
61717d450faaSAdrian Chadd 
61727d450faaSAdrian Chadd 		/*
61737d450faaSAdrian Chadd 		 * For at least STA mode we likely should clear the ANI
61747d450faaSAdrian Chadd 		 * and NF calibration state and allow the NIC/HAL to figure
61757d450faaSAdrian Chadd 		 * out optimal parameters at runtime.  Otherwise if we
61767d450faaSAdrian Chadd 		 * disassociate due to interference / deafness it may persist
61777d450faaSAdrian Chadd 		 * when we reconnect.
61787d450faaSAdrian Chadd 		 *
61797d450faaSAdrian Chadd 		 * Note: may need to do this for other states too, not just
61807d450faaSAdrian Chadd 		 * _S_INIT.
61817d450faaSAdrian Chadd 		 */
6182584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
618310ad9a77SSam Leffler 		ath_hal_setcca(ah, AH_TRUE);
618410ad9a77SSam Leffler #endif
6185f5c30c4eSAdrian Chadd 	} else if (nstate == IEEE80211_S_SLEEP) {
6186f5c30c4eSAdrian Chadd 		/* We're going to sleep, so transition appropriately */
6187f5c30c4eSAdrian Chadd 		/* For now, only do this if we're a single STA vap */
6188f5c30c4eSAdrian Chadd 		if (sc->sc_nvaps == 1 &&
6189f5c30c4eSAdrian Chadd 		    vap->iv_opmode == IEEE80211_M_STA) {
6190f5c30c4eSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_BEACON, "%s: syncbeacon=%d\n", __func__, sc->sc_syncbeacon);
6191f5c30c4eSAdrian Chadd 			ATH_LOCK(sc);
61927d567ed6SAdrian Chadd 			/*
61937d567ed6SAdrian Chadd 			 * Always at least set the self-generated
61947d567ed6SAdrian Chadd 			 * frame config to set PWRMGT=1.
61957d567ed6SAdrian Chadd 			 */
61967d567ed6SAdrian Chadd 			ath_power_setselfgen(sc, HAL_PM_NETWORK_SLEEP);
61977d567ed6SAdrian Chadd 
61987d567ed6SAdrian Chadd 			/*
61997d567ed6SAdrian Chadd 			 * If we're not syncing beacons, transition
62007d567ed6SAdrian Chadd 			 * to NETWORK_SLEEP.
62017d567ed6SAdrian Chadd 			 *
62027d567ed6SAdrian Chadd 			 * We stay awake if syncbeacon > 0 in case
62037d567ed6SAdrian Chadd 			 * we need to listen for some beacons otherwise
62047d567ed6SAdrian Chadd 			 * our beacon timer config may be wrong.
62057d567ed6SAdrian Chadd 			 */
6206f5c30c4eSAdrian Chadd 			if (sc->sc_syncbeacon == 0) {
62078c03e55dSAdrian Chadd 				ath_power_setpower(sc, HAL_PM_NETWORK_SLEEP, 1);
6208f5c30c4eSAdrian Chadd 			}
6209f5c30c4eSAdrian Chadd 			ATH_UNLOCK(sc);
6210f5c30c4eSAdrian Chadd 		}
62117d450faaSAdrian Chadd 
62127d450faaSAdrian Chadd 		/*
62137d450faaSAdrian Chadd 		 * Note - the ANI/calibration timer isn't re-enabled during
62147d450faaSAdrian Chadd 		 * network sleep for now.  One unfortunate side-effect is that
62157d450faaSAdrian Chadd 		 * the PHY/airtime statistics aren't gathered on the channel
62167d450faaSAdrian Chadd 		 * but I haven't yet tested to see if reading those registers
62177d450faaSAdrian Chadd 		 * CAN occur during network sleep.
62187d450faaSAdrian Chadd 		 *
62197d450faaSAdrian Chadd 		 * This should be revisited in a future commit, even if it's
62207d450faaSAdrian Chadd 		 * just to split out the airtime polling from ANI/calibration.
62217d450faaSAdrian Chadd 		 */
62221410ca56SAdrian Chadd 	} else if (nstate == IEEE80211_S_SCAN) {
62231410ca56SAdrian Chadd 		/* Quiet time handling - ensure we resync */
62241410ca56SAdrian Chadd 		memset(&avp->quiet_ie, 0, sizeof(avp->quiet_ie));
62257d450faaSAdrian Chadd 
62267d450faaSAdrian Chadd 		/*
62277d450faaSAdrian Chadd 		 * If we're in scan mode then startpcureceive() is
62287d450faaSAdrian Chadd 		 * hopefully being called with "reset ANI" for this channel;
62297d450faaSAdrian Chadd 		 * but once we attempt to reassociate we program in the previous
62307d450faaSAdrian Chadd 		 * ANI values and.. not do any calibration until we're running.
62317d450faaSAdrian Chadd 		 * This may mean we stay deaf unless we can associate successfully.
62327d450faaSAdrian Chadd 		 *
62337d450faaSAdrian Chadd 		 * So do kick off the cal timer to get NF/ANI going.
62347d450faaSAdrian Chadd 		 */
62357d450faaSAdrian Chadd 		ATH_LOCK(sc);
62367d450faaSAdrian Chadd 		if (ath_longcalinterval != 0) {
62377d450faaSAdrian Chadd 			/* start periodic recalibration timer */
62387d450faaSAdrian Chadd 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
62397d450faaSAdrian Chadd 		} else {
62407d450faaSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
62417d450faaSAdrian Chadd 			    "%s: calibration disabled\n", __func__);
62427d450faaSAdrian Chadd 		}
62437d450faaSAdrian Chadd 		ATH_UNLOCK(sc);
6244b032f27cSSam Leffler 	}
62455591b213SSam Leffler bad:
624680767531SAdrian Chadd 	ieee80211_free_node(ni);
6247f5c30c4eSAdrian Chadd 
6248f5c30c4eSAdrian Chadd 	/*
6249f5c30c4eSAdrian Chadd 	 * Restore the power state - either to what it was, or
6250f5c30c4eSAdrian Chadd 	 * to network_sleep if it's alright.
6251f5c30c4eSAdrian Chadd 	 */
6252f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
6253f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
6254f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
62555591b213SSam Leffler 	return error;
62565591b213SSam Leffler }
62575591b213SSam Leffler 
62585591b213SSam Leffler /*
6259e8fd88a3SSam Leffler  * Allocate a key cache slot to the station so we can
6260e8fd88a3SSam Leffler  * setup a mapping from key index to node. The key cache
6261e8fd88a3SSam Leffler  * slot is needed for managing antenna state and for
6262e8fd88a3SSam Leffler  * compression when stations do not use crypto.  We do
6263e8fd88a3SSam Leffler  * it uniliaterally here; if crypto is employed this slot
6264e8fd88a3SSam Leffler  * will be reassigned.
6265e8fd88a3SSam Leffler  */
6266e8fd88a3SSam Leffler static void
ath_setup_stationkey(struct ieee80211_node * ni)6267e8fd88a3SSam Leffler ath_setup_stationkey(struct ieee80211_node *ni)
6268e8fd88a3SSam Leffler {
6269b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
62703797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
6271c1225b52SSam Leffler 	ieee80211_keyix keyix, rxkeyix;
6272e8fd88a3SSam Leffler 
627380767531SAdrian Chadd 	/* XXX should take a locked ref to vap->iv_bss */
6274b032f27cSSam Leffler 	if (!ath_key_alloc(vap, &ni->ni_ucastkey, &keyix, &rxkeyix)) {
6275e8fd88a3SSam Leffler 		/*
6276e8fd88a3SSam Leffler 		 * Key cache is full; we'll fall back to doing
6277e8fd88a3SSam Leffler 		 * the more expensive lookup in software.  Note
6278e8fd88a3SSam Leffler 		 * this also means no h/w compression.
6279e8fd88a3SSam Leffler 		 */
6280e8fd88a3SSam Leffler 		/* XXX msg+statistic */
6281e8fd88a3SSam Leffler 	} else {
6282c1225b52SSam Leffler 		/* XXX locking? */
6283e8fd88a3SSam Leffler 		ni->ni_ucastkey.wk_keyix = keyix;
6284c1225b52SSam Leffler 		ni->ni_ucastkey.wk_rxkeyix = rxkeyix;
628533052833SSam Leffler 		/* NB: must mark device key to get called back on delete */
628633052833SSam Leffler 		ni->ni_ucastkey.wk_flags |= IEEE80211_KEY_DEVKEY;
6287d3ac945bSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_ucastkey.wk_macaddr, ni->ni_macaddr);
6288e8fd88a3SSam Leffler 		/* NB: this will create a pass-thru key entry */
628955c7b877SAdrian Chadd 		ath_keyset(sc, vap, &ni->ni_ucastkey, vap->iv_bss);
6290e8fd88a3SSam Leffler 	}
6291e8fd88a3SSam Leffler }
6292e8fd88a3SSam Leffler 
6293e8fd88a3SSam Leffler /*
62945591b213SSam Leffler  * Setup driver-specific state for a newly associated node.
62955591b213SSam Leffler  * Note that we're called also on a re-associate, the isnew
62965591b213SSam Leffler  * param tells us if this is the first time or not.
62975591b213SSam Leffler  */
62985591b213SSam Leffler static void
ath_newassoc(struct ieee80211_node * ni,int isnew)6299e9962332SSam Leffler ath_newassoc(struct ieee80211_node *ni, int isnew)
63005591b213SSam Leffler {
6301b032f27cSSam Leffler 	struct ath_node *an = ATH_NODE(ni);
6302b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
63033797bf08SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_softc;
6304c62362cbSSam Leffler 	const struct ieee80211_txparam *tp = ni->ni_txparms;
63055591b213SSam Leffler 
6306ab06fdf2SSam Leffler 	an->an_mcastrix = ath_tx_findrix(sc, tp->mcastrate);
6307ab06fdf2SSam Leffler 	an->an_mgmtrix = ath_tx_findrix(sc, tp->mgmtrate);
6308b032f27cSSam Leffler 
6309f5c30c4eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: reassoc; isnew=%d, is_powersave=%d\n",
6310f5c30c4eSAdrian Chadd 	    __func__,
6311f5c30c4eSAdrian Chadd 	    ni->ni_macaddr,
6312f5c30c4eSAdrian Chadd 	    ":",
6313f5c30c4eSAdrian Chadd 	    isnew,
6314f5c30c4eSAdrian Chadd 	    an->an_is_powersave);
6315f5c30c4eSAdrian Chadd 
6316656380e7SAdrian Chadd 	ATH_NODE_LOCK(an);
6317b032f27cSSam Leffler 	ath_rate_newassoc(sc, an, isnew);
6318656380e7SAdrian Chadd 	ATH_NODE_UNLOCK(an);
631932da86a0SAdrian Chadd 
6320e8fd88a3SSam Leffler 	if (isnew &&
6321b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey &&
6322b032f27cSSam Leffler 	    ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE)
6323e8fd88a3SSam Leffler 		ath_setup_stationkey(ni);
63244bed2b67SAdrian Chadd 
63254bed2b67SAdrian Chadd 	/*
63264bed2b67SAdrian Chadd 	 * If we're reassociating, make sure that any paused queues
63274bed2b67SAdrian Chadd 	 * get unpaused.
63284bed2b67SAdrian Chadd 	 *
6329f6b6084bSPedro F. Giffuni 	 * Now, we may have frames in the hardware queue for this node.
63304bed2b67SAdrian Chadd 	 * So if we are reassociating and there are frames in the queue,
63314bed2b67SAdrian Chadd 	 * we need to go through the cleanup path to ensure that they're
63324bed2b67SAdrian Chadd 	 * marked as non-aggregate.
63334bed2b67SAdrian Chadd 	 */
63344bed2b67SAdrian Chadd 	if (! isnew) {
633532da86a0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE,
63364bed2b67SAdrian Chadd 		    "%s: %6D: reassoc; is_powersave=%d\n",
63374bed2b67SAdrian Chadd 		    __func__,
63384bed2b67SAdrian Chadd 		    ni->ni_macaddr,
63394bed2b67SAdrian Chadd 		    ":",
63404bed2b67SAdrian Chadd 		    an->an_is_powersave);
63414bed2b67SAdrian Chadd 
63424bed2b67SAdrian Chadd 		/* XXX for now, we can't hold the lock across assoc */
63434bed2b67SAdrian Chadd 		ath_tx_node_reassoc(sc, an);
63444bed2b67SAdrian Chadd 
63454bed2b67SAdrian Chadd 		/* XXX for now, we can't hold the lock across wakeup */
63464bed2b67SAdrian Chadd 		if (an->an_is_powersave)
63474bed2b67SAdrian Chadd 			ath_tx_node_wakeup(sc, an);
63484bed2b67SAdrian Chadd 	}
6349e8fd88a3SSam Leffler }
63505591b213SSam Leffler 
63515591b213SSam Leffler static int
ath_setregdomain(struct ieee80211com * ic,struct ieee80211_regdomain * reg,int nchans,struct ieee80211_channel chans[])635259efa8b5SSam Leffler ath_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *reg,
6353b032f27cSSam Leffler 	int nchans, struct ieee80211_channel chans[])
6354b032f27cSSam Leffler {
63553797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
6356b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
635759efa8b5SSam Leffler 	HAL_STATUS status;
6358b032f27cSSam Leffler 
6359033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
636059efa8b5SSam Leffler 	    "%s: rd %u cc %u location %c%s\n",
636159efa8b5SSam Leffler 	    __func__, reg->regdomain, reg->country, reg->location,
636259efa8b5SSam Leffler 	    reg->ecm ? " ecm" : "");
6363033022a9SSam Leffler 
636459efa8b5SSam Leffler 	status = ath_hal_set_channels(ah, chans, nchans,
636559efa8b5SSam Leffler 	    reg->country, reg->regdomain);
636659efa8b5SSam Leffler 	if (status != HAL_OK) {
636759efa8b5SSam Leffler 		DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: failed, status %u\n",
636859efa8b5SSam Leffler 		    __func__, status);
636959efa8b5SSam Leffler 		return EINVAL;		/* XXX */
6370b032f27cSSam Leffler 	}
63718db87e40SAdrian Chadd 
6372b032f27cSSam Leffler 	return 0;
6373b032f27cSSam Leffler }
6374b032f27cSSam Leffler 
6375b032f27cSSam Leffler static void
ath_getradiocaps(struct ieee80211com * ic,int maxchans,int * nchans,struct ieee80211_channel chans[])6376b032f27cSSam Leffler ath_getradiocaps(struct ieee80211com *ic,
63775fe9f044SSam Leffler 	int maxchans, int *nchans, struct ieee80211_channel chans[])
6378b032f27cSSam Leffler {
63793797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
6380b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6381b032f27cSSam Leffler 
638259efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: use rd %u cc %d\n",
638359efa8b5SSam Leffler 	    __func__, SKU_DEBUG, CTRY_DEFAULT);
6384033022a9SSam Leffler 
638559efa8b5SSam Leffler 	/* XXX check return */
638659efa8b5SSam Leffler 	(void) ath_hal_getchannels(ah, chans, maxchans, nchans,
638759efa8b5SSam Leffler 	    HAL_MODE_ALL, CTRY_DEFAULT, SKU_DEBUG, AH_TRUE);
6388033022a9SSam Leffler 
6389b032f27cSSam Leffler }
6390b032f27cSSam Leffler 
6391b032f27cSSam Leffler static int
ath_getchannels(struct ath_softc * sc)6392b032f27cSSam Leffler ath_getchannels(struct ath_softc *sc)
6393b032f27cSSam Leffler {
63947a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
6395b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
639659efa8b5SSam Leffler 	HAL_STATUS status;
6397b032f27cSSam Leffler 
6398b032f27cSSam Leffler 	/*
639959efa8b5SSam Leffler 	 * Collect channel set based on EEPROM contents.
6400b032f27cSSam Leffler 	 */
640159efa8b5SSam Leffler 	status = ath_hal_init_channels(ah, ic->ic_channels, IEEE80211_CHAN_MAX,
640259efa8b5SSam Leffler 	    &ic->ic_nchans, HAL_MODE_ALL, CTRY_DEFAULT, SKU_NONE, AH_TRUE);
640359efa8b5SSam Leffler 	if (status != HAL_OK) {
640476e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev,
640576e6fd5dSGleb Smirnoff 		    "%s: unable to collect channel list from hal, status %d\n",
640676e6fd5dSGleb Smirnoff 		    __func__, status);
640759efa8b5SSam Leffler 		return EINVAL;
640859efa8b5SSam Leffler 	}
6409ca876918SSam Leffler 	(void) ath_hal_getregdomain(ah, &sc->sc_eerd);
6410ca876918SSam Leffler 	ath_hal_getcountrycode(ah, &sc->sc_eecc);	/* NB: cannot fail */
641159efa8b5SSam Leffler 	/* XXX map Atheros sku's to net80211 SKU's */
641259efa8b5SSam Leffler 	/* XXX net80211 types too small */
641359efa8b5SSam Leffler 	ic->ic_regdomain.regdomain = (uint16_t) sc->sc_eerd;
641459efa8b5SSam Leffler 	ic->ic_regdomain.country = (uint16_t) sc->sc_eecc;
641559efa8b5SSam Leffler 	ic->ic_regdomain.isocc[0] = ' ';	/* XXX don't know */
641659efa8b5SSam Leffler 	ic->ic_regdomain.isocc[1] = ' ';
641759efa8b5SSam Leffler 
6418b032f27cSSam Leffler 	ic->ic_regdomain.ecm = 1;
6419b032f27cSSam Leffler 	ic->ic_regdomain.location = 'I';
6420033022a9SSam Leffler 
6421033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
642259efa8b5SSam Leffler 	    "%s: eeprom rd %u cc %u (mapped rd %u cc %u) location %c%s\n",
6423033022a9SSam Leffler 	    __func__, sc->sc_eerd, sc->sc_eecc,
6424033022a9SSam Leffler 	    ic->ic_regdomain.regdomain, ic->ic_regdomain.country,
642559efa8b5SSam Leffler 	    ic->ic_regdomain.location, ic->ic_regdomain.ecm ? " ecm" : "");
64265591b213SSam Leffler 	return 0;
64275591b213SSam Leffler }
64285591b213SSam Leffler 
64296c4612b9SSam Leffler static int
ath_rate_setup(struct ath_softc * sc,u_int mode)64306c4612b9SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode)
64316c4612b9SSam Leffler {
64326c4612b9SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
64336c4612b9SSam Leffler 	const HAL_RATE_TABLE *rt;
64346c4612b9SSam Leffler 
64356c4612b9SSam Leffler 	switch (mode) {
64366c4612b9SSam Leffler 	case IEEE80211_MODE_11A:
64376c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A);
64386c4612b9SSam Leffler 		break;
6439724c193aSSam Leffler 	case IEEE80211_MODE_HALF:
6440aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_HALF_RATE);
6441aaa70f2fSSam Leffler 		break;
6442724c193aSSam Leffler 	case IEEE80211_MODE_QUARTER:
6443aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_QUARTER_RATE);
6444aaa70f2fSSam Leffler 		break;
64456c4612b9SSam Leffler 	case IEEE80211_MODE_11B:
64466c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11B);
64476c4612b9SSam Leffler 		break;
64486c4612b9SSam Leffler 	case IEEE80211_MODE_11G:
64496c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11G);
64506c4612b9SSam Leffler 		break;
64516c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_A:
645268e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108A);
64536c4612b9SSam Leffler 		break;
64546c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_G:
64556c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108G);
64566c4612b9SSam Leffler 		break;
645768e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
645868e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_TURBO);
645968e8e04eSSam Leffler 		break;
646068e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
646168e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NA_HT20);
646268e8e04eSSam Leffler 		break;
646368e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
646468e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NG_HT20);
646568e8e04eSSam Leffler 		break;
64666c4612b9SSam Leffler 	default:
64676c4612b9SSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
64686c4612b9SSam Leffler 			__func__, mode);
64696c4612b9SSam Leffler 		return 0;
64706c4612b9SSam Leffler 	}
64716c4612b9SSam Leffler 	sc->sc_rates[mode] = rt;
6472aaa70f2fSSam Leffler 	return (rt != NULL);
64735591b213SSam Leffler }
64745591b213SSam Leffler 
64755591b213SSam Leffler static void
ath_setcurmode(struct ath_softc * sc,enum ieee80211_phymode mode)64765591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
64775591b213SSam Leffler {
64783e50ec2cSSam Leffler 	/* NB: on/off times from the Atheros NDIS driver, w/ permission */
64793e50ec2cSSam Leffler 	static const struct {
64803e50ec2cSSam Leffler 		u_int		rate;		/* tx/rx 802.11 rate */
64813e50ec2cSSam Leffler 		u_int16_t	timeOn;		/* LED on time (ms) */
64823e50ec2cSSam Leffler 		u_int16_t	timeOff;	/* LED off time (ms) */
64833e50ec2cSSam Leffler 	} blinkrates[] = {
64843e50ec2cSSam Leffler 		{ 108,  40,  10 },
64853e50ec2cSSam Leffler 		{  96,  44,  11 },
64863e50ec2cSSam Leffler 		{  72,  50,  13 },
64873e50ec2cSSam Leffler 		{  48,  57,  14 },
64883e50ec2cSSam Leffler 		{  36,  67,  16 },
64893e50ec2cSSam Leffler 		{  24,  80,  20 },
64903e50ec2cSSam Leffler 		{  22, 100,  25 },
64913e50ec2cSSam Leffler 		{  18, 133,  34 },
64923e50ec2cSSam Leffler 		{  12, 160,  40 },
64933e50ec2cSSam Leffler 		{  10, 200,  50 },
64943e50ec2cSSam Leffler 		{   6, 240,  58 },
64953e50ec2cSSam Leffler 		{   4, 267,  66 },
64963e50ec2cSSam Leffler 		{   2, 400, 100 },
64973e50ec2cSSam Leffler 		{   0, 500, 130 },
6498724c193aSSam Leffler 		/* XXX half/quarter rates */
64993e50ec2cSSam Leffler 	};
65005591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
65013e50ec2cSSam Leffler 	int i, j;
65025591b213SSam Leffler 
65035591b213SSam Leffler 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
65045591b213SSam Leffler 	rt = sc->sc_rates[mode];
65055591b213SSam Leffler 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
6506180f268dSSam Leffler 	for (i = 0; i < rt->rateCount; i++) {
6507180f268dSSam Leffler 		uint8_t ieeerate = rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
6508180f268dSSam Leffler 		if (rt->info[i].phy != IEEE80211_T_HT)
6509180f268dSSam Leffler 			sc->sc_rixmap[ieeerate] = i;
6510180f268dSSam Leffler 		else
6511180f268dSSam Leffler 			sc->sc_rixmap[ieeerate | IEEE80211_RATE_MCS] = i;
6512180f268dSSam Leffler 	}
65131b1a8e41SSam Leffler 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
6514d6166defSAdrian Chadd 	for (i = 0; i < nitems(sc->sc_hwmap); i++) {
651546d4d74cSSam Leffler 		if (i >= rt->rateCount) {
65163e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledon = (500 * hz) / 1000;
65173e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledoff = (130 * hz) / 1000;
651816b4851aSSam Leffler 			continue;
65193e50ec2cSSam Leffler 		}
65203e50ec2cSSam Leffler 		sc->sc_hwmap[i].ieeerate =
652146d4d74cSSam Leffler 			rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
652246d4d74cSSam Leffler 		if (rt->info[i].phy == IEEE80211_T_HT)
652326041a14SSam Leffler 			sc->sc_hwmap[i].ieeerate |= IEEE80211_RATE_MCS;
6524d3be6f5bSSam Leffler 		sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
652546d4d74cSSam Leffler 		if (rt->info[i].shortPreamble ||
652646d4d74cSSam Leffler 		    rt->info[i].phy == IEEE80211_T_OFDM)
6527d3be6f5bSSam Leffler 			sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE;
65285463c4a4SSam Leffler 		sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags;
6529d6166defSAdrian Chadd 		for (j = 0; j < nitems(blinkrates)-1; j++)
65303e50ec2cSSam Leffler 			if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate)
65313e50ec2cSSam Leffler 				break;
65323e50ec2cSSam Leffler 		/* NB: this uses the last entry if the rate isn't found */
65333e50ec2cSSam Leffler 		/* XXX beware of overlow */
65343e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000;
65353e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000;
6536c42a7b7eSSam Leffler 	}
65375591b213SSam Leffler 	sc->sc_currates = rt;
65385591b213SSam Leffler 	sc->sc_curmode = mode;
65395591b213SSam Leffler 	/*
6540f6b6084bSPedro F. Giffuni 	 * All protection frames are transmitted at 2Mb/s for
6541c42a7b7eSSam Leffler 	 * 11g, otherwise at 1Mb/s.
65425591b213SSam Leffler 	 */
6543913a1ba1SSam Leffler 	if (mode == IEEE80211_MODE_11G)
6544ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*2);
6545913a1ba1SSam Leffler 	else
6546ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*1);
65474fa8d4efSDaniel Eischen 	/* NB: caller is responsible for resetting rate control state */
65485591b213SSam Leffler }
65495591b213SSam Leffler 
6550c42a7b7eSSam Leffler static void
ath_watchdog(void * arg)65512e986da5SSam Leffler ath_watchdog(void *arg)
6552c42a7b7eSSam Leffler {
65532e986da5SSam Leffler 	struct ath_softc *sc = arg;
65547a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
6555ef27340cSAdrian Chadd 	int do_reset = 0;
6556c42a7b7eSSam Leffler 
6557adcdc8f2SAdrian Chadd 	ATH_LOCK_ASSERT(sc);
65587707f31dSAdrian Chadd 
65592e986da5SSam Leffler 	if (sc->sc_wd_timer != 0 && --sc->sc_wd_timer == 0) {
6560459bc4f0SSam Leffler 		uint32_t hangs;
6561459bc4f0SSam Leffler 
6562f5c30c4eSAdrian Chadd 		ath_power_set_power_state(sc, HAL_PM_AWAKE);
6563f5c30c4eSAdrian Chadd 
6564459bc4f0SSam Leffler 		if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) &&
6565459bc4f0SSam Leffler 		    hangs != 0) {
656676e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev, "%s hang detected (0x%x)\n",
6567459bc4f0SSam Leffler 			    hangs & 0xff ? "bb" : "mac", hangs);
6568459bc4f0SSam Leffler 		} else
656976e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev, "device timeout\n");
6570ef27340cSAdrian Chadd 		do_reset = 1;
65717a79cebfSGleb Smirnoff 		counter_u64_add(ic->ic_oerrors, 1);
6572c42a7b7eSSam Leffler 		sc->sc_stats.ast_watchdog++;
6573f5c30c4eSAdrian Chadd 
6574f5c30c4eSAdrian Chadd 		ath_power_restore_power_state(sc);
6575c42a7b7eSSam Leffler 	}
6576ef27340cSAdrian Chadd 
6577ef27340cSAdrian Chadd 	/*
6578ef27340cSAdrian Chadd 	 * We can't hold the lock across the ath_reset() call.
6579d52f7132SAdrian Chadd 	 *
6580d52f7132SAdrian Chadd 	 * And since this routine can't hold a lock and sleep,
6581d52f7132SAdrian Chadd 	 * do the reset deferred.
6582ef27340cSAdrian Chadd 	 */
6583ef27340cSAdrian Chadd 	if (do_reset) {
6584d52f7132SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
6585ef27340cSAdrian Chadd 	}
6586ef27340cSAdrian Chadd 
65872e986da5SSam Leffler 	callout_schedule(&sc->sc_wd_ch, hz);
6588c42a7b7eSSam Leffler }
6589c42a7b7eSSam Leffler 
65907a79cebfSGleb Smirnoff static void
ath_parent(struct ieee80211com * ic)65917a79cebfSGleb Smirnoff ath_parent(struct ieee80211com *ic)
6592c42a7b7eSSam Leffler {
65933797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
65947a79cebfSGleb Smirnoff 	int error = EDOOFUS;
6595c42a7b7eSSam Leffler 
65967a79cebfSGleb Smirnoff 	ATH_LOCK(sc);
65977a79cebfSGleb Smirnoff 	if (ic->ic_nrunning > 0) {
6598c42a7b7eSSam Leffler 		/*
6599c42a7b7eSSam Leffler 		 * To avoid rescanning another access point,
6600c42a7b7eSSam Leffler 		 * do not call ath_init() here.  Instead,
6601c42a7b7eSSam Leffler 		 * only reflect promisc mode settings.
6602c42a7b7eSSam Leffler 		 */
66037a79cebfSGleb Smirnoff 		if (sc->sc_running) {
66044b734a1cSAdrian Chadd 			ath_power_set_power_state(sc, HAL_PM_AWAKE);
6605c42a7b7eSSam Leffler 			ath_mode_init(sc);
66064b734a1cSAdrian Chadd 			ath_power_restore_power_state(sc);
66077a79cebfSGleb Smirnoff 		} else if (!sc->sc_invalid) {
6608c42a7b7eSSam Leffler 			/*
6609c42a7b7eSSam Leffler 			 * Beware of being called during attach/detach
6610c42a7b7eSSam Leffler 			 * to reset promiscuous mode.  In that case we
6611c42a7b7eSSam Leffler 			 * will still be marked UP but not RUNNING.
6612c42a7b7eSSam Leffler 			 * However trying to re-init the interface
6613c42a7b7eSSam Leffler 			 * is the wrong thing to do as we've already
6614c42a7b7eSSam Leffler 			 * torn down much of our state.  There's
6615c42a7b7eSSam Leffler 			 * probably a better way to deal with this.
6616c42a7b7eSSam Leffler 			 */
66177a79cebfSGleb Smirnoff 			error = ath_init(sc);
66187a79cebfSGleb Smirnoff 		}
6619d3ac945bSSam Leffler 	} else {
66207a79cebfSGleb Smirnoff 		ath_stop(sc);
6621d3ac945bSSam Leffler 		if (!sc->sc_invalid)
66228c03e55dSAdrian Chadd 			ath_power_setpower(sc, HAL_PM_FULL_SLEEP, 1);
6623410302ebSAdrian Chadd 	}
66247a79cebfSGleb Smirnoff 	ATH_UNLOCK(sc);
66257a79cebfSGleb Smirnoff 
66267a79cebfSGleb Smirnoff 	if (error == 0) {
66277a79cebfSGleb Smirnoff #ifdef ATH_TX99_DIAG
66287a79cebfSGleb Smirnoff 		if (sc->sc_tx99 != NULL)
66297a79cebfSGleb Smirnoff 			sc->sc_tx99->start(sc->sc_tx99);
66307a79cebfSGleb Smirnoff 		else
66317a79cebfSGleb Smirnoff #endif
66327a79cebfSGleb Smirnoff 		ieee80211_start_all(ic);
66337a79cebfSGleb Smirnoff 	}
66347a79cebfSGleb Smirnoff }
66357a79cebfSGleb Smirnoff 
6636c42a7b7eSSam Leffler /*
6637c42a7b7eSSam Leffler  * Announce various information on device/driver attach.
6638c42a7b7eSSam Leffler  */
6639c42a7b7eSSam Leffler static void
ath_announce(struct ath_softc * sc)6640c42a7b7eSSam Leffler ath_announce(struct ath_softc *sc)
6641c42a7b7eSSam Leffler {
6642c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6643c42a7b7eSSam Leffler 
6644b2585567SAdrian Chadd 	device_printf(sc->sc_dev, "%s mac %d.%d RF%s phy %d.%d\n",
6645498657cfSSam Leffler 		ath_hal_mac_name(ah), ah->ah_macVersion, ah->ah_macRev,
6646498657cfSSam Leffler 		ath_hal_rf_name(ah), ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
664776e6fd5dSGleb Smirnoff 	device_printf(sc->sc_dev, "2GHz radio: 0x%.4x; 5GHz radio: 0x%.4x\n",
664846a924c4SAdrian Chadd 		ah->ah_analog2GhzRev, ah->ah_analog5GhzRev);
6649c42a7b7eSSam Leffler 	if (bootverbose) {
6650c42a7b7eSSam Leffler 		int i;
6651c42a7b7eSSam Leffler 		for (i = 0; i <= WME_AC_VO; i++) {
6652c42a7b7eSSam Leffler 			struct ath_txq *txq = sc->sc_ac2q[i];
665376e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev,
665476e6fd5dSGleb Smirnoff 			    "Use hw queue %u for %s traffic\n",
6655c42a7b7eSSam Leffler 			    txq->axq_qnum, ieee80211_wme_acnames[i]);
6656c42a7b7eSSam Leffler 		}
665776e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "Use hw queue %u for CAB traffic\n",
6658c42a7b7eSSam Leffler 		    sc->sc_cabq->axq_qnum);
665976e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "Use hw queue %u for beacons\n",
666076e6fd5dSGleb Smirnoff 		    sc->sc_bhalq);
6661c42a7b7eSSam Leffler 	}
6662e2d787faSSam Leffler 	if (ath_rxbuf != ATH_RXBUF)
666376e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "using %u rx buffers\n", ath_rxbuf);
6664e2d787faSSam Leffler 	if (ath_txbuf != ATH_TXBUF)
666576e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "using %u tx buffers\n", ath_txbuf);
66669ac01d39SRui Paulo 	if (sc->sc_mcastkey && bootverbose)
666776e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "using multicast key search\n");
6668c42a7b7eSSam Leffler }
666910ad9a77SSam Leffler 
667048237774SAdrian Chadd static void
ath_dfs_tasklet(void * p,int npending)667148237774SAdrian Chadd ath_dfs_tasklet(void *p, int npending)
667248237774SAdrian Chadd {
667348237774SAdrian Chadd 	struct ath_softc *sc = (struct ath_softc *) p;
66747a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
667548237774SAdrian Chadd 
667648237774SAdrian Chadd 	/*
667748237774SAdrian Chadd 	 * If previous processing has found a radar event,
667848237774SAdrian Chadd 	 * signal this to the net80211 layer to begin DFS
667948237774SAdrian Chadd 	 * processing.
668048237774SAdrian Chadd 	 */
668148237774SAdrian Chadd 	if (ath_dfs_process_radar_event(sc, sc->sc_curchan)) {
668248237774SAdrian Chadd 		/* DFS event found, initiate channel change */
668346be12aeSAdrian Chadd 
668446be12aeSAdrian Chadd 		/*
668546be12aeSAdrian Chadd 		 * XXX TODO: immediately disable ACK processing
668646be12aeSAdrian Chadd 		 * on the current channel.  This would be done
668746be12aeSAdrian Chadd 		 * by setting AR_DIAG_ACK_DIS (AR5212; may be
668846be12aeSAdrian Chadd 		 * different for others) until we are out of
668946be12aeSAdrian Chadd 		 * CAC.
669046be12aeSAdrian Chadd 		 */
669146be12aeSAdrian Chadd 
669206fc4a10SAdrian Chadd 		/*
669306fc4a10SAdrian Chadd 		 * XXX doesn't currently tell us whether the event
669406fc4a10SAdrian Chadd 		 * XXX was found in the primary or extension
669506fc4a10SAdrian Chadd 		 * XXX channel!
669606fc4a10SAdrian Chadd 		 */
669706fc4a10SAdrian Chadd 		IEEE80211_LOCK(ic);
669848237774SAdrian Chadd 		ieee80211_dfs_notify_radar(ic, sc->sc_curchan);
669906fc4a10SAdrian Chadd 		IEEE80211_UNLOCK(ic);
670048237774SAdrian Chadd 	}
670148237774SAdrian Chadd }
670248237774SAdrian Chadd 
67030eb81626SAdrian Chadd /*
67040eb81626SAdrian Chadd  * Enable/disable power save.  This must be called with
67050eb81626SAdrian Chadd  * no TX driver locks currently held, so it should only
67060eb81626SAdrian Chadd  * be called from the RX path (which doesn't hold any
67070eb81626SAdrian Chadd  * TX driver locks.)
67080eb81626SAdrian Chadd  */
67090eb81626SAdrian Chadd static void
ath_node_powersave(struct ieee80211_node * ni,int enable)67100eb81626SAdrian Chadd ath_node_powersave(struct ieee80211_node *ni, int enable)
67110eb81626SAdrian Chadd {
6712bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
67130eb81626SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
67140eb81626SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
67153797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
67160eb81626SAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
67170eb81626SAdrian Chadd 
67180eb81626SAdrian Chadd 	/* XXX and no TXQ locks should be held here */
67190eb81626SAdrian Chadd 
67209b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, "%s: %6D: enable=%d\n",
67219b48fb4bSAdrian Chadd 	    __func__,
67229b48fb4bSAdrian Chadd 	    ni->ni_macaddr,
67239b48fb4bSAdrian Chadd 	    ":",
67249b48fb4bSAdrian Chadd 	    !! enable);
67250eb81626SAdrian Chadd 
67260eb81626SAdrian Chadd 	/* Suspend or resume software queue handling */
67270eb81626SAdrian Chadd 	if (enable)
67280eb81626SAdrian Chadd 		ath_tx_node_sleep(sc, an);
67290eb81626SAdrian Chadd 	else
67300eb81626SAdrian Chadd 		ath_tx_node_wakeup(sc, an);
67310eb81626SAdrian Chadd 
67320eb81626SAdrian Chadd 	/* Update net80211 state */
67330eb81626SAdrian Chadd 	avp->av_node_ps(ni, enable);
6734bdbb6e5bSAdrian Chadd #else
6735bdbb6e5bSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6736bdbb6e5bSAdrian Chadd 
6737bdbb6e5bSAdrian Chadd 	/* Update net80211 state */
6738bdbb6e5bSAdrian Chadd 	avp->av_node_ps(ni, enable);
6739bdbb6e5bSAdrian Chadd #endif/* ATH_SW_PSQ */
67400eb81626SAdrian Chadd }
67410eb81626SAdrian Chadd 
6742548a605dSAdrian Chadd /*
6743548a605dSAdrian Chadd  * Notification from net80211 that the powersave queue state has
6744548a605dSAdrian Chadd  * changed.
6745548a605dSAdrian Chadd  *
6746548a605dSAdrian Chadd  * Since the software queue also may have some frames:
6747548a605dSAdrian Chadd  *
6748548a605dSAdrian Chadd  * + if the node software queue has frames and the TID state
6749548a605dSAdrian Chadd  *   is 0, we set the TIM;
6750548a605dSAdrian Chadd  * + if the node and the stack are both empty, we clear the TIM bit.
6751548a605dSAdrian Chadd  * + If the stack tries to set the bit, always set it.
6752548a605dSAdrian Chadd  * + If the stack tries to clear the bit, only clear it if the
6753548a605dSAdrian Chadd  *   software queue in question is also cleared.
6754548a605dSAdrian Chadd  *
6755548a605dSAdrian Chadd  * TODO: this is called during node teardown; so let's ensure this
6756548a605dSAdrian Chadd  * is all correctly handled and that the TIM bit is cleared.
6757548a605dSAdrian Chadd  * It may be that the node flush is called _AFTER_ the net80211
6758548a605dSAdrian Chadd  * stack clears the TIM.
6759548a605dSAdrian Chadd  *
6760548a605dSAdrian Chadd  * Here is the racy part.  Since it's possible >1 concurrent,
6761548a605dSAdrian Chadd  * overlapping TXes will appear complete with a TX completion in
6762548a605dSAdrian Chadd  * another thread, it's possible that the concurrent TIM calls will
6763548a605dSAdrian Chadd  * clash.  We can't hold the node lock here because setting the
6764548a605dSAdrian Chadd  * TIM grabs the net80211 comlock and this may cause a LOR.
6765548a605dSAdrian Chadd  * The solution is either to totally serialise _everything_ at
6766548a605dSAdrian Chadd  * this point (ie, all TX, completion and any reset/flush go into
6767548a605dSAdrian Chadd  * one taskqueue) or a new "ath TIM lock" needs to be created that
6768548a605dSAdrian Chadd  * just wraps the driver state change and this call to avp->av_set_tim().
6769548a605dSAdrian Chadd  *
6770548a605dSAdrian Chadd  * The same race exists in the net80211 power save queue handling
6771548a605dSAdrian Chadd  * as well.  Since multiple transmitting threads may queue frames
6772548a605dSAdrian Chadd  * into the driver, as well as ps-poll and the driver transmitting
6773548a605dSAdrian Chadd  * frames (and thus clearing the psq), it's quite possible that
6774548a605dSAdrian Chadd  * a packet entering the PSQ and a ps-poll being handled will
6775548a605dSAdrian Chadd  * race, causing the TIM to be cleared and not re-set.
6776548a605dSAdrian Chadd  */
6777548a605dSAdrian Chadd static int
ath_node_set_tim(struct ieee80211_node * ni,int enable)6778548a605dSAdrian Chadd ath_node_set_tim(struct ieee80211_node *ni, int enable)
6779548a605dSAdrian Chadd {
6780bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
6781548a605dSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
67823797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
6783548a605dSAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
6784548a605dSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6785548a605dSAdrian Chadd 	int changed = 0;
6786548a605dSAdrian Chadd 
67874bed2b67SAdrian Chadd 	ATH_TX_LOCK(sc);
6788548a605dSAdrian Chadd 	an->an_stack_psq = enable;
6789548a605dSAdrian Chadd 
6790548a605dSAdrian Chadd 	/*
6791548a605dSAdrian Chadd 	 * This will get called for all operating modes,
6792548a605dSAdrian Chadd 	 * even if avp->av_set_tim is unset.
6793548a605dSAdrian Chadd 	 * It's currently set for hostap/ibss modes; but
6794548a605dSAdrian Chadd 	 * the same infrastructure is used for both STA
6795548a605dSAdrian Chadd 	 * and AP/IBSS node power save.
6796548a605dSAdrian Chadd 	 */
6797548a605dSAdrian Chadd 	if (avp->av_set_tim == NULL) {
67984bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6799548a605dSAdrian Chadd 		return (0);
6800548a605dSAdrian Chadd 	}
6801548a605dSAdrian Chadd 
6802548a605dSAdrian Chadd 	/*
6803548a605dSAdrian Chadd 	 * If setting the bit, always set it here.
6804548a605dSAdrian Chadd 	 * If clearing the bit, only clear it if the
6805548a605dSAdrian Chadd 	 * software queue is also empty.
6806548a605dSAdrian Chadd 	 *
6807548a605dSAdrian Chadd 	 * If the node has left power save, just clear the TIM
6808548a605dSAdrian Chadd 	 * bit regardless of the state of the power save queue.
6809548a605dSAdrian Chadd 	 *
6810548a605dSAdrian Chadd 	 * XXX TODO: although atomics are used, it's quite possible
6811548a605dSAdrian Chadd 	 * that a race will occur between this and setting/clearing
6812548a605dSAdrian Chadd 	 * in another thread.  TX completion will occur always in
6813548a605dSAdrian Chadd 	 * one thread, however setting/clearing the TIM bit can come
6814548a605dSAdrian Chadd 	 * from a variety of different process contexts!
6815548a605dSAdrian Chadd 	 */
6816548a605dSAdrian Chadd 	if (enable && an->an_tim_set == 1) {
6817548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
68189b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, tim_set=1, ignoring\n",
68199b48fb4bSAdrian Chadd 		    __func__,
68209b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
68219b48fb4bSAdrian Chadd 		    ":",
68229b48fb4bSAdrian Chadd 		    enable);
68234bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6824548a605dSAdrian Chadd 	} else if (enable) {
6825548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
68269b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, enabling TIM\n",
68279b48fb4bSAdrian Chadd 		    __func__,
68289b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
68299b48fb4bSAdrian Chadd 		    ":",
68309b48fb4bSAdrian Chadd 		    enable);
6831548a605dSAdrian Chadd 		an->an_tim_set = 1;
68324bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6833548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6834ba83edd4SAdrian Chadd 	} else if (an->an_swq_depth == 0) {
6835548a605dSAdrian Chadd 		/* disable */
6836548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
68379b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_swq_depth == 0, disabling\n",
68389b48fb4bSAdrian Chadd 		    __func__,
68399b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
68409b48fb4bSAdrian Chadd 		    ":",
68419b48fb4bSAdrian Chadd 		    enable);
6842548a605dSAdrian Chadd 		an->an_tim_set = 0;
68434bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6844548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6845548a605dSAdrian Chadd 	} else if (! an->an_is_powersave) {
6846548a605dSAdrian Chadd 		/*
6847548a605dSAdrian Chadd 		 * disable regardless; the node isn't in powersave now
6848548a605dSAdrian Chadd 		 */
6849548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
68509b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_pwrsave=0, disabling\n",
68519b48fb4bSAdrian Chadd 		    __func__,
68529b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
68539b48fb4bSAdrian Chadd 		    ":",
68549b48fb4bSAdrian Chadd 		    enable);
6855548a605dSAdrian Chadd 		an->an_tim_set = 0;
68564bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6857548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6858548a605dSAdrian Chadd 	} else {
6859548a605dSAdrian Chadd 		/*
6860548a605dSAdrian Chadd 		 * psq disable, node is currently in powersave, node
6861548a605dSAdrian Chadd 		 * software queue isn't empty, so don't clear the TIM bit
6862548a605dSAdrian Chadd 		 * for now.
6863548a605dSAdrian Chadd 		 */
68644bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6865548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
68669b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_swq_depth > 0, ignoring\n",
68679b48fb4bSAdrian Chadd 		    __func__,
68689b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
68699b48fb4bSAdrian Chadd 		    ":",
68709b48fb4bSAdrian Chadd 		    enable);
6871548a605dSAdrian Chadd 		changed = 0;
6872548a605dSAdrian Chadd 	}
6873548a605dSAdrian Chadd 
6874548a605dSAdrian Chadd 	return (changed);
6875bdbb6e5bSAdrian Chadd #else
6876bdbb6e5bSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6877bdbb6e5bSAdrian Chadd 
687860328038SAdrian Chadd 	/*
6879661c81c3SBaptiste Daroussin 	 * Some operating modes don't set av_set_tim(), so don't
688060328038SAdrian Chadd 	 * update it here.
688160328038SAdrian Chadd 	 */
688260328038SAdrian Chadd 	if (avp->av_set_tim == NULL)
688360328038SAdrian Chadd 		return (0);
688460328038SAdrian Chadd 
6885bdbb6e5bSAdrian Chadd 	return (avp->av_set_tim(ni, enable));
6886bdbb6e5bSAdrian Chadd #endif /* ATH_SW_PSQ */
6887548a605dSAdrian Chadd }
6888548a605dSAdrian Chadd 
6889548a605dSAdrian Chadd /*
6890548a605dSAdrian Chadd  * Set or update the TIM from the software queue.
6891548a605dSAdrian Chadd  *
6892548a605dSAdrian Chadd  * Check the software queue depth before attempting to do lock
6893548a605dSAdrian Chadd  * anything; that avoids trying to obtain the lock.  Then,
6894548a605dSAdrian Chadd  * re-check afterwards to ensure nothing has changed in the
6895548a605dSAdrian Chadd  * meantime.
6896548a605dSAdrian Chadd  *
6897548a605dSAdrian Chadd  * set:   This is designed to be called from the TX path, after
6898548a605dSAdrian Chadd  *        a frame has been queued; to see if the swq > 0.
6899548a605dSAdrian Chadd  *
6900548a605dSAdrian Chadd  * clear: This is designed to be called from the buffer completion point
6901548a605dSAdrian Chadd  *        (right now it's ath_tx_default_comp()) where the state of
6902548a605dSAdrian Chadd  *        a software queue has changed.
6903548a605dSAdrian Chadd  *
6904548a605dSAdrian Chadd  * It makes sense to place it at buffer free / completion rather
6905548a605dSAdrian Chadd  * than after each software queue operation, as there's no real
6906548a605dSAdrian Chadd  * point in churning the TIM bit as the last frames in the software
6907548a605dSAdrian Chadd  * queue are transmitted.  If they fail and we retry them, we'd
6908548a605dSAdrian Chadd  * just be setting the TIM bit again anyway.
6909548a605dSAdrian Chadd  */
6910548a605dSAdrian Chadd void
ath_tx_update_tim(struct ath_softc * sc,struct ieee80211_node * ni,int enable)6911548a605dSAdrian Chadd ath_tx_update_tim(struct ath_softc *sc, struct ieee80211_node *ni,
6912548a605dSAdrian Chadd      int enable)
6913548a605dSAdrian Chadd {
6914bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
6915548a605dSAdrian Chadd 	struct ath_node *an;
6916548a605dSAdrian Chadd 	struct ath_vap *avp;
6917548a605dSAdrian Chadd 
6918548a605dSAdrian Chadd 	/* Don't do this for broadcast/etc frames */
6919548a605dSAdrian Chadd 	if (ni == NULL)
6920548a605dSAdrian Chadd 		return;
6921548a605dSAdrian Chadd 
6922548a605dSAdrian Chadd 	an = ATH_NODE(ni);
6923548a605dSAdrian Chadd 	avp = ATH_VAP(ni->ni_vap);
6924548a605dSAdrian Chadd 
6925548a605dSAdrian Chadd 	/*
6926548a605dSAdrian Chadd 	 * And for operating modes without the TIM handler set, let's
6927548a605dSAdrian Chadd 	 * just skip those.
6928548a605dSAdrian Chadd 	 */
6929548a605dSAdrian Chadd 	if (avp->av_set_tim == NULL)
6930548a605dSAdrian Chadd 		return;
6931548a605dSAdrian Chadd 
69324bed2b67SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
6933548a605dSAdrian Chadd 
6934548a605dSAdrian Chadd 	if (enable) {
6935548a605dSAdrian Chadd 		if (an->an_is_powersave &&
6936548a605dSAdrian Chadd 		    an->an_tim_set == 0 &&
6937ba83edd4SAdrian Chadd 		    an->an_swq_depth != 0) {
6938548a605dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
69399b48fb4bSAdrian Chadd 			    "%s: %6D: swq_depth>0, tim_set=0, set!\n",
69409b48fb4bSAdrian Chadd 			    __func__,
69419b48fb4bSAdrian Chadd 			    ni->ni_macaddr,
69429b48fb4bSAdrian Chadd 			    ":");
6943548a605dSAdrian Chadd 			an->an_tim_set = 1;
6944548a605dSAdrian Chadd 			(void) avp->av_set_tim(ni, 1);
6945548a605dSAdrian Chadd 		}
6946548a605dSAdrian Chadd 	} else {
6947548a605dSAdrian Chadd 		/*
6948548a605dSAdrian Chadd 		 * Don't bother grabbing the lock unless the queue is empty.
6949548a605dSAdrian Chadd 		 */
69503b48f36eSAdrian Chadd 		if (an->an_swq_depth != 0)
6951548a605dSAdrian Chadd 			return;
6952548a605dSAdrian Chadd 
6953548a605dSAdrian Chadd 		if (an->an_is_powersave &&
6954548a605dSAdrian Chadd 		    an->an_stack_psq == 0 &&
6955548a605dSAdrian Chadd 		    an->an_tim_set == 1 &&
6956ba83edd4SAdrian Chadd 		    an->an_swq_depth == 0) {
6957548a605dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
695822a3aee6SAdrian Chadd 			    "%s: %6D: swq_depth=0, tim_set=1, psq_set=0,"
6959548a605dSAdrian Chadd 			    " clear!\n",
696022a3aee6SAdrian Chadd 			    __func__,
696122a3aee6SAdrian Chadd 			    ni->ni_macaddr,
696222a3aee6SAdrian Chadd 			    ":");
6963548a605dSAdrian Chadd 			an->an_tim_set = 0;
6964548a605dSAdrian Chadd 			(void) avp->av_set_tim(ni, 0);
6965548a605dSAdrian Chadd 		}
6966548a605dSAdrian Chadd 	}
6967bdbb6e5bSAdrian Chadd #else
6968bdbb6e5bSAdrian Chadd 	return;
6969bdbb6e5bSAdrian Chadd #endif	/* ATH_SW_PSQ */
6970548a605dSAdrian Chadd }
69710eb81626SAdrian Chadd 
697222a3aee6SAdrian Chadd /*
697322a3aee6SAdrian Chadd  * Received a ps-poll frame from net80211.
697422a3aee6SAdrian Chadd  *
697522a3aee6SAdrian Chadd  * Here we get a chance to serve out a software-queued frame ourselves
697622a3aee6SAdrian Chadd  * before we punt it to net80211 to transmit us one itself - either
697722a3aee6SAdrian Chadd  * because there's traffic in the net80211 psq, or a NULL frame to
697822a3aee6SAdrian Chadd  * indicate there's nothing else.
697922a3aee6SAdrian Chadd  */
698022a3aee6SAdrian Chadd static void
ath_node_recv_pspoll(struct ieee80211_node * ni,struct mbuf * m)698122a3aee6SAdrian Chadd ath_node_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m)
698222a3aee6SAdrian Chadd {
698322a3aee6SAdrian Chadd #ifdef	ATH_SW_PSQ
698422a3aee6SAdrian Chadd 	struct ath_node *an;
698522a3aee6SAdrian Chadd 	struct ath_vap *avp;
698622a3aee6SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
69873797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
698822a3aee6SAdrian Chadd 	int tid;
698922a3aee6SAdrian Chadd 
699022a3aee6SAdrian Chadd 	/* Just paranoia */
699122a3aee6SAdrian Chadd 	if (ni == NULL)
699222a3aee6SAdrian Chadd 		return;
699322a3aee6SAdrian Chadd 
699422a3aee6SAdrian Chadd 	/*
699522a3aee6SAdrian Chadd 	 * Unassociated (temporary node) station.
699622a3aee6SAdrian Chadd 	 */
699722a3aee6SAdrian Chadd 	if (ni->ni_associd == 0)
699822a3aee6SAdrian Chadd 		return;
699922a3aee6SAdrian Chadd 
700022a3aee6SAdrian Chadd 	/*
700122a3aee6SAdrian Chadd 	 * We do have an active node, so let's begin looking into it.
700222a3aee6SAdrian Chadd 	 */
700322a3aee6SAdrian Chadd 	an = ATH_NODE(ni);
700422a3aee6SAdrian Chadd 	avp = ATH_VAP(ni->ni_vap);
700522a3aee6SAdrian Chadd 
700622a3aee6SAdrian Chadd 	/*
700722a3aee6SAdrian Chadd 	 * For now, we just call the original ps-poll method.
700822a3aee6SAdrian Chadd 	 * Once we're ready to flip this on:
700922a3aee6SAdrian Chadd 	 *
701022a3aee6SAdrian Chadd 	 * + Set leak to 1, as no matter what we're going to have
701122a3aee6SAdrian Chadd 	 *   to send a frame;
701222a3aee6SAdrian Chadd 	 * + Check the software queue and if there's something in it,
701322a3aee6SAdrian Chadd 	 *   schedule the highest TID thas has traffic from this node.
701422a3aee6SAdrian Chadd 	 *   Then make sure we schedule the software scheduler to
701522a3aee6SAdrian Chadd 	 *   run so it picks up said frame.
701622a3aee6SAdrian Chadd 	 *
701722a3aee6SAdrian Chadd 	 * That way whatever happens, we'll at least send _a_ frame
701822a3aee6SAdrian Chadd 	 * to the given node.
701922a3aee6SAdrian Chadd 	 *
702022a3aee6SAdrian Chadd 	 * Again, yes, it's crappy QoS if the node has multiple
702122a3aee6SAdrian Chadd 	 * TIDs worth of traffic - but let's get it working first
702222a3aee6SAdrian Chadd 	 * before we optimise it.
702322a3aee6SAdrian Chadd 	 *
702422a3aee6SAdrian Chadd 	 * Also yes, there's definitely latency here - we're not
702522a3aee6SAdrian Chadd 	 * direct dispatching to the hardware in this path (and
702622a3aee6SAdrian Chadd 	 * we're likely being called from the packet receive path,
702722a3aee6SAdrian Chadd 	 * so going back into TX may be a little hairy!) but again
702822a3aee6SAdrian Chadd 	 * I'd like to get this working first before optimising
702922a3aee6SAdrian Chadd 	 * turn-around time.
703022a3aee6SAdrian Chadd 	 */
703122a3aee6SAdrian Chadd 
703222a3aee6SAdrian Chadd 	ATH_TX_LOCK(sc);
703322a3aee6SAdrian Chadd 
703422a3aee6SAdrian Chadd 	/*
703522a3aee6SAdrian Chadd 	 * Legacy - we're called and the node isn't asleep.
703622a3aee6SAdrian Chadd 	 * Immediately punt.
703722a3aee6SAdrian Chadd 	 */
703822a3aee6SAdrian Chadd 	if (! an->an_is_powersave) {
703983bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
704022a3aee6SAdrian Chadd 		    "%s: %6D: not in powersave?\n",
704122a3aee6SAdrian Chadd 		    __func__,
704222a3aee6SAdrian Chadd 		    ni->ni_macaddr,
704322a3aee6SAdrian Chadd 		    ":");
704422a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
704522a3aee6SAdrian Chadd 		avp->av_recv_pspoll(ni, m);
704622a3aee6SAdrian Chadd 		return;
704722a3aee6SAdrian Chadd 	}
704822a3aee6SAdrian Chadd 
704922a3aee6SAdrian Chadd 	/*
705022a3aee6SAdrian Chadd 	 * We're in powersave.
705122a3aee6SAdrian Chadd 	 *
705222a3aee6SAdrian Chadd 	 * Leak a frame.
705322a3aee6SAdrian Chadd 	 */
705422a3aee6SAdrian Chadd 	an->an_leak_count = 1;
705522a3aee6SAdrian Chadd 
705622a3aee6SAdrian Chadd 	/*
705722a3aee6SAdrian Chadd 	 * Now, if there's no frames in the node, just punt to
705822a3aee6SAdrian Chadd 	 * recv_pspoll.
705922a3aee6SAdrian Chadd 	 *
706022a3aee6SAdrian Chadd 	 * Don't bother checking if the TIM bit is set, we really
706122a3aee6SAdrian Chadd 	 * only care if there are any frames here!
706222a3aee6SAdrian Chadd 	 */
706322a3aee6SAdrian Chadd 	if (an->an_swq_depth == 0) {
706422a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
706522a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
706622a3aee6SAdrian Chadd 		    "%s: %6D: SWQ empty; punting to net80211\n",
706722a3aee6SAdrian Chadd 		    __func__,
706822a3aee6SAdrian Chadd 		    ni->ni_macaddr,
706922a3aee6SAdrian Chadd 		    ":");
707022a3aee6SAdrian Chadd 		avp->av_recv_pspoll(ni, m);
707122a3aee6SAdrian Chadd 		return;
707222a3aee6SAdrian Chadd 	}
707322a3aee6SAdrian Chadd 
707422a3aee6SAdrian Chadd 	/*
707522a3aee6SAdrian Chadd 	 * Ok, let's schedule the highest TID that has traffic
707622a3aee6SAdrian Chadd 	 * and then schedule something.
707722a3aee6SAdrian Chadd 	 */
707822a3aee6SAdrian Chadd 	for (tid = IEEE80211_TID_SIZE - 1; tid >= 0; tid--) {
707922a3aee6SAdrian Chadd 		struct ath_tid *atid = &an->an_tid[tid];
708022a3aee6SAdrian Chadd 		/*
708122a3aee6SAdrian Chadd 		 * No frames? Skip.
708222a3aee6SAdrian Chadd 		 */
708322a3aee6SAdrian Chadd 		if (atid->axq_depth == 0)
708422a3aee6SAdrian Chadd 			continue;
708522a3aee6SAdrian Chadd 		ath_tx_tid_sched(sc, atid);
708622a3aee6SAdrian Chadd 		/*
708722a3aee6SAdrian Chadd 		 * XXX we could do a direct call to the TXQ
708822a3aee6SAdrian Chadd 		 * scheduler code here to optimise latency
708922a3aee6SAdrian Chadd 		 * at the expense of a REALLY deep callstack.
709022a3aee6SAdrian Chadd 		 */
709122a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
709222a3aee6SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_txqtask);
709322a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
709422a3aee6SAdrian Chadd 		    "%s: %6D: leaking frame to TID %d\n",
709522a3aee6SAdrian Chadd 		    __func__,
709622a3aee6SAdrian Chadd 		    ni->ni_macaddr,
709722a3aee6SAdrian Chadd 		    ":",
709822a3aee6SAdrian Chadd 		    tid);
709922a3aee6SAdrian Chadd 		return;
710022a3aee6SAdrian Chadd 	}
710122a3aee6SAdrian Chadd 
710222a3aee6SAdrian Chadd 	ATH_TX_UNLOCK(sc);
710322a3aee6SAdrian Chadd 
710422a3aee6SAdrian Chadd 	/*
710522a3aee6SAdrian Chadd 	 * XXX nothing in the TIDs at this point? Eek.
710622a3aee6SAdrian Chadd 	 */
710783bbd5ebSRui Paulo 	DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
710883bbd5ebSRui Paulo 	    "%s: %6D: TIDs empty, but ath_node showed traffic?!\n",
710922a3aee6SAdrian Chadd 	    __func__,
711022a3aee6SAdrian Chadd 	    ni->ni_macaddr,
711122a3aee6SAdrian Chadd 	    ":");
711222a3aee6SAdrian Chadd 	avp->av_recv_pspoll(ni, m);
711322a3aee6SAdrian Chadd #else
711422a3aee6SAdrian Chadd 	avp->av_recv_pspoll(ni, m);
711522a3aee6SAdrian Chadd #endif	/* ATH_SW_PSQ */
711622a3aee6SAdrian Chadd }
711722a3aee6SAdrian Chadd 
711841059135SAdrian Chadd MODULE_VERSION(ath_main, 1);
711941059135SAdrian Chadd MODULE_DEPEND(ath_main, wlan, 1, 1, 1);          /* 802.11 media layer */
712041059135SAdrian Chadd MODULE_DEPEND(ath_main, ath_rate, 1, 1, 1);
712141059135SAdrian Chadd MODULE_DEPEND(ath_main, ath_dfs, 1, 1, 1);
712241059135SAdrian Chadd MODULE_DEPEND(ath_main, ath_hal, 1, 1, 1);
71239389d5a9SAdrian Chadd #if	defined(IEEE80211_ALQ) || defined(AH_DEBUG_ALQ) || defined(ATH_DEBUG_ALQ)
712441059135SAdrian Chadd MODULE_DEPEND(ath_main, alq, 1, 1, 1);
712558816f3fSAdrian Chadd #endif
7126