xref: /freebsd/sys/dev/ath/if_ath.c (revision 76039bc84fae9915788b54ff28fe0cc4876952d2)
15591b213SSam Leffler /*-
210ad9a77SSam Leffler  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
35591b213SSam Leffler  * All rights reserved.
45591b213SSam Leffler  *
55591b213SSam Leffler  * Redistribution and use in source and binary forms, with or without
65591b213SSam Leffler  * modification, are permitted provided that the following conditions
75591b213SSam Leffler  * are met:
85591b213SSam Leffler  * 1. Redistributions of source code must retain the above copyright
95591b213SSam Leffler  *    notice, this list of conditions and the following disclaimer,
105591b213SSam Leffler  *    without modification.
115591b213SSam Leffler  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
125591b213SSam Leffler  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
135591b213SSam Leffler  *    redistribution must be conditioned upon including a substantially
145591b213SSam Leffler  *    similar Disclaimer requirement for further binary redistribution.
155591b213SSam Leffler  *
165591b213SSam Leffler  * NO WARRANTY
175591b213SSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
185591b213SSam Leffler  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
195591b213SSam Leffler  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
205591b213SSam Leffler  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
215591b213SSam Leffler  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
225591b213SSam Leffler  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
235591b213SSam Leffler  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
245591b213SSam Leffler  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
255591b213SSam Leffler  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
265591b213SSam Leffler  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
275591b213SSam Leffler  * THE POSSIBILITY OF SUCH DAMAGES.
285591b213SSam Leffler  */
295591b213SSam Leffler 
305591b213SSam Leffler #include <sys/cdefs.h>
315591b213SSam Leffler __FBSDID("$FreeBSD$");
325591b213SSam Leffler 
335591b213SSam Leffler /*
345591b213SSam Leffler  * Driver for the Atheros Wireless LAN controller.
355f3721d5SSam Leffler  *
365f3721d5SSam Leffler  * This software is derived from work of Atsushi Onoe; his contribution
375f3721d5SSam Leffler  * is greatly appreciated.
385591b213SSam Leffler  */
395591b213SSam Leffler 
405591b213SSam Leffler #include "opt_inet.h"
41a585a9a1SSam Leffler #include "opt_ath.h"
423f3087fdSAdrian Chadd /*
433f3087fdSAdrian Chadd  * This is needed for register operations which are performed
443f3087fdSAdrian Chadd  * by the driver - eg, calls to ath_hal_gettsf32().
4558816f3fSAdrian Chadd  *
4658816f3fSAdrian Chadd  * It's also required for any AH_DEBUG checks in here, eg the
4758816f3fSAdrian Chadd  * module dependencies.
483f3087fdSAdrian Chadd  */
493f3087fdSAdrian Chadd #include "opt_ah.h"
50584f7327SSam Leffler #include "opt_wlan.h"
515591b213SSam Leffler 
525591b213SSam Leffler #include <sys/param.h>
535591b213SSam Leffler #include <sys/systm.h>
545591b213SSam Leffler #include <sys/sysctl.h>
555591b213SSam Leffler #include <sys/mbuf.h>
565591b213SSam Leffler #include <sys/malloc.h>
575591b213SSam Leffler #include <sys/lock.h>
585591b213SSam Leffler #include <sys/mutex.h>
595591b213SSam Leffler #include <sys/kernel.h>
605591b213SSam Leffler #include <sys/socket.h>
615591b213SSam Leffler #include <sys/sockio.h>
625591b213SSam Leffler #include <sys/errno.h>
635591b213SSam Leffler #include <sys/callout.h>
645591b213SSam Leffler #include <sys/bus.h>
655591b213SSam Leffler #include <sys/endian.h>
660bbf5441SSam Leffler #include <sys/kthread.h>
670bbf5441SSam Leffler #include <sys/taskqueue.h>
683fc21fedSSam Leffler #include <sys/priv.h>
69dba9c859SAdrian Chadd #include <sys/module.h>
70f52d3452SAdrian Chadd #include <sys/ktr.h>
71ddbe3036SAdrian Chadd #include <sys/smp.h>	/* for mp_ncpus */
725591b213SSam Leffler 
735591b213SSam Leffler #include <machine/bus.h>
745591b213SSam Leffler 
755591b213SSam Leffler #include <net/if.h>
76*76039bc8SGleb Smirnoff #include <net/if_var.h>
775591b213SSam Leffler #include <net/if_dl.h>
785591b213SSam Leffler #include <net/if_media.h>
79fc74a9f9SBrooks Davis #include <net/if_types.h>
805591b213SSam Leffler #include <net/if_arp.h>
815591b213SSam Leffler #include <net/ethernet.h>
825591b213SSam Leffler #include <net/if_llc.h>
835591b213SSam Leffler 
845591b213SSam Leffler #include <net80211/ieee80211_var.h>
8559efa8b5SSam Leffler #include <net80211/ieee80211_regdomain.h>
86339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
87339ccfb3SSam Leffler #include <net80211/ieee80211_superg.h>
88339ccfb3SSam Leffler #endif
89584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
9010ad9a77SSam Leffler #include <net80211/ieee80211_tdma.h>
9110ad9a77SSam Leffler #endif
925591b213SSam Leffler 
935591b213SSam Leffler #include <net/bpf.h>
945591b213SSam Leffler 
955591b213SSam Leffler #ifdef INET
965591b213SSam Leffler #include <netinet/in.h>
975591b213SSam Leffler #include <netinet/if_ether.h>
985591b213SSam Leffler #endif
995591b213SSam Leffler 
1005591b213SSam Leffler #include <dev/ath/if_athvar.h>
10133644623SSam Leffler #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
1020dbe9289SAdrian Chadd #include <dev/ath/ath_hal/ah_diagcodes.h>
1035591b213SSam Leffler 
1045bc8125aSAdrian Chadd #include <dev/ath/if_ath_debug.h>
105b8e788a5SAdrian Chadd #include <dev/ath/if_ath_misc.h>
106e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_tsf.h>
107b8e788a5SAdrian Chadd #include <dev/ath/if_ath_tx.h>
1086079fdbeSAdrian Chadd #include <dev/ath/if_ath_sysctl.h>
109c65ee21dSAdrian Chadd #include <dev/ath/if_ath_led.h>
110d2d7a00aSAdrian Chadd #include <dev/ath/if_ath_keycache.h>
111e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_rx.h>
112f8cc9b09SAdrian Chadd #include <dev/ath/if_ath_rx_edma.h>
1133fdfc330SAdrian Chadd #include <dev/ath/if_ath_tx_edma.h>
114a35dae8dSAdrian Chadd #include <dev/ath/if_ath_beacon.h>
115b70f530bSAdrian Chadd #include <dev/ath/if_ath_btcoex.h>
1169af351f9SAdrian Chadd #include <dev/ath/if_ath_spectral.h>
117216ca234SAdrian Chadd #include <dev/ath/if_ath_lna_div.h>
11848237774SAdrian Chadd #include <dev/ath/if_athdfs.h>
1195bc8125aSAdrian Chadd 
12086e07743SSam Leffler #ifdef ATH_TX99_DIAG
12186e07743SSam Leffler #include <dev/ath/ath_tx99/ath_tx99.h>
12286e07743SSam Leffler #endif
12386e07743SSam Leffler 
12489d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
125bdbb6e5bSAdrian Chadd #include <dev/ath/if_ath_alq.h>
126bdbb6e5bSAdrian Chadd #endif
127bdbb6e5bSAdrian Chadd 
128bdbb6e5bSAdrian Chadd /*
129bdbb6e5bSAdrian Chadd  * Only enable this if you're working on PS-POLL support.
130bdbb6e5bSAdrian Chadd  */
13122a3aee6SAdrian Chadd #define	ATH_SW_PSQ
132bdbb6e5bSAdrian Chadd 
133b032f27cSSam Leffler /*
134b032f27cSSam Leffler  * ATH_BCBUF determines the number of vap's that can transmit
135b032f27cSSam Leffler  * beacons and also (currently) the number of vap's that can
136b032f27cSSam Leffler  * have unique mac addresses/bssid.  When staggering beacons
137b032f27cSSam Leffler  * 4 is probably a good max as otherwise the beacons become
138b032f27cSSam Leffler  * very closely spaced and there is limited time for cab q traffic
139b032f27cSSam Leffler  * to go out.  You can burst beacons instead but that is not good
140b032f27cSSam Leffler  * for stations in power save and at some point you really want
141b032f27cSSam Leffler  * another radio (and channel).
142b032f27cSSam Leffler  *
143b032f27cSSam Leffler  * The limit on the number of mac addresses is tied to our use of
144b032f27cSSam Leffler  * the U/L bit and tracking addresses in a byte; it would be
145b032f27cSSam Leffler  * worthwhile to allow more for applications like proxy sta.
146b032f27cSSam Leffler  */
147b032f27cSSam Leffler CTASSERT(ATH_BCBUF <= 8);
148b032f27cSSam Leffler 
149b032f27cSSam Leffler static struct ieee80211vap *ath_vap_create(struct ieee80211com *,
150fcd9500fSBernhard Schmidt 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
151fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN],
152fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN]);
153b032f27cSSam Leffler static void	ath_vap_delete(struct ieee80211vap *);
1545591b213SSam Leffler static void	ath_init(void *);
155c42a7b7eSSam Leffler static void	ath_stop_locked(struct ifnet *);
1565591b213SSam Leffler static void	ath_stop(struct ifnet *);
157b032f27cSSam Leffler static int	ath_reset_vap(struct ieee80211vap *, u_long);
158cd7dffd0SAdrian Chadd static int	ath_transmit(struct ifnet *ifp, struct mbuf *m);
159cd7dffd0SAdrian Chadd static void	ath_qflush(struct ifnet *ifp);
1605591b213SSam Leffler static int	ath_media_change(struct ifnet *);
1612e986da5SSam Leffler static void	ath_watchdog(void *);
1625591b213SSam Leffler static int	ath_ioctl(struct ifnet *, u_long, caddr_t);
1635591b213SSam Leffler static void	ath_fatal_proc(void *, int);
164b032f27cSSam Leffler static void	ath_bmiss_vap(struct ieee80211vap *);
1655591b213SSam Leffler static void	ath_bmiss_proc(void *, int);
166b032f27cSSam Leffler static void	ath_key_update_begin(struct ieee80211vap *);
167b032f27cSSam Leffler static void	ath_key_update_end(struct ieee80211vap *);
168b032f27cSSam Leffler static void	ath_update_mcast(struct ifnet *);
169b032f27cSSam Leffler static void	ath_update_promisc(struct ifnet *);
170c42a7b7eSSam Leffler static void	ath_updateslot(struct ifnet *);
171c42a7b7eSSam Leffler static void	ath_bstuck_proc(void *, int);
172d52f7132SAdrian Chadd static void	ath_reset_proc(void *, int);
1735591b213SSam Leffler static int	ath_desc_alloc(struct ath_softc *);
1745591b213SSam Leffler static void	ath_desc_free(struct ath_softc *);
17538c208f8SSam Leffler static struct ieee80211_node *ath_node_alloc(struct ieee80211vap *,
17638c208f8SSam Leffler 			const uint8_t [IEEE80211_ADDR_LEN]);
1774afa805eSAdrian Chadd static void	ath_node_cleanup(struct ieee80211_node *);
178c42a7b7eSSam Leffler static void	ath_node_free(struct ieee80211_node *);
17968e8e04eSSam Leffler static void	ath_node_getsignal(const struct ieee80211_node *,
18068e8e04eSSam Leffler 			int8_t *, int8_t *);
181622b3fd2SSam Leffler static void	ath_txq_init(struct ath_softc *sc, struct ath_txq *, int);
182c42a7b7eSSam Leffler static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype);
183c42a7b7eSSam Leffler static int	ath_tx_setup(struct ath_softc *, int, int);
184c42a7b7eSSam Leffler static void	ath_tx_cleanupq(struct ath_softc *, struct ath_txq *);
185c42a7b7eSSam Leffler static void	ath_tx_cleanup(struct ath_softc *);
186788e6aa9SAdrian Chadd static int	ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq,
187788e6aa9SAdrian Chadd 		    int dosched);
188c42a7b7eSSam Leffler static void	ath_tx_proc_q0(void *, int);
189c42a7b7eSSam Leffler static void	ath_tx_proc_q0123(void *, int);
1905591b213SSam Leffler static void	ath_tx_proc(void *, int);
19103e9308fSAdrian Chadd static void	ath_txq_sched_tasklet(void *, int);
1925591b213SSam Leffler static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
193c42a7b7eSSam Leffler static void	ath_chan_change(struct ath_softc *, struct ieee80211_channel *);
19468e8e04eSSam Leffler static void	ath_scan_start(struct ieee80211com *);
19568e8e04eSSam Leffler static void	ath_scan_end(struct ieee80211com *);
19668e8e04eSSam Leffler static void	ath_set_channel(struct ieee80211com *);
197fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
198e7200579SAdrian Chadd static void	ath_update_chw(struct ieee80211com *);
199fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
2005591b213SSam Leffler static void	ath_calibrate(void *);
201b032f27cSSam Leffler static int	ath_newstate(struct ieee80211vap *, enum ieee80211_state, int);
202e8fd88a3SSam Leffler static void	ath_setup_stationkey(struct ieee80211_node *);
203e9962332SSam Leffler static void	ath_newassoc(struct ieee80211_node *, int);
204b032f27cSSam Leffler static int	ath_setregdomain(struct ieee80211com *,
205b032f27cSSam Leffler 		    struct ieee80211_regdomain *, int,
206b032f27cSSam Leffler 		    struct ieee80211_channel []);
2075fe9f044SSam Leffler static void	ath_getradiocaps(struct ieee80211com *, int, int *,
208b032f27cSSam Leffler 		    struct ieee80211_channel []);
209b032f27cSSam Leffler static int	ath_getchannels(struct ath_softc *);
2105591b213SSam Leffler 
211c42a7b7eSSam Leffler static int	ath_rate_setup(struct ath_softc *, u_int mode);
2125591b213SSam Leffler static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
213c42a7b7eSSam Leffler 
214c42a7b7eSSam Leffler static void	ath_announce(struct ath_softc *);
2155591b213SSam Leffler 
21648237774SAdrian Chadd static void	ath_dfs_tasklet(void *, int);
2170eb81626SAdrian Chadd static void	ath_node_powersave(struct ieee80211_node *, int);
218548a605dSAdrian Chadd static int	ath_node_set_tim(struct ieee80211_node *, int);
21922a3aee6SAdrian Chadd static void	ath_node_recv_pspoll(struct ieee80211_node *, struct mbuf *);
22048237774SAdrian Chadd 
221584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
222a35dae8dSAdrian Chadd #include <dev/ath/if_ath_tdma.h>
223a35dae8dSAdrian Chadd #endif
22410ad9a77SSam Leffler 
2255591b213SSam Leffler SYSCTL_DECL(_hw_ath);
2265591b213SSam Leffler 
2275591b213SSam Leffler /* XXX validate sysctl values */
2282dc7fcc4SSam Leffler static	int ath_longcalinterval = 30;		/* long cals every 30 secs */
2292dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, longcal, CTLFLAG_RW, &ath_longcalinterval,
2302dc7fcc4SSam Leffler 	    0, "long chip calibration interval (secs)");
2312dc7fcc4SSam Leffler static	int ath_shortcalinterval = 100;		/* short cals every 100 ms */
2322dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, shortcal, CTLFLAG_RW, &ath_shortcalinterval,
2332dc7fcc4SSam Leffler 	    0, "short chip calibration interval (msecs)");
2342dc7fcc4SSam Leffler static	int ath_resetcalinterval = 20*60;	/* reset cal state 20 mins */
2352dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, resetcal, CTLFLAG_RW, &ath_resetcalinterval,
2362dc7fcc4SSam Leffler 	    0, "reset chip calibration results (secs)");
237a108ab63SAdrian Chadd static	int ath_anicalinterval = 100;		/* ANI calibration - 100 msec */
238a108ab63SAdrian Chadd SYSCTL_INT(_hw_ath, OID_AUTO, anical, CTLFLAG_RW, &ath_anicalinterval,
239a108ab63SAdrian Chadd 	    0, "ANI calibration (msecs)");
2405591b213SSam Leffler 
2413d184db2SAdrian Chadd int ath_rxbuf = ATH_RXBUF;		/* # rx buffers to allocate */
242aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RW, &ath_rxbuf,
243e2d787faSSam Leffler 	    0, "rx buffers allocated");
244e2d787faSSam Leffler TUNABLE_INT("hw.ath.rxbuf", &ath_rxbuf);
2453d184db2SAdrian Chadd int ath_txbuf = ATH_TXBUF;		/* # tx buffers to allocate */
246aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RW, &ath_txbuf,
247e2d787faSSam Leffler 	    0, "tx buffers allocated");
248e2d787faSSam Leffler TUNABLE_INT("hw.ath.txbuf", &ath_txbuf);
2493d184db2SAdrian Chadd int ath_txbuf_mgmt = ATH_MGMT_TXBUF;	/* # mgmt tx buffers to allocate */
250af33d486SAdrian Chadd SYSCTL_INT(_hw_ath, OID_AUTO, txbuf_mgmt, CTLFLAG_RW, &ath_txbuf_mgmt,
251af33d486SAdrian Chadd 	    0, "tx (mgmt) buffers allocated");
252af33d486SAdrian Chadd TUNABLE_INT("hw.ath.txbuf_mgmt", &ath_txbuf_mgmt);
253e2d787faSSam Leffler 
254a35dae8dSAdrian Chadd int ath_bstuck_threshold = 4;		/* max missed beacons */
255a32ac9d3SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, bstuck, CTLFLAG_RW, &ath_bstuck_threshold,
256a32ac9d3SSam Leffler 	    0, "max missed beacon xmits before chip reset");
257a32ac9d3SSam Leffler 
2586b349e5aSAdrian Chadd MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
259c42a7b7eSSam Leffler 
260f8418db5SAdrian Chadd void
261f8418db5SAdrian Chadd ath_legacy_attach_comp_func(struct ath_softc *sc)
262f8418db5SAdrian Chadd {
263f8418db5SAdrian Chadd 
264f8418db5SAdrian Chadd 	/*
265f8418db5SAdrian Chadd 	 * Special case certain configurations.  Note the
266f8418db5SAdrian Chadd 	 * CAB queue is handled by these specially so don't
267f8418db5SAdrian Chadd 	 * include them when checking the txq setup mask.
268f8418db5SAdrian Chadd 	 */
269f8418db5SAdrian Chadd 	switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
270f8418db5SAdrian Chadd 	case 0x01:
271f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
272f8418db5SAdrian Chadd 		break;
273f8418db5SAdrian Chadd 	case 0x0f:
274f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
275f8418db5SAdrian Chadd 		break;
276f8418db5SAdrian Chadd 	default:
277f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
278f8418db5SAdrian Chadd 		break;
279f8418db5SAdrian Chadd 	}
280f8418db5SAdrian Chadd }
281f8418db5SAdrian Chadd 
28267397d39SAdrian Chadd #define	HAL_MODE_HT20 (HAL_MODE_11NG_HT20 | HAL_MODE_11NA_HT20)
28367397d39SAdrian Chadd #define	HAL_MODE_HT40 \
28467397d39SAdrian Chadd 	(HAL_MODE_11NG_HT40PLUS | HAL_MODE_11NG_HT40MINUS | \
28567397d39SAdrian Chadd 	HAL_MODE_11NA_HT40PLUS | HAL_MODE_11NA_HT40MINUS)
2865591b213SSam Leffler int
2875591b213SSam Leffler ath_attach(u_int16_t devid, struct ath_softc *sc)
2885591b213SSam Leffler {
289fc74a9f9SBrooks Davis 	struct ifnet *ifp;
290b032f27cSSam Leffler 	struct ieee80211com *ic;
291fc74a9f9SBrooks Davis 	struct ath_hal *ah = NULL;
2925591b213SSam Leffler 	HAL_STATUS status;
293c42a7b7eSSam Leffler 	int error = 0, i;
294411373ebSSam Leffler 	u_int wmodes;
29529aca940SSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
296a865860dSAdrian Chadd 	int rx_chainmask, tx_chainmask;
2975591b213SSam Leffler 
298c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
2995591b213SSam Leffler 
300a93c5097SAdrian Chadd 	CURVNET_SET(vnet0);
301b032f27cSSam Leffler 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
302fc74a9f9SBrooks Davis 	if (ifp == NULL) {
303fc74a9f9SBrooks Davis 		device_printf(sc->sc_dev, "can not if_alloc()\n");
304fc74a9f9SBrooks Davis 		error = ENOSPC;
305bb327d28SAdrian Chadd 		CURVNET_RESTORE();
306fc74a9f9SBrooks Davis 		goto bad;
307fc74a9f9SBrooks Davis 	}
308b032f27cSSam Leffler 	ic = ifp->if_l2com;
309fc74a9f9SBrooks Davis 
3105591b213SSam Leffler 	/* set these up early for if_printf use */
3119bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(sc->sc_dev),
3129bf40edeSBrooks Davis 		device_get_unit(sc->sc_dev));
313a93c5097SAdrian Chadd 	CURVNET_RESTORE();
3145591b213SSam Leffler 
3157e97436bSAdrian Chadd 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh,
3167e97436bSAdrian Chadd 	    sc->sc_eepromdata, &status);
3175591b213SSam Leffler 	if (ah == NULL) {
3185591b213SSam Leffler 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
3195591b213SSam Leffler 			status);
3205591b213SSam Leffler 		error = ENXIO;
3215591b213SSam Leffler 		goto bad;
3225591b213SSam Leffler 	}
3235591b213SSam Leffler 	sc->sc_ah = ah;
324b58b3803SSam Leffler 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
3253297be13SSam Leffler #ifdef	ATH_DEBUG
3263297be13SSam Leffler 	sc->sc_debug = ath_debug;
3273297be13SSam Leffler #endif
3285591b213SSam Leffler 
3295591b213SSam Leffler 	/*
330f8cc9b09SAdrian Chadd 	 * Setup the DMA/EDMA functions based on the current
331f8cc9b09SAdrian Chadd 	 * hardware support.
332f8cc9b09SAdrian Chadd 	 *
333f8cc9b09SAdrian Chadd 	 * This is required before the descriptors are allocated.
334f8cc9b09SAdrian Chadd 	 */
3353d184db2SAdrian Chadd 	if (ath_hal_hasedma(sc->sc_ah)) {
3363d184db2SAdrian Chadd 		sc->sc_isedma = 1;
337f8cc9b09SAdrian Chadd 		ath_recv_setup_edma(sc);
3383fdfc330SAdrian Chadd 		ath_xmit_setup_edma(sc);
3393fdfc330SAdrian Chadd 	} else {
340f8cc9b09SAdrian Chadd 		ath_recv_setup_legacy(sc);
3413fdfc330SAdrian Chadd 		ath_xmit_setup_legacy(sc);
3423fdfc330SAdrian Chadd 	}
343f8cc9b09SAdrian Chadd 
344f8cc9b09SAdrian Chadd 	/*
345c42a7b7eSSam Leffler 	 * Check if the MAC has multi-rate retry support.
346c42a7b7eSSam Leffler 	 * We do this by trying to setup a fake extended
347c42a7b7eSSam Leffler 	 * descriptor.  MAC's that don't have support will
348c42a7b7eSSam Leffler 	 * return false w/o doing anything.  MAC's that do
349c42a7b7eSSam Leffler 	 * support it will return true w/o doing anything.
350c42a7b7eSSam Leffler 	 */
351c42a7b7eSSam Leffler 	sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
352c42a7b7eSSam Leffler 
353c42a7b7eSSam Leffler 	/*
354c42a7b7eSSam Leffler 	 * Check if the device has hardware counters for PHY
355c42a7b7eSSam Leffler 	 * errors.  If so we need to enable the MIB interrupt
356c42a7b7eSSam Leffler 	 * so we can act on stat triggers.
357c42a7b7eSSam Leffler 	 */
358c42a7b7eSSam Leffler 	if (ath_hal_hwphycounters(ah))
359c42a7b7eSSam Leffler 		sc->sc_needmib = 1;
360c42a7b7eSSam Leffler 
361c42a7b7eSSam Leffler 	/*
362c42a7b7eSSam Leffler 	 * Get the hardware key cache size.
363c42a7b7eSSam Leffler 	 */
364c42a7b7eSSam Leffler 	sc->sc_keymax = ath_hal_keycachesize(ah);
365e8fd88a3SSam Leffler 	if (sc->sc_keymax > ATH_KEYMAX) {
366e8fd88a3SSam Leffler 		if_printf(ifp, "Warning, using only %u of %u key cache slots\n",
367e8fd88a3SSam Leffler 			ATH_KEYMAX, sc->sc_keymax);
368e8fd88a3SSam Leffler 		sc->sc_keymax = ATH_KEYMAX;
369c42a7b7eSSam Leffler 	}
370c42a7b7eSSam Leffler 	/*
371c42a7b7eSSam Leffler 	 * Reset the key cache since some parts do not
372c42a7b7eSSam Leffler 	 * reset the contents on initial power up.
373c42a7b7eSSam Leffler 	 */
374c42a7b7eSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
375c42a7b7eSSam Leffler 		ath_hal_keyreset(ah, i);
376c42a7b7eSSam Leffler 
377c42a7b7eSSam Leffler 	/*
378b032f27cSSam Leffler 	 * Collect the default channel list.
3795591b213SSam Leffler 	 */
380b032f27cSSam Leffler 	error = ath_getchannels(sc);
3815591b213SSam Leffler 	if (error != 0)
3825591b213SSam Leffler 		goto bad;
3835591b213SSam Leffler 
3845591b213SSam Leffler 	/*
3855591b213SSam Leffler 	 * Setup rate tables for all potential media types.
3865591b213SSam Leffler 	 */
3875591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11A);
3885591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11B);
3895591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11G);
390c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
391c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
39268e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_STURBO_A);
39368e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NA);
39468e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NG);
395724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_HALF);
396724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_QUARTER);
397aaa70f2fSSam Leffler 
398c42a7b7eSSam Leffler 	/* NB: setup here so ath_rate_update is happy */
399c42a7b7eSSam Leffler 	ath_setcurmode(sc, IEEE80211_MODE_11A);
4005591b213SSam Leffler 
401c42a7b7eSSam Leffler 	/*
4023fdfc330SAdrian Chadd 	 * Allocate TX descriptors and populate the lists.
403c42a7b7eSSam Leffler 	 */
4045591b213SSam Leffler 	error = ath_desc_alloc(sc);
4055591b213SSam Leffler 	if (error != 0) {
4063fdfc330SAdrian Chadd 		if_printf(ifp, "failed to allocate TX descriptors: %d\n",
4073fdfc330SAdrian Chadd 		    error);
4083fdfc330SAdrian Chadd 		goto bad;
4093fdfc330SAdrian Chadd 	}
4103fdfc330SAdrian Chadd 	error = ath_txdma_setup(sc);
4113fdfc330SAdrian Chadd 	if (error != 0) {
4123fdfc330SAdrian Chadd 		if_printf(ifp, "failed to allocate TX descriptors: %d\n",
4133fdfc330SAdrian Chadd 		    error);
4145591b213SSam Leffler 		goto bad;
4155591b213SSam Leffler 	}
4163d184db2SAdrian Chadd 
4173fdfc330SAdrian Chadd 	/*
4183fdfc330SAdrian Chadd 	 * Allocate RX descriptors and populate the lists.
4193fdfc330SAdrian Chadd 	 */
4203d184db2SAdrian Chadd 	error = ath_rxdma_setup(sc);
4213d184db2SAdrian Chadd 	if (error != 0) {
4223d184db2SAdrian Chadd 		if_printf(ifp, "failed to allocate RX descriptors: %d\n",
4233d184db2SAdrian Chadd 		    error);
4243d184db2SAdrian Chadd 		goto bad;
4253d184db2SAdrian Chadd 	}
4263d184db2SAdrian Chadd 
4272e986da5SSam Leffler 	callout_init_mtx(&sc->sc_cal_ch, &sc->sc_mtx, 0);
4282e986da5SSam Leffler 	callout_init_mtx(&sc->sc_wd_ch, &sc->sc_mtx, 0);
4295591b213SSam Leffler 
430f0b2a0beSSam Leffler 	ATH_TXBUF_LOCK_INIT(sc);
4315591b213SSam Leffler 
4320bbf5441SSam Leffler 	sc->sc_tq = taskqueue_create("ath_taskq", M_NOWAIT,
4330bbf5441SSam Leffler 		taskqueue_thread_enqueue, &sc->sc_tq);
4340bbf5441SSam Leffler 	taskqueue_start_threads(&sc->sc_tq, 1, PI_NET,
4350bbf5441SSam Leffler 		"%s taskq", ifp->if_xname);
4360bbf5441SSam Leffler 
437f8cc9b09SAdrian Chadd 	TASK_INIT(&sc->sc_rxtask, 0, sc->sc_rx.recv_tasklet, sc);
4385591b213SSam Leffler 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
439c42a7b7eSSam Leffler 	TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc);
440d52f7132SAdrian Chadd 	TASK_INIT(&sc->sc_resettask,0, ath_reset_proc, sc);
44103e9308fSAdrian Chadd 	TASK_INIT(&sc->sc_txqtask, 0, ath_txq_sched_tasklet, sc);
442f846cf42SAdrian Chadd 	TASK_INIT(&sc->sc_fataltask, 0, ath_fatal_proc, sc);
4435591b213SSam Leffler 
4445591b213SSam Leffler 	/*
445c42a7b7eSSam Leffler 	 * Allocate hardware transmit queues: one queue for
446c42a7b7eSSam Leffler 	 * beacon frames and one data queue for each QoS
4474fa8d4efSDaniel Eischen 	 * priority.  Note that the hal handles resetting
448c42a7b7eSSam Leffler 	 * these queues at the needed time.
449c42a7b7eSSam Leffler 	 *
450c42a7b7eSSam Leffler 	 * XXX PS-Poll
4515591b213SSam Leffler 	 */
452e1252ce1SAdrian Chadd 	sc->sc_bhalq = ath_beaconq_setup(sc);
4535591b213SSam Leffler 	if (sc->sc_bhalq == (u_int) -1) {
4545591b213SSam Leffler 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
455c42a7b7eSSam Leffler 		error = EIO;
456b28b4653SSam Leffler 		goto bad2;
4575591b213SSam Leffler 	}
458c42a7b7eSSam Leffler 	sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
459c42a7b7eSSam Leffler 	if (sc->sc_cabq == NULL) {
460c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup CAB xmit queue!\n");
461c42a7b7eSSam Leffler 		error = EIO;
462c42a7b7eSSam Leffler 		goto bad2;
463c42a7b7eSSam Leffler 	}
464c42a7b7eSSam Leffler 	/* NB: insure BK queue is the lowest priority h/w queue */
465c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
466c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup xmit queue for %s traffic!\n",
467c42a7b7eSSam Leffler 			ieee80211_wme_acnames[WME_AC_BK]);
468c42a7b7eSSam Leffler 		error = EIO;
469c42a7b7eSSam Leffler 		goto bad2;
470c42a7b7eSSam Leffler 	}
471c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
472c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
473c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
474c42a7b7eSSam Leffler 		/*
475c42a7b7eSSam Leffler 		 * Not enough hardware tx queues to properly do WME;
476c42a7b7eSSam Leffler 		 * just punt and assign them all to the same h/w queue.
477c42a7b7eSSam Leffler 		 * We could do a better job of this if, for example,
478c42a7b7eSSam Leffler 		 * we allocate queues when we switch from station to
479c42a7b7eSSam Leffler 		 * AP mode.
480c42a7b7eSSam Leffler 		 */
481c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_VI] != NULL)
482c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
483c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_BE] != NULL)
484c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
485c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
486c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
487c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
488c42a7b7eSSam Leffler 	}
489c42a7b7eSSam Leffler 
490c42a7b7eSSam Leffler 	/*
491f8418db5SAdrian Chadd 	 * Attach the TX completion function.
492f8418db5SAdrian Chadd 	 *
493f8418db5SAdrian Chadd 	 * The non-EDMA chips may have some special case optimisations;
494f8418db5SAdrian Chadd 	 * this method gives everyone a chance to attach cleanly.
495c42a7b7eSSam Leffler 	 */
496f8418db5SAdrian Chadd 	sc->sc_tx.xmit_attach_comp_func(sc);
497c42a7b7eSSam Leffler 
498c42a7b7eSSam Leffler 	/*
499c42a7b7eSSam Leffler 	 * Setup rate control.  Some rate control modules
500c42a7b7eSSam Leffler 	 * call back to change the anntena state so expose
501c42a7b7eSSam Leffler 	 * the necessary entry points.
502c42a7b7eSSam Leffler 	 * XXX maybe belongs in struct ath_ratectrl?
503c42a7b7eSSam Leffler 	 */
504c42a7b7eSSam Leffler 	sc->sc_setdefantenna = ath_setdefantenna;
505c42a7b7eSSam Leffler 	sc->sc_rc = ath_rate_attach(sc);
506c42a7b7eSSam Leffler 	if (sc->sc_rc == NULL) {
507c42a7b7eSSam Leffler 		error = EIO;
508c42a7b7eSSam Leffler 		goto bad2;
509c42a7b7eSSam Leffler 	}
510c42a7b7eSSam Leffler 
51148237774SAdrian Chadd 	/* Attach DFS module */
51248237774SAdrian Chadd 	if (! ath_dfs_attach(sc)) {
5137e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
5147e97436bSAdrian Chadd 		    "%s: unable to attach DFS\n", __func__);
51548237774SAdrian Chadd 		error = EIO;
51648237774SAdrian Chadd 		goto bad2;
51748237774SAdrian Chadd 	}
51848237774SAdrian Chadd 
5199af351f9SAdrian Chadd 	/* Attach spectral module */
5209af351f9SAdrian Chadd 	if (ath_spectral_attach(sc) < 0) {
5219af351f9SAdrian Chadd 		device_printf(sc->sc_dev,
5229af351f9SAdrian Chadd 		    "%s: unable to attach spectral\n", __func__);
5239af351f9SAdrian Chadd 		error = EIO;
5249af351f9SAdrian Chadd 		goto bad2;
5259af351f9SAdrian Chadd 	}
5269af351f9SAdrian Chadd 
527b70f530bSAdrian Chadd 	/* Attach bluetooth coexistence module */
528b70f530bSAdrian Chadd 	if (ath_btcoex_attach(sc) < 0) {
529b70f530bSAdrian Chadd 		device_printf(sc->sc_dev,
530b70f530bSAdrian Chadd 		    "%s: unable to attach bluetooth coexistence\n", __func__);
531b70f530bSAdrian Chadd 		error = EIO;
532b70f530bSAdrian Chadd 		goto bad2;
533b70f530bSAdrian Chadd 	}
534b70f530bSAdrian Chadd 
535216ca234SAdrian Chadd 	/* Attach LNA diversity module */
536216ca234SAdrian Chadd 	if (ath_lna_div_attach(sc) < 0) {
537216ca234SAdrian Chadd 		device_printf(sc->sc_dev,
538216ca234SAdrian Chadd 		    "%s: unable to attach LNA diversity\n", __func__);
539216ca234SAdrian Chadd 		error = EIO;
540216ca234SAdrian Chadd 		goto bad2;
541216ca234SAdrian Chadd 	}
542216ca234SAdrian Chadd 
54348237774SAdrian Chadd 	/* Start DFS processing tasklet */
54448237774SAdrian Chadd 	TASK_INIT(&sc->sc_dfstask, 0, ath_dfs_tasklet, sc);
54548237774SAdrian Chadd 
5463440495aSAdrian Chadd 	/* Configure LED state */
5473e50ec2cSSam Leffler 	sc->sc_blinking = 0;
548c42a7b7eSSam Leffler 	sc->sc_ledstate = 1;
5493e50ec2cSSam Leffler 	sc->sc_ledon = 0;			/* low true */
5503e50ec2cSSam Leffler 	sc->sc_ledidle = (2700*hz)/1000;	/* 2.7sec */
5513e50ec2cSSam Leffler 	callout_init(&sc->sc_ledtimer, CALLOUT_MPSAFE);
5523440495aSAdrian Chadd 
5533440495aSAdrian Chadd 	/*
5543440495aSAdrian Chadd 	 * Don't setup hardware-based blinking.
5553440495aSAdrian Chadd 	 *
5563440495aSAdrian Chadd 	 * Although some NICs may have this configured in the
5573440495aSAdrian Chadd 	 * default reset register values, the user may wish
5583440495aSAdrian Chadd 	 * to alter which pins have which function.
5593440495aSAdrian Chadd 	 *
5603440495aSAdrian Chadd 	 * The reference driver attaches the MAC network LED to GPIO1 and
5613440495aSAdrian Chadd 	 * the MAC power LED to GPIO2.  However, the DWA-552 cardbus
5623440495aSAdrian Chadd 	 * NIC has these reversed.
5633440495aSAdrian Chadd 	 */
5643440495aSAdrian Chadd 	sc->sc_hardled = (1 == 0);
5653440495aSAdrian Chadd 	sc->sc_led_net_pin = -1;
5663440495aSAdrian Chadd 	sc->sc_led_pwr_pin = -1;
567c42a7b7eSSam Leffler 	/*
568c42a7b7eSSam Leffler 	 * Auto-enable soft led processing for IBM cards and for
569c42a7b7eSSam Leffler 	 * 5211 minipci cards.  Users can also manually enable/disable
570c42a7b7eSSam Leffler 	 * support with a sysctl.
571c42a7b7eSSam Leffler 	 */
572c42a7b7eSSam Leffler 	sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
5736558ffd9SAdrian Chadd 	ath_led_config(sc);
574a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
5755591b213SSam Leffler 
5765591b213SSam Leffler 	ifp->if_softc = sc;
5775591b213SSam Leffler 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
578cd7dffd0SAdrian Chadd 	ifp->if_transmit = ath_transmit;
579cd7dffd0SAdrian Chadd 	ifp->if_qflush = ath_qflush;
5805591b213SSam Leffler 	ifp->if_ioctl = ath_ioctl;
5815591b213SSam Leffler 	ifp->if_init = ath_init;
582e50d35e6SMaxim Sobolev 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
583e50d35e6SMaxim Sobolev 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
584154b8df2SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
5855591b213SSam Leffler 
586c42a7b7eSSam Leffler 	ic->ic_ifp = ifp;
5875591b213SSam Leffler 	/* XXX not right but it's not used anywhere important */
5885591b213SSam Leffler 	ic->ic_phytype = IEEE80211_T_OFDM;
5895591b213SSam Leffler 	ic->ic_opmode = IEEE80211_M_STA;
590c42a7b7eSSam Leffler 	ic->ic_caps =
591c43feedeSSam Leffler 		  IEEE80211_C_STA		/* station mode */
592c43feedeSSam Leffler 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
593fe32c3efSSam Leffler 		| IEEE80211_C_HOSTAP		/* hostap mode */
594fe32c3efSSam Leffler 		| IEEE80211_C_MONITOR		/* monitor mode */
5957a04dc27SSam Leffler 		| IEEE80211_C_AHDEMO		/* adhoc demo mode */
596b032f27cSSam Leffler 		| IEEE80211_C_WDS		/* 4-address traffic works */
59759aa14a9SRui Paulo 		| IEEE80211_C_MBSS		/* mesh point link mode */
598fe32c3efSSam Leffler 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
599c42a7b7eSSam Leffler 		| IEEE80211_C_SHSLOT		/* short slot time supported */
600c42a7b7eSSam Leffler 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
6013b324f57SAdrian Chadd #ifndef	ATH_ENABLE_11N
60268e8e04eSSam Leffler 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
6033b324f57SAdrian Chadd #endif
60468e8e04eSSam Leffler 		| IEEE80211_C_TXFRAG		/* handle tx frags */
60510dc8de4SAdrian Chadd #ifdef	ATH_ENABLE_DFS
6067e97436bSAdrian Chadd 		| IEEE80211_C_DFS		/* Enable radar detection */
60710dc8de4SAdrian Chadd #endif
60801e7e035SSam Leffler 		;
609c42a7b7eSSam Leffler 	/*
610c42a7b7eSSam Leffler 	 * Query the hal to figure out h/w crypto support.
611c42a7b7eSSam Leffler 	 */
612c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
613b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
614c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
615b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_OCB;
616c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
617b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_CCM;
618c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
619b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_CKIP;
620c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
621b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIP;
622c42a7b7eSSam Leffler 		/*
623c42a7b7eSSam Leffler 		 * Check if h/w does the MIC and/or whether the
624c42a7b7eSSam Leffler 		 * separate key cache entries are required to
625c42a7b7eSSam Leffler 		 * handle both tx+rx MIC keys.
626c42a7b7eSSam Leffler 		 */
627c42a7b7eSSam Leffler 		if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
628b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
6295901d2d3SSam Leffler 		/*
6305901d2d3SSam Leffler 		 * If the h/w supports storing tx+rx MIC keys
6315901d2d3SSam Leffler 		 * in one cache slot automatically enable use.
6325901d2d3SSam Leffler 		 */
6335901d2d3SSam Leffler 		if (ath_hal_hastkipsplit(ah) ||
6345901d2d3SSam Leffler 		    !ath_hal_settkipsplit(ah, AH_FALSE))
635c42a7b7eSSam Leffler 			sc->sc_splitmic = 1;
636b032f27cSSam Leffler 		/*
637b032f27cSSam Leffler 		 * If the h/w can do TKIP MIC together with WME then
638b032f27cSSam Leffler 		 * we use it; otherwise we force the MIC to be done
639b032f27cSSam Leffler 		 * in software by the net80211 layer.
640b032f27cSSam Leffler 		 */
641b032f27cSSam Leffler 		if (ath_hal_haswmetkipmic(ah))
642b032f27cSSam Leffler 			sc->sc_wmetkipmic = 1;
643c42a7b7eSSam Leffler 	}
644e8fd88a3SSam Leffler 	sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR);
6459ac01d39SRui Paulo 	/*
6461ac5dac2SRui Paulo 	 * Check for multicast key search support.
6479ac01d39SRui Paulo 	 */
6489ac01d39SRui Paulo 	if (ath_hal_hasmcastkeysearch(sc->sc_ah) &&
6499ac01d39SRui Paulo 	    !ath_hal_getmcastkeysearch(sc->sc_ah)) {
6509ac01d39SRui Paulo 		ath_hal_setmcastkeysearch(sc->sc_ah, 1);
6519ac01d39SRui Paulo 	}
652e8fd88a3SSam Leffler 	sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah);
653c42a7b7eSSam Leffler 	/*
6545901d2d3SSam Leffler 	 * Mark key cache slots associated with global keys
6555901d2d3SSam Leffler 	 * as in use.  If we knew TKIP was not to be used we
6565901d2d3SSam Leffler 	 * could leave the +32, +64, and +32+64 slots free.
6575901d2d3SSam Leffler 	 */
6585901d2d3SSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
6595901d2d3SSam Leffler 		setbit(sc->sc_keymap, i);
6605901d2d3SSam Leffler 		setbit(sc->sc_keymap, i+64);
6615901d2d3SSam Leffler 		if (sc->sc_splitmic) {
6625901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32);
6635901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32+64);
6645901d2d3SSam Leffler 		}
6655901d2d3SSam Leffler 	}
6665901d2d3SSam Leffler 	/*
667c42a7b7eSSam Leffler 	 * TPC support can be done either with a global cap or
668c42a7b7eSSam Leffler 	 * per-packet support.  The latter is not available on
669c42a7b7eSSam Leffler 	 * all parts.  We're a bit pedantic here as all parts
670c42a7b7eSSam Leffler 	 * support a global cap.
671c42a7b7eSSam Leffler 	 */
672c59005e9SSam Leffler 	if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah))
673c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TXPMGT;
674c42a7b7eSSam Leffler 
675c42a7b7eSSam Leffler 	/*
676c42a7b7eSSam Leffler 	 * Mark WME capability only if we have sufficient
677c42a7b7eSSam Leffler 	 * hardware queues to do proper priority scheduling.
678c42a7b7eSSam Leffler 	 */
679c42a7b7eSSam Leffler 	if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
680c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_WME;
681c42a7b7eSSam Leffler 	/*
682e8fd88a3SSam Leffler 	 * Check for misc other capabilities.
683c42a7b7eSSam Leffler 	 */
684c42a7b7eSSam Leffler 	if (ath_hal_hasbursting(ah))
685c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_BURST;
686b032f27cSSam Leffler 	sc->sc_hasbmask = ath_hal_hasbssidmask(ah);
68759aa14a9SRui Paulo 	sc->sc_hasbmatch = ath_hal_hasbssidmatch(ah);
688b032f27cSSam Leffler 	sc->sc_hastsfadd = ath_hal_hastsfadjust(ah);
6898a2a6beeSAdrian Chadd 	sc->sc_rxslink = ath_hal_self_linked_final_rxdesc(ah);
690fc4de9b7SAdrian Chadd 	sc->sc_rxtsf32 = ath_hal_has_long_rxdesc_tsf(ah);
691dd6a574eSAdrian Chadd 	sc->sc_hasenforcetxop = ath_hal_hasenforcetxop(ah);
6923df7a8abSAdrian Chadd 	sc->sc_rx_lnamixer = ath_hal_hasrxlnamixer(ah);
693216ca234SAdrian Chadd 	sc->sc_hasdivcomb = ath_hal_hasdivantcomb(ah);
694216ca234SAdrian Chadd 
69568e8e04eSSam Leffler 	if (ath_hal_hasfastframes(ah))
69668e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_FF;
69759efa8b5SSam Leffler 	wmodes = ath_hal_getwirelessmodes(ah);
698411373ebSSam Leffler 	if (wmodes & (HAL_MODE_108G|HAL_MODE_TURBO))
69968e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TURBOP;
700584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
70110ad9a77SSam Leffler 	if (ath_hal_macversion(ah) > 0x78) {
70210ad9a77SSam Leffler 		ic->ic_caps |= IEEE80211_C_TDMA; /* capable of TDMA */
70310ad9a77SSam Leffler 		ic->ic_tdma_update = ath_tdma_update;
70410ad9a77SSam Leffler 	}
70510ad9a77SSam Leffler #endif
70667397d39SAdrian Chadd 
70767397d39SAdrian Chadd 	/*
7089c85ff91SAdrian Chadd 	 * TODO: enforce that at least this many frames are available
7099c85ff91SAdrian Chadd 	 * in the txbuf list before allowing data frames (raw or
7109c85ff91SAdrian Chadd 	 * otherwise) to be transmitted.
7119c85ff91SAdrian Chadd 	 */
7129c85ff91SAdrian Chadd 	sc->sc_txq_data_minfree = 10;
7139c85ff91SAdrian Chadd 	/*
7149c85ff91SAdrian Chadd 	 * Leave this as default to maintain legacy behaviour.
7159c85ff91SAdrian Chadd 	 * Shortening the cabq/mcastq may end up causing some
7169c85ff91SAdrian Chadd 	 * undesirable behaviour.
7179c85ff91SAdrian Chadd 	 */
7189c85ff91SAdrian Chadd 	sc->sc_txq_mcastq_maxdepth = ath_txbuf;
7199c85ff91SAdrian Chadd 
7207dcb2beaSAdrian Chadd 	/*
72122a3aee6SAdrian Chadd 	 * How deep can the node software TX queue get whilst it's asleep.
72222a3aee6SAdrian Chadd 	 */
72322a3aee6SAdrian Chadd 	sc->sc_txq_node_psq_maxdepth = 16;
72422a3aee6SAdrian Chadd 
72522a3aee6SAdrian Chadd 	/*
7267dcb2beaSAdrian Chadd 	 * Default the maximum queue depth for a given node
7277dcb2beaSAdrian Chadd 	 * to 1/4'th the TX buffers, or 64, whichever
7287dcb2beaSAdrian Chadd 	 * is larger.
7297dcb2beaSAdrian Chadd 	 */
7307dcb2beaSAdrian Chadd 	sc->sc_txq_node_maxdepth = MAX(64, ath_txbuf / 4);
7317dcb2beaSAdrian Chadd 
732b837332dSAdrian Chadd 	/* Enable CABQ by default */
733b837332dSAdrian Chadd 	sc->sc_cabq_enable = 1;
734b837332dSAdrian Chadd 
7359c85ff91SAdrian Chadd 	/*
736a865860dSAdrian Chadd 	 * Allow the TX and RX chainmasks to be overridden by
737a865860dSAdrian Chadd 	 * environment variables and/or device.hints.
738a865860dSAdrian Chadd 	 *
739a865860dSAdrian Chadd 	 * This must be done early - before the hardware is
740a865860dSAdrian Chadd 	 * calibrated or before the 802.11n stream calculation
741a865860dSAdrian Chadd 	 * is done.
742a865860dSAdrian Chadd 	 */
743a865860dSAdrian Chadd 	if (resource_int_value(device_get_name(sc->sc_dev),
744a865860dSAdrian Chadd 	    device_get_unit(sc->sc_dev), "rx_chainmask",
745a865860dSAdrian Chadd 	    &rx_chainmask) == 0) {
746a865860dSAdrian Chadd 		device_printf(sc->sc_dev, "Setting RX chainmask to 0x%x\n",
747a865860dSAdrian Chadd 		    rx_chainmask);
748a865860dSAdrian Chadd 		(void) ath_hal_setrxchainmask(sc->sc_ah, rx_chainmask);
749a865860dSAdrian Chadd 	}
750a865860dSAdrian Chadd 	if (resource_int_value(device_get_name(sc->sc_dev),
751a865860dSAdrian Chadd 	    device_get_unit(sc->sc_dev), "tx_chainmask",
752a865860dSAdrian Chadd 	    &tx_chainmask) == 0) {
753a865860dSAdrian Chadd 		device_printf(sc->sc_dev, "Setting TX chainmask to 0x%x\n",
754a865860dSAdrian Chadd 		    tx_chainmask);
755dc8552d5SAdrian Chadd 		(void) ath_hal_settxchainmask(sc->sc_ah, tx_chainmask);
756a865860dSAdrian Chadd 	}
757a865860dSAdrian Chadd 
758af017101SAdrian Chadd 	/*
759ff5b5634SAdrian Chadd 	 * Query the TX/RX chainmask configuration.
760ff5b5634SAdrian Chadd 	 *
761ff5b5634SAdrian Chadd 	 * This is only relevant for 11n devices.
762ff5b5634SAdrian Chadd 	 */
763ff5b5634SAdrian Chadd 	ath_hal_getrxchainmask(ah, &sc->sc_rxchainmask);
764ff5b5634SAdrian Chadd 	ath_hal_gettxchainmask(ah, &sc->sc_txchainmask);
765ff5b5634SAdrian Chadd 
766ff5b5634SAdrian Chadd 	/*
767af017101SAdrian Chadd 	 * Disable MRR with protected frames by default.
768af017101SAdrian Chadd 	 * Only 802.11n series NICs can handle this.
769af017101SAdrian Chadd 	 */
770af017101SAdrian Chadd 	sc->sc_mrrprot = 0;	/* XXX should be a capability */
771af017101SAdrian Chadd 
7725540369bSAdrian Chadd 	/*
7735540369bSAdrian Chadd 	 * Query the enterprise mode information the HAL.
7745540369bSAdrian Chadd 	 */
7755540369bSAdrian Chadd 	if (ath_hal_getcapability(ah, HAL_CAP_ENTERPRISE_MODE, 0,
7765540369bSAdrian Chadd 	    &sc->sc_ent_cfg) == HAL_OK)
7775540369bSAdrian Chadd 		sc->sc_use_ent = 1;
7785540369bSAdrian Chadd 
7798fd67f92SAdrian Chadd #ifdef	ATH_ENABLE_11N
78067397d39SAdrian Chadd 	/*
78167397d39SAdrian Chadd 	 * Query HT capabilities
78267397d39SAdrian Chadd 	 */
78367397d39SAdrian Chadd 	if (ath_hal_getcapability(ah, HAL_CAP_HT, 0, NULL) == HAL_OK &&
78467397d39SAdrian Chadd 	    (wmodes & (HAL_MODE_HT20 | HAL_MODE_HT40))) {
7856f4fb2d8SAdrian Chadd 		uint32_t rxs, txs;
78667397d39SAdrian Chadd 
78767397d39SAdrian Chadd 		device_printf(sc->sc_dev, "[HT] enabling HT modes\n");
788af017101SAdrian Chadd 
789af017101SAdrian Chadd 		sc->sc_mrrprot = 1;	/* XXX should be a capability */
790af017101SAdrian Chadd 
79167397d39SAdrian Chadd 		ic->ic_htcaps = IEEE80211_HTC_HT	/* HT operation */
79267397d39SAdrian Chadd 			    | IEEE80211_HTC_AMPDU	/* A-MPDU tx/rx */
79367397d39SAdrian Chadd 			    | IEEE80211_HTC_AMSDU	/* A-MSDU tx/rx */
7947e97436bSAdrian Chadd 			    | IEEE80211_HTCAP_MAXAMSDU_3839
7957e97436bSAdrian Chadd 			    				/* max A-MSDU length */
79667397d39SAdrian Chadd 			    | IEEE80211_HTCAP_SMPS_OFF;	/* SM power save off */
79767397d39SAdrian Chadd 			;
79867397d39SAdrian Chadd 
79976355edbSAdrian Chadd 		/*
80076355edbSAdrian Chadd 		 * Enable short-GI for HT20 only if the hardware
80176355edbSAdrian Chadd 		 * advertises support.
80276355edbSAdrian Chadd 		 * Notably, anything earlier than the AR9287 doesn't.
80376355edbSAdrian Chadd 		 */
80476355edbSAdrian Chadd 		if ((ath_hal_getcapability(ah,
80576355edbSAdrian Chadd 		    HAL_CAP_HT20_SGI, 0, NULL) == HAL_OK) &&
80676355edbSAdrian Chadd 		    (wmodes & HAL_MODE_HT20)) {
80776355edbSAdrian Chadd 			device_printf(sc->sc_dev,
80876355edbSAdrian Chadd 			    "[HT] enabling short-GI in 20MHz mode\n");
80976355edbSAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
81076355edbSAdrian Chadd 		}
81176355edbSAdrian Chadd 
81267397d39SAdrian Chadd 		if (wmodes & HAL_MODE_HT40)
81367397d39SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40
81467397d39SAdrian Chadd 			    |  IEEE80211_HTCAP_SHORTGI40;
81567397d39SAdrian Chadd 
81667397d39SAdrian Chadd 		/*
8177e97436bSAdrian Chadd 		 * TX/RX streams need to be taken into account when
8187e97436bSAdrian Chadd 		 * negotiating which MCS rates it'll receive and
81967397d39SAdrian Chadd 		 * what MCS rates are available for TX.
82067397d39SAdrian Chadd 		 */
82154517070SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 0, &txs);
82254517070SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 1, &rxs);
82367397d39SAdrian Chadd 		ic->ic_txstream = txs;
82467397d39SAdrian Chadd 		ic->ic_rxstream = rxs;
82567397d39SAdrian Chadd 
8266606ba81SAdrian Chadd 		/*
8276606ba81SAdrian Chadd 		 * Setup TX and RX STBC based on what the HAL allows and
8286606ba81SAdrian Chadd 		 * the currently configured chainmask set.
8296606ba81SAdrian Chadd 		 * Ie - don't enable STBC TX if only one chain is enabled.
8306606ba81SAdrian Chadd 		 * STBC RX is fine on a single RX chain; it just won't
8316606ba81SAdrian Chadd 		 * provide any real benefit.
8326606ba81SAdrian Chadd 		 */
8336606ba81SAdrian Chadd 		if (ath_hal_getcapability(ah, HAL_CAP_RX_STBC, 0,
8346606ba81SAdrian Chadd 		    NULL) == HAL_OK) {
8356606ba81SAdrian Chadd 			sc->sc_rx_stbc = 1;
8366606ba81SAdrian Chadd 			device_printf(sc->sc_dev,
8376606ba81SAdrian Chadd 			    "[HT] 1 stream STBC receive enabled\n");
8386606ba81SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_1STREAM;
8396606ba81SAdrian Chadd 		}
8406606ba81SAdrian Chadd 		if (txs > 1 && ath_hal_getcapability(ah, HAL_CAP_TX_STBC, 0,
8416606ba81SAdrian Chadd 		    NULL) == HAL_OK) {
8426606ba81SAdrian Chadd 			sc->sc_tx_stbc = 1;
8436606ba81SAdrian Chadd 			device_printf(sc->sc_dev,
8446606ba81SAdrian Chadd 			    "[HT] 1 stream STBC transmit enabled\n");
8456606ba81SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
8466606ba81SAdrian Chadd 		}
8476606ba81SAdrian Chadd 
848ce656facSAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_RTS_AGGR_LIMIT, 1,
849ce656facSAdrian Chadd 		    &sc->sc_rts_aggr_limit);
850ce656facSAdrian Chadd 		if (sc->sc_rts_aggr_limit != (64 * 1024))
851ce656facSAdrian Chadd 			device_printf(sc->sc_dev,
852ce656facSAdrian Chadd 			    "[HT] RTS aggregates limited to %d KiB\n",
853ce656facSAdrian Chadd 			    sc->sc_rts_aggr_limit / 1024);
854ce656facSAdrian Chadd 
8557e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
8567e97436bSAdrian Chadd 		    "[HT] %d RX streams; %d TX streams\n", rxs, txs);
85767397d39SAdrian Chadd 	}
85867397d39SAdrian Chadd #endif
85967397d39SAdrian Chadd 
860c42a7b7eSSam Leffler 	/*
861f8aa9fd5SAdrian Chadd 	 * Initial aggregation settings.
862f8aa9fd5SAdrian Chadd 	 */
86372910f03SAdrian Chadd 	sc->sc_hwq_limit_aggr = ATH_AGGR_MIN_QDEPTH;
86472910f03SAdrian Chadd 	sc->sc_hwq_limit_nonaggr = ATH_NONAGGR_MIN_QDEPTH;
865f8aa9fd5SAdrian Chadd 	sc->sc_tid_hwq_lo = ATH_AGGR_SCHED_LOW;
866f8aa9fd5SAdrian Chadd 	sc->sc_tid_hwq_hi = ATH_AGGR_SCHED_HIGH;
8674a502c33SAdrian Chadd 	sc->sc_aggr_limit = ATH_AGGR_MAXSIZE;
868a54ecf78SAdrian Chadd 	sc->sc_delim_min_pad = 0;
869f8aa9fd5SAdrian Chadd 
870f8aa9fd5SAdrian Chadd 	/*
871ddbe3036SAdrian Chadd 	 * Check if the hardware requires PCI register serialisation.
872ddbe3036SAdrian Chadd 	 * Some of the Owl based MACs require this.
873ddbe3036SAdrian Chadd 	 */
874ddbe3036SAdrian Chadd 	if (mp_ncpus > 1 &&
875ddbe3036SAdrian Chadd 	    ath_hal_getcapability(ah, HAL_CAP_SERIALISE_WAR,
876ddbe3036SAdrian Chadd 	     0, NULL) == HAL_OK) {
877ddbe3036SAdrian Chadd 		sc->sc_ah->ah_config.ah_serialise_reg_war = 1;
8787e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
8797e97436bSAdrian Chadd 		    "Enabling register serialisation\n");
880ddbe3036SAdrian Chadd 	}
881ddbe3036SAdrian Chadd 
882ddbe3036SAdrian Chadd 	/*
883f0db652cSAdrian Chadd 	 * Initialise the deferred completed RX buffer list.
884f0db652cSAdrian Chadd 	 */
8855d4dedadSAdrian Chadd 	TAILQ_INIT(&sc->sc_rx_rxlist[HAL_RX_QUEUE_HP]);
8865d4dedadSAdrian Chadd 	TAILQ_INIT(&sc->sc_rx_rxlist[HAL_RX_QUEUE_LP]);
887f0db652cSAdrian Chadd 
888f0db652cSAdrian Chadd 	/*
889c42a7b7eSSam Leffler 	 * Indicate we need the 802.11 header padded to a
890c42a7b7eSSam Leffler 	 * 32-bit boundary for 4-address and QoS frames.
891c42a7b7eSSam Leffler 	 */
892c42a7b7eSSam Leffler 	ic->ic_flags |= IEEE80211_F_DATAPAD;
893c42a7b7eSSam Leffler 
894c42a7b7eSSam Leffler 	/*
895c42a7b7eSSam Leffler 	 * Query the hal about antenna support.
896c42a7b7eSSam Leffler 	 */
897c42a7b7eSSam Leffler 	sc->sc_defant = ath_hal_getdefantenna(ah);
898c42a7b7eSSam Leffler 
899c42a7b7eSSam Leffler 	/*
900c42a7b7eSSam Leffler 	 * Not all chips have the VEOL support we want to
901c42a7b7eSSam Leffler 	 * use with IBSS beacons; check here for it.
902c42a7b7eSSam Leffler 	 */
903c42a7b7eSSam Leffler 	sc->sc_hasveol = ath_hal_hasveol(ah);
9045591b213SSam Leffler 
9055591b213SSam Leffler 	/* get mac address from hardware */
90629aca940SSam Leffler 	ath_hal_getmac(ah, macaddr);
907b032f27cSSam Leffler 	if (sc->sc_hasbmask)
908b032f27cSSam Leffler 		ath_hal_getbssidmask(ah, sc->sc_hwbssidmask);
9095591b213SSam Leffler 
910b032f27cSSam Leffler 	/* NB: used to size node table key mapping array */
911b032f27cSSam Leffler 	ic->ic_max_keyix = sc->sc_keymax;
9125591b213SSam Leffler 	/* call MI attach routine. */
91329aca940SSam Leffler 	ieee80211_ifattach(ic, macaddr);
914b032f27cSSam Leffler 	ic->ic_setregdomain = ath_setregdomain;
915b032f27cSSam Leffler 	ic->ic_getradiocaps = ath_getradiocaps;
916b032f27cSSam Leffler 	sc->sc_opmode = HAL_M_STA;
917b032f27cSSam Leffler 
9185591b213SSam Leffler 	/* override default methods */
919b032f27cSSam Leffler 	ic->ic_newassoc = ath_newassoc;
920b032f27cSSam Leffler 	ic->ic_updateslot = ath_updateslot;
921b032f27cSSam Leffler 	ic->ic_wme.wme_update = ath_wme_update;
922b032f27cSSam Leffler 	ic->ic_vap_create = ath_vap_create;
923b032f27cSSam Leffler 	ic->ic_vap_delete = ath_vap_delete;
924b032f27cSSam Leffler 	ic->ic_raw_xmit = ath_raw_xmit;
925b032f27cSSam Leffler 	ic->ic_update_mcast = ath_update_mcast;
926b032f27cSSam Leffler 	ic->ic_update_promisc = ath_update_promisc;
9275591b213SSam Leffler 	ic->ic_node_alloc = ath_node_alloc;
9281e774079SSam Leffler 	sc->sc_node_free = ic->ic_node_free;
9295591b213SSam Leffler 	ic->ic_node_free = ath_node_free;
9304afa805eSAdrian Chadd 	sc->sc_node_cleanup = ic->ic_node_cleanup;
9314afa805eSAdrian Chadd 	ic->ic_node_cleanup = ath_node_cleanup;
93268e8e04eSSam Leffler 	ic->ic_node_getsignal = ath_node_getsignal;
93368e8e04eSSam Leffler 	ic->ic_scan_start = ath_scan_start;
93468e8e04eSSam Leffler 	ic->ic_scan_end = ath_scan_end;
93568e8e04eSSam Leffler 	ic->ic_set_channel = ath_set_channel;
936fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
937eb6f0de0SAdrian Chadd 	/* 802.11n specific - but just override anyway */
938eb6f0de0SAdrian Chadd 	sc->sc_addba_request = ic->ic_addba_request;
939eb6f0de0SAdrian Chadd 	sc->sc_addba_response = ic->ic_addba_response;
940eb6f0de0SAdrian Chadd 	sc->sc_addba_stop = ic->ic_addba_stop;
941eb6f0de0SAdrian Chadd 	sc->sc_bar_response = ic->ic_bar_response;
942eb6f0de0SAdrian Chadd 	sc->sc_addba_response_timeout = ic->ic_addba_response_timeout;
943eb6f0de0SAdrian Chadd 
944eb6f0de0SAdrian Chadd 	ic->ic_addba_request = ath_addba_request;
945eb6f0de0SAdrian Chadd 	ic->ic_addba_response = ath_addba_response;
946eb6f0de0SAdrian Chadd 	ic->ic_addba_response_timeout = ath_addba_response_timeout;
947eb6f0de0SAdrian Chadd 	ic->ic_addba_stop = ath_addba_stop;
948eb6f0de0SAdrian Chadd 	ic->ic_bar_response = ath_bar_response;
949eb6f0de0SAdrian Chadd 
950fdd72b4aSAdrian Chadd 	ic->ic_update_chw = ath_update_chw;
951fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
952fdd72b4aSAdrian Chadd 
953e1b5ab97SAdrian Chadd #ifdef	ATH_ENABLE_RADIOTAP_VENDOR_EXT
954e1b5ab97SAdrian Chadd 	/*
955e1b5ab97SAdrian Chadd 	 * There's one vendor bitmap entry in the RX radiotap
956e1b5ab97SAdrian Chadd 	 * header; make sure that's taken into account.
957e1b5ab97SAdrian Chadd 	 */
958e1b5ab97SAdrian Chadd 	ieee80211_radiotap_attachv(ic,
959e1b5ab97SAdrian Chadd 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th), 0,
960e1b5ab97SAdrian Chadd 		ATH_TX_RADIOTAP_PRESENT,
961e1b5ab97SAdrian Chadd 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th), 1,
962e1b5ab97SAdrian Chadd 		ATH_RX_RADIOTAP_PRESENT);
963e1b5ab97SAdrian Chadd #else
964e1b5ab97SAdrian Chadd 	/*
965e1b5ab97SAdrian Chadd 	 * No vendor bitmap/extensions are present.
966e1b5ab97SAdrian Chadd 	 */
9675463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
9685463c4a4SSam Leffler 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
9695463c4a4SSam Leffler 		ATH_TX_RADIOTAP_PRESENT,
9705463c4a4SSam Leffler 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
9715463c4a4SSam Leffler 		ATH_RX_RADIOTAP_PRESENT);
972e1b5ab97SAdrian Chadd #endif	/* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
9735463c4a4SSam Leffler 
9744866e6c2SSam Leffler 	/*
975bdbb6e5bSAdrian Chadd 	 * Setup the ALQ logging if required
976bdbb6e5bSAdrian Chadd 	 */
97789d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
978bdbb6e5bSAdrian Chadd 	if_ath_alq_init(&sc->sc_alq, device_get_nameunit(sc->sc_dev));
979bb327d28SAdrian Chadd 	if_ath_alq_setcfg(&sc->sc_alq,
980bb327d28SAdrian Chadd 	    sc->sc_ah->ah_macVersion,
981bb327d28SAdrian Chadd 	    sc->sc_ah->ah_macRev,
982bb327d28SAdrian Chadd 	    sc->sc_ah->ah_phyRev,
983bb327d28SAdrian Chadd 	    sc->sc_ah->ah_magic);
984bdbb6e5bSAdrian Chadd #endif
985bdbb6e5bSAdrian Chadd 
986bdbb6e5bSAdrian Chadd 	/*
9874866e6c2SSam Leffler 	 * Setup dynamic sysctl's now that country code and
9884866e6c2SSam Leffler 	 * regdomain are available from the hal.
9894866e6c2SSam Leffler 	 */
9904866e6c2SSam Leffler 	ath_sysctlattach(sc);
991e8dabfbeSAdrian Chadd 	ath_sysctl_stats_attach(sc);
99237931a35SAdrian Chadd 	ath_sysctl_hal_attach(sc);
99373454c73SSam Leffler 
994c42a7b7eSSam Leffler 	if (bootverbose)
995c42a7b7eSSam Leffler 		ieee80211_announce(ic);
996c42a7b7eSSam Leffler 	ath_announce(sc);
9975591b213SSam Leffler 	return 0;
998b28b4653SSam Leffler bad2:
999c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
1000b28b4653SSam Leffler 	ath_desc_free(sc);
10013fdfc330SAdrian Chadd 	ath_txdma_teardown(sc);
10023d184db2SAdrian Chadd 	ath_rxdma_teardown(sc);
10035591b213SSam Leffler bad:
10045591b213SSam Leffler 	if (ah)
10055591b213SSam Leffler 		ath_hal_detach(ah);
10068bf40208SAdrian Chadd 
10078bf40208SAdrian Chadd 	/*
10088bf40208SAdrian Chadd 	 * To work around scoping issues with CURVNET_SET/CURVNET_RESTORE..
10098bf40208SAdrian Chadd 	 */
10108bf40208SAdrian Chadd 	if (ifp != NULL && ifp->if_vnet) {
1011a93c5097SAdrian Chadd 		CURVNET_SET(ifp->if_vnet);
1012fc74a9f9SBrooks Davis 		if_free(ifp);
1013a93c5097SAdrian Chadd 		CURVNET_RESTORE();
10148bf40208SAdrian Chadd 	} else if (ifp != NULL)
10158bf40208SAdrian Chadd 		if_free(ifp);
10165591b213SSam Leffler 	sc->sc_invalid = 1;
10175591b213SSam Leffler 	return error;
10185591b213SSam Leffler }
10195591b213SSam Leffler 
10205591b213SSam Leffler int
10215591b213SSam Leffler ath_detach(struct ath_softc *sc)
10225591b213SSam Leffler {
1023fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
10245591b213SSam Leffler 
1025c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1026c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
10275591b213SSam Leffler 
1028c42a7b7eSSam Leffler 	/*
1029c42a7b7eSSam Leffler 	 * NB: the order of these is important:
103071b85077SSam Leffler 	 * o stop the chip so no more interrupts will fire
1031c42a7b7eSSam Leffler 	 * o call the 802.11 layer before detaching the hal to
1032c42a7b7eSSam Leffler 	 *   insure callbacks into the driver to delete global
1033c42a7b7eSSam Leffler 	 *   key cache entries can be handled
103471b85077SSam Leffler 	 * o free the taskqueue which drains any pending tasks
1035c42a7b7eSSam Leffler 	 * o reclaim the tx queue data structures after calling
1036c42a7b7eSSam Leffler 	 *   the 802.11 layer as we'll get called back to reclaim
1037c42a7b7eSSam Leffler 	 *   node state and potentially want to use them
1038c42a7b7eSSam Leffler 	 * o to cleanup the tx queues the hal is called, so detach
1039c42a7b7eSSam Leffler 	 *   it last
1040c42a7b7eSSam Leffler 	 * Other than that, it's straightforward...
1041c42a7b7eSSam Leffler 	 */
104271b85077SSam Leffler 	ath_stop(ifp);
1043b032f27cSSam Leffler 	ieee80211_ifdetach(ifp->if_l2com);
104471b85077SSam Leffler 	taskqueue_free(sc->sc_tq);
104586e07743SSam Leffler #ifdef ATH_TX99_DIAG
104686e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
104786e07743SSam Leffler 		sc->sc_tx99->detach(sc->sc_tx99);
104886e07743SSam Leffler #endif
1049c42a7b7eSSam Leffler 	ath_rate_detach(sc->sc_rc);
105089d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
1051bdbb6e5bSAdrian Chadd 	if_ath_alq_tidyup(&sc->sc_alq);
1052bdbb6e5bSAdrian Chadd #endif
1053216ca234SAdrian Chadd 	ath_lna_div_detach(sc);
1054b70f530bSAdrian Chadd 	ath_btcoex_detach(sc);
10559af351f9SAdrian Chadd 	ath_spectral_detach(sc);
105648237774SAdrian Chadd 	ath_dfs_detach(sc);
10575591b213SSam Leffler 	ath_desc_free(sc);
10584bf404eaSAdrian Chadd 	ath_txdma_teardown(sc);
10593d184db2SAdrian Chadd 	ath_rxdma_teardown(sc);
1060c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
106171b85077SSam Leffler 	ath_hal_detach(sc->sc_ah);	/* NB: sets chip in full sleep */
1062a93c5097SAdrian Chadd 
1063a93c5097SAdrian Chadd 	CURVNET_SET(ifp->if_vnet);
1064c4c6f08fSRuslan Ermilov 	if_free(ifp);
1065a93c5097SAdrian Chadd 	CURVNET_RESTORE();
1066f0b2a0beSSam Leffler 
10675591b213SSam Leffler 	return 0;
10685591b213SSam Leffler }
10695591b213SSam Leffler 
1070b032f27cSSam Leffler /*
1071b032f27cSSam Leffler  * MAC address handling for multiple BSS on the same radio.
1072b032f27cSSam Leffler  * The first vap uses the MAC address from the EEPROM.  For
1073b032f27cSSam Leffler  * subsequent vap's we set the U/L bit (bit 1) in the MAC
1074b032f27cSSam Leffler  * address and use the next six bits as an index.
1075b032f27cSSam Leffler  */
1076b032f27cSSam Leffler static void
1077b032f27cSSam Leffler assign_address(struct ath_softc *sc, uint8_t mac[IEEE80211_ADDR_LEN], int clone)
1078b032f27cSSam Leffler {
1079b032f27cSSam Leffler 	int i;
1080b032f27cSSam Leffler 
1081b032f27cSSam Leffler 	if (clone && sc->sc_hasbmask) {
1082b032f27cSSam Leffler 		/* NB: we only do this if h/w supports multiple bssid */
1083b032f27cSSam Leffler 		for (i = 0; i < 8; i++)
1084b032f27cSSam Leffler 			if ((sc->sc_bssidmask & (1<<i)) == 0)
1085b032f27cSSam Leffler 				break;
1086b032f27cSSam Leffler 		if (i != 0)
1087b032f27cSSam Leffler 			mac[0] |= (i << 2)|0x2;
1088b032f27cSSam Leffler 	} else
1089b032f27cSSam Leffler 		i = 0;
1090b032f27cSSam Leffler 	sc->sc_bssidmask |= 1<<i;
1091b032f27cSSam Leffler 	sc->sc_hwbssidmask[0] &= ~mac[0];
1092b032f27cSSam Leffler 	if (i == 0)
1093b032f27cSSam Leffler 		sc->sc_nbssid0++;
1094b032f27cSSam Leffler }
1095b032f27cSSam Leffler 
1096b032f27cSSam Leffler static void
1097b032f27cSSam Leffler reclaim_address(struct ath_softc *sc, const uint8_t mac[IEEE80211_ADDR_LEN])
1098b032f27cSSam Leffler {
1099b032f27cSSam Leffler 	int i = mac[0] >> 2;
1100b032f27cSSam Leffler 	uint8_t mask;
1101b032f27cSSam Leffler 
1102b032f27cSSam Leffler 	if (i != 0 || --sc->sc_nbssid0 == 0) {
1103b032f27cSSam Leffler 		sc->sc_bssidmask &= ~(1<<i);
1104b032f27cSSam Leffler 		/* recalculate bssid mask from remaining addresses */
1105b032f27cSSam Leffler 		mask = 0xff;
1106b032f27cSSam Leffler 		for (i = 1; i < 8; i++)
1107b032f27cSSam Leffler 			if (sc->sc_bssidmask & (1<<i))
1108b032f27cSSam Leffler 				mask &= ~((i<<2)|0x2);
1109b032f27cSSam Leffler 		sc->sc_hwbssidmask[0] |= mask;
1110b032f27cSSam Leffler 	}
1111b032f27cSSam Leffler }
1112b032f27cSSam Leffler 
1113b032f27cSSam Leffler /*
1114b032f27cSSam Leffler  * Assign a beacon xmit slot.  We try to space out
1115b032f27cSSam Leffler  * assignments so when beacons are staggered the
1116b032f27cSSam Leffler  * traffic coming out of the cab q has maximal time
1117b032f27cSSam Leffler  * to go out before the next beacon is scheduled.
1118b032f27cSSam Leffler  */
1119b032f27cSSam Leffler static int
1120b032f27cSSam Leffler assign_bslot(struct ath_softc *sc)
1121b032f27cSSam Leffler {
1122b032f27cSSam Leffler 	u_int slot, free;
1123b032f27cSSam Leffler 
1124b032f27cSSam Leffler 	free = 0;
1125b032f27cSSam Leffler 	for (slot = 0; slot < ATH_BCBUF; slot++)
1126b032f27cSSam Leffler 		if (sc->sc_bslot[slot] == NULL) {
1127b032f27cSSam Leffler 			if (sc->sc_bslot[(slot+1)%ATH_BCBUF] == NULL &&
1128b032f27cSSam Leffler 			    sc->sc_bslot[(slot-1)%ATH_BCBUF] == NULL)
1129b032f27cSSam Leffler 				return slot;
1130b032f27cSSam Leffler 			free = slot;
1131b032f27cSSam Leffler 			/* NB: keep looking for a double slot */
1132b032f27cSSam Leffler 		}
1133b032f27cSSam Leffler 	return free;
1134b032f27cSSam Leffler }
1135b032f27cSSam Leffler 
1136b032f27cSSam Leffler static struct ieee80211vap *
1137fcd9500fSBernhard Schmidt ath_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
1138fcd9500fSBernhard Schmidt     enum ieee80211_opmode opmode, int flags,
1139b032f27cSSam Leffler     const uint8_t bssid[IEEE80211_ADDR_LEN],
1140b032f27cSSam Leffler     const uint8_t mac0[IEEE80211_ADDR_LEN])
1141b032f27cSSam Leffler {
1142b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
1143b032f27cSSam Leffler 	struct ath_vap *avp;
1144b032f27cSSam Leffler 	struct ieee80211vap *vap;
1145b032f27cSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN];
1146fcd9500fSBernhard Schmidt 	int needbeacon, error;
1147fcd9500fSBernhard Schmidt 	enum ieee80211_opmode ic_opmode;
1148b032f27cSSam Leffler 
1149b032f27cSSam Leffler 	avp = (struct ath_vap *) malloc(sizeof(struct ath_vap),
1150b032f27cSSam Leffler 	    M_80211_VAP, M_WAITOK | M_ZERO);
1151b032f27cSSam Leffler 	needbeacon = 0;
1152b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(mac, mac0);
1153b032f27cSSam Leffler 
1154b032f27cSSam Leffler 	ATH_LOCK(sc);
1155a8962181SSam Leffler 	ic_opmode = opmode;		/* default to opmode of new vap */
1156b032f27cSSam Leffler 	switch (opmode) {
1157b032f27cSSam Leffler 	case IEEE80211_M_STA:
1158a8962181SSam Leffler 		if (sc->sc_nstavaps != 0) {	/* XXX only 1 for now */
1159b032f27cSSam Leffler 			device_printf(sc->sc_dev, "only 1 sta vap supported\n");
1160b032f27cSSam Leffler 			goto bad;
1161b032f27cSSam Leffler 		}
1162b032f27cSSam Leffler 		if (sc->sc_nvaps) {
1163b032f27cSSam Leffler 			/*
1164a8962181SSam Leffler 			 * With multiple vaps we must fall back
1165a8962181SSam Leffler 			 * to s/w beacon miss handling.
1166b032f27cSSam Leffler 			 */
1167b032f27cSSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
1168b032f27cSSam Leffler 		}
1169a8962181SSam Leffler 		if (flags & IEEE80211_CLONE_NOBEACONS) {
1170a8962181SSam Leffler 			/*
1171a8962181SSam Leffler 			 * Station mode w/o beacons are implemented w/ AP mode.
1172a8962181SSam Leffler 			 */
1173b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
1174a8962181SSam Leffler 		}
1175b032f27cSSam Leffler 		break;
1176b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1177b032f27cSSam Leffler 		if (sc->sc_nvaps != 0) {	/* XXX only 1 for now */
1178b032f27cSSam Leffler 			device_printf(sc->sc_dev,
1179b032f27cSSam Leffler 			    "only 1 ibss vap supported\n");
1180b032f27cSSam Leffler 			goto bad;
1181b032f27cSSam Leffler 		}
1182b032f27cSSam Leffler 		needbeacon = 1;
1183b032f27cSSam Leffler 		break;
1184b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1185584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
118610ad9a77SSam Leffler 		if (flags & IEEE80211_CLONE_TDMA) {
1187a8962181SSam Leffler 			if (sc->sc_nvaps != 0) {
1188a8962181SSam Leffler 				device_printf(sc->sc_dev,
1189a8962181SSam Leffler 				    "only 1 tdma vap supported\n");
1190a8962181SSam Leffler 				goto bad;
1191a8962181SSam Leffler 			}
119210ad9a77SSam Leffler 			needbeacon = 1;
119310ad9a77SSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
119410ad9a77SSam Leffler 		}
1195b032f27cSSam Leffler 		/* fall thru... */
119610ad9a77SSam Leffler #endif
1197b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1198b032f27cSSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode != opmode) {
1199a8962181SSam Leffler 			/*
1200a8962181SSam Leffler 			 * Adopt existing mode.  Adding a monitor or ahdemo
1201a8962181SSam Leffler 			 * vap to an existing configuration is of dubious
1202a8962181SSam Leffler 			 * value but should be ok.
1203a8962181SSam Leffler 			 */
1204b032f27cSSam Leffler 			/* XXX not right for monitor mode */
1205b032f27cSSam Leffler 			ic_opmode = ic->ic_opmode;
1206a8962181SSam Leffler 		}
1207b032f27cSSam Leffler 		break;
1208b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
120959aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1210b032f27cSSam Leffler 		needbeacon = 1;
1211a8962181SSam Leffler 		break;
1212b032f27cSSam Leffler 	case IEEE80211_M_WDS:
1213a8962181SSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode == IEEE80211_M_STA) {
1214b032f27cSSam Leffler 			device_printf(sc->sc_dev,
1215b032f27cSSam Leffler 			    "wds not supported in sta mode\n");
1216b032f27cSSam Leffler 			goto bad;
1217b032f27cSSam Leffler 		}
1218b032f27cSSam Leffler 		/*
1219b032f27cSSam Leffler 		 * Silently remove any request for a unique
1220b032f27cSSam Leffler 		 * bssid; WDS vap's always share the local
1221b032f27cSSam Leffler 		 * mac address.
1222b032f27cSSam Leffler 		 */
1223b032f27cSSam Leffler 		flags &= ~IEEE80211_CLONE_BSSID;
1224a8962181SSam Leffler 		if (sc->sc_nvaps == 0)
1225b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
1226a8962181SSam Leffler 		else
1227a8962181SSam Leffler 			ic_opmode = ic->ic_opmode;
12287d261891SRui Paulo 		break;
1229b032f27cSSam Leffler 	default:
1230b032f27cSSam Leffler 		device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
1231b032f27cSSam Leffler 		goto bad;
1232b032f27cSSam Leffler 	}
1233b032f27cSSam Leffler 	/*
1234b032f27cSSam Leffler 	 * Check that a beacon buffer is available; the code below assumes it.
1235b032f27cSSam Leffler 	 */
12366b349e5aSAdrian Chadd 	if (needbeacon & TAILQ_EMPTY(&sc->sc_bbuf)) {
1237b032f27cSSam Leffler 		device_printf(sc->sc_dev, "no beacon buffer available\n");
1238b032f27cSSam Leffler 		goto bad;
1239b032f27cSSam Leffler 	}
1240b032f27cSSam Leffler 
1241b032f27cSSam Leffler 	/* STA, AHDEMO? */
124259aa14a9SRui Paulo 	if (opmode == IEEE80211_M_HOSTAP || opmode == IEEE80211_M_MBSS) {
1243b032f27cSSam Leffler 		assign_address(sc, mac, flags & IEEE80211_CLONE_BSSID);
1244b032f27cSSam Leffler 		ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1245b032f27cSSam Leffler 	}
1246b032f27cSSam Leffler 
1247b032f27cSSam Leffler 	vap = &avp->av_vap;
1248b032f27cSSam Leffler 	/* XXX can't hold mutex across if_alloc */
1249b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1250b032f27cSSam Leffler 	error = ieee80211_vap_setup(ic, vap, name, unit, opmode, flags,
1251b032f27cSSam Leffler 	    bssid, mac);
1252b032f27cSSam Leffler 	ATH_LOCK(sc);
1253b032f27cSSam Leffler 	if (error != 0) {
1254b032f27cSSam Leffler 		device_printf(sc->sc_dev, "%s: error %d creating vap\n",
1255b032f27cSSam Leffler 		    __func__, error);
1256b032f27cSSam Leffler 		goto bad2;
1257b032f27cSSam Leffler 	}
1258b032f27cSSam Leffler 
1259b032f27cSSam Leffler 	/* h/w crypto support */
1260b032f27cSSam Leffler 	vap->iv_key_alloc = ath_key_alloc;
1261b032f27cSSam Leffler 	vap->iv_key_delete = ath_key_delete;
1262b032f27cSSam Leffler 	vap->iv_key_set = ath_key_set;
1263b032f27cSSam Leffler 	vap->iv_key_update_begin = ath_key_update_begin;
1264b032f27cSSam Leffler 	vap->iv_key_update_end = ath_key_update_end;
1265b032f27cSSam Leffler 
1266b032f27cSSam Leffler 	/* override various methods */
1267b032f27cSSam Leffler 	avp->av_recv_mgmt = vap->iv_recv_mgmt;
1268b032f27cSSam Leffler 	vap->iv_recv_mgmt = ath_recv_mgmt;
1269b032f27cSSam Leffler 	vap->iv_reset = ath_reset_vap;
1270b032f27cSSam Leffler 	vap->iv_update_beacon = ath_beacon_update;
1271b032f27cSSam Leffler 	avp->av_newstate = vap->iv_newstate;
1272b032f27cSSam Leffler 	vap->iv_newstate = ath_newstate;
1273b032f27cSSam Leffler 	avp->av_bmiss = vap->iv_bmiss;
1274b032f27cSSam Leffler 	vap->iv_bmiss = ath_bmiss_vap;
1275b032f27cSSam Leffler 
12760eb81626SAdrian Chadd 	avp->av_node_ps = vap->iv_node_ps;
12770eb81626SAdrian Chadd 	vap->iv_node_ps = ath_node_powersave;
12780eb81626SAdrian Chadd 
1279548a605dSAdrian Chadd 	avp->av_set_tim = vap->iv_set_tim;
1280548a605dSAdrian Chadd 	vap->iv_set_tim = ath_node_set_tim;
1281548a605dSAdrian Chadd 
128222a3aee6SAdrian Chadd 	avp->av_recv_pspoll = vap->iv_recv_pspoll;
128322a3aee6SAdrian Chadd 	vap->iv_recv_pspoll = ath_node_recv_pspoll;
128422a3aee6SAdrian Chadd 
12859be25f4aSAdrian Chadd 	/* Set default parameters */
12869be25f4aSAdrian Chadd 
12879be25f4aSAdrian Chadd 	/*
12889be25f4aSAdrian Chadd 	 * Anything earlier than some AR9300 series MACs don't
12899be25f4aSAdrian Chadd 	 * support a smaller MPDU density.
12909be25f4aSAdrian Chadd 	 */
12919be25f4aSAdrian Chadd 	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_8;
12929be25f4aSAdrian Chadd 	/*
12939be25f4aSAdrian Chadd 	 * All NICs can handle the maximum size, however
12949be25f4aSAdrian Chadd 	 * AR5416 based MACs can only TX aggregates w/ RTS
12959be25f4aSAdrian Chadd 	 * protection when the total aggregate size is <= 8k.
12969be25f4aSAdrian Chadd 	 * However, for now that's enforced by the TX path.
12979be25f4aSAdrian Chadd 	 */
12989be25f4aSAdrian Chadd 	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K;
12999be25f4aSAdrian Chadd 
1300b032f27cSSam Leffler 	avp->av_bslot = -1;
1301b032f27cSSam Leffler 	if (needbeacon) {
1302b032f27cSSam Leffler 		/*
1303b032f27cSSam Leffler 		 * Allocate beacon state and setup the q for buffered
1304b032f27cSSam Leffler 		 * multicast frames.  We know a beacon buffer is
1305b032f27cSSam Leffler 		 * available because we checked above.
1306b032f27cSSam Leffler 		 */
13076b349e5aSAdrian Chadd 		avp->av_bcbuf = TAILQ_FIRST(&sc->sc_bbuf);
13086b349e5aSAdrian Chadd 		TAILQ_REMOVE(&sc->sc_bbuf, avp->av_bcbuf, bf_list);
1309b032f27cSSam Leffler 		if (opmode != IEEE80211_M_IBSS || !sc->sc_hasveol) {
1310b032f27cSSam Leffler 			/*
1311b032f27cSSam Leffler 			 * Assign the vap to a beacon xmit slot.  As above
1312b032f27cSSam Leffler 			 * this cannot fail to find a free one.
1313b032f27cSSam Leffler 			 */
1314b032f27cSSam Leffler 			avp->av_bslot = assign_bslot(sc);
1315b032f27cSSam Leffler 			KASSERT(sc->sc_bslot[avp->av_bslot] == NULL,
1316b032f27cSSam Leffler 			    ("beacon slot %u not empty", avp->av_bslot));
1317b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = vap;
1318b032f27cSSam Leffler 			sc->sc_nbcnvaps++;
1319b032f27cSSam Leffler 		}
1320b032f27cSSam Leffler 		if (sc->sc_hastsfadd && sc->sc_nbcnvaps > 0) {
1321b032f27cSSam Leffler 			/*
1322b032f27cSSam Leffler 			 * Multple vaps are to transmit beacons and we
1323b032f27cSSam Leffler 			 * have h/w support for TSF adjusting; enable
1324b032f27cSSam Leffler 			 * use of staggered beacons.
1325b032f27cSSam Leffler 			 */
1326b032f27cSSam Leffler 			sc->sc_stagbeacons = 1;
1327b032f27cSSam Leffler 		}
1328b032f27cSSam Leffler 		ath_txq_init(sc, &avp->av_mcastq, ATH_TXQ_SWQ);
1329b032f27cSSam Leffler 	}
1330b032f27cSSam Leffler 
1331b032f27cSSam Leffler 	ic->ic_opmode = ic_opmode;
1332b032f27cSSam Leffler 	if (opmode != IEEE80211_M_WDS) {
1333b032f27cSSam Leffler 		sc->sc_nvaps++;
1334b032f27cSSam Leffler 		if (opmode == IEEE80211_M_STA)
1335b032f27cSSam Leffler 			sc->sc_nstavaps++;
1336fe0dd789SSam Leffler 		if (opmode == IEEE80211_M_MBSS)
1337fe0dd789SSam Leffler 			sc->sc_nmeshvaps++;
1338b032f27cSSam Leffler 	}
1339b032f27cSSam Leffler 	switch (ic_opmode) {
1340b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1341b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_IBSS;
1342b032f27cSSam Leffler 		break;
1343b032f27cSSam Leffler 	case IEEE80211_M_STA:
1344b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_STA;
1345b032f27cSSam Leffler 		break;
1346b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1347584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
134810ad9a77SSam Leffler 		if (vap->iv_caps & IEEE80211_C_TDMA) {
134910ad9a77SSam Leffler 			sc->sc_tdma = 1;
135010ad9a77SSam Leffler 			/* NB: disable tsf adjust */
135110ad9a77SSam Leffler 			sc->sc_stagbeacons = 0;
135210ad9a77SSam Leffler 		}
135310ad9a77SSam Leffler 		/*
135410ad9a77SSam Leffler 		 * NB: adhoc demo mode is a pseudo mode; to the hal it's
135510ad9a77SSam Leffler 		 * just ap mode.
135610ad9a77SSam Leffler 		 */
135710ad9a77SSam Leffler 		/* fall thru... */
135810ad9a77SSam Leffler #endif
1359b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
136059aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1361b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_HOSTAP;
1362b032f27cSSam Leffler 		break;
1363b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1364b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_MONITOR;
1365b032f27cSSam Leffler 		break;
1366b032f27cSSam Leffler 	default:
1367b032f27cSSam Leffler 		/* XXX should not happen */
1368b032f27cSSam Leffler 		break;
1369b032f27cSSam Leffler 	}
1370b032f27cSSam Leffler 	if (sc->sc_hastsfadd) {
1371b032f27cSSam Leffler 		/*
1372b032f27cSSam Leffler 		 * Configure whether or not TSF adjust should be done.
1373b032f27cSSam Leffler 		 */
1374b032f27cSSam Leffler 		ath_hal_settsfadjust(sc->sc_ah, sc->sc_stagbeacons);
1375b032f27cSSam Leffler 	}
137610ad9a77SSam Leffler 	if (flags & IEEE80211_CLONE_NOBEACONS) {
137710ad9a77SSam Leffler 		/*
137810ad9a77SSam Leffler 		 * Enable s/w beacon miss handling.
137910ad9a77SSam Leffler 		 */
138010ad9a77SSam Leffler 		sc->sc_swbmiss = 1;
138110ad9a77SSam Leffler 	}
1382b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1383b032f27cSSam Leffler 
1384b032f27cSSam Leffler 	/* complete setup */
1385b032f27cSSam Leffler 	ieee80211_vap_attach(vap, ath_media_change, ieee80211_media_status);
1386b032f27cSSam Leffler 	return vap;
1387b032f27cSSam Leffler bad2:
1388b032f27cSSam Leffler 	reclaim_address(sc, mac);
1389b032f27cSSam Leffler 	ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1390b032f27cSSam Leffler bad:
1391b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1392b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1393b032f27cSSam Leffler 	return NULL;
1394b032f27cSSam Leffler }
1395b032f27cSSam Leffler 
1396b032f27cSSam Leffler static void
1397b032f27cSSam Leffler ath_vap_delete(struct ieee80211vap *vap)
1398b032f27cSSam Leffler {
1399b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1400b032f27cSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1401b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1402b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1403b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
1404b032f27cSSam Leffler 
1405f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
1406b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1407b032f27cSSam Leffler 		/*
1408b032f27cSSam Leffler 		 * Quiesce the hardware while we remove the vap.  In
1409b032f27cSSam Leffler 		 * particular we need to reclaim all references to
1410b032f27cSSam Leffler 		 * the vap state by any frames pending on the tx queues.
1411b032f27cSSam Leffler 		 */
1412b032f27cSSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
1413517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);		/* stop hw xmit side */
1414517526efSAdrian Chadd 		/* XXX Do all frames from all vaps/nodes need draining here? */
14159a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* stop recv side */
1416b032f27cSSam Leffler 	}
1417b032f27cSSam Leffler 
1418b032f27cSSam Leffler 	ieee80211_vap_detach(vap);
141916d4de92SAdrian Chadd 
142016d4de92SAdrian Chadd 	/*
142116d4de92SAdrian Chadd 	 * XXX Danger Will Robinson! Danger!
142216d4de92SAdrian Chadd 	 *
142316d4de92SAdrian Chadd 	 * Because ieee80211_vap_detach() can queue a frame (the station
142416d4de92SAdrian Chadd 	 * diassociate message?) after we've drained the TXQ and
142516d4de92SAdrian Chadd 	 * flushed the software TXQ, we will end up with a frame queued
142616d4de92SAdrian Chadd 	 * to a node whose vap is about to be freed.
142716d4de92SAdrian Chadd 	 *
142816d4de92SAdrian Chadd 	 * To work around this, flush the hardware/software again.
142916d4de92SAdrian Chadd 	 * This may be racy - the ath task may be running and the packet
143016d4de92SAdrian Chadd 	 * may be being scheduled between sw->hw txq. Tsk.
143116d4de92SAdrian Chadd 	 *
143216d4de92SAdrian Chadd 	 * TODO: figure out why a new node gets allocated somewhere around
143316d4de92SAdrian Chadd 	 * here (after the ath_tx_swq() call; and after an ath_stop_locked()
143416d4de92SAdrian Chadd 	 * call!)
143516d4de92SAdrian Chadd 	 */
143616d4de92SAdrian Chadd 
143716d4de92SAdrian Chadd 	ath_draintxq(sc, ATH_RESET_DEFAULT);
143816d4de92SAdrian Chadd 
1439b032f27cSSam Leffler 	ATH_LOCK(sc);
1440b032f27cSSam Leffler 	/*
1441b032f27cSSam Leffler 	 * Reclaim beacon state.  Note this must be done before
1442b032f27cSSam Leffler 	 * the vap instance is reclaimed as we may have a reference
1443b032f27cSSam Leffler 	 * to it in the buffer for the beacon frame.
1444b032f27cSSam Leffler 	 */
1445b032f27cSSam Leffler 	if (avp->av_bcbuf != NULL) {
1446b032f27cSSam Leffler 		if (avp->av_bslot != -1) {
1447b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = NULL;
1448b032f27cSSam Leffler 			sc->sc_nbcnvaps--;
1449b032f27cSSam Leffler 		}
1450b032f27cSSam Leffler 		ath_beacon_return(sc, avp->av_bcbuf);
1451b032f27cSSam Leffler 		avp->av_bcbuf = NULL;
1452b032f27cSSam Leffler 		if (sc->sc_nbcnvaps == 0) {
1453b032f27cSSam Leffler 			sc->sc_stagbeacons = 0;
1454b032f27cSSam Leffler 			if (sc->sc_hastsfadd)
1455b032f27cSSam Leffler 				ath_hal_settsfadjust(sc->sc_ah, 0);
1456b032f27cSSam Leffler 		}
1457b032f27cSSam Leffler 		/*
1458b032f27cSSam Leffler 		 * Reclaim any pending mcast frames for the vap.
1459b032f27cSSam Leffler 		 */
1460b032f27cSSam Leffler 		ath_tx_draintxq(sc, &avp->av_mcastq);
1461b032f27cSSam Leffler 	}
1462b032f27cSSam Leffler 	/*
1463b032f27cSSam Leffler 	 * Update bookkeeping.
1464b032f27cSSam Leffler 	 */
1465b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
1466b032f27cSSam Leffler 		sc->sc_nstavaps--;
1467b032f27cSSam Leffler 		if (sc->sc_nstavaps == 0 && sc->sc_swbmiss)
1468b032f27cSSam Leffler 			sc->sc_swbmiss = 0;
146959aa14a9SRui Paulo 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
147059aa14a9SRui Paulo 	    vap->iv_opmode == IEEE80211_M_MBSS) {
1471b032f27cSSam Leffler 		reclaim_address(sc, vap->iv_myaddr);
1472b032f27cSSam Leffler 		ath_hal_setbssidmask(ah, sc->sc_hwbssidmask);
1473fe0dd789SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_MBSS)
1474fe0dd789SSam Leffler 			sc->sc_nmeshvaps--;
1475b032f27cSSam Leffler 	}
1476b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_WDS)
1477b032f27cSSam Leffler 		sc->sc_nvaps--;
1478584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
147910ad9a77SSam Leffler 	/* TDMA operation ceases when the last vap is destroyed */
148010ad9a77SSam Leffler 	if (sc->sc_tdma && sc->sc_nvaps == 0) {
148110ad9a77SSam Leffler 		sc->sc_tdma = 0;
148210ad9a77SSam Leffler 		sc->sc_swbmiss = 0;
148310ad9a77SSam Leffler 	}
148410ad9a77SSam Leffler #endif
1485b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1486b032f27cSSam Leffler 
1487b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1488b032f27cSSam Leffler 		/*
1489b032f27cSSam Leffler 		 * Restart rx+tx machines if still running (RUNNING will
1490b032f27cSSam Leffler 		 * be reset if we just destroyed the last vap).
1491b032f27cSSam Leffler 		 */
1492b032f27cSSam Leffler 		if (ath_startrecv(sc) != 0)
1493b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to restart recv logic\n",
1494b032f27cSSam Leffler 			    __func__);
1495c89b957aSSam Leffler 		if (sc->sc_beacons) {		/* restart beacons */
1496c89b957aSSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
1497c89b957aSSam Leffler 			if (sc->sc_tdma)
1498c89b957aSSam Leffler 				ath_tdma_config(sc, NULL);
1499c89b957aSSam Leffler 			else
1500c89b957aSSam Leffler #endif
1501b032f27cSSam Leffler 				ath_beacon_config(sc, NULL);
1502c89b957aSSam Leffler 		}
1503b032f27cSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
1504b032f27cSSam Leffler 	}
150516d4de92SAdrian Chadd 	ATH_UNLOCK(sc);
1506b032f27cSSam Leffler }
1507b032f27cSSam Leffler 
15085591b213SSam Leffler void
15095591b213SSam Leffler ath_suspend(struct ath_softc *sc)
15105591b213SSam Leffler {
1511fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1512d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
15135591b213SSam Leffler 
1514c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1515c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
15165591b213SSam Leffler 
1517d3ac945bSSam Leffler 	sc->sc_resume_up = (ifp->if_flags & IFF_UP) != 0;
1518d1328898SAdrian Chadd 
1519d3ac945bSSam Leffler 	ieee80211_suspend_all(ic);
1520d3ac945bSSam Leffler 	/*
1521d3ac945bSSam Leffler 	 * NB: don't worry about putting the chip in low power
1522d3ac945bSSam Leffler 	 * mode; pci will power off our socket on suspend and
1523f29b8b7fSWarner Losh 	 * CardBus detaches the device.
1524d3ac945bSSam Leffler 	 */
1525d73df6d5SAdrian Chadd 
1526ae2a0aa4SAdrian Chadd 	/*
1527ae2a0aa4SAdrian Chadd 	 * XXX ensure none of the taskqueues are running
1528ae2a0aa4SAdrian Chadd 	 * XXX ensure sc_invalid is 1
1529ae2a0aa4SAdrian Chadd 	 * XXX ensure the calibration callout is disabled
1530ae2a0aa4SAdrian Chadd 	 */
1531ae2a0aa4SAdrian Chadd 
1532ae2a0aa4SAdrian Chadd 	/* Disable the PCIe PHY, complete with workarounds */
1533ae2a0aa4SAdrian Chadd 	ath_hal_enablepcie(sc->sc_ah, 1, 1);
1534d3ac945bSSam Leffler }
1535d3ac945bSSam Leffler 
1536d3ac945bSSam Leffler /*
1537d3ac945bSSam Leffler  * Reset the key cache since some parts do not reset the
1538d3ac945bSSam Leffler  * contents on resume.  First we clear all entries, then
1539d3ac945bSSam Leffler  * re-load keys that the 802.11 layer assumes are setup
1540d3ac945bSSam Leffler  * in h/w.
1541d3ac945bSSam Leffler  */
1542d3ac945bSSam Leffler static void
1543d3ac945bSSam Leffler ath_reset_keycache(struct ath_softc *sc)
1544d3ac945bSSam Leffler {
1545d3ac945bSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1546d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1547d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1548d3ac945bSSam Leffler 	int i;
1549d3ac945bSSam Leffler 
1550d3ac945bSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
1551d3ac945bSSam Leffler 		ath_hal_keyreset(ah, i);
1552d3ac945bSSam Leffler 	ieee80211_crypto_reload_keys(ic);
15535591b213SSam Leffler }
15545591b213SSam Leffler 
15556322256bSAdrian Chadd /*
15566322256bSAdrian Chadd  * Fetch the current chainmask configuration based on the current
15576322256bSAdrian Chadd  * operating channel and options.
15586322256bSAdrian Chadd  */
15596322256bSAdrian Chadd static void
15606322256bSAdrian Chadd ath_update_chainmasks(struct ath_softc *sc, struct ieee80211_channel *chan)
15616322256bSAdrian Chadd {
15626322256bSAdrian Chadd 
15636322256bSAdrian Chadd 	/*
15646322256bSAdrian Chadd 	 * Set TX chainmask to the currently configured chainmask;
15656322256bSAdrian Chadd 	 * the TX chainmask depends upon the current operating mode.
15666322256bSAdrian Chadd 	 */
15676322256bSAdrian Chadd 	sc->sc_cur_rxchainmask = sc->sc_rxchainmask;
15686322256bSAdrian Chadd 	if (IEEE80211_IS_CHAN_HT(chan)) {
15696322256bSAdrian Chadd 		sc->sc_cur_txchainmask = sc->sc_txchainmask;
15706322256bSAdrian Chadd 	} else {
15716322256bSAdrian Chadd 		sc->sc_cur_txchainmask = 1;
15726322256bSAdrian Chadd 	}
15737904f516SAdrian Chadd 
15747904f516SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
15757904f516SAdrian Chadd 	    "%s: TX chainmask is now 0x%x, RX is now 0x%x\n",
15767904f516SAdrian Chadd 	    __func__,
15777904f516SAdrian Chadd 	    sc->sc_cur_txchainmask,
15787904f516SAdrian Chadd 	    sc->sc_cur_rxchainmask);
15796322256bSAdrian Chadd }
15806322256bSAdrian Chadd 
15815591b213SSam Leffler void
15825591b213SSam Leffler ath_resume(struct ath_softc *sc)
15835591b213SSam Leffler {
1584fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1585d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1586d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1587d3ac945bSSam Leffler 	HAL_STATUS status;
15885591b213SSam Leffler 
1589c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1590c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
15915591b213SSam Leffler 
1592d73df6d5SAdrian Chadd 	/* Re-enable PCIe, re-enable the PCIe bus */
1593ae2a0aa4SAdrian Chadd 	ath_hal_enablepcie(ah, 0, 0);
1594d73df6d5SAdrian Chadd 
1595d3ac945bSSam Leffler 	/*
1596d3ac945bSSam Leffler 	 * Must reset the chip before we reload the
1597d3ac945bSSam Leffler 	 * keycache as we were powered down on suspend.
1598d3ac945bSSam Leffler 	 */
15996322256bSAdrian Chadd 	ath_update_chainmasks(sc,
16006322256bSAdrian Chadd 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan);
16016322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
16026322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
1603054d7b69SSam Leffler 	ath_hal_reset(ah, sc->sc_opmode,
1604054d7b69SSam Leffler 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan,
1605054d7b69SSam Leffler 	    AH_FALSE, &status);
1606d3ac945bSSam Leffler 	ath_reset_keycache(sc);
16077e5eb44dSAdrian Chadd 
16087e5eb44dSAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
16097e5eb44dSAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
16107e5eb44dSAdrian Chadd 
16119af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
16129af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
16139af351f9SAdrian Chadd 
1614dd6a574eSAdrian Chadd 	/*
1615b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
1616b70f530bSAdrian Chadd 	 */
1617b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
1618b70f530bSAdrian Chadd 
1619b70f530bSAdrian Chadd 	/*
1620dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
1621dd6a574eSAdrian Chadd 	 * support it.
1622dd6a574eSAdrian Chadd 	 */
1623dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
1624dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
1625dd6a574eSAdrian Chadd 	else
1626dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
1627dd6a574eSAdrian Chadd 
1628a497cd88SAdrian Chadd 	/* Restore the LED configuration */
1629a497cd88SAdrian Chadd 	ath_led_config(sc);
1630a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
1631a497cd88SAdrian Chadd 
1632d1328898SAdrian Chadd 	if (sc->sc_resume_up)
1633021a0db5SAdrian Chadd 		ieee80211_resume_all(ic);
16342fd9aabbSAdrian Chadd 
16352fd9aabbSAdrian Chadd 	/* XXX beacons ? */
16366b59f5e3SSam Leffler }
16375591b213SSam Leffler 
16385591b213SSam Leffler void
16395591b213SSam Leffler ath_shutdown(struct ath_softc *sc)
16405591b213SSam Leffler {
1641fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
16425591b213SSam Leffler 
1643c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1644c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
16455591b213SSam Leffler 
16465591b213SSam Leffler 	ath_stop(ifp);
1647d3ac945bSSam Leffler 	/* NB: no point powering down chip as we're about to reboot */
16485591b213SSam Leffler }
16495591b213SSam Leffler 
1650c42a7b7eSSam Leffler /*
1651c42a7b7eSSam Leffler  * Interrupt handler.  Most of the actual processing is deferred.
1652c42a7b7eSSam Leffler  */
16535591b213SSam Leffler void
16545591b213SSam Leffler ath_intr(void *arg)
16555591b213SSam Leffler {
16565591b213SSam Leffler 	struct ath_softc *sc = arg;
1657fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
16585591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
16596f5fe81eSAdrian Chadd 	HAL_INT status = 0;
16608f939e79SAdrian Chadd 	uint32_t txqs;
16615591b213SSam Leffler 
1662ef27340cSAdrian Chadd 	/*
1663ef27340cSAdrian Chadd 	 * If we're inside a reset path, just print a warning and
1664ef27340cSAdrian Chadd 	 * clear the ISR. The reset routine will finish it for us.
1665ef27340cSAdrian Chadd 	 */
1666ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
1667ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt) {
1668ef27340cSAdrian Chadd 		HAL_INT status;
1669ef27340cSAdrian Chadd 		ath_hal_getisr(ah, &status);	/* clear ISR */
1670ef27340cSAdrian Chadd 		ath_hal_intrset(ah, 0);		/* disable further intr's */
1671ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_ANY,
1672ef27340cSAdrian Chadd 		    "%s: in reset, ignoring: status=0x%x\n",
1673ef27340cSAdrian Chadd 		    __func__, status);
1674ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
1675ef27340cSAdrian Chadd 		return;
1676ef27340cSAdrian Chadd 	}
1677ef27340cSAdrian Chadd 
16785591b213SSam Leffler 	if (sc->sc_invalid) {
16795591b213SSam Leffler 		/*
1680b58b3803SSam Leffler 		 * The hardware is not ready/present, don't touch anything.
1681b58b3803SSam Leffler 		 * Note this can happen early on if the IRQ is shared.
16825591b213SSam Leffler 		 */
1683c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
1684ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
16855591b213SSam Leffler 		return;
16865591b213SSam Leffler 	}
1687ef27340cSAdrian Chadd 	if (!ath_hal_intrpend(ah)) {		/* shared irq, not for us */
1688ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
1689fdd758d4SSam Leffler 		return;
1690ef27340cSAdrian Chadd 	}
1691ef27340cSAdrian Chadd 
169268e8e04eSSam Leffler 	if ((ifp->if_flags & IFF_UP) == 0 ||
169368e8e04eSSam Leffler 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
169468e8e04eSSam Leffler 		HAL_INT status;
169568e8e04eSSam Leffler 
1696c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1697c42a7b7eSSam Leffler 			__func__, ifp->if_flags);
16985591b213SSam Leffler 		ath_hal_getisr(ah, &status);	/* clear ISR */
16995591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable further intr's */
1700ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
17015591b213SSam Leffler 		return;
17025591b213SSam Leffler 	}
1703ef27340cSAdrian Chadd 
1704c42a7b7eSSam Leffler 	/*
1705c42a7b7eSSam Leffler 	 * Figure out the reason(s) for the interrupt.  Note
1706c42a7b7eSSam Leffler 	 * that the hal returns a pseudo-ISR that may include
1707c42a7b7eSSam Leffler 	 * bits we haven't explicitly enabled so we mask the
1708c42a7b7eSSam Leffler 	 * value to insure we only process bits we requested.
1709c42a7b7eSSam Leffler 	 */
17105591b213SSam Leffler 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
1711c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
171203682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 1, "ath_intr: mask=0x%.8x", status);
1713a26f3327SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
1714a26f3327SAdrian Chadd 	if_ath_alq_post_intr(&sc->sc_alq, status, ah->ah_intrstate,
1715a26f3327SAdrian Chadd 	    ah->ah_syncstate);
1716a26f3327SAdrian Chadd #endif	/* ATH_DEBUG_ALQ */
171731fdf3d6SAdrian Chadd #ifdef	ATH_KTR_INTR_DEBUG
171803682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 5,
1719f52d3452SAdrian Chadd 	    "ath_intr: ISR=0x%.8x, ISR_S0=0x%.8x, ISR_S1=0x%.8x, ISR_S2=0x%.8x, ISR_S5=0x%.8x",
1720f52d3452SAdrian Chadd 	    ah->ah_intrstate[0],
1721f52d3452SAdrian Chadd 	    ah->ah_intrstate[1],
1722f52d3452SAdrian Chadd 	    ah->ah_intrstate[2],
1723f52d3452SAdrian Chadd 	    ah->ah_intrstate[3],
1724f52d3452SAdrian Chadd 	    ah->ah_intrstate[6]);
172531fdf3d6SAdrian Chadd #endif
17269467e3f3SAdrian Chadd 
17279467e3f3SAdrian Chadd 	/* Squirrel away SYNC interrupt debugging */
17289467e3f3SAdrian Chadd 	if (ah->ah_syncstate != 0) {
17299467e3f3SAdrian Chadd 		int i;
17309467e3f3SAdrian Chadd 		for (i = 0; i < 32; i++)
17319467e3f3SAdrian Chadd 			if (ah->ah_syncstate & (i << i))
17329467e3f3SAdrian Chadd 				sc->sc_intr_stats.sync_intr[i]++;
17339467e3f3SAdrian Chadd 	}
17349467e3f3SAdrian Chadd 
1735ecddff40SSam Leffler 	status &= sc->sc_imask;			/* discard unasked for bits */
17366f5fe81eSAdrian Chadd 
17376f5fe81eSAdrian Chadd 	/* Short-circuit un-handled interrupts */
1738ef27340cSAdrian Chadd 	if (status == 0x0) {
1739ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
17406f5fe81eSAdrian Chadd 		return;
1741ef27340cSAdrian Chadd 	}
17426f5fe81eSAdrian Chadd 
1743ef27340cSAdrian Chadd 	/*
1744ef27340cSAdrian Chadd 	 * Take a note that we're inside the interrupt handler, so
1745ef27340cSAdrian Chadd 	 * the reset routines know to wait.
1746ef27340cSAdrian Chadd 	 */
1747ef27340cSAdrian Chadd 	sc->sc_intr_cnt++;
1748ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
1749ef27340cSAdrian Chadd 
1750ef27340cSAdrian Chadd 	/*
1751ef27340cSAdrian Chadd 	 * Handle the interrupt. We won't run concurrent with the reset
1752ef27340cSAdrian Chadd 	 * or channel change routines as they'll wait for sc_intr_cnt
1753ef27340cSAdrian Chadd 	 * to be 0 before continuing.
1754ef27340cSAdrian Chadd 	 */
17555591b213SSam Leffler 	if (status & HAL_INT_FATAL) {
17565591b213SSam Leffler 		sc->sc_stats.ast_hardware++;
17575591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
1758f846cf42SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_fataltask);
17595591b213SSam Leffler 	} else {
1760c42a7b7eSSam Leffler 		if (status & HAL_INT_SWBA) {
1761c42a7b7eSSam Leffler 			/*
1762c42a7b7eSSam Leffler 			 * Software beacon alert--time to send a beacon.
1763c42a7b7eSSam Leffler 			 * Handle beacon transmission directly; deferring
1764c42a7b7eSSam Leffler 			 * this is too slow to meet timing constraints
1765c42a7b7eSSam Leffler 			 * under load.
1766c42a7b7eSSam Leffler 			 */
1767584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
176810ad9a77SSam Leffler 			if (sc->sc_tdma) {
176910ad9a77SSam Leffler 				if (sc->sc_tdmaswba == 0) {
177010ad9a77SSam Leffler 					struct ieee80211com *ic = ifp->if_l2com;
177110ad9a77SSam Leffler 					struct ieee80211vap *vap =
177210ad9a77SSam Leffler 					    TAILQ_FIRST(&ic->ic_vaps);
177310ad9a77SSam Leffler 					ath_tdma_beacon_send(sc, vap);
177410ad9a77SSam Leffler 					sc->sc_tdmaswba =
177510ad9a77SSam Leffler 					    vap->iv_tdma->tdma_bintval;
177610ad9a77SSam Leffler 				} else
177710ad9a77SSam Leffler 					sc->sc_tdmaswba--;
177810ad9a77SSam Leffler 			} else
177910ad9a77SSam Leffler #endif
1780339ccfb3SSam Leffler 			{
1781c42a7b7eSSam Leffler 				ath_beacon_proc(sc, 0);
1782339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1783339ccfb3SSam Leffler 				/*
1784339ccfb3SSam Leffler 				 * Schedule the rx taskq in case there's no
1785339ccfb3SSam Leffler 				 * traffic so any frames held on the staging
1786339ccfb3SSam Leffler 				 * queue are aged and potentially flushed.
1787339ccfb3SSam Leffler 				 */
1788f0db652cSAdrian Chadd 				sc->sc_rx.recv_sched(sc, 1);
1789339ccfb3SSam Leffler #endif
1790339ccfb3SSam Leffler 			}
1791c42a7b7eSSam Leffler 		}
17925591b213SSam Leffler 		if (status & HAL_INT_RXEOL) {
17938f939e79SAdrian Chadd 			int imask;
179403682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXEOL");
1795ef27340cSAdrian Chadd 			ATH_PCU_LOCK(sc);
17965591b213SSam Leffler 			/*
17975591b213SSam Leffler 			 * NB: the hardware should re-read the link when
17985591b213SSam Leffler 			 *     RXE bit is written, but it doesn't work at
17995591b213SSam Leffler 			 *     least on older hardware revs.
18005591b213SSam Leffler 			 */
18015591b213SSam Leffler 			sc->sc_stats.ast_rxeol++;
180273f895fcSAdrian Chadd 			/*
180373f895fcSAdrian Chadd 			 * Disable RXEOL/RXORN - prevent an interrupt
180473f895fcSAdrian Chadd 			 * storm until the PCU logic can be reset.
18051fdadc0fSAdrian Chadd 			 * In case the interface is reset some other
18061fdadc0fSAdrian Chadd 			 * way before "sc_kickpcu" is called, don't
18071fdadc0fSAdrian Chadd 			 * modify sc_imask - that way if it is reset
18081fdadc0fSAdrian Chadd 			 * by a call to ath_reset() somehow, the
18091fdadc0fSAdrian Chadd 			 * interrupt mask will be correctly reprogrammed.
181073f895fcSAdrian Chadd 			 */
18118f939e79SAdrian Chadd 			imask = sc->sc_imask;
18121fdadc0fSAdrian Chadd 			imask &= ~(HAL_INT_RXEOL | HAL_INT_RXORN);
18131fdadc0fSAdrian Chadd 			ath_hal_intrset(ah, imask);
18141fdadc0fSAdrian Chadd 			/*
18158f939e79SAdrian Chadd 			 * Only blank sc_rxlink if we've not yet kicked
18168f939e79SAdrian Chadd 			 * the PCU.
18178f939e79SAdrian Chadd 			 *
18188f939e79SAdrian Chadd 			 * This isn't entirely correct - the correct solution
18198f939e79SAdrian Chadd 			 * would be to have a PCU lock and engage that for
18208f939e79SAdrian Chadd 			 * the duration of the PCU fiddling; which would include
18218f939e79SAdrian Chadd 			 * running the RX process. Otherwise we could end up
18228f939e79SAdrian Chadd 			 * messing up the RX descriptor chain and making the
18238f939e79SAdrian Chadd 			 * RX desc list much shorter.
18248f939e79SAdrian Chadd 			 */
18258f939e79SAdrian Chadd 			if (! sc->sc_kickpcu)
18268f939e79SAdrian Chadd 				sc->sc_rxlink = NULL;
18278f939e79SAdrian Chadd 			sc->sc_kickpcu = 1;
1828f0db652cSAdrian Chadd 			ATH_PCU_UNLOCK(sc);
18298f939e79SAdrian Chadd 			/*
18301fdadc0fSAdrian Chadd 			 * Enqueue an RX proc, to handled whatever
18311fdadc0fSAdrian Chadd 			 * is in the RX queue.
18321fdadc0fSAdrian Chadd 			 * This will then kick the PCU.
18331fdadc0fSAdrian Chadd 			 */
1834f0db652cSAdrian Chadd 			sc->sc_rx.recv_sched(sc, 1);
18355591b213SSam Leffler 		}
18365591b213SSam Leffler 		if (status & HAL_INT_TXURN) {
18375591b213SSam Leffler 			sc->sc_stats.ast_txurn++;
18385591b213SSam Leffler 			/* bump tx trigger level */
18395591b213SSam Leffler 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
18405591b213SSam Leffler 		}
1841bcbb08ceSAdrian Chadd 		/*
1842bcbb08ceSAdrian Chadd 		 * Handle both the legacy and RX EDMA interrupt bits.
1843bcbb08ceSAdrian Chadd 		 * Note that HAL_INT_RXLP is also HAL_INT_RXDESC.
1844bcbb08ceSAdrian Chadd 		 */
1845bcbb08ceSAdrian Chadd 		if (status & (HAL_INT_RX | HAL_INT_RXHP | HAL_INT_RXLP)) {
18468f939e79SAdrian Chadd 			sc->sc_stats.ast_rx_intr++;
1847f0db652cSAdrian Chadd 			sc->sc_rx.recv_sched(sc, 1);
18488f939e79SAdrian Chadd 		}
18498f939e79SAdrian Chadd 		if (status & HAL_INT_TX) {
18508f939e79SAdrian Chadd 			sc->sc_stats.ast_tx_intr++;
18518f939e79SAdrian Chadd 			/*
18528f939e79SAdrian Chadd 			 * Grab all the currently set bits in the HAL txq bitmap
18538f939e79SAdrian Chadd 			 * and blank them. This is the only place we should be
18548f939e79SAdrian Chadd 			 * doing this.
18558f939e79SAdrian Chadd 			 */
1856bad98824SAdrian Chadd 			if (! sc->sc_isedma) {
1857ef27340cSAdrian Chadd 				ATH_PCU_LOCK(sc);
18588f939e79SAdrian Chadd 				txqs = 0xffffffff;
18598f939e79SAdrian Chadd 				ath_hal_gettxintrtxqs(sc->sc_ah, &txqs);
186003682514SAdrian Chadd 				ATH_KTR(sc, ATH_KTR_INTERRUPTS, 3,
186103682514SAdrian Chadd 				    "ath_intr: TX; txqs=0x%08x, txq_active was 0x%08x, now 0x%08x",
186203682514SAdrian Chadd 				    txqs,
186303682514SAdrian Chadd 				    sc->sc_txq_active,
186403682514SAdrian Chadd 				    sc->sc_txq_active | txqs);
18658f939e79SAdrian Chadd 				sc->sc_txq_active |= txqs;
1866ef27340cSAdrian Chadd 				ATH_PCU_UNLOCK(sc);
18678f939e79SAdrian Chadd 			}
1868bad98824SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask);
1869bad98824SAdrian Chadd 		}
18705591b213SSam Leffler 		if (status & HAL_INT_BMISS) {
18715591b213SSam Leffler 			sc->sc_stats.ast_bmiss++;
18720bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask);
18735591b213SSam Leffler 		}
18746ad02dbaSAdrian Chadd 		if (status & HAL_INT_GTT)
18756ad02dbaSAdrian Chadd 			sc->sc_stats.ast_tx_timeout++;
18765594f5c0SAdrian Chadd 		if (status & HAL_INT_CST)
18775594f5c0SAdrian Chadd 			sc->sc_stats.ast_tx_cst++;
1878c42a7b7eSSam Leffler 		if (status & HAL_INT_MIB) {
1879c42a7b7eSSam Leffler 			sc->sc_stats.ast_mib++;
1880ef27340cSAdrian Chadd 			ATH_PCU_LOCK(sc);
1881c42a7b7eSSam Leffler 			/*
1882c42a7b7eSSam Leffler 			 * Disable interrupts until we service the MIB
1883c42a7b7eSSam Leffler 			 * interrupt; otherwise it will continue to fire.
1884c42a7b7eSSam Leffler 			 */
1885c42a7b7eSSam Leffler 			ath_hal_intrset(ah, 0);
1886c42a7b7eSSam Leffler 			/*
1887c42a7b7eSSam Leffler 			 * Let the hal handle the event.  We assume it will
1888c42a7b7eSSam Leffler 			 * clear whatever condition caused the interrupt.
1889c42a7b7eSSam Leffler 			 */
1890ffa2cab6SSam Leffler 			ath_hal_mibevent(ah, &sc->sc_halstats);
18918f939e79SAdrian Chadd 			/*
18928f939e79SAdrian Chadd 			 * Don't reset the interrupt if we've just
18938f939e79SAdrian Chadd 			 * kicked the PCU, or we may get a nested
18948f939e79SAdrian Chadd 			 * RXEOL before the rxproc has had a chance
18958f939e79SAdrian Chadd 			 * to run.
18968f939e79SAdrian Chadd 			 */
18978f939e79SAdrian Chadd 			if (sc->sc_kickpcu == 0)
1898c42a7b7eSSam Leffler 				ath_hal_intrset(ah, sc->sc_imask);
1899ef27340cSAdrian Chadd 			ATH_PCU_UNLOCK(sc);
1900c42a7b7eSSam Leffler 		}
19019c4fc1e8SSam Leffler 		if (status & HAL_INT_RXORN) {
19029c4fc1e8SSam Leffler 			/* NB: hal marks HAL_INT_FATAL when RXORN is fatal */
190303682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXORN");
19049c4fc1e8SSam Leffler 			sc->sc_stats.ast_rxorn++;
19059c4fc1e8SSam Leffler 		}
19065591b213SSam Leffler 	}
1907ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
1908ef27340cSAdrian Chadd 	sc->sc_intr_cnt--;
1909ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
19105591b213SSam Leffler }
19115591b213SSam Leffler 
19125591b213SSam Leffler static void
19135591b213SSam Leffler ath_fatal_proc(void *arg, int pending)
19145591b213SSam Leffler {
19155591b213SSam Leffler 	struct ath_softc *sc = arg;
1916fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
191716c8acaaSSam Leffler 	u_int32_t *state;
191816c8acaaSSam Leffler 	u_int32_t len;
191968e8e04eSSam Leffler 	void *sp;
19205591b213SSam Leffler 
1921c42a7b7eSSam Leffler 	if_printf(ifp, "hardware error; resetting\n");
192216c8acaaSSam Leffler 	/*
192316c8acaaSSam Leffler 	 * Fatal errors are unrecoverable.  Typically these
192416c8acaaSSam Leffler 	 * are caused by DMA errors.  Collect h/w state from
192516c8acaaSSam Leffler 	 * the hal so we can diagnose what's going on.
192616c8acaaSSam Leffler 	 */
192768e8e04eSSam Leffler 	if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) {
192816c8acaaSSam Leffler 		KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len));
192968e8e04eSSam Leffler 		state = sp;
193016c8acaaSSam Leffler 		if_printf(ifp, "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n",
193116c8acaaSSam Leffler 		    state[0], state[1] , state[2], state[3],
193216c8acaaSSam Leffler 		    state[4], state[5]);
193316c8acaaSSam Leffler 	}
1934517526efSAdrian Chadd 	ath_reset(ifp, ATH_RESET_NOLOSS);
19355591b213SSam Leffler }
19365591b213SSam Leffler 
19375591b213SSam Leffler static void
1938b032f27cSSam Leffler ath_bmiss_vap(struct ieee80211vap *vap)
19395591b213SSam Leffler {
194059fbb257SSam Leffler 	/*
194159fbb257SSam Leffler 	 * Workaround phantom bmiss interrupts by sanity-checking
194259fbb257SSam Leffler 	 * the time of our last rx'd frame.  If it is within the
194359fbb257SSam Leffler 	 * beacon miss interval then ignore the interrupt.  If it's
194459fbb257SSam Leffler 	 * truly a bmiss we'll get another interrupt soon and that'll
194559fbb257SSam Leffler 	 * be dispatched up for processing.  Note this applies only
194659fbb257SSam Leffler 	 * for h/w beacon miss events.
194759fbb257SSam Leffler 	 */
194859fbb257SSam Leffler 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
1949a7ace843SSam Leffler 		struct ifnet *ifp = vap->iv_ic->ic_ifp;
1950a7ace843SSam Leffler 		struct ath_softc *sc = ifp->if_softc;
1951d7736e13SSam Leffler 		u_int64_t lastrx = sc->sc_lastrx;
1952d7736e13SSam Leffler 		u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah);
195380767531SAdrian Chadd 		/* XXX should take a locked ref to iv_bss */
1954d7736e13SSam Leffler 		u_int bmisstimeout =
1955b032f27cSSam Leffler 			vap->iv_bmissthreshold * vap->iv_bss->ni_intval * 1024;
1956d7736e13SSam Leffler 
1957d7736e13SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
1958d7736e13SSam Leffler 		    "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n",
1959d7736e13SSam Leffler 		    __func__, (unsigned long long) tsf,
1960d7736e13SSam Leffler 		    (unsigned long long)(tsf - lastrx),
1961d7736e13SSam Leffler 		    (unsigned long long) lastrx, bmisstimeout);
196259fbb257SSam Leffler 
196359fbb257SSam Leffler 		if (tsf - lastrx <= bmisstimeout) {
1964d7736e13SSam Leffler 			sc->sc_stats.ast_bmiss_phantom++;
196559fbb257SSam Leffler 			return;
196659fbb257SSam Leffler 		}
196759fbb257SSam Leffler 	}
196859fbb257SSam Leffler 	ATH_VAP(vap)->av_bmiss(vap);
1969e585d188SSam Leffler }
1970b032f27cSSam Leffler 
1971b837332dSAdrian Chadd int
1972459bc4f0SSam Leffler ath_hal_gethangstate(struct ath_hal *ah, uint32_t mask, uint32_t *hangs)
1973459bc4f0SSam Leffler {
1974459bc4f0SSam Leffler 	uint32_t rsize;
1975459bc4f0SSam Leffler 	void *sp;
1976459bc4f0SSam Leffler 
197725c96056SAdrian Chadd 	if (!ath_hal_getdiagstate(ah, HAL_DIAG_CHECK_HANGS, &mask, sizeof(mask), &sp, &rsize))
1978459bc4f0SSam Leffler 		return 0;
1979459bc4f0SSam Leffler 	KASSERT(rsize == sizeof(uint32_t), ("resultsize %u", rsize));
1980459bc4f0SSam Leffler 	*hangs = *(uint32_t *)sp;
1981459bc4f0SSam Leffler 	return 1;
1982459bc4f0SSam Leffler }
1983459bc4f0SSam Leffler 
1984b032f27cSSam Leffler static void
1985b032f27cSSam Leffler ath_bmiss_proc(void *arg, int pending)
1986b032f27cSSam Leffler {
1987b032f27cSSam Leffler 	struct ath_softc *sc = arg;
1988b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1989459bc4f0SSam Leffler 	uint32_t hangs;
1990b032f27cSSam Leffler 
1991b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
1992459bc4f0SSam Leffler 
1993a74ebfe5SAdrian Chadd 	/*
1994a74ebfe5SAdrian Chadd 	 * Do a reset upon any becaon miss event.
1995a74ebfe5SAdrian Chadd 	 *
1996a74ebfe5SAdrian Chadd 	 * It may be a non-recognised RX clear hang which needs a reset
1997a74ebfe5SAdrian Chadd 	 * to clear.
1998a74ebfe5SAdrian Chadd 	 */
1999459bc4f0SSam Leffler 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) {
2000517526efSAdrian Chadd 		ath_reset(ifp, ATH_RESET_NOLOSS);
2001a74ebfe5SAdrian Chadd 		if_printf(ifp, "bb hang detected (0x%x), resetting\n", hangs);
2002a74ebfe5SAdrian Chadd 	} else {
2003a74ebfe5SAdrian Chadd 		ath_reset(ifp, ATH_RESET_NOLOSS);
2004b032f27cSSam Leffler 		ieee80211_beacon_miss(ifp->if_l2com);
20055591b213SSam Leffler 	}
2006a74ebfe5SAdrian Chadd }
20075591b213SSam Leffler 
2008724c193aSSam Leffler /*
2009b032f27cSSam Leffler  * Handle TKIP MIC setup to deal hardware that doesn't do MIC
2010b032f27cSSam Leffler  * calcs together with WME.  If necessary disable the crypto
2011b032f27cSSam Leffler  * hardware and mark the 802.11 state so keys will be setup
2012b032f27cSSam Leffler  * with the MIC work done in software.
2013b032f27cSSam Leffler  */
2014b032f27cSSam Leffler static void
2015b032f27cSSam Leffler ath_settkipmic(struct ath_softc *sc)
2016b032f27cSSam Leffler {
2017b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
2018b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
2019b032f27cSSam Leffler 
2020b032f27cSSam Leffler 	if ((ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIP) && !sc->sc_wmetkipmic) {
2021b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WME) {
2022b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_FALSE);
2023b032f27cSSam Leffler 			ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC;
2024b032f27cSSam Leffler 		} else {
2025b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_TRUE);
2026b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
2027b032f27cSSam Leffler 		}
2028b032f27cSSam Leffler 	}
2029b032f27cSSam Leffler }
2030b032f27cSSam Leffler 
20315591b213SSam Leffler static void
20325591b213SSam Leffler ath_init(void *arg)
20335591b213SSam Leffler {
20345591b213SSam Leffler 	struct ath_softc *sc = (struct ath_softc *) arg;
2035fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
2036b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
20375591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
20385591b213SSam Leffler 	HAL_STATUS status;
20395591b213SSam Leffler 
2040c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
2041c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
20425591b213SSam Leffler 
2043f0b2a0beSSam Leffler 	ATH_LOCK(sc);
20445591b213SSam Leffler 	/*
20455591b213SSam Leffler 	 * Stop anything previously setup.  This is safe
20465591b213SSam Leffler 	 * whether this is the first time through or not.
20475591b213SSam Leffler 	 */
2048c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
20495591b213SSam Leffler 
20505591b213SSam Leffler 	/*
20515591b213SSam Leffler 	 * The basic interface to setting the hardware in a good
20525591b213SSam Leffler 	 * state is ``reset''.  On return the hardware is known to
20535591b213SSam Leffler 	 * be powered up and with interrupts disabled.  This must
20545591b213SSam Leffler 	 * be followed by initialization of the appropriate bits
20555591b213SSam Leffler 	 * and then setup of the interrupt mask.
20565591b213SSam Leffler 	 */
2057b032f27cSSam Leffler 	ath_settkipmic(sc);
20586322256bSAdrian Chadd 	ath_update_chainmasks(sc, ic->ic_curchan);
20596322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
20606322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
206159efa8b5SSam Leffler 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_FALSE, &status)) {
20625591b213SSam Leffler 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
20635591b213SSam Leffler 			status);
2064b032f27cSSam Leffler 		ATH_UNLOCK(sc);
2065b032f27cSSam Leffler 		return;
20665591b213SSam Leffler 	}
2067b032f27cSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
20685591b213SSam Leffler 
206948237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
207048237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
207148237774SAdrian Chadd 
20729af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
20739af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
20749af351f9SAdrian Chadd 
20755591b213SSam Leffler 	/*
2076b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
2077b70f530bSAdrian Chadd 	 */
2078b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
2079b70f530bSAdrian Chadd 
2080b70f530bSAdrian Chadd 	/*
2081dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
2082dd6a574eSAdrian Chadd 	 * support it.
2083dd6a574eSAdrian Chadd 	 */
2084dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
2085dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
2086dd6a574eSAdrian Chadd 	else
2087dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
2088dd6a574eSAdrian Chadd 
2089dd6a574eSAdrian Chadd 	/*
2090c59005e9SSam Leffler 	 * Likewise this is set during reset so update
2091c59005e9SSam Leffler 	 * state cached in the driver.
2092c59005e9SSam Leffler 	 */
2093c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
20942dc7fcc4SSam Leffler 	sc->sc_lastlongcal = 0;
20952dc7fcc4SSam Leffler 	sc->sc_resetcal = 1;
20962dc7fcc4SSam Leffler 	sc->sc_lastcalreset = 0;
2097a108ab63SAdrian Chadd 	sc->sc_lastani = 0;
2098a108ab63SAdrian Chadd 	sc->sc_lastshortcal = 0;
2099a108ab63SAdrian Chadd 	sc->sc_doresetcal = AH_FALSE;
21002fd9aabbSAdrian Chadd 	/*
21012fd9aabbSAdrian Chadd 	 * Beacon timers were cleared here; give ath_newstate()
21022fd9aabbSAdrian Chadd 	 * a hint that the beacon timers should be poked when
21032fd9aabbSAdrian Chadd 	 * things transition to the RUN state.
21042fd9aabbSAdrian Chadd 	 */
21052fd9aabbSAdrian Chadd 	sc->sc_beacons = 0;
2106c42a7b7eSSam Leffler 
2107c42a7b7eSSam Leffler 	/*
21085591b213SSam Leffler 	 * Setup the hardware after reset: the key cache
21095591b213SSam Leffler 	 * is filled as needed and the receive engine is
21105591b213SSam Leffler 	 * set going.  Frame transmit is handled entirely
21115591b213SSam Leffler 	 * in the frame output path; there's nothing to do
21125591b213SSam Leffler 	 * here except setup the interrupt mask.
21135591b213SSam Leffler 	 */
21145591b213SSam Leffler 	if (ath_startrecv(sc) != 0) {
21155591b213SSam Leffler 		if_printf(ifp, "unable to start recv logic\n");
2116b032f27cSSam Leffler 		ATH_UNLOCK(sc);
2117b032f27cSSam Leffler 		return;
21185591b213SSam Leffler 	}
21195591b213SSam Leffler 
21205591b213SSam Leffler 	/*
21215591b213SSam Leffler 	 * Enable interrupts.
21225591b213SSam Leffler 	 */
21235591b213SSam Leffler 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
21245591b213SSam Leffler 		  | HAL_INT_RXEOL | HAL_INT_RXORN
212569930f87SAdrian Chadd 		  | HAL_INT_TXURN
21265591b213SSam Leffler 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
2127bcbb08ceSAdrian Chadd 
2128bcbb08ceSAdrian Chadd 	/*
2129bcbb08ceSAdrian Chadd 	 * Enable RX EDMA bits.  Note these overlap with
2130bcbb08ceSAdrian Chadd 	 * HAL_INT_RX and HAL_INT_RXDESC respectively.
2131bcbb08ceSAdrian Chadd 	 */
2132bcbb08ceSAdrian Chadd 	if (sc->sc_isedma)
2133bcbb08ceSAdrian Chadd 		sc->sc_imask |= (HAL_INT_RXHP | HAL_INT_RXLP);
2134bcbb08ceSAdrian Chadd 
2135c42a7b7eSSam Leffler 	/*
2136c42a7b7eSSam Leffler 	 * Enable MIB interrupts when there are hardware phy counters.
2137c42a7b7eSSam Leffler 	 * Note we only do this (at the moment) for station mode.
2138c42a7b7eSSam Leffler 	 */
2139c42a7b7eSSam Leffler 	if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
2140c42a7b7eSSam Leffler 		sc->sc_imask |= HAL_INT_MIB;
21415591b213SSam Leffler 
21425594f5c0SAdrian Chadd 	/* Enable global TX timeout and carrier sense timeout if available */
21436ad02dbaSAdrian Chadd 	if (ath_hal_gtxto_supported(ah))
21443788ebedSAdrian Chadd 		sc->sc_imask |= HAL_INT_GTT;
2145d0a0ebc6SAdrian Chadd 
2146d0a0ebc6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: imask=0x%x\n",
2147d0a0ebc6SAdrian Chadd 		__func__, sc->sc_imask);
21486ad02dbaSAdrian Chadd 
214913f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
21502e986da5SSam Leffler 	callout_reset(&sc->sc_wd_ch, hz, ath_watchdog, sc);
2151b032f27cSSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
21525591b213SSam Leffler 
2153b032f27cSSam Leffler 	ATH_UNLOCK(sc);
2154b032f27cSSam Leffler 
215586e07743SSam Leffler #ifdef ATH_TX99_DIAG
215686e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
215786e07743SSam Leffler 		sc->sc_tx99->start(sc->sc_tx99);
215886e07743SSam Leffler 	else
215986e07743SSam Leffler #endif
2160b032f27cSSam Leffler 	ieee80211_start_all(ic);		/* start all vap's */
21615591b213SSam Leffler }
21625591b213SSam Leffler 
21635591b213SSam Leffler static void
2164c42a7b7eSSam Leffler ath_stop_locked(struct ifnet *ifp)
21655591b213SSam Leffler {
21665591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
21675591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
21685591b213SSam Leffler 
2169c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
2170c42a7b7eSSam Leffler 		__func__, sc->sc_invalid, ifp->if_flags);
21715591b213SSam Leffler 
2172c42a7b7eSSam Leffler 	ATH_LOCK_ASSERT(sc);
217313f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
21745591b213SSam Leffler 		/*
21755591b213SSam Leffler 		 * Shutdown the hardware and driver:
2176c42a7b7eSSam Leffler 		 *    reset 802.11 state machine
21775591b213SSam Leffler 		 *    turn off timers
2178c42a7b7eSSam Leffler 		 *    disable interrupts
2179c42a7b7eSSam Leffler 		 *    turn off the radio
21805591b213SSam Leffler 		 *    clear transmit machinery
21815591b213SSam Leffler 		 *    clear receive machinery
21825591b213SSam Leffler 		 *    drain and release tx queues
21835591b213SSam Leffler 		 *    reclaim beacon resources
21845591b213SSam Leffler 		 *    power down hardware
21855591b213SSam Leffler 		 *
21865591b213SSam Leffler 		 * Note that some of this work is not possible if the
21875591b213SSam Leffler 		 * hardware is gone (invalid).
21885591b213SSam Leffler 		 */
218986e07743SSam Leffler #ifdef ATH_TX99_DIAG
219086e07743SSam Leffler 		if (sc->sc_tx99 != NULL)
219186e07743SSam Leffler 			sc->sc_tx99->stop(sc->sc_tx99);
219286e07743SSam Leffler #endif
21932e986da5SSam Leffler 		callout_stop(&sc->sc_wd_ch);
21942e986da5SSam Leffler 		sc->sc_wd_timer = 0;
219513f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2196c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
21973e50ec2cSSam Leffler 			if (sc->sc_softled) {
21983e50ec2cSSam Leffler 				callout_stop(&sc->sc_ledtimer);
21993e50ec2cSSam Leffler 				ath_hal_gpioset(ah, sc->sc_ledpin,
22003e50ec2cSSam Leffler 					!sc->sc_ledon);
22013e50ec2cSSam Leffler 				sc->sc_blinking = 0;
22023e50ec2cSSam Leffler 			}
22035591b213SSam Leffler 			ath_hal_intrset(ah, 0);
2204c42a7b7eSSam Leffler 		}
2205517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);
2206c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
22079a842e8bSAdrian Chadd 			ath_stoprecv(sc, 1);
2208c42a7b7eSSam Leffler 			ath_hal_phydisable(ah);
2209c42a7b7eSSam Leffler 		} else
22105591b213SSam Leffler 			sc->sc_rxlink = NULL;
2211b032f27cSSam Leffler 		ath_beacon_free(sc);	/* XXX not needed */
2212c42a7b7eSSam Leffler 	}
2213c42a7b7eSSam Leffler }
2214c42a7b7eSSam Leffler 
2215ef27340cSAdrian Chadd #define	MAX_TXRX_ITERATIONS	1000
2216ef27340cSAdrian Chadd static void
221721008bf1SAdrian Chadd ath_txrx_stop_locked(struct ath_softc *sc)
2218ef27340cSAdrian Chadd {
2219ef27340cSAdrian Chadd 	int i = MAX_TXRX_ITERATIONS;
2220ef27340cSAdrian Chadd 
2221ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
222221008bf1SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
222321008bf1SAdrian Chadd 
2224ef27340cSAdrian Chadd 	/*
2225ef27340cSAdrian Chadd 	 * Sleep until all the pending operations have completed.
2226ef27340cSAdrian Chadd 	 *
2227ef27340cSAdrian Chadd 	 * The caller must ensure that reset has been incremented
2228ef27340cSAdrian Chadd 	 * or the pending operations may continue being queued.
2229ef27340cSAdrian Chadd 	 */
2230ef27340cSAdrian Chadd 	while (sc->sc_rxproc_cnt || sc->sc_txproc_cnt ||
2231ef27340cSAdrian Chadd 	    sc->sc_txstart_cnt || sc->sc_intr_cnt) {
2232ef27340cSAdrian Chadd 		if (i <= 0)
2233ef27340cSAdrian Chadd 			break;
2234a2d8240dSAdrian Chadd 		msleep(sc, &sc->sc_pcu_mtx, 0, "ath_txrx_stop", 1);
2235ef27340cSAdrian Chadd 		i--;
2236ef27340cSAdrian Chadd 	}
2237ef27340cSAdrian Chadd 
2238ef27340cSAdrian Chadd 	if (i <= 0)
2239ef27340cSAdrian Chadd 		device_printf(sc->sc_dev,
2240ef27340cSAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2241ef27340cSAdrian Chadd 		    __func__, MAX_TXRX_ITERATIONS);
2242ef27340cSAdrian Chadd }
2243ef27340cSAdrian Chadd #undef	MAX_TXRX_ITERATIONS
2244ef27340cSAdrian Chadd 
2245e78719adSAdrian Chadd #if 0
2246ef27340cSAdrian Chadd static void
224721008bf1SAdrian Chadd ath_txrx_stop(struct ath_softc *sc)
224821008bf1SAdrian Chadd {
224921008bf1SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
225021008bf1SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
225121008bf1SAdrian Chadd 
225221008bf1SAdrian Chadd 	ATH_PCU_LOCK(sc);
225321008bf1SAdrian Chadd 	ath_txrx_stop_locked(sc);
225421008bf1SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
225521008bf1SAdrian Chadd }
2256e78719adSAdrian Chadd #endif
225721008bf1SAdrian Chadd 
225821008bf1SAdrian Chadd static void
2259ef27340cSAdrian Chadd ath_txrx_start(struct ath_softc *sc)
2260ef27340cSAdrian Chadd {
2261ef27340cSAdrian Chadd 
2262ef27340cSAdrian Chadd 	taskqueue_unblock(sc->sc_tq);
2263ef27340cSAdrian Chadd }
2264ef27340cSAdrian Chadd 
2265ee321975SAdrian Chadd /*
2266ee321975SAdrian Chadd  * Grab the reset lock, and wait around until noone else
2267ee321975SAdrian Chadd  * is trying to do anything with it.
2268ee321975SAdrian Chadd  *
2269ee321975SAdrian Chadd  * This is totally horrible but we can't hold this lock for
2270ee321975SAdrian Chadd  * long enough to do TX/RX or we end up with net80211/ip stack
2271ee321975SAdrian Chadd  * LORs and eventual deadlock.
2272ee321975SAdrian Chadd  *
2273ee321975SAdrian Chadd  * "dowait" signals whether to spin, waiting for the reset
2274ee321975SAdrian Chadd  * lock count to reach 0. This should (for now) only be used
2275ee321975SAdrian Chadd  * during the reset path, as the rest of the code may not
2276ee321975SAdrian Chadd  * be locking-reentrant enough to behave correctly.
2277ee321975SAdrian Chadd  *
2278ee321975SAdrian Chadd  * Another, cleaner way should be found to serialise all of
2279ee321975SAdrian Chadd  * these operations.
2280ee321975SAdrian Chadd  */
2281ee321975SAdrian Chadd #define	MAX_RESET_ITERATIONS	10
2282ee321975SAdrian Chadd static int
2283ee321975SAdrian Chadd ath_reset_grablock(struct ath_softc *sc, int dowait)
2284ee321975SAdrian Chadd {
2285ee321975SAdrian Chadd 	int w = 0;
2286ee321975SAdrian Chadd 	int i = MAX_RESET_ITERATIONS;
2287ee321975SAdrian Chadd 
2288ee321975SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
2289ee321975SAdrian Chadd 	do {
2290ee321975SAdrian Chadd 		if (sc->sc_inreset_cnt == 0) {
2291ee321975SAdrian Chadd 			w = 1;
2292ee321975SAdrian Chadd 			break;
2293ee321975SAdrian Chadd 		}
2294ee321975SAdrian Chadd 		if (dowait == 0) {
2295ee321975SAdrian Chadd 			w = 0;
2296ee321975SAdrian Chadd 			break;
2297ee321975SAdrian Chadd 		}
2298ee321975SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2299ee321975SAdrian Chadd 		pause("ath_reset_grablock", 1);
2300ee321975SAdrian Chadd 		i--;
2301ee321975SAdrian Chadd 		ATH_PCU_LOCK(sc);
2302ee321975SAdrian Chadd 	} while (i > 0);
2303ee321975SAdrian Chadd 
2304ee321975SAdrian Chadd 	/*
2305ee321975SAdrian Chadd 	 * We always increment the refcounter, regardless
2306ee321975SAdrian Chadd 	 * of whether we succeeded to get it in an exclusive
2307ee321975SAdrian Chadd 	 * way.
2308ee321975SAdrian Chadd 	 */
2309ee321975SAdrian Chadd 	sc->sc_inreset_cnt++;
2310ee321975SAdrian Chadd 
2311ee321975SAdrian Chadd 	if (i <= 0)
2312ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2313ee321975SAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2314ee321975SAdrian Chadd 		    __func__, MAX_RESET_ITERATIONS);
2315ee321975SAdrian Chadd 
2316ee321975SAdrian Chadd 	if (w == 0)
2317ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2318ee321975SAdrian Chadd 		    "%s: warning, recursive reset path!\n",
2319ee321975SAdrian Chadd 		    __func__);
2320ee321975SAdrian Chadd 
2321ee321975SAdrian Chadd 	return w;
2322ee321975SAdrian Chadd }
2323ee321975SAdrian Chadd #undef MAX_RESET_ITERATIONS
2324ee321975SAdrian Chadd 
2325ee321975SAdrian Chadd /*
2326ee321975SAdrian Chadd  * XXX TODO: write ath_reset_releaselock
2327ee321975SAdrian Chadd  */
2328ee321975SAdrian Chadd 
2329c42a7b7eSSam Leffler static void
2330c42a7b7eSSam Leffler ath_stop(struct ifnet *ifp)
2331c42a7b7eSSam Leffler {
2332c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2333c42a7b7eSSam Leffler 
2334c42a7b7eSSam Leffler 	ATH_LOCK(sc);
2335c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
2336f0b2a0beSSam Leffler 	ATH_UNLOCK(sc);
23375591b213SSam Leffler }
23385591b213SSam Leffler 
23395591b213SSam Leffler /*
23405591b213SSam Leffler  * Reset the hardware w/o losing operational state.  This is
23415591b213SSam Leffler  * basically a more efficient way of doing ath_stop, ath_init,
23425591b213SSam Leffler  * followed by state transitions to the current 802.11
2343c42a7b7eSSam Leffler  * operational state.  Used to recover from various errors and
2344c42a7b7eSSam Leffler  * to reset or reload hardware state.
23455591b213SSam Leffler  */
23466079fdbeSAdrian Chadd int
2347517526efSAdrian Chadd ath_reset(struct ifnet *ifp, ATH_RESET_TYPE reset_type)
23485591b213SSam Leffler {
2349c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2350b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
23515591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
23525591b213SSam Leffler 	HAL_STATUS status;
2353ef27340cSAdrian Chadd 	int i;
23545591b213SSam Leffler 
2355f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
235616d4de92SAdrian Chadd 
2357ee321975SAdrian Chadd 	/* Ensure ATH_LOCK isn't held; ath_rx_proc can't be locked */
2358ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
2359ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
2360ef27340cSAdrian Chadd 
2361d52f7132SAdrian Chadd 	/* Try to (stop any further TX/RX from occuring */
2362d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
2363d52f7132SAdrian Chadd 
2364ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2365904e385eSAdrian Chadd 
2366904e385eSAdrian Chadd 	/*
2367904e385eSAdrian Chadd 	 * Grab the reset lock before TX/RX is stopped.
2368904e385eSAdrian Chadd 	 *
2369904e385eSAdrian Chadd 	 * This is needed to ensure that when the TX/RX actually does finish,
2370904e385eSAdrian Chadd 	 * no further TX/RX/reset runs in parallel with this.
2371904e385eSAdrian Chadd 	 */
2372ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
2373ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
2374ef27340cSAdrian Chadd 		    __func__);
2375ef27340cSAdrian Chadd 	}
2376904e385eSAdrian Chadd 
2377904e385eSAdrian Chadd 	/* disable interrupts */
2378904e385eSAdrian Chadd 	ath_hal_intrset(ah, 0);
2379904e385eSAdrian Chadd 
2380904e385eSAdrian Chadd 	/*
2381904e385eSAdrian Chadd 	 * Now, ensure that any in progress TX/RX completes before we
2382904e385eSAdrian Chadd 	 * continue.
2383904e385eSAdrian Chadd 	 */
2384904e385eSAdrian Chadd 	ath_txrx_stop_locked(sc);
2385904e385eSAdrian Chadd 
2386ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2387ef27340cSAdrian Chadd 
2388f52d3452SAdrian Chadd 	/*
23899a842e8bSAdrian Chadd 	 * Should now wait for pending TX/RX to complete
23909a842e8bSAdrian Chadd 	 * and block future ones from occuring. This needs to be
23919a842e8bSAdrian Chadd 	 * done before the TX queue is drained.
2392f52d3452SAdrian Chadd 	 */
2393ef27340cSAdrian Chadd 	ath_draintxq(sc, reset_type);	/* stop xmit side */
2394ef27340cSAdrian Chadd 
2395ef27340cSAdrian Chadd 	/*
2396ef27340cSAdrian Chadd 	 * Regardless of whether we're doing a no-loss flush or
2397ef27340cSAdrian Chadd 	 * not, stop the PCU and handle what's in the RX queue.
2398ef27340cSAdrian Chadd 	 * That way frames aren't dropped which shouldn't be.
2399ef27340cSAdrian Chadd 	 */
24009a842e8bSAdrian Chadd 	ath_stoprecv(sc, (reset_type != ATH_RESET_NOLOSS));
2401f8cc9b09SAdrian Chadd 	ath_rx_flush(sc);
2402ef27340cSAdrian Chadd 
2403b032f27cSSam Leffler 	ath_settkipmic(sc);		/* configure TKIP MIC handling */
24045591b213SSam Leffler 	/* NB: indicate channel change so we do a full reset */
24056322256bSAdrian Chadd 	ath_update_chainmasks(sc, ic->ic_curchan);
24066322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
24076322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
240859efa8b5SSam Leffler 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_TRUE, &status))
24095591b213SSam Leffler 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
24105591b213SSam Leffler 			__func__, status);
2411c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
241248237774SAdrian Chadd 
241348237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
241448237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
241548237774SAdrian Chadd 
24169af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
24179af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
24189af351f9SAdrian Chadd 
2419dd6a574eSAdrian Chadd 	/*
2420b70f530bSAdrian Chadd 	 * Let bluetooth coexistence at in case it's needed for this channel
2421b70f530bSAdrian Chadd 	 */
2422b70f530bSAdrian Chadd 	ath_btcoex_enable(sc, ic->ic_curchan);
2423b70f530bSAdrian Chadd 
2424b70f530bSAdrian Chadd 	/*
2425dd6a574eSAdrian Chadd 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
2426dd6a574eSAdrian Chadd 	 * support it.
2427dd6a574eSAdrian Chadd 	 */
2428dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
2429dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 1);
2430dd6a574eSAdrian Chadd 	else
2431dd6a574eSAdrian Chadd 		ath_hal_setenforcetxop(sc->sc_ah, 0);
2432dd6a574eSAdrian Chadd 
243368e8e04eSSam Leffler 	if (ath_startrecv(sc) != 0)	/* restart recv */
243468e8e04eSSam Leffler 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
2435c42a7b7eSSam Leffler 	/*
2436c42a7b7eSSam Leffler 	 * We may be doing a reset in response to an ioctl
2437c42a7b7eSSam Leffler 	 * that changes the channel so update any state that
2438c42a7b7eSSam Leffler 	 * might change as a result.
2439c42a7b7eSSam Leffler 	 */
2440724c193aSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
2441c89b957aSSam Leffler 	if (sc->sc_beacons) {		/* restart beacons */
2442584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
244310ad9a77SSam Leffler 		if (sc->sc_tdma)
244410ad9a77SSam Leffler 			ath_tdma_config(sc, NULL);
244510ad9a77SSam Leffler 		else
244610ad9a77SSam Leffler #endif
2447c89b957aSSam Leffler 			ath_beacon_config(sc, NULL);
244810ad9a77SSam Leffler 	}
2449c42a7b7eSSam Leffler 
2450ef27340cSAdrian Chadd 	/*
2451ef27340cSAdrian Chadd 	 * Release the reset lock and re-enable interrupts here.
2452ef27340cSAdrian Chadd 	 * If an interrupt was being processed in ath_intr(),
2453ef27340cSAdrian Chadd 	 * it would disable interrupts at this point. So we have
2454ef27340cSAdrian Chadd 	 * to atomically enable interrupts and decrement the
2455ef27340cSAdrian Chadd 	 * reset counter - this way ath_intr() doesn't end up
2456ef27340cSAdrian Chadd 	 * disabling interrupts without a corresponding enable
2457ef27340cSAdrian Chadd 	 * in the rest or channel change path.
2458ef27340cSAdrian Chadd 	 */
2459ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2460ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
2461ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
2462ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
2463ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2464ef27340cSAdrian Chadd 
2465ef27340cSAdrian Chadd 	/*
2466ef27340cSAdrian Chadd 	 * TX and RX can be started here. If it were started with
2467ef27340cSAdrian Chadd 	 * sc_inreset_cnt > 0, the TX and RX path would abort.
2468ef27340cSAdrian Chadd 	 * Thus if this is a nested call through the reset or
2469ef27340cSAdrian Chadd 	 * channel change code, TX completion will occur but
2470ef27340cSAdrian Chadd 	 * RX completion and ath_start / ath_tx_start will not
2471ef27340cSAdrian Chadd 	 * run.
2472ef27340cSAdrian Chadd 	 */
2473ef27340cSAdrian Chadd 
2474ef27340cSAdrian Chadd 	/* Restart TX/RX as needed */
2475ef27340cSAdrian Chadd 	ath_txrx_start(sc);
2476ef27340cSAdrian Chadd 
2477375307d4SAdrian Chadd 	/* Restart TX completion and pending TX */
2478ef27340cSAdrian Chadd 	if (reset_type == ATH_RESET_NOLOSS) {
2479ef27340cSAdrian Chadd 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
2480ef27340cSAdrian Chadd 			if (ATH_TXQ_SETUP(sc, i)) {
2481b837332dSAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
2482ef27340cSAdrian Chadd 				ath_txq_restart_dma(sc, &sc->sc_txq[i]);
2483b837332dSAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
2484b837332dSAdrian Chadd 
2485b837332dSAdrian Chadd 				ATH_TX_LOCK(sc);
2486ef27340cSAdrian Chadd 				ath_txq_sched(sc, &sc->sc_txq[i]);
2487375307d4SAdrian Chadd 				ATH_TX_UNLOCK(sc);
2488ef27340cSAdrian Chadd 			}
2489b837332dSAdrian Chadd 		}
2490b837332dSAdrian Chadd 	}
2491ef27340cSAdrian Chadd 
2492ef27340cSAdrian Chadd 	/*
2493ef27340cSAdrian Chadd 	 * This may have been set during an ath_start() call which
2494ef27340cSAdrian Chadd 	 * set this once it detected a concurrent TX was going on.
2495ef27340cSAdrian Chadd 	 * So, clear it.
2496ef27340cSAdrian Chadd 	 */
2497e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
2498ef27340cSAdrian Chadd 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2499e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
2500ef27340cSAdrian Chadd 
2501ef27340cSAdrian Chadd 	/* Handle any frames in the TX queue */
2502ef27340cSAdrian Chadd 	/*
2503ef27340cSAdrian Chadd 	 * XXX should this be done by the caller, rather than
2504ef27340cSAdrian Chadd 	 * ath_reset() ?
2505ef27340cSAdrian Chadd 	 */
25068e739394SAdrian Chadd 	ath_tx_kick(sc);		/* restart xmit */
2507c42a7b7eSSam Leffler 	return 0;
25085591b213SSam Leffler }
25095591b213SSam Leffler 
251068e8e04eSSam Leffler static int
2511b032f27cSSam Leffler ath_reset_vap(struct ieee80211vap *vap, u_long cmd)
2512b032f27cSSam Leffler {
25134b54a231SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
25144b54a231SSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
25154b54a231SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
25164b54a231SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
25174b54a231SSam Leffler 
25184b54a231SSam Leffler 	switch (cmd) {
25194b54a231SSam Leffler 	case IEEE80211_IOC_TXPOWER:
25204b54a231SSam Leffler 		/*
25214b54a231SSam Leffler 		 * If per-packet TPC is enabled, then we have nothing
25224b54a231SSam Leffler 		 * to do; otherwise we need to force the global limit.
25234b54a231SSam Leffler 		 * All this can happen directly; no need to reset.
25244b54a231SSam Leffler 		 */
25254b54a231SSam Leffler 		if (!ath_hal_gettpc(ah))
25264b54a231SSam Leffler 			ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
25274b54a231SSam Leffler 		return 0;
25284b54a231SSam Leffler 	}
2529517526efSAdrian Chadd 	/* XXX? Full or NOLOSS? */
2530517526efSAdrian Chadd 	return ath_reset(ifp, ATH_RESET_FULL);
2531b032f27cSSam Leffler }
2532b032f27cSSam Leffler 
2533b8e788a5SAdrian Chadd struct ath_buf *
2534af33d486SAdrian Chadd _ath_getbuf_locked(struct ath_softc *sc, ath_buf_type_t btype)
253510ad9a77SSam Leffler {
253610ad9a77SSam Leffler 	struct ath_buf *bf;
253710ad9a77SSam Leffler 
253810ad9a77SSam Leffler 	ATH_TXBUF_LOCK_ASSERT(sc);
253910ad9a77SSam Leffler 
2540af33d486SAdrian Chadd 	if (btype == ATH_BUFTYPE_MGMT)
2541af33d486SAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_txbuf_mgmt);
2542af33d486SAdrian Chadd 	else
25436b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_txbuf);
2544af33d486SAdrian Chadd 
2545e346b073SAdrian Chadd 	if (bf == NULL) {
2546e346b073SAdrian Chadd 		sc->sc_stats.ast_tx_getnobuf++;
2547e346b073SAdrian Chadd 	} else {
2548e346b073SAdrian Chadd 		if (bf->bf_flags & ATH_BUF_BUSY) {
2549e346b073SAdrian Chadd 			sc->sc_stats.ast_tx_getbusybuf++;
2550e346b073SAdrian Chadd 			bf = NULL;
2551e346b073SAdrian Chadd 		}
2552e346b073SAdrian Chadd 	}
2553e346b073SAdrian Chadd 
2554af33d486SAdrian Chadd 	if (bf != NULL && (bf->bf_flags & ATH_BUF_BUSY) == 0) {
2555af33d486SAdrian Chadd 		if (btype == ATH_BUFTYPE_MGMT)
2556af33d486SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_txbuf_mgmt, bf, bf_list);
255723ced6c1SAdrian Chadd 		else {
2558af33d486SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
255923ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt--;
256023ced6c1SAdrian Chadd 
256123ced6c1SAdrian Chadd 			/*
256223ced6c1SAdrian Chadd 			 * This shuldn't happen; however just to be
256323ced6c1SAdrian Chadd 			 * safe print a warning and fudge the txbuf
256423ced6c1SAdrian Chadd 			 * count.
256523ced6c1SAdrian Chadd 			 */
256623ced6c1SAdrian Chadd 			if (sc->sc_txbuf_cnt < 0) {
256723ced6c1SAdrian Chadd 				device_printf(sc->sc_dev,
256823ced6c1SAdrian Chadd 				    "%s: sc_txbuf_cnt < 0?\n",
256923ced6c1SAdrian Chadd 				    __func__);
257023ced6c1SAdrian Chadd 				sc->sc_txbuf_cnt = 0;
257123ced6c1SAdrian Chadd 			}
257223ced6c1SAdrian Chadd 		}
2573af33d486SAdrian Chadd 	} else
257410ad9a77SSam Leffler 		bf = NULL;
2575e346b073SAdrian Chadd 
257610ad9a77SSam Leffler 	if (bf == NULL) {
2577af33d486SAdrian Chadd 		/* XXX should check which list, mgmt or otherwise */
257810ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: %s\n", __func__,
25796b349e5aSAdrian Chadd 		    TAILQ_FIRST(&sc->sc_txbuf) == NULL ?
258010ad9a77SSam Leffler 			"out of xmit buffers" : "xmit buffer busy");
2581e346b073SAdrian Chadd 		return NULL;
258210ad9a77SSam Leffler 	}
2583e346b073SAdrian Chadd 
2584af33d486SAdrian Chadd 	/* XXX TODO: should do this at buffer list initialisation */
2585af33d486SAdrian Chadd 	/* XXX (then, ensure the buffer has the right flag set) */
25863feffbd7SAdrian Chadd 	bf->bf_flags = 0;
2587af33d486SAdrian Chadd 	if (btype == ATH_BUFTYPE_MGMT)
2588af33d486SAdrian Chadd 		bf->bf_flags |= ATH_BUF_MGMT;
2589af33d486SAdrian Chadd 	else
2590af33d486SAdrian Chadd 		bf->bf_flags &= (~ATH_BUF_MGMT);
2591af33d486SAdrian Chadd 
2592e346b073SAdrian Chadd 	/* Valid bf here; clear some basic fields */
2593e346b073SAdrian Chadd 	bf->bf_next = NULL;	/* XXX just to be sure */
2594e346b073SAdrian Chadd 	bf->bf_last = NULL;	/* XXX again, just to be sure */
2595e346b073SAdrian Chadd 	bf->bf_comp = NULL;	/* XXX again, just to be sure */
2596e346b073SAdrian Chadd 	bzero(&bf->bf_state, sizeof(bf->bf_state));
2597e346b073SAdrian Chadd 
259885bf9bc3SAdrian Chadd 	/*
259985bf9bc3SAdrian Chadd 	 * Track the descriptor ID only if doing EDMA
260085bf9bc3SAdrian Chadd 	 */
260185bf9bc3SAdrian Chadd 	if (sc->sc_isedma) {
260285bf9bc3SAdrian Chadd 		bf->bf_descid = sc->sc_txbuf_descid;
260385bf9bc3SAdrian Chadd 		sc->sc_txbuf_descid++;
260485bf9bc3SAdrian Chadd 	}
260585bf9bc3SAdrian Chadd 
260610ad9a77SSam Leffler 	return bf;
260710ad9a77SSam Leffler }
260810ad9a77SSam Leffler 
2609e346b073SAdrian Chadd /*
2610e346b073SAdrian Chadd  * When retrying a software frame, buffers marked ATH_BUF_BUSY
2611e346b073SAdrian Chadd  * can't be thrown back on the queue as they could still be
2612e346b073SAdrian Chadd  * in use by the hardware.
2613e346b073SAdrian Chadd  *
2614e346b073SAdrian Chadd  * This duplicates the buffer, or returns NULL.
2615e346b073SAdrian Chadd  *
2616e346b073SAdrian Chadd  * The descriptor is also copied but the link pointers and
2617e346b073SAdrian Chadd  * the DMA segments aren't copied; this frame should thus
2618e346b073SAdrian Chadd  * be again passed through the descriptor setup/chain routines
2619e346b073SAdrian Chadd  * so the link is correct.
2620e346b073SAdrian Chadd  *
2621e346b073SAdrian Chadd  * The caller must free the buffer using ath_freebuf().
2622e346b073SAdrian Chadd  */
2623e346b073SAdrian Chadd struct ath_buf *
26243f3a5dbdSAdrian Chadd ath_buf_clone(struct ath_softc *sc, struct ath_buf *bf)
2625e346b073SAdrian Chadd {
2626e346b073SAdrian Chadd 	struct ath_buf *tbf;
2627e346b073SAdrian Chadd 
2628af33d486SAdrian Chadd 	tbf = ath_getbuf(sc,
2629af33d486SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_MGMT) ?
2630af33d486SAdrian Chadd 	     ATH_BUFTYPE_MGMT : ATH_BUFTYPE_NORMAL);
2631e346b073SAdrian Chadd 	if (tbf == NULL)
2632e346b073SAdrian Chadd 		return NULL;	/* XXX failure? Why? */
2633e346b073SAdrian Chadd 
2634e346b073SAdrian Chadd 	/* Copy basics */
2635e346b073SAdrian Chadd 	tbf->bf_next = NULL;
2636e346b073SAdrian Chadd 	tbf->bf_nseg = bf->bf_nseg;
26373feffbd7SAdrian Chadd 	tbf->bf_flags = bf->bf_flags & ATH_BUF_FLAGS_CLONE;
2638e346b073SAdrian Chadd 	tbf->bf_status = bf->bf_status;
2639e346b073SAdrian Chadd 	tbf->bf_m = bf->bf_m;
2640e346b073SAdrian Chadd 	tbf->bf_node = bf->bf_node;
2641e346b073SAdrian Chadd 	/* will be setup by the chain/setup function */
2642e346b073SAdrian Chadd 	tbf->bf_lastds = NULL;
2643e346b073SAdrian Chadd 	/* for now, last == self */
2644e346b073SAdrian Chadd 	tbf->bf_last = tbf;
2645e346b073SAdrian Chadd 	tbf->bf_comp = bf->bf_comp;
2646e346b073SAdrian Chadd 
2647e346b073SAdrian Chadd 	/* NOTE: DMA segments will be setup by the setup/chain functions */
2648e346b073SAdrian Chadd 
2649e346b073SAdrian Chadd 	/* The caller has to re-init the descriptor + links */
2650e346b073SAdrian Chadd 
26513f3a5dbdSAdrian Chadd 	/*
26523f3a5dbdSAdrian Chadd 	 * Free the DMA mapping here, before we NULL the mbuf.
26533f3a5dbdSAdrian Chadd 	 * We must only call bus_dmamap_unload() once per mbuf chain
26543f3a5dbdSAdrian Chadd 	 * or behaviour is undefined.
26553f3a5dbdSAdrian Chadd 	 */
26563f3a5dbdSAdrian Chadd 	if (bf->bf_m != NULL) {
26573f3a5dbdSAdrian Chadd 		/*
26583f3a5dbdSAdrian Chadd 		 * XXX is this POSTWRITE call required?
26593f3a5dbdSAdrian Chadd 		 */
26603f3a5dbdSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
26613f3a5dbdSAdrian Chadd 		    BUS_DMASYNC_POSTWRITE);
26623f3a5dbdSAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
26633f3a5dbdSAdrian Chadd 	}
26643f3a5dbdSAdrian Chadd 
26653f3a5dbdSAdrian Chadd 	bf->bf_m = NULL;
26663f3a5dbdSAdrian Chadd 	bf->bf_node = NULL;
26673f3a5dbdSAdrian Chadd 
2668e346b073SAdrian Chadd 	/* Copy state */
2669e346b073SAdrian Chadd 	memcpy(&tbf->bf_state, &bf->bf_state, sizeof(bf->bf_state));
2670e346b073SAdrian Chadd 
2671e346b073SAdrian Chadd 	return tbf;
2672e346b073SAdrian Chadd }
2673e346b073SAdrian Chadd 
2674b8e788a5SAdrian Chadd struct ath_buf *
2675af33d486SAdrian Chadd ath_getbuf(struct ath_softc *sc, ath_buf_type_t btype)
267610ad9a77SSam Leffler {
267710ad9a77SSam Leffler 	struct ath_buf *bf;
267810ad9a77SSam Leffler 
267910ad9a77SSam Leffler 	ATH_TXBUF_LOCK(sc);
2680af33d486SAdrian Chadd 	bf = _ath_getbuf_locked(sc, btype);
2681af33d486SAdrian Chadd 	/*
2682af33d486SAdrian Chadd 	 * If a mgmt buffer was requested but we're out of those,
2683af33d486SAdrian Chadd 	 * try requesting a normal one.
2684af33d486SAdrian Chadd 	 */
2685af33d486SAdrian Chadd 	if (bf == NULL && btype == ATH_BUFTYPE_MGMT)
2686af33d486SAdrian Chadd 		bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL);
2687e4e7938aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
268810ad9a77SSam Leffler 	if (bf == NULL) {
268910ad9a77SSam Leffler 		struct ifnet *ifp = sc->sc_ifp;
269010ad9a77SSam Leffler 
269110ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: stop queue\n", __func__);
269210ad9a77SSam Leffler 		sc->sc_stats.ast_tx_qstop++;
2693e4e7938aSAdrian Chadd 		IF_LOCK(&ifp->if_snd);
269410ad9a77SSam Leffler 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2695e4e7938aSAdrian Chadd 		IF_UNLOCK(&ifp->if_snd);
269610ad9a77SSam Leffler 	}
269710ad9a77SSam Leffler 	return bf;
269810ad9a77SSam Leffler }
269910ad9a77SSam Leffler 
27008e739394SAdrian Chadd static void
2701cd7dffd0SAdrian Chadd ath_qflush(struct ifnet *ifp)
27025591b213SSam Leffler {
27035591b213SSam Leffler 
2704cd7dffd0SAdrian Chadd 	/* XXX TODO */
27058e739394SAdrian Chadd }
27068e739394SAdrian Chadd 
27077dcb2beaSAdrian Chadd /*
2708cd7dffd0SAdrian Chadd  * Transmit a single frame.
2709cd7dffd0SAdrian Chadd  *
2710cd7dffd0SAdrian Chadd  * net80211 will free the node reference if the transmit
2711cd7dffd0SAdrian Chadd  * fails, so don't free the node reference here.
27127dcb2beaSAdrian Chadd  */
2713cd7dffd0SAdrian Chadd static int
2714cd7dffd0SAdrian Chadd ath_transmit(struct ifnet *ifp, struct mbuf *m)
2715cd7dffd0SAdrian Chadd {
2716cd7dffd0SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
2717cd7dffd0SAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
2718cd7dffd0SAdrian Chadd 	struct ieee80211_node *ni;
2719cd7dffd0SAdrian Chadd 	struct mbuf *next;
2720cd7dffd0SAdrian Chadd 	struct ath_buf *bf;
2721cd7dffd0SAdrian Chadd 	ath_bufhead frags;
2722cd7dffd0SAdrian Chadd 	int retval = 0;
2723cd7dffd0SAdrian Chadd 
2724cd7dffd0SAdrian Chadd 	/*
2725cd7dffd0SAdrian Chadd 	 * Tell the reset path that we're currently transmitting.
2726cd7dffd0SAdrian Chadd 	 */
2727cd7dffd0SAdrian Chadd 	ATH_PCU_LOCK(sc);
2728cd7dffd0SAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
272983bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
2730cd7dffd0SAdrian Chadd 		    "%s: sc_inreset_cnt > 0; bailing\n", __func__);
2731cd7dffd0SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2732cd7dffd0SAdrian Chadd 		IF_LOCK(&ifp->if_snd);
2733cd7dffd0SAdrian Chadd 		sc->sc_stats.ast_tx_qstop++;
2734cd7dffd0SAdrian Chadd 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2735cd7dffd0SAdrian Chadd 		IF_UNLOCK(&ifp->if_snd);
2736cd7dffd0SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TX, 0, "ath_start_task: OACTIVE, finish");
2737cd7dffd0SAdrian Chadd 		return (ENOBUFS);	/* XXX should be EINVAL or? */
2738cd7dffd0SAdrian Chadd 	}
2739cd7dffd0SAdrian Chadd 	sc->sc_txstart_cnt++;
2740cd7dffd0SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2741cd7dffd0SAdrian Chadd 
2742cd7dffd0SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_transmit: start");
2743cd7dffd0SAdrian Chadd 	/*
2744cd7dffd0SAdrian Chadd 	 * Grab the TX lock - it's ok to do this here; we haven't
2745cd7dffd0SAdrian Chadd 	 * yet started transmitting.
2746cd7dffd0SAdrian Chadd 	 */
2747cd7dffd0SAdrian Chadd 	ATH_TX_LOCK(sc);
2748cd7dffd0SAdrian Chadd 
2749cd7dffd0SAdrian Chadd 	/*
2750cd7dffd0SAdrian Chadd 	 * Node reference, if there's one.
2751cd7dffd0SAdrian Chadd 	 */
27527dcb2beaSAdrian Chadd 	ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
27537dcb2beaSAdrian Chadd 
27547dcb2beaSAdrian Chadd 	/*
27557dcb2beaSAdrian Chadd 	 * Enforce how deep a node queue can get.
27567dcb2beaSAdrian Chadd 	 *
27577dcb2beaSAdrian Chadd 	 * XXX it would be nicer if we kept an mbuf queue per
27587dcb2beaSAdrian Chadd 	 * node and only whacked them into ath_bufs when we
27597dcb2beaSAdrian Chadd 	 * are ready to schedule some traffic from them.
27607dcb2beaSAdrian Chadd 	 * .. that may come later.
27617dcb2beaSAdrian Chadd 	 *
27627dcb2beaSAdrian Chadd 	 * XXX we should also track the per-node hardware queue
27637dcb2beaSAdrian Chadd 	 * depth so it is easy to limit the _SUM_ of the swq and
27647dcb2beaSAdrian Chadd 	 * hwq frames.  Since we only schedule two HWQ frames
27657dcb2beaSAdrian Chadd 	 * at a time, this should be OK for now.
27667dcb2beaSAdrian Chadd 	 */
27677dcb2beaSAdrian Chadd 	if ((!(m->m_flags & M_EAPOL)) &&
27687dcb2beaSAdrian Chadd 	    (ATH_NODE(ni)->an_swq_depth > sc->sc_txq_node_maxdepth)) {
27697dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nodeq_overflow++;
27707dcb2beaSAdrian Chadd 		m_freem(m);
27717dcb2beaSAdrian Chadd 		m = NULL;
2772cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
2773cd7dffd0SAdrian Chadd 		goto finish;
27747dcb2beaSAdrian Chadd 	}
27757dcb2beaSAdrian Chadd 
27767dcb2beaSAdrian Chadd 	/*
27777dcb2beaSAdrian Chadd 	 * Check how many TX buffers are available.
27787dcb2beaSAdrian Chadd 	 *
27797dcb2beaSAdrian Chadd 	 * If this is for non-EAPOL traffic, just leave some
27807dcb2beaSAdrian Chadd 	 * space free in order for buffer cloning and raw
27817dcb2beaSAdrian Chadd 	 * frame transmission to occur.
27827dcb2beaSAdrian Chadd 	 *
27837dcb2beaSAdrian Chadd 	 * If it's for EAPOL traffic, ignore this for now.
27847dcb2beaSAdrian Chadd 	 * Management traffic will be sent via the raw transmit
27857dcb2beaSAdrian Chadd 	 * method which bypasses this check.
27867dcb2beaSAdrian Chadd 	 *
27877dcb2beaSAdrian Chadd 	 * This is needed to ensure that EAPOL frames during
27887dcb2beaSAdrian Chadd 	 * (re) keying have a chance to go out.
27897dcb2beaSAdrian Chadd 	 *
27907dcb2beaSAdrian Chadd 	 * See kern/138379 for more information.
27917dcb2beaSAdrian Chadd 	 */
27927dcb2beaSAdrian Chadd 	if ((!(m->m_flags & M_EAPOL)) &&
27937dcb2beaSAdrian Chadd 	    (sc->sc_txbuf_cnt <= sc->sc_txq_data_minfree)) {
27947dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
2795cd7dffd0SAdrian Chadd 		m_freem(m);
27967dcb2beaSAdrian Chadd 		m = NULL;
2797cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
2798cd7dffd0SAdrian Chadd 		goto finish;
279923ced6c1SAdrian Chadd 	}
280023ced6c1SAdrian Chadd 
28015591b213SSam Leffler 	/*
28025591b213SSam Leffler 	 * Grab a TX buffer and associated resources.
28037dcb2beaSAdrian Chadd 	 *
28047dcb2beaSAdrian Chadd 	 * If it's an EAPOL frame, allocate a MGMT ath_buf.
28057dcb2beaSAdrian Chadd 	 * That way even with temporary buffer exhaustion due to
28067dcb2beaSAdrian Chadd 	 * the data path doesn't leave us without the ability
28077dcb2beaSAdrian Chadd 	 * to transmit management frames.
28087dcb2beaSAdrian Chadd 	 *
28097dcb2beaSAdrian Chadd 	 * Otherwise allocate a normal buffer.
28105591b213SSam Leffler 	 */
28117dcb2beaSAdrian Chadd 	if (m->m_flags & M_EAPOL)
28127dcb2beaSAdrian Chadd 		bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT);
28137dcb2beaSAdrian Chadd 	else
2814af33d486SAdrian Chadd 		bf = ath_getbuf(sc, ATH_BUFTYPE_NORMAL);
28151a85141aSAdrian Chadd 
28167dcb2beaSAdrian Chadd 	if (bf == NULL) {
28177dcb2beaSAdrian Chadd 		/*
2818cd7dffd0SAdrian Chadd 		 * If we failed to allocate a buffer, fail.
28197dcb2beaSAdrian Chadd 		 *
28207dcb2beaSAdrian Chadd 		 * We shouldn't fail normally, due to the check
28217dcb2beaSAdrian Chadd 		 * above.
28227dcb2beaSAdrian Chadd 		 */
28237dcb2beaSAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
28247dcb2beaSAdrian Chadd 		IF_LOCK(&ifp->if_snd);
28257dcb2beaSAdrian Chadd 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
28267dcb2beaSAdrian Chadd 		IF_UNLOCK(&ifp->if_snd);
2827cd7dffd0SAdrian Chadd 		m_freem(m);
28287dcb2beaSAdrian Chadd 		m = NULL;
2829cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
2830cd7dffd0SAdrian Chadd 		goto finish;
2831b032f27cSSam Leffler 	}
28327dcb2beaSAdrian Chadd 
2833cd7dffd0SAdrian Chadd 	/*
2834cd7dffd0SAdrian Chadd 	 * At this point we have a buffer; so we need to free it
2835cd7dffd0SAdrian Chadd 	 * if we hit any error conditions.
2836cd7dffd0SAdrian Chadd 	 */
28377dcb2beaSAdrian Chadd 
283868e8e04eSSam Leffler 	/*
283968e8e04eSSam Leffler 	 * Check for fragmentation.  If this frame
284068e8e04eSSam Leffler 	 * has been broken up verify we have enough
284168e8e04eSSam Leffler 	 * buffers to send all the fragments so all
284268e8e04eSSam Leffler 	 * go out or none...
284368e8e04eSSam Leffler 	 */
28446b349e5aSAdrian Chadd 	TAILQ_INIT(&frags);
28451a85141aSAdrian Chadd 	if ((m->m_flags & M_FRAG) &&
28461a85141aSAdrian Chadd 	    !ath_txfrag_setup(sc, &frags, m, ni)) {
284768e8e04eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT,
284868e8e04eSSam Leffler 		    "%s: out of txfrag buffers\n", __func__);
284936c6be9aSSam Leffler 		sc->sc_stats.ast_tx_nofrag++;
28509cb93076SSam Leffler 		ifp->if_oerrors++;
28511a85141aSAdrian Chadd 		ath_freetx(m);
285268e8e04eSSam Leffler 		goto bad;
285368e8e04eSSam Leffler 	}
2854cd7dffd0SAdrian Chadd 
2855cd7dffd0SAdrian Chadd 	/*
2856cd7dffd0SAdrian Chadd 	 * At this point if we have any TX fragments, then we will
2857cd7dffd0SAdrian Chadd 	 * have bumped the node reference once for each of those.
2858cd7dffd0SAdrian Chadd 	 */
2859cd7dffd0SAdrian Chadd 
2860cd7dffd0SAdrian Chadd 	/*
2861cd7dffd0SAdrian Chadd 	 * XXX Is there anything actually _enforcing_ that the
2862cd7dffd0SAdrian Chadd 	 * fragments are being transmitted in one hit, rather than
2863cd7dffd0SAdrian Chadd 	 * being interleaved with other transmissions on that
2864cd7dffd0SAdrian Chadd 	 * hardware queue?
2865cd7dffd0SAdrian Chadd 	 *
2866cd7dffd0SAdrian Chadd 	 * The ATH TX output lock is the only thing serialising this
2867cd7dffd0SAdrian Chadd 	 * right now.
2868cd7dffd0SAdrian Chadd 	 */
2869cd7dffd0SAdrian Chadd 
2870cd7dffd0SAdrian Chadd 	/*
2871cd7dffd0SAdrian Chadd 	 * Calculate the "next fragment" length field in ath_buf
2872cd7dffd0SAdrian Chadd 	 * in order to let the transmit path know enough about
2873cd7dffd0SAdrian Chadd 	 * what to next write to the hardware.
2874cd7dffd0SAdrian Chadd 	 */
2875cd7dffd0SAdrian Chadd 	if (m->m_flags & M_FRAG) {
2876cd7dffd0SAdrian Chadd 		struct ath_buf *fbf = bf;
2877cd7dffd0SAdrian Chadd 		struct ath_buf *n_fbf = NULL;
2878cd7dffd0SAdrian Chadd 		struct mbuf *fm = m->m_nextpkt;
2879cd7dffd0SAdrian Chadd 
2880cd7dffd0SAdrian Chadd 		/*
2881cd7dffd0SAdrian Chadd 		 * We need to walk the list of fragments and set
2882cd7dffd0SAdrian Chadd 		 * the next size to the following buffer.
2883cd7dffd0SAdrian Chadd 		 * However, the first buffer isn't in the frag
2884cd7dffd0SAdrian Chadd 		 * list, so we have to do some gymnastics here.
2885cd7dffd0SAdrian Chadd 		 */
2886cd7dffd0SAdrian Chadd 		TAILQ_FOREACH(n_fbf, &frags, bf_list) {
2887cd7dffd0SAdrian Chadd 			fbf->bf_nextfraglen = fm->m_pkthdr.len;
2888cd7dffd0SAdrian Chadd 			fbf = n_fbf;
2889cd7dffd0SAdrian Chadd 			fm = fm->m_nextpkt;
2890cd7dffd0SAdrian Chadd 		}
2891cd7dffd0SAdrian Chadd 	}
2892cd7dffd0SAdrian Chadd 
2893cd7dffd0SAdrian Chadd 	/*
2894cd7dffd0SAdrian Chadd 	 * Bump the ifp output counter.
2895cd7dffd0SAdrian Chadd 	 *
2896cd7dffd0SAdrian Chadd 	 * XXX should use atomics?
2897cd7dffd0SAdrian Chadd 	 */
28981a85141aSAdrian Chadd 	ifp->if_opackets++;
28991a85141aSAdrian Chadd nextfrag:
290068e8e04eSSam Leffler 	/*
29011a85141aSAdrian Chadd 	 * Pass the frame to the h/w for transmission.
29021a85141aSAdrian Chadd 	 * Fragmented frames have each frag chained together
29031a85141aSAdrian Chadd 	 * with m_nextpkt.  We know there are sufficient ath_buf's
29041a85141aSAdrian Chadd 	 * to send all the frags because of work done by
29051a85141aSAdrian Chadd 	 * ath_txfrag_setup.  We leave m_nextpkt set while
29061a85141aSAdrian Chadd 	 * calling ath_tx_start so it can use it to extend the
29071a85141aSAdrian Chadd 	 * the tx duration to cover the subsequent frag and
29081a85141aSAdrian Chadd 	 * so it can reclaim all the mbufs in case of an error;
29091a85141aSAdrian Chadd 	 * ath_tx_start clears m_nextpkt once it commits to
29101a85141aSAdrian Chadd 	 * handing the frame to the hardware.
2911cd7dffd0SAdrian Chadd 	 *
2912cd7dffd0SAdrian Chadd 	 * Note: if this fails, then the mbufs are freed but
2913cd7dffd0SAdrian Chadd 	 * not the node reference.
291468e8e04eSSam Leffler 	 */
29151a85141aSAdrian Chadd 	next = m->m_nextpkt;
29161a85141aSAdrian Chadd 	if (ath_tx_start(sc, ni, bf, m)) {
29175591b213SSam Leffler bad:
29181a85141aSAdrian Chadd 		ifp->if_oerrors++;
29191a85141aSAdrian Chadd reclaim:
292068e8e04eSSam Leffler 		bf->bf_m = NULL;
292168e8e04eSSam Leffler 		bf->bf_node = NULL;
2922c42a7b7eSSam Leffler 		ATH_TXBUF_LOCK(sc);
2923e1a50456SAdrian Chadd 		ath_returnbuf_head(sc, bf);
2924cd7dffd0SAdrian Chadd 		/*
2925cd7dffd0SAdrian Chadd 		 * Free the rest of the node references and
2926cd7dffd0SAdrian Chadd 		 * buffers for the fragment list.
2927cd7dffd0SAdrian Chadd 		 */
292868e8e04eSSam Leffler 		ath_txfrag_cleanup(sc, &frags, ni);
2929c42a7b7eSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
2930cd7dffd0SAdrian Chadd 		retval = ENOBUFS;
2931cd7dffd0SAdrian Chadd 		goto finish;
29321a85141aSAdrian Chadd 	}
29331a85141aSAdrian Chadd 
2934548a605dSAdrian Chadd 	/*
2935548a605dSAdrian Chadd 	 * Check here if the node is in power save state.
2936548a605dSAdrian Chadd 	 */
2937548a605dSAdrian Chadd 	ath_tx_update_tim(sc, ni, 1);
2938548a605dSAdrian Chadd 
29391a85141aSAdrian Chadd 	if (next != NULL) {
294068e8e04eSSam Leffler 		/*
29411a85141aSAdrian Chadd 		 * Beware of state changing between frags.
29421a85141aSAdrian Chadd 		 * XXX check sta power-save state?
294368e8e04eSSam Leffler 		 */
29441a85141aSAdrian Chadd 		if (ni->ni_vap->iv_state != IEEE80211_S_RUN) {
2945c5239edbSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
29461a85141aSAdrian Chadd 			    "%s: flush fragmented packet, state %s\n",
29471a85141aSAdrian Chadd 			    __func__,
29481a85141aSAdrian Chadd 			    ieee80211_state_name[ni->ni_vap->iv_state]);
2949a91ab3c0SAdrian Chadd 			/* XXX dmamap */
29501a85141aSAdrian Chadd 			ath_freetx(next);
29511a85141aSAdrian Chadd 			goto reclaim;
2952c5239edbSAdrian Chadd 		}
29531a85141aSAdrian Chadd 		m = next;
29541a85141aSAdrian Chadd 		bf = TAILQ_FIRST(&frags);
29551a85141aSAdrian Chadd 		KASSERT(bf != NULL, ("no buf for txfrag"));
29561a85141aSAdrian Chadd 		TAILQ_REMOVE(&frags, bf, bf_list);
29571a85141aSAdrian Chadd 		goto nextfrag;
2958c5239edbSAdrian Chadd 	}
2959c5239edbSAdrian Chadd 
2960cd7dffd0SAdrian Chadd 	/*
2961cd7dffd0SAdrian Chadd 	 * Bump watchdog timer.
2962cd7dffd0SAdrian Chadd 	 */
29631a85141aSAdrian Chadd 	sc->sc_wd_timer = 5;
2964cd7dffd0SAdrian Chadd 
2965cd7dffd0SAdrian Chadd finish:
2966cd7dffd0SAdrian Chadd 	ATH_TX_UNLOCK(sc);
2967cd7dffd0SAdrian Chadd 
2968cd7dffd0SAdrian Chadd 	/*
2969cd7dffd0SAdrian Chadd 	 * Finished transmitting!
2970cd7dffd0SAdrian Chadd 	 */
2971cd7dffd0SAdrian Chadd 	ATH_PCU_LOCK(sc);
2972cd7dffd0SAdrian Chadd 	sc->sc_txstart_cnt--;
2973cd7dffd0SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2974cd7dffd0SAdrian Chadd 
2975cd7dffd0SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_transmit: finished");
2976cd7dffd0SAdrian Chadd 
2977cd7dffd0SAdrian Chadd 	return (retval);
29785591b213SSam Leffler }
2979cd7dffd0SAdrian Chadd 
29805591b213SSam Leffler static int
29815591b213SSam Leffler ath_media_change(struct ifnet *ifp)
29825591b213SSam Leffler {
2983b032f27cSSam Leffler 	int error = ieee80211_media_change(ifp);
2984b032f27cSSam Leffler 	/* NB: only the fixed rate can change and that doesn't need a reset */
2985b032f27cSSam Leffler 	return (error == ENETRESET ? 0 : error);
29865591b213SSam Leffler }
29875591b213SSam Leffler 
2988c42a7b7eSSam Leffler /*
2989c42a7b7eSSam Leffler  * Block/unblock tx+rx processing while a key change is done.
2990c42a7b7eSSam Leffler  * We assume the caller serializes key management operations
2991c42a7b7eSSam Leffler  * so we only need to worry about synchronization with other
2992c42a7b7eSSam Leffler  * uses that originate in the driver.
2993c42a7b7eSSam Leffler  */
2994c42a7b7eSSam Leffler static void
2995b032f27cSSam Leffler ath_key_update_begin(struct ieee80211vap *vap)
2996c42a7b7eSSam Leffler {
2997b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2998c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2999c42a7b7eSSam Leffler 
3000c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
3001b032f27cSSam Leffler 	taskqueue_block(sc->sc_tq);
3002c42a7b7eSSam Leffler 	IF_LOCK(&ifp->if_snd);		/* NB: doesn't block mgmt frames */
3003c42a7b7eSSam Leffler }
3004c42a7b7eSSam Leffler 
3005c42a7b7eSSam Leffler static void
3006b032f27cSSam Leffler ath_key_update_end(struct ieee80211vap *vap)
3007c42a7b7eSSam Leffler {
3008b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
3009c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
3010c42a7b7eSSam Leffler 
3011c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
3012c42a7b7eSSam Leffler 	IF_UNLOCK(&ifp->if_snd);
3013b032f27cSSam Leffler 	taskqueue_unblock(sc->sc_tq);
3014c42a7b7eSSam Leffler }
30155591b213SSam Leffler 
3016b032f27cSSam Leffler static void
3017b032f27cSSam Leffler ath_update_promisc(struct ifnet *ifp)
3018b032f27cSSam Leffler {
3019b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
3020b032f27cSSam Leffler 	u_int32_t rfilt;
3021b032f27cSSam Leffler 
3022b032f27cSSam Leffler 	/* configure rx filter */
3023b032f27cSSam Leffler 	rfilt = ath_calcrxfilter(sc);
3024b032f27cSSam Leffler 	ath_hal_setrxfilter(sc->sc_ah, rfilt);
3025b032f27cSSam Leffler 
3026b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x\n", __func__, rfilt);
3027b032f27cSSam Leffler }
3028b032f27cSSam Leffler 
3029b032f27cSSam Leffler static void
3030b032f27cSSam Leffler ath_update_mcast(struct ifnet *ifp)
3031b032f27cSSam Leffler {
3032b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
3033b032f27cSSam Leffler 	u_int32_t mfilt[2];
3034b032f27cSSam Leffler 
3035b032f27cSSam Leffler 	/* calculate and install multicast filter */
3036b032f27cSSam Leffler 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
3037b032f27cSSam Leffler 		struct ifmultiaddr *ifma;
3038b032f27cSSam Leffler 		/*
3039b032f27cSSam Leffler 		 * Merge multicast addresses to form the hardware filter.
3040b032f27cSSam Leffler 		 */
3041b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = 0;
3042eb956cd0SRobert Watson 		if_maddr_rlock(ifp);	/* XXX need some fiddling to remove? */
3043b032f27cSSam Leffler 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3044b032f27cSSam Leffler 			caddr_t dl;
3045b032f27cSSam Leffler 			u_int32_t val;
3046b032f27cSSam Leffler 			u_int8_t pos;
3047b032f27cSSam Leffler 
3048b032f27cSSam Leffler 			/* calculate XOR of eight 6bit values */
3049b032f27cSSam Leffler 			dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
3050b032f27cSSam Leffler 			val = LE_READ_4(dl + 0);
3051b032f27cSSam Leffler 			pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
3052b032f27cSSam Leffler 			val = LE_READ_4(dl + 3);
3053b032f27cSSam Leffler 			pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
3054b032f27cSSam Leffler 			pos &= 0x3f;
3055b032f27cSSam Leffler 			mfilt[pos / 32] |= (1 << (pos % 32));
3056b032f27cSSam Leffler 		}
3057eb956cd0SRobert Watson 		if_maddr_runlock(ifp);
3058b032f27cSSam Leffler 	} else
3059b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = ~0;
3060b032f27cSSam Leffler 	ath_hal_setmcastfilter(sc->sc_ah, mfilt[0], mfilt[1]);
3061b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: MC filter %08x:%08x\n",
3062b032f27cSSam Leffler 		__func__, mfilt[0], mfilt[1]);
30634bc0e754SSam Leffler }
30644bc0e754SSam Leffler 
3065e60c4fc2SAdrian Chadd void
30665591b213SSam Leffler ath_mode_init(struct ath_softc *sc)
30675591b213SSam Leffler {
3068fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
3069b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3070b032f27cSSam Leffler 	u_int32_t rfilt;
30715591b213SSam Leffler 
30724bc0e754SSam Leffler 	/* configure rx filter */
307368e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
30744bc0e754SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
30754bc0e754SSam Leffler 
30765591b213SSam Leffler 	/* configure operational mode */
3077c42a7b7eSSam Leffler 	ath_hal_setopmode(ah);
3078c42a7b7eSSam Leffler 
30793d184db2SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_STATE | ATH_DEBUG_MODE,
30803d184db2SAdrian Chadd 	    "%s: ah=%p, ifp=%p, if_addr=%p\n",
30813d184db2SAdrian Chadd 	    __func__,
30823d184db2SAdrian Chadd 	    ah,
30833d184db2SAdrian Chadd 	    ifp,
30843d184db2SAdrian Chadd 	    (ifp == NULL) ? NULL : ifp->if_addr);
30853d184db2SAdrian Chadd 
308629aca940SSam Leffler 	/* handle any link-level address change */
308729aca940SSam Leffler 	ath_hal_setmac(ah, IF_LLADDR(ifp));
30885591b213SSam Leffler 
30895591b213SSam Leffler 	/* calculate and install multicast filter */
3090b032f27cSSam Leffler 	ath_update_mcast(ifp);
30915591b213SSam Leffler }
30925591b213SSam Leffler 
3093c42a7b7eSSam Leffler /*
3094c42a7b7eSSam Leffler  * Set the slot time based on the current setting.
3095c42a7b7eSSam Leffler  */
3096ba5c15d9SAdrian Chadd void
3097c42a7b7eSSam Leffler ath_setslottime(struct ath_softc *sc)
3098c42a7b7eSSam Leffler {
3099b032f27cSSam Leffler 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3100c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3101aaa70f2fSSam Leffler 	u_int usec;
3102c42a7b7eSSam Leffler 
3103aaa70f2fSSam Leffler 	if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan))
3104aaa70f2fSSam Leffler 		usec = 13;
3105aaa70f2fSSam Leffler 	else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan))
3106aaa70f2fSSam Leffler 		usec = 21;
3107724c193aSSam Leffler 	else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) {
3108724c193aSSam Leffler 		/* honor short/long slot time only in 11g */
3109724c193aSSam Leffler 		/* XXX shouldn't honor on pure g or turbo g channel */
3110724c193aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
3111aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_9;
3112aaa70f2fSSam Leffler 		else
3113aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_20;
3114724c193aSSam Leffler 	} else
3115724c193aSSam Leffler 		usec = HAL_SLOT_TIME_9;
3116aaa70f2fSSam Leffler 
3117aaa70f2fSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
3118aaa70f2fSSam Leffler 	    "%s: chan %u MHz flags 0x%x %s slot, %u usec\n",
3119aaa70f2fSSam Leffler 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
3120aaa70f2fSSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec);
3121aaa70f2fSSam Leffler 
3122aaa70f2fSSam Leffler 	ath_hal_setslottime(ah, usec);
3123c42a7b7eSSam Leffler 	sc->sc_updateslot = OK;
3124c42a7b7eSSam Leffler }
3125c42a7b7eSSam Leffler 
3126c42a7b7eSSam Leffler /*
3127c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update the
3128c42a7b7eSSam Leffler  * slot time based on the current setting.
3129c42a7b7eSSam Leffler  */
3130c42a7b7eSSam Leffler static void
3131c42a7b7eSSam Leffler ath_updateslot(struct ifnet *ifp)
3132c42a7b7eSSam Leffler {
3133c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
3134b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
3135c42a7b7eSSam Leffler 
3136c42a7b7eSSam Leffler 	/*
3137c42a7b7eSSam Leffler 	 * When not coordinating the BSS, change the hardware
3138c42a7b7eSSam Leffler 	 * immediately.  For other operation we defer the change
3139c42a7b7eSSam Leffler 	 * until beacon updates have propagated to the stations.
3140c42a7b7eSSam Leffler 	 */
314159aa14a9SRui Paulo 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
314259aa14a9SRui Paulo 	    ic->ic_opmode == IEEE80211_M_MBSS)
3143c42a7b7eSSam Leffler 		sc->sc_updateslot = UPDATE;
3144c42a7b7eSSam Leffler 	else
3145c42a7b7eSSam Leffler 		ath_setslottime(sc);
3146c42a7b7eSSam Leffler }
3147c42a7b7eSSam Leffler 
3148c42a7b7eSSam Leffler /*
3149622b3fd2SSam Leffler  * Append the contents of src to dst; both queues
3150622b3fd2SSam Leffler  * are assumed to be locked.
3151622b3fd2SSam Leffler  */
3152ba5c15d9SAdrian Chadd void
3153622b3fd2SSam Leffler ath_txqmove(struct ath_txq *dst, struct ath_txq *src)
3154622b3fd2SSam Leffler {
3155e86fd7a7SAdrian Chadd 
3156b837332dSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(src);
3157b837332dSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(dst);
3158b837332dSAdrian Chadd 
31596b349e5aSAdrian Chadd 	TAILQ_CONCAT(&dst->axq_q, &src->axq_q, bf_list);
3160622b3fd2SSam Leffler 	dst->axq_link = src->axq_link;
3161622b3fd2SSam Leffler 	src->axq_link = NULL;
3162622b3fd2SSam Leffler 	dst->axq_depth += src->axq_depth;
31636edf1dc7SAdrian Chadd 	dst->axq_aggr_depth += src->axq_aggr_depth;
3164622b3fd2SSam Leffler 	src->axq_depth = 0;
31656edf1dc7SAdrian Chadd 	src->axq_aggr_depth = 0;
3166622b3fd2SSam Leffler }
3167622b3fd2SSam Leffler 
3168622b3fd2SSam Leffler /*
3169d52f7132SAdrian Chadd  * Reset the hardware, with no loss.
3170d52f7132SAdrian Chadd  *
3171d52f7132SAdrian Chadd  * This can't be used for a general case reset.
3172d52f7132SAdrian Chadd  */
3173d52f7132SAdrian Chadd static void
3174d52f7132SAdrian Chadd ath_reset_proc(void *arg, int pending)
3175d52f7132SAdrian Chadd {
3176d52f7132SAdrian Chadd 	struct ath_softc *sc = arg;
3177d52f7132SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
3178d52f7132SAdrian Chadd 
3179d52f7132SAdrian Chadd #if 0
3180d52f7132SAdrian Chadd 	if_printf(ifp, "%s: resetting\n", __func__);
3181d52f7132SAdrian Chadd #endif
3182d52f7132SAdrian Chadd 	ath_reset(ifp, ATH_RESET_NOLOSS);
3183d52f7132SAdrian Chadd }
3184d52f7132SAdrian Chadd 
3185d52f7132SAdrian Chadd /*
3186c42a7b7eSSam Leffler  * Reset the hardware after detecting beacons have stopped.
3187c42a7b7eSSam Leffler  */
3188c42a7b7eSSam Leffler static void
3189c42a7b7eSSam Leffler ath_bstuck_proc(void *arg, int pending)
3190c42a7b7eSSam Leffler {
3191c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
3192fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
319316d4de92SAdrian Chadd 	uint32_t hangs = 0;
319416d4de92SAdrian Chadd 
319516d4de92SAdrian Chadd 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0)
319616d4de92SAdrian Chadd 		if_printf(ifp, "bb hang detected (0x%x)\n", hangs);
3197c42a7b7eSSam Leffler 
3198370f81faSAdrian Chadd #ifdef	ATH_DEBUG_ALQ
3199370f81faSAdrian Chadd 	if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_STUCK_BEACON))
3200370f81faSAdrian Chadd 		if_ath_alq_post(&sc->sc_alq, ATH_ALQ_STUCK_BEACON, 0, NULL);
3201370f81faSAdrian Chadd #endif
3202370f81faSAdrian Chadd 
3203c42a7b7eSSam Leffler 	if_printf(ifp, "stuck beacon; resetting (bmiss count %u)\n",
3204c42a7b7eSSam Leffler 		sc->sc_bmisscount);
3205c2e34459SSam Leffler 	sc->sc_stats.ast_bstuck++;
320616d4de92SAdrian Chadd 	/*
320716d4de92SAdrian Chadd 	 * This assumes that there's no simultaneous channel mode change
320816d4de92SAdrian Chadd 	 * occuring.
320916d4de92SAdrian Chadd 	 */
3210517526efSAdrian Chadd 	ath_reset(ifp, ATH_RESET_NOLOSS);
3211c42a7b7eSSam Leffler }
3212c42a7b7eSSam Leffler 
32135591b213SSam Leffler static void
32145591b213SSam Leffler ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
32155591b213SSam Leffler {
32165591b213SSam Leffler 	bus_addr_t *paddr = (bus_addr_t*) arg;
3217d77367bfSSam Leffler 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
32185591b213SSam Leffler 	*paddr = segs->ds_addr;
32195591b213SSam Leffler }
32205591b213SSam Leffler 
3221c9f78537SAdrian Chadd /*
3222c9f78537SAdrian Chadd  * Allocate the descriptors and appropriate DMA tag/setup.
3223c9f78537SAdrian Chadd  *
3224c9f78537SAdrian Chadd  * For some situations (eg EDMA TX completion), there isn't a requirement
3225c9f78537SAdrian Chadd  * for the ath_buf entries to be allocated.
3226c9f78537SAdrian Chadd  */
32273d184db2SAdrian Chadd int
3228c9f78537SAdrian Chadd ath_descdma_alloc_desc(struct ath_softc *sc,
3229c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head,
3230b39722d6SAdrian Chadd 	const char *name, int ds_size, int ndesc)
3231c42a7b7eSSam Leffler {
3232c42a7b7eSSam Leffler #define	DS2PHYS(_dd, _ds) \
3233c42a7b7eSSam Leffler 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
323445abcd6cSAdrian Chadd #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
323545abcd6cSAdrian Chadd 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
3236fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
3237c9f78537SAdrian Chadd 	int error;
323845abcd6cSAdrian Chadd 
32391006fc0cSAdrian Chadd 	dd->dd_descsize = ds_size;
3240c42a7b7eSSam Leffler 
32413d9b1596SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
3242b39722d6SAdrian Chadd 	    "%s: %s DMA: %u desc, %d bytes per descriptor\n",
3243b39722d6SAdrian Chadd 	    __func__, name, ndesc, dd->dd_descsize);
3244c42a7b7eSSam Leffler 
3245c42a7b7eSSam Leffler 	dd->dd_name = name;
3246b39722d6SAdrian Chadd 	dd->dd_desc_len = dd->dd_descsize * ndesc;
324745abcd6cSAdrian Chadd 
324845abcd6cSAdrian Chadd 	/*
324945abcd6cSAdrian Chadd 	 * Merlin work-around:
325045abcd6cSAdrian Chadd 	 * Descriptors that cross the 4KB boundary can't be used.
325145abcd6cSAdrian Chadd 	 * Assume one skipped descriptor per 4KB page.
325245abcd6cSAdrian Chadd 	 */
325345abcd6cSAdrian Chadd 	if (! ath_hal_split4ktrans(sc->sc_ah)) {
3254b39722d6SAdrian Chadd 		int numpages = dd->dd_desc_len / 4096;
3255b39722d6SAdrian Chadd 		dd->dd_desc_len += ds_size * numpages;
325645abcd6cSAdrian Chadd 	}
3257c42a7b7eSSam Leffler 
3258c42a7b7eSSam Leffler 	/*
3259c42a7b7eSSam Leffler 	 * Setup DMA descriptor area.
3260a91ab3c0SAdrian Chadd 	 *
3261a91ab3c0SAdrian Chadd 	 * BUS_DMA_ALLOCNOW is not used; we never use bounce
3262a91ab3c0SAdrian Chadd 	 * buffers for the descriptors themselves.
3263c42a7b7eSSam Leffler 	 */
3264c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev),	/* parent */
3265c42a7b7eSSam Leffler 		       PAGE_SIZE, 0,		/* alignment, bounds */
3266c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
3267c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR,	/* highaddr */
3268c42a7b7eSSam Leffler 		       NULL, NULL,		/* filter, filterarg */
3269c42a7b7eSSam Leffler 		       dd->dd_desc_len,		/* maxsize */
3270c42a7b7eSSam Leffler 		       1,			/* nsegments */
32716ccb8ea7SSam Leffler 		       dd->dd_desc_len,		/* maxsegsize */
3272a91ab3c0SAdrian Chadd 		       0,			/* flags */
3273c42a7b7eSSam Leffler 		       NULL,			/* lockfunc */
3274c42a7b7eSSam Leffler 		       NULL,			/* lockarg */
3275c42a7b7eSSam Leffler 		       &dd->dd_dmat);
3276c42a7b7eSSam Leffler 	if (error != 0) {
3277c42a7b7eSSam Leffler 		if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name);
3278c42a7b7eSSam Leffler 		return error;
3279c42a7b7eSSam Leffler 	}
3280c42a7b7eSSam Leffler 
3281c42a7b7eSSam Leffler 	/* allocate descriptors */
3282c42a7b7eSSam Leffler 	error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
32830553a01fSSam Leffler 				 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
32840553a01fSSam Leffler 				 &dd->dd_dmamap);
3285c42a7b7eSSam Leffler 	if (error != 0) {
3286c42a7b7eSSam Leffler 		if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
3287b39722d6SAdrian Chadd 			"error %u\n", ndesc, dd->dd_name, error);
3288c42a7b7eSSam Leffler 		goto fail1;
3289c42a7b7eSSam Leffler 	}
3290c42a7b7eSSam Leffler 
3291c42a7b7eSSam Leffler 	error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
3292c42a7b7eSSam Leffler 				dd->dd_desc, dd->dd_desc_len,
3293c42a7b7eSSam Leffler 				ath_load_cb, &dd->dd_desc_paddr,
3294c42a7b7eSSam Leffler 				BUS_DMA_NOWAIT);
3295c42a7b7eSSam Leffler 	if (error != 0) {
3296c42a7b7eSSam Leffler 		if_printf(ifp, "unable to map %s descriptors, error %u\n",
3297c42a7b7eSSam Leffler 			dd->dd_name, error);
3298c42a7b7eSSam Leffler 		goto fail2;
3299c42a7b7eSSam Leffler 	}
3300c42a7b7eSSam Leffler 
3301c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
3302c9f78537SAdrian Chadd 	    __func__, dd->dd_name, (uint8_t *) dd->dd_desc,
3303c9f78537SAdrian Chadd 	    (u_long) dd->dd_desc_len, (caddr_t) dd->dd_desc_paddr,
3304c9f78537SAdrian Chadd 	    /*XXX*/ (u_long) dd->dd_desc_len);
3305c9f78537SAdrian Chadd 
3306c9f78537SAdrian Chadd 	return (0);
3307c9f78537SAdrian Chadd 
3308c9f78537SAdrian Chadd fail2:
3309c9f78537SAdrian Chadd 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3310c9f78537SAdrian Chadd fail1:
3311c9f78537SAdrian Chadd 	bus_dma_tag_destroy(dd->dd_dmat);
3312c9f78537SAdrian Chadd 	memset(dd, 0, sizeof(*dd));
3313c9f78537SAdrian Chadd 	return error;
3314c9f78537SAdrian Chadd #undef DS2PHYS
3315c9f78537SAdrian Chadd #undef ATH_DESC_4KB_BOUND_CHECK
3316c9f78537SAdrian Chadd }
3317c9f78537SAdrian Chadd 
3318c9f78537SAdrian Chadd int
3319c9f78537SAdrian Chadd ath_descdma_setup(struct ath_softc *sc,
3320c9f78537SAdrian Chadd 	struct ath_descdma *dd, ath_bufhead *head,
3321c9f78537SAdrian Chadd 	const char *name, int ds_size, int nbuf, int ndesc)
3322c9f78537SAdrian Chadd {
3323c9f78537SAdrian Chadd #define	DS2PHYS(_dd, _ds) \
3324c9f78537SAdrian Chadd 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
3325c9f78537SAdrian Chadd #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
3326c9f78537SAdrian Chadd 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
3327c9f78537SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
3328c9f78537SAdrian Chadd 	uint8_t *ds;
3329c9f78537SAdrian Chadd 	struct ath_buf *bf;
3330c9f78537SAdrian Chadd 	int i, bsize, error;
3331c9f78537SAdrian Chadd 
3332c9f78537SAdrian Chadd 	/* Allocate descriptors */
3333c9f78537SAdrian Chadd 	error = ath_descdma_alloc_desc(sc, dd, head, name, ds_size,
3334b39722d6SAdrian Chadd 	    nbuf * ndesc);
3335c9f78537SAdrian Chadd 
3336c9f78537SAdrian Chadd 	/* Assume any errors during allocation were dealt with */
3337c9f78537SAdrian Chadd 	if (error != 0) {
3338c9f78537SAdrian Chadd 		return (error);
3339c9f78537SAdrian Chadd 	}
3340c9f78537SAdrian Chadd 
3341c9f78537SAdrian Chadd 	ds = (uint8_t *) dd->dd_desc;
3342c42a7b7eSSam Leffler 
3343ebecf802SSam Leffler 	/* allocate rx buffers */
3344c42a7b7eSSam Leffler 	bsize = sizeof(struct ath_buf) * nbuf;
3345c42a7b7eSSam Leffler 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
3346c42a7b7eSSam Leffler 	if (bf == NULL) {
3347c42a7b7eSSam Leffler 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
3348c42a7b7eSSam Leffler 			dd->dd_name, bsize);
3349c42a7b7eSSam Leffler 		goto fail3;
3350c42a7b7eSSam Leffler 	}
3351c42a7b7eSSam Leffler 	dd->dd_bufptr = bf;
3352c42a7b7eSSam Leffler 
33536b349e5aSAdrian Chadd 	TAILQ_INIT(head);
33543d9b1596SAdrian Chadd 	for (i = 0; i < nbuf; i++, bf++, ds += (ndesc * dd->dd_descsize)) {
335545abcd6cSAdrian Chadd 		bf->bf_desc = (struct ath_desc *) ds;
3356c42a7b7eSSam Leffler 		bf->bf_daddr = DS2PHYS(dd, ds);
335745abcd6cSAdrian Chadd 		if (! ath_hal_split4ktrans(sc->sc_ah)) {
335845abcd6cSAdrian Chadd 			/*
335945abcd6cSAdrian Chadd 			 * Merlin WAR: Skip descriptor addresses which
336045abcd6cSAdrian Chadd 			 * cause 4KB boundary crossing along any point
336145abcd6cSAdrian Chadd 			 * in the descriptor.
336245abcd6cSAdrian Chadd 			 */
336345abcd6cSAdrian Chadd 			 if (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr,
33647ef7f613SAdrian Chadd 			     dd->dd_descsize)) {
336545abcd6cSAdrian Chadd 				/* Start at the next page */
336645abcd6cSAdrian Chadd 				ds += 0x1000 - (bf->bf_daddr & 0xFFF);
336745abcd6cSAdrian Chadd 				bf->bf_desc = (struct ath_desc *) ds;
336845abcd6cSAdrian Chadd 				bf->bf_daddr = DS2PHYS(dd, ds);
336945abcd6cSAdrian Chadd 			}
337045abcd6cSAdrian Chadd 		}
3371c42a7b7eSSam Leffler 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
3372c42a7b7eSSam Leffler 				&bf->bf_dmamap);
3373c42a7b7eSSam Leffler 		if (error != 0) {
3374c42a7b7eSSam Leffler 			if_printf(ifp, "unable to create dmamap for %s "
3375c42a7b7eSSam Leffler 				"buffer %u, error %u\n", dd->dd_name, i, error);
3376c42a7b7eSSam Leffler 			ath_descdma_cleanup(sc, dd, head);
3377c42a7b7eSSam Leffler 			return error;
3378c42a7b7eSSam Leffler 		}
33796edf1dc7SAdrian Chadd 		bf->bf_lastds = bf->bf_desc;	/* Just an initial value */
33806b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(head, bf, bf_list);
3381c42a7b7eSSam Leffler 	}
33827ef7f613SAdrian Chadd 
33837ef7f613SAdrian Chadd 	/*
33847ef7f613SAdrian Chadd 	 * XXX TODO: ensure that ds doesn't overflow the descriptor
33857ef7f613SAdrian Chadd 	 * allocation otherwise weird stuff will occur and crash your
33867ef7f613SAdrian Chadd 	 * machine.
33877ef7f613SAdrian Chadd 	 */
3388c42a7b7eSSam Leffler 	return 0;
3389c9f78537SAdrian Chadd 	/* XXX this should likely just call ath_descdma_cleanup() */
3390c42a7b7eSSam Leffler fail3:
3391c42a7b7eSSam Leffler 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3392c42a7b7eSSam Leffler 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3393c42a7b7eSSam Leffler 	bus_dma_tag_destroy(dd->dd_dmat);
3394c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3395c42a7b7eSSam Leffler 	return error;
3396c42a7b7eSSam Leffler #undef DS2PHYS
339745abcd6cSAdrian Chadd #undef ATH_DESC_4KB_BOUND_CHECK
3398c42a7b7eSSam Leffler }
3399c42a7b7eSSam Leffler 
340039abbd9bSAdrian Chadd /*
340139abbd9bSAdrian Chadd  * Allocate ath_buf entries but no descriptor contents.
340239abbd9bSAdrian Chadd  *
340339abbd9bSAdrian Chadd  * This is for RX EDMA where the descriptors are the header part of
340439abbd9bSAdrian Chadd  * the RX buffer.
340539abbd9bSAdrian Chadd  */
340639abbd9bSAdrian Chadd int
340739abbd9bSAdrian Chadd ath_descdma_setup_rx_edma(struct ath_softc *sc,
340839abbd9bSAdrian Chadd 	struct ath_descdma *dd, ath_bufhead *head,
340939abbd9bSAdrian Chadd 	const char *name, int nbuf, int rx_status_len)
341039abbd9bSAdrian Chadd {
341139abbd9bSAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
341239abbd9bSAdrian Chadd 	struct ath_buf *bf;
341339abbd9bSAdrian Chadd 	int i, bsize, error;
341439abbd9bSAdrian Chadd 
341539abbd9bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers\n",
341639abbd9bSAdrian Chadd 	    __func__, name, nbuf);
341739abbd9bSAdrian Chadd 
341839abbd9bSAdrian Chadd 	dd->dd_name = name;
341939abbd9bSAdrian Chadd 	/*
342039abbd9bSAdrian Chadd 	 * This is (mostly) purely for show.  We're not allocating any actual
342139abbd9bSAdrian Chadd 	 * descriptors here as EDMA RX has the descriptor be part
342239abbd9bSAdrian Chadd 	 * of the RX buffer.
342339abbd9bSAdrian Chadd 	 *
342439abbd9bSAdrian Chadd 	 * However, dd_desc_len is used by ath_descdma_free() to determine
342539abbd9bSAdrian Chadd 	 * whether we have already freed this DMA mapping.
342639abbd9bSAdrian Chadd 	 */
34273d9b1596SAdrian Chadd 	dd->dd_desc_len = rx_status_len * nbuf;
34283d9b1596SAdrian Chadd 	dd->dd_descsize = rx_status_len;
342939abbd9bSAdrian Chadd 
343039abbd9bSAdrian Chadd 	/* allocate rx buffers */
343139abbd9bSAdrian Chadd 	bsize = sizeof(struct ath_buf) * nbuf;
343239abbd9bSAdrian Chadd 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
343339abbd9bSAdrian Chadd 	if (bf == NULL) {
343439abbd9bSAdrian Chadd 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
343539abbd9bSAdrian Chadd 			dd->dd_name, bsize);
3436b5b60f35SAdrian Chadd 		error = ENOMEM;
343739abbd9bSAdrian Chadd 		goto fail3;
343839abbd9bSAdrian Chadd 	}
343939abbd9bSAdrian Chadd 	dd->dd_bufptr = bf;
344039abbd9bSAdrian Chadd 
344139abbd9bSAdrian Chadd 	TAILQ_INIT(head);
344239abbd9bSAdrian Chadd 	for (i = 0; i < nbuf; i++, bf++) {
344339abbd9bSAdrian Chadd 		bf->bf_desc = NULL;
344439abbd9bSAdrian Chadd 		bf->bf_daddr = 0;
344539abbd9bSAdrian Chadd 		bf->bf_lastds = NULL;	/* Just an initial value */
344639abbd9bSAdrian Chadd 
344739abbd9bSAdrian Chadd 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
344839abbd9bSAdrian Chadd 				&bf->bf_dmamap);
344939abbd9bSAdrian Chadd 		if (error != 0) {
345039abbd9bSAdrian Chadd 			if_printf(ifp, "unable to create dmamap for %s "
345139abbd9bSAdrian Chadd 				"buffer %u, error %u\n", dd->dd_name, i, error);
345239abbd9bSAdrian Chadd 			ath_descdma_cleanup(sc, dd, head);
345339abbd9bSAdrian Chadd 			return error;
345439abbd9bSAdrian Chadd 		}
345539abbd9bSAdrian Chadd 		TAILQ_INSERT_TAIL(head, bf, bf_list);
345639abbd9bSAdrian Chadd 	}
345739abbd9bSAdrian Chadd 	return 0;
345839abbd9bSAdrian Chadd fail3:
345939abbd9bSAdrian Chadd 	memset(dd, 0, sizeof(*dd));
346039abbd9bSAdrian Chadd 	return error;
346139abbd9bSAdrian Chadd }
346239abbd9bSAdrian Chadd 
34633d184db2SAdrian Chadd void
3464c42a7b7eSSam Leffler ath_descdma_cleanup(struct ath_softc *sc,
3465c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head)
3466c42a7b7eSSam Leffler {
3467c42a7b7eSSam Leffler 	struct ath_buf *bf;
3468c42a7b7eSSam Leffler 	struct ieee80211_node *ni;
3469a91ab3c0SAdrian Chadd 	int do_warning = 0;
3470c42a7b7eSSam Leffler 
34718d467c41SAdrian Chadd 	if (dd->dd_dmamap != 0) {
3472c42a7b7eSSam Leffler 		bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3473c42a7b7eSSam Leffler 		bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3474c42a7b7eSSam Leffler 		bus_dma_tag_destroy(dd->dd_dmat);
34758d467c41SAdrian Chadd 	}
3476c42a7b7eSSam Leffler 
34779ed9f02bSAdrian Chadd 	if (head != NULL) {
34786b349e5aSAdrian Chadd 		TAILQ_FOREACH(bf, head, bf_list) {
3479c42a7b7eSSam Leffler 			if (bf->bf_m) {
3480a91ab3c0SAdrian Chadd 				/*
3481a91ab3c0SAdrian Chadd 				 * XXX warn if there's buffers here.
3482a91ab3c0SAdrian Chadd 				 * XXX it should have been freed by the
3483a91ab3c0SAdrian Chadd 				 * owner!
3484a91ab3c0SAdrian Chadd 				 */
3485a91ab3c0SAdrian Chadd 
3486a91ab3c0SAdrian Chadd 				if (do_warning == 0) {
3487a91ab3c0SAdrian Chadd 					do_warning = 1;
3488a91ab3c0SAdrian Chadd 					device_printf(sc->sc_dev,
3489a91ab3c0SAdrian Chadd 					    "%s: %s: mbuf should've been"
3490a91ab3c0SAdrian Chadd 					    " unmapped/freed!\n",
3491a91ab3c0SAdrian Chadd 					    __func__,
3492a91ab3c0SAdrian Chadd 					    dd->dd_name);
3493a91ab3c0SAdrian Chadd 				}
3494a91ab3c0SAdrian Chadd 				bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
3495a91ab3c0SAdrian Chadd 				    BUS_DMASYNC_POSTREAD);
3496a91ab3c0SAdrian Chadd 				bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3497c42a7b7eSSam Leffler 				m_freem(bf->bf_m);
3498c42a7b7eSSam Leffler 				bf->bf_m = NULL;
3499c42a7b7eSSam Leffler 			}
3500c42a7b7eSSam Leffler 			if (bf->bf_dmamap != NULL) {
3501c42a7b7eSSam Leffler 				bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
3502c42a7b7eSSam Leffler 				bf->bf_dmamap = NULL;
3503c42a7b7eSSam Leffler 			}
3504c42a7b7eSSam Leffler 			ni = bf->bf_node;
3505c42a7b7eSSam Leffler 			bf->bf_node = NULL;
3506c42a7b7eSSam Leffler 			if (ni != NULL) {
3507c42a7b7eSSam Leffler 				/*
3508c42a7b7eSSam Leffler 				 * Reclaim node reference.
3509c42a7b7eSSam Leffler 				 */
3510c42a7b7eSSam Leffler 				ieee80211_free_node(ni);
3511c42a7b7eSSam Leffler 			}
3512c42a7b7eSSam Leffler 		}
35139ed9f02bSAdrian Chadd 	}
3514c42a7b7eSSam Leffler 
35159ed9f02bSAdrian Chadd 	if (head != NULL)
35166b349e5aSAdrian Chadd 		TAILQ_INIT(head);
35179ed9f02bSAdrian Chadd 
35189ed9f02bSAdrian Chadd 	if (dd->dd_bufptr != NULL)
3519c42a7b7eSSam Leffler 		free(dd->dd_bufptr, M_ATHDEV);
3520c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3521c42a7b7eSSam Leffler }
3522c42a7b7eSSam Leffler 
3523c42a7b7eSSam Leffler static int
35245591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc)
35255591b213SSam Leffler {
3526c42a7b7eSSam Leffler 	int error;
35275591b213SSam Leffler 
3528c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
352909067b6eSAdrian Chadd 		    "tx", sc->sc_tx_desclen, ath_txbuf, ATH_MAX_SCATTER);
3530c42a7b7eSSam Leffler 	if (error != 0) {
35315591b213SSam Leffler 		return error;
3532c42a7b7eSSam Leffler 	}
353323ced6c1SAdrian Chadd 	sc->sc_txbuf_cnt = ath_txbuf;
3534c42a7b7eSSam Leffler 
3535af33d486SAdrian Chadd 	error = ath_descdma_setup(sc, &sc->sc_txdma_mgmt, &sc->sc_txbuf_mgmt,
35361006fc0cSAdrian Chadd 		    "tx_mgmt", sc->sc_tx_desclen, ath_txbuf_mgmt,
35371006fc0cSAdrian Chadd 		    ATH_TXDESC);
3538af33d486SAdrian Chadd 	if (error != 0) {
3539af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3540af33d486SAdrian Chadd 		return error;
3541af33d486SAdrian Chadd 	}
3542af33d486SAdrian Chadd 
3543af33d486SAdrian Chadd 	/*
3544af33d486SAdrian Chadd 	 * XXX mark txbuf_mgmt frames with ATH_BUF_MGMT, so the
3545af33d486SAdrian Chadd 	 * flag doesn't have to be set in ath_getbuf_locked().
3546af33d486SAdrian Chadd 	 */
3547af33d486SAdrian Chadd 
3548c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
35491006fc0cSAdrian Chadd 			"beacon", sc->sc_tx_desclen, ATH_BCBUF, 1);
3550c42a7b7eSSam Leffler 	if (error != 0) {
3551af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3552af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
3553af33d486SAdrian Chadd 		    &sc->sc_txbuf_mgmt);
3554c42a7b7eSSam Leffler 		return error;
3555c42a7b7eSSam Leffler 	}
35565591b213SSam Leffler 	return 0;
35575591b213SSam Leffler }
35585591b213SSam Leffler 
35595591b213SSam Leffler static void
35605591b213SSam Leffler ath_desc_free(struct ath_softc *sc)
35615591b213SSam Leffler {
35625591b213SSam Leffler 
3563c42a7b7eSSam Leffler 	if (sc->sc_bdma.dd_desc_len != 0)
3564c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
3565c42a7b7eSSam Leffler 	if (sc->sc_txdma.dd_desc_len != 0)
3566c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3567af33d486SAdrian Chadd 	if (sc->sc_txdma_mgmt.dd_desc_len != 0)
3568af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
3569af33d486SAdrian Chadd 		    &sc->sc_txbuf_mgmt);
35705591b213SSam Leffler }
35715591b213SSam Leffler 
35725591b213SSam Leffler static struct ieee80211_node *
357338c208f8SSam Leffler ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
35745591b213SSam Leffler {
357538c208f8SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3576c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3577c42a7b7eSSam Leffler 	const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
3578c42a7b7eSSam Leffler 	struct ath_node *an;
3579c42a7b7eSSam Leffler 
3580c42a7b7eSSam Leffler 	an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
3581c42a7b7eSSam Leffler 	if (an == NULL) {
3582c42a7b7eSSam Leffler 		/* XXX stat+msg */
3583de5af704SSam Leffler 		return NULL;
35845591b213SSam Leffler 	}
3585c42a7b7eSSam Leffler 	ath_rate_node_init(sc, an);
35865591b213SSam Leffler 
35873dd85b26SAdrian Chadd 	/* Setup the mutex - there's no associd yet so set the name to NULL */
35883dd85b26SAdrian Chadd 	snprintf(an->an_name, sizeof(an->an_name), "%s: node %p",
35893dd85b26SAdrian Chadd 	    device_get_nameunit(sc->sc_dev), an);
35903dd85b26SAdrian Chadd 	mtx_init(&an->an_mtx, an->an_name, NULL, MTX_DEF);
35913dd85b26SAdrian Chadd 
3592eb6f0de0SAdrian Chadd 	/* XXX setup ath_tid */
3593eb6f0de0SAdrian Chadd 	ath_tx_tid_init(sc, an);
3594eb6f0de0SAdrian Chadd 
35959b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__, mac, ":", an);
3596c42a7b7eSSam Leffler 	return &an->an_node;
3597c42a7b7eSSam Leffler }
3598c42a7b7eSSam Leffler 
35995591b213SSam Leffler static void
36004afa805eSAdrian Chadd ath_node_cleanup(struct ieee80211_node *ni)
36014afa805eSAdrian Chadd {
36024afa805eSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
36034afa805eSAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
36044afa805eSAdrian Chadd 
36059b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__,
36069b48fb4bSAdrian Chadd 	    ni->ni_macaddr, ":", ATH_NODE(ni));
36079b48fb4bSAdrian Chadd 
36084afa805eSAdrian Chadd 	/* Cleanup ath_tid, free unused bufs, unlink bufs in TXQ */
3609eb6f0de0SAdrian Chadd 	ath_tx_node_flush(sc, ATH_NODE(ni));
36104afa805eSAdrian Chadd 	ath_rate_node_cleanup(sc, ATH_NODE(ni));
36114afa805eSAdrian Chadd 	sc->sc_node_cleanup(ni);
36124afa805eSAdrian Chadd }
36134afa805eSAdrian Chadd 
36144afa805eSAdrian Chadd static void
3615c42a7b7eSSam Leffler ath_node_free(struct ieee80211_node *ni)
36165591b213SSam Leffler {
3617c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
3618c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
36191e774079SSam Leffler 
36209b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__,
36219b48fb4bSAdrian Chadd 	    ni->ni_macaddr, ":", ATH_NODE(ni));
36223dd85b26SAdrian Chadd 	mtx_destroy(&ATH_NODE(ni)->an_mtx);
3623c42a7b7eSSam Leffler 	sc->sc_node_free(ni);
36245591b213SSam Leffler }
36255591b213SSam Leffler 
362668e8e04eSSam Leffler static void
362768e8e04eSSam Leffler ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
362868e8e04eSSam Leffler {
362968e8e04eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
363068e8e04eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
363168e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
363268e8e04eSSam Leffler 
3633b032f27cSSam Leffler 	*rssi = ic->ic_node_getrssi(ni);
363459efa8b5SSam Leffler 	if (ni->ni_chan != IEEE80211_CHAN_ANYC)
363559efa8b5SSam Leffler 		*noise = ath_hal_getchannoise(ah, ni->ni_chan);
363659efa8b5SSam Leffler 	else
363768e8e04eSSam Leffler 		*noise = -95;		/* nominally correct */
363868e8e04eSSam Leffler }
363968e8e04eSSam Leffler 
3640c42a7b7eSSam Leffler /*
3641c42a7b7eSSam Leffler  * Set the default antenna.
3642c42a7b7eSSam Leffler  */
3643e60c4fc2SAdrian Chadd void
3644c42a7b7eSSam Leffler ath_setdefantenna(struct ath_softc *sc, u_int antenna)
3645c42a7b7eSSam Leffler {
3646c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3647c42a7b7eSSam Leffler 
3648c42a7b7eSSam Leffler 	/* XXX block beacon interrupts */
3649c42a7b7eSSam Leffler 	ath_hal_setdefantenna(ah, antenna);
3650c42a7b7eSSam Leffler 	if (sc->sc_defant != antenna)
3651c42a7b7eSSam Leffler 		sc->sc_stats.ast_ant_defswitch++;
3652c42a7b7eSSam Leffler 	sc->sc_defant = antenna;
3653c42a7b7eSSam Leffler 	sc->sc_rxotherant = 0;
3654c42a7b7eSSam Leffler }
3655c42a7b7eSSam Leffler 
36565463c4a4SSam Leffler static void
3657622b3fd2SSam Leffler ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum)
3658622b3fd2SSam Leffler {
3659622b3fd2SSam Leffler 	txq->axq_qnum = qnum;
3660339ccfb3SSam Leffler 	txq->axq_ac = 0;
3661622b3fd2SSam Leffler 	txq->axq_depth = 0;
366216d4de92SAdrian Chadd 	txq->axq_aggr_depth = 0;
3663622b3fd2SSam Leffler 	txq->axq_intrcnt = 0;
3664622b3fd2SSam Leffler 	txq->axq_link = NULL;
36656b349e5aSAdrian Chadd 	txq->axq_softc = sc;
36666b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_q);
36676b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_tidq);
36683feffbd7SAdrian Chadd 	TAILQ_INIT(&txq->fifo.axq_q);
3669b837332dSAdrian Chadd 	ATH_TXQ_LOCK_INIT(sc, txq);
3670622b3fd2SSam Leffler }
3671622b3fd2SSam Leffler 
36725591b213SSam Leffler /*
3673c42a7b7eSSam Leffler  * Setup a h/w transmit queue.
36745591b213SSam Leffler  */
3675c42a7b7eSSam Leffler static struct ath_txq *
3676c42a7b7eSSam Leffler ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
3677c42a7b7eSSam Leffler {
3678c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
3679c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3680c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
3681c42a7b7eSSam Leffler 	int qnum;
3682c42a7b7eSSam Leffler 
3683c42a7b7eSSam Leffler 	memset(&qi, 0, sizeof(qi));
3684c42a7b7eSSam Leffler 	qi.tqi_subtype = subtype;
3685c42a7b7eSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
3686c42a7b7eSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
3687c42a7b7eSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
3688c42a7b7eSSam Leffler 	/*
3689c42a7b7eSSam Leffler 	 * Enable interrupts only for EOL and DESC conditions.
3690c42a7b7eSSam Leffler 	 * We mark tx descriptors to receive a DESC interrupt
3691c42a7b7eSSam Leffler 	 * when a tx queue gets deep; otherwise waiting for the
3692c42a7b7eSSam Leffler 	 * EOL to reap descriptors.  Note that this is done to
3693c42a7b7eSSam Leffler 	 * reduce interrupt load and this only defers reaping
3694c42a7b7eSSam Leffler 	 * descriptors, never transmitting frames.  Aside from
3695c42a7b7eSSam Leffler 	 * reducing interrupts this also permits more concurrency.
3696c42a7b7eSSam Leffler 	 * The only potential downside is if the tx queue backs
3697c42a7b7eSSam Leffler 	 * up in which case the top half of the kernel may backup
3698c42a7b7eSSam Leffler 	 * due to a lack of tx descriptors.
3699c42a7b7eSSam Leffler 	 */
37006961e9edSAdrian Chadd 	if (sc->sc_isedma)
37016961e9edSAdrian Chadd 		qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE |
37026961e9edSAdrian Chadd 		    HAL_TXQ_TXOKINT_ENABLE;
37036961e9edSAdrian Chadd 	else
37046961e9edSAdrian Chadd 		qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE |
37056961e9edSAdrian Chadd 		    HAL_TXQ_TXDESCINT_ENABLE;
37066961e9edSAdrian Chadd 
3707c42a7b7eSSam Leffler 	qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
3708c42a7b7eSSam Leffler 	if (qnum == -1) {
3709c42a7b7eSSam Leffler 		/*
3710c42a7b7eSSam Leffler 		 * NB: don't print a message, this happens
3711a614e076SSam Leffler 		 * normally on parts with too few tx queues
3712c42a7b7eSSam Leffler 		 */
3713c42a7b7eSSam Leffler 		return NULL;
3714c42a7b7eSSam Leffler 	}
3715c42a7b7eSSam Leffler 	if (qnum >= N(sc->sc_txq)) {
37166891c875SPeter Wemm 		device_printf(sc->sc_dev,
37176891c875SPeter Wemm 			"hal qnum %u out of range, max %zu!\n",
3718c42a7b7eSSam Leffler 			qnum, N(sc->sc_txq));
3719c42a7b7eSSam Leffler 		ath_hal_releasetxqueue(ah, qnum);
3720c42a7b7eSSam Leffler 		return NULL;
3721c42a7b7eSSam Leffler 	}
3722c42a7b7eSSam Leffler 	if (!ATH_TXQ_SETUP(sc, qnum)) {
3723622b3fd2SSam Leffler 		ath_txq_init(sc, &sc->sc_txq[qnum], qnum);
3724c42a7b7eSSam Leffler 		sc->sc_txqsetup |= 1<<qnum;
3725c42a7b7eSSam Leffler 	}
3726c42a7b7eSSam Leffler 	return &sc->sc_txq[qnum];
3727c42a7b7eSSam Leffler #undef N
3728c42a7b7eSSam Leffler }
3729c42a7b7eSSam Leffler 
3730c42a7b7eSSam Leffler /*
3731c42a7b7eSSam Leffler  * Setup a hardware data transmit queue for the specified
3732c42a7b7eSSam Leffler  * access control.  The hal may not support all requested
3733c42a7b7eSSam Leffler  * queues in which case it will return a reference to a
3734c42a7b7eSSam Leffler  * previously setup queue.  We record the mapping from ac's
3735c42a7b7eSSam Leffler  * to h/w queues for use by ath_tx_start and also track
3736c42a7b7eSSam Leffler  * the set of h/w queues being used to optimize work in the
3737c42a7b7eSSam Leffler  * transmit interrupt handler and related routines.
3738c42a7b7eSSam Leffler  */
3739c42a7b7eSSam Leffler static int
3740c42a7b7eSSam Leffler ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
3741c42a7b7eSSam Leffler {
3742c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
3743c42a7b7eSSam Leffler 	struct ath_txq *txq;
3744c42a7b7eSSam Leffler 
3745c42a7b7eSSam Leffler 	if (ac >= N(sc->sc_ac2q)) {
37466891c875SPeter Wemm 		device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
3747c42a7b7eSSam Leffler 			ac, N(sc->sc_ac2q));
3748c42a7b7eSSam Leffler 		return 0;
3749c42a7b7eSSam Leffler 	}
3750c42a7b7eSSam Leffler 	txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
3751c42a7b7eSSam Leffler 	if (txq != NULL) {
3752339ccfb3SSam Leffler 		txq->axq_ac = ac;
3753c42a7b7eSSam Leffler 		sc->sc_ac2q[ac] = txq;
3754c42a7b7eSSam Leffler 		return 1;
3755c42a7b7eSSam Leffler 	} else
3756c42a7b7eSSam Leffler 		return 0;
3757c42a7b7eSSam Leffler #undef N
3758c42a7b7eSSam Leffler }
3759c42a7b7eSSam Leffler 
3760c42a7b7eSSam Leffler /*
3761c42a7b7eSSam Leffler  * Update WME parameters for a transmit queue.
3762c42a7b7eSSam Leffler  */
3763c42a7b7eSSam Leffler static int
3764c42a7b7eSSam Leffler ath_txq_update(struct ath_softc *sc, int ac)
3765c42a7b7eSSam Leffler {
3766c42a7b7eSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<v)-1)
3767c42a7b7eSSam Leffler #define	ATH_TXOP_TO_US(v)		(v<<5)
3768b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
3769b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
3770c42a7b7eSSam Leffler 	struct ath_txq *txq = sc->sc_ac2q[ac];
3771c42a7b7eSSam Leffler 	struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
3772c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3773c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
3774c42a7b7eSSam Leffler 
3775c42a7b7eSSam Leffler 	ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
3776584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
377710ad9a77SSam Leffler 	if (sc->sc_tdma) {
377810ad9a77SSam Leffler 		/*
377910ad9a77SSam Leffler 		 * AIFS is zero so there's no pre-transmit wait.  The
378010ad9a77SSam Leffler 		 * burst time defines the slot duration and is configured
378109be6601SSam Leffler 		 * through net80211.  The QCU is setup to not do post-xmit
378210ad9a77SSam Leffler 		 * back off, lockout all lower-priority QCU's, and fire
378310ad9a77SSam Leffler 		 * off the DMA beacon alert timer which is setup based
378410ad9a77SSam Leffler 		 * on the slot configuration.
378510ad9a77SSam Leffler 		 */
378610ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
378710ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
378810ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
378910ad9a77SSam Leffler 			      | HAL_TXQ_TXEOLINT_ENABLE
379010ad9a77SSam Leffler 			      | HAL_TXQ_DBA_GATED
379110ad9a77SSam Leffler 			      | HAL_TXQ_BACKOFF_DISABLE
379210ad9a77SSam Leffler 			      | HAL_TXQ_ARB_LOCKOUT_GLOBAL
379310ad9a77SSam Leffler 			      ;
379410ad9a77SSam Leffler 		qi.tqi_aifs = 0;
379510ad9a77SSam Leffler 		/* XXX +dbaprep? */
379610ad9a77SSam Leffler 		qi.tqi_readyTime = sc->sc_tdmaslotlen;
379710ad9a77SSam Leffler 		qi.tqi_burstTime = qi.tqi_readyTime;
379810ad9a77SSam Leffler 	} else {
379910ad9a77SSam Leffler #endif
380016d4de92SAdrian Chadd 		/*
380116d4de92SAdrian Chadd 		 * XXX shouldn't this just use the default flags
380216d4de92SAdrian Chadd 		 * used in the previous queue setup?
380316d4de92SAdrian Chadd 		 */
380410ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
380510ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
380610ad9a77SSam Leffler 			      | HAL_TXQ_TXDESCINT_ENABLE
380710ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
38081f25c0f7SAdrian Chadd 			      | HAL_TXQ_TXEOLINT_ENABLE
380910ad9a77SSam Leffler 			      ;
3810c42a7b7eSSam Leffler 		qi.tqi_aifs = wmep->wmep_aifsn;
3811c42a7b7eSSam Leffler 		qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
3812c42a7b7eSSam Leffler 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
381310ad9a77SSam Leffler 		qi.tqi_readyTime = 0;
3814c42a7b7eSSam Leffler 		qi.tqi_burstTime = ATH_TXOP_TO_US(wmep->wmep_txopLimit);
3815584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
381610ad9a77SSam Leffler 	}
381710ad9a77SSam Leffler #endif
381810ad9a77SSam Leffler 
381910ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
382010ad9a77SSam Leffler 	    "%s: Q%u qflags 0x%x aifs %u cwmin %u cwmax %u burstTime %u\n",
382110ad9a77SSam Leffler 	    __func__, txq->axq_qnum, qi.tqi_qflags,
382210ad9a77SSam Leffler 	    qi.tqi_aifs, qi.tqi_cwmin, qi.tqi_cwmax, qi.tqi_burstTime);
3823c42a7b7eSSam Leffler 
3824c42a7b7eSSam Leffler 	if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
3825b032f27cSSam Leffler 		if_printf(ifp, "unable to update hardware queue "
3826c42a7b7eSSam Leffler 			"parameters for %s traffic!\n",
3827c42a7b7eSSam Leffler 			ieee80211_wme_acnames[ac]);
3828c42a7b7eSSam Leffler 		return 0;
3829c42a7b7eSSam Leffler 	} else {
3830c42a7b7eSSam Leffler 		ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
3831c42a7b7eSSam Leffler 		return 1;
3832c42a7b7eSSam Leffler 	}
3833c42a7b7eSSam Leffler #undef ATH_TXOP_TO_US
3834c42a7b7eSSam Leffler #undef ATH_EXPONENT_TO_VALUE
3835c42a7b7eSSam Leffler }
3836c42a7b7eSSam Leffler 
3837c42a7b7eSSam Leffler /*
3838c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update WME parameters.
3839c42a7b7eSSam Leffler  */
3840a35dae8dSAdrian Chadd int
3841c42a7b7eSSam Leffler ath_wme_update(struct ieee80211com *ic)
3842c42a7b7eSSam Leffler {
3843c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3844c42a7b7eSSam Leffler 
3845c42a7b7eSSam Leffler 	return !ath_txq_update(sc, WME_AC_BE) ||
3846c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_BK) ||
3847c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VI) ||
3848c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
3849c42a7b7eSSam Leffler }
3850c42a7b7eSSam Leffler 
3851c42a7b7eSSam Leffler /*
3852c42a7b7eSSam Leffler  * Reclaim resources for a setup queue.
3853c42a7b7eSSam Leffler  */
3854c42a7b7eSSam Leffler static void
3855c42a7b7eSSam Leffler ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
3856c42a7b7eSSam Leffler {
3857c42a7b7eSSam Leffler 
3858c42a7b7eSSam Leffler 	ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
3859c42a7b7eSSam Leffler 	sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
3860b837332dSAdrian Chadd 	ATH_TXQ_LOCK_DESTROY(txq);
3861c42a7b7eSSam Leffler }
3862c42a7b7eSSam Leffler 
3863c42a7b7eSSam Leffler /*
3864c42a7b7eSSam Leffler  * Reclaim all tx queue resources.
3865c42a7b7eSSam Leffler  */
3866c42a7b7eSSam Leffler static void
3867c42a7b7eSSam Leffler ath_tx_cleanup(struct ath_softc *sc)
3868c42a7b7eSSam Leffler {
3869c42a7b7eSSam Leffler 	int i;
3870c42a7b7eSSam Leffler 
3871c42a7b7eSSam Leffler 	ATH_TXBUF_LOCK_DESTROY(sc);
3872c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
3873c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
3874c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, &sc->sc_txq[i]);
3875c42a7b7eSSam Leffler }
38765591b213SSam Leffler 
387799d258fdSSam Leffler /*
3878ab06fdf2SSam Leffler  * Return h/w rate index for an IEEE rate (w/o basic rate bit)
3879ab06fdf2SSam Leffler  * using the current rates in sc_rixmap.
38808b5341deSSam Leffler  */
3881b8e788a5SAdrian Chadd int
3882ab06fdf2SSam Leffler ath_tx_findrix(const struct ath_softc *sc, uint8_t rate)
38838b5341deSSam Leffler {
3884ab06fdf2SSam Leffler 	int rix = sc->sc_rixmap[rate];
3885ab06fdf2SSam Leffler 	/* NB: return lowest rix for invalid rate */
3886ab06fdf2SSam Leffler 	return (rix == 0xff ? 0 : rix);
38878b5341deSSam Leffler }
38888b5341deSSam Leffler 
38899352fb7aSAdrian Chadd static void
38909352fb7aSAdrian Chadd ath_tx_update_stats(struct ath_softc *sc, struct ath_tx_status *ts,
38919352fb7aSAdrian Chadd     struct ath_buf *bf)
38929352fb7aSAdrian Chadd {
38939352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
38949352fb7aSAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
38959352fb7aSAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
38969352fb7aSAdrian Chadd 	int sr, lr, pri;
38979352fb7aSAdrian Chadd 
38989352fb7aSAdrian Chadd 	if (ts->ts_status == 0) {
38999352fb7aSAdrian Chadd 		u_int8_t txant = ts->ts_antenna;
39009352fb7aSAdrian Chadd 		sc->sc_stats.ast_ant_tx[txant]++;
39019352fb7aSAdrian Chadd 		sc->sc_ant_tx[txant]++;
39029352fb7aSAdrian Chadd 		if (ts->ts_finaltsi != 0)
39039352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_altrate++;
39049352fb7aSAdrian Chadd 		pri = M_WME_GETAC(bf->bf_m);
39059352fb7aSAdrian Chadd 		if (pri >= WME_AC_VO)
39069352fb7aSAdrian Chadd 			ic->ic_wme.wme_hipri_traffic++;
3907875a9451SAdrian Chadd 		if ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)
39089352fb7aSAdrian Chadd 			ni->ni_inact = ni->ni_inact_reload;
39099352fb7aSAdrian Chadd 	} else {
39109352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XRETRY)
39119352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xretries++;
39129352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FIFO)
39139352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_fifoerr++;
39149352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FILT)
39159352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_filtered++;
39169352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XTXOP)
39179352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xtxop++;
39189352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_TIMER_EXPIRED)
39199352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_timerexpired++;
39209352fb7aSAdrian Chadd 
39219352fb7aSAdrian Chadd 		if (bf->bf_m->m_flags & M_FF)
39229352fb7aSAdrian Chadd 			sc->sc_stats.ast_ff_txerr++;
39239352fb7aSAdrian Chadd 	}
39249352fb7aSAdrian Chadd 	/* XXX when is this valid? */
3925158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DESC_CFG_ERR)
39269352fb7aSAdrian Chadd 		sc->sc_stats.ast_tx_desccfgerr++;
3927158cb431SAdrian Chadd 	/*
3928158cb431SAdrian Chadd 	 * This can be valid for successful frame transmission!
3929158cb431SAdrian Chadd 	 * If there's a TX FIFO underrun during aggregate transmission,
3930158cb431SAdrian Chadd 	 * the MAC will pad the rest of the aggregate with delimiters.
3931158cb431SAdrian Chadd 	 * If a BA is returned, the frame is marked as "OK" and it's up
3932158cb431SAdrian Chadd 	 * to the TX completion code to notice which frames weren't
3933158cb431SAdrian Chadd 	 * successfully transmitted.
3934158cb431SAdrian Chadd 	 */
3935158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DATA_UNDERRUN)
3936158cb431SAdrian Chadd 		sc->sc_stats.ast_tx_data_underrun++;
3937158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DELIM_UNDERRUN)
3938158cb431SAdrian Chadd 		sc->sc_stats.ast_tx_delim_underrun++;
39399352fb7aSAdrian Chadd 
39409352fb7aSAdrian Chadd 	sr = ts->ts_shortretry;
39419352fb7aSAdrian Chadd 	lr = ts->ts_longretry;
39429352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_shortretry += sr;
39439352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_longretry += lr;
39449352fb7aSAdrian Chadd 
39459352fb7aSAdrian Chadd }
39469352fb7aSAdrian Chadd 
39479352fb7aSAdrian Chadd /*
39489352fb7aSAdrian Chadd  * The default completion. If fail is 1, this means
39499352fb7aSAdrian Chadd  * "please don't retry the frame, and just return -1 status
39509352fb7aSAdrian Chadd  * to the net80211 stack.
39519352fb7aSAdrian Chadd  */
39529352fb7aSAdrian Chadd void
39539352fb7aSAdrian Chadd ath_tx_default_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
39549352fb7aSAdrian Chadd {
39559352fb7aSAdrian Chadd 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
39569352fb7aSAdrian Chadd 	int st;
39579352fb7aSAdrian Chadd 
39589352fb7aSAdrian Chadd 	if (fail == 1)
39599352fb7aSAdrian Chadd 		st = -1;
39609352fb7aSAdrian Chadd 	else
3961875a9451SAdrian Chadd 		st = ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) ?
39629352fb7aSAdrian Chadd 		    ts->ts_status : HAL_TXERR_XRETRY;
39639352fb7aSAdrian Chadd 
3964ce597531SAdrian Chadd #if 0
39659352fb7aSAdrian Chadd 	if (bf->bf_state.bfs_dobaw)
39669352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
3967a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: dobaw should've been cleared!\n",
3968a66d5089SAdrian Chadd 		    __func__,
3969a66d5089SAdrian Chadd 		    bf,
3970a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
3971ce597531SAdrian Chadd #endif
39729352fb7aSAdrian Chadd 	if (bf->bf_next != NULL)
39739352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
3974a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: bf_next not NULL!\n",
3975a66d5089SAdrian Chadd 		    __func__,
3976a66d5089SAdrian Chadd 		    bf,
3977a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
39789352fb7aSAdrian Chadd 
39799352fb7aSAdrian Chadd 	/*
3980548a605dSAdrian Chadd 	 * Check if the node software queue is empty; if so
3981548a605dSAdrian Chadd 	 * then clear the TIM.
3982548a605dSAdrian Chadd 	 *
3983548a605dSAdrian Chadd 	 * This needs to be done before the buffer is freed as
3984548a605dSAdrian Chadd 	 * otherwise the node reference will have been released
3985548a605dSAdrian Chadd 	 * and the node may not actually exist any longer.
3986548a605dSAdrian Chadd 	 *
3987548a605dSAdrian Chadd 	 * XXX I don't like this belonging here, but it's cleaner
3988548a605dSAdrian Chadd 	 * to do it here right now then all the other places
3989548a605dSAdrian Chadd 	 * where ath_tx_default_comp() is called.
3990548a605dSAdrian Chadd 	 *
3991548a605dSAdrian Chadd 	 * XXX TODO: during drain, ensure that the callback is
3992548a605dSAdrian Chadd 	 * being called so we get a chance to update the TIM.
3993548a605dSAdrian Chadd 	 */
39944bed2b67SAdrian Chadd 	if (bf->bf_node) {
39954bed2b67SAdrian Chadd 		ATH_TX_LOCK(sc);
3996548a605dSAdrian Chadd 		ath_tx_update_tim(sc, bf->bf_node, 0);
39974bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
39984bed2b67SAdrian Chadd 	}
3999548a605dSAdrian Chadd 
4000548a605dSAdrian Chadd 	/*
40019352fb7aSAdrian Chadd 	 * Do any tx complete callback.  Note this must
40029352fb7aSAdrian Chadd 	 * be done before releasing the node reference.
40039352fb7aSAdrian Chadd 	 * This will free the mbuf, release the net80211
40049352fb7aSAdrian Chadd 	 * node and recycle the ath_buf.
40059352fb7aSAdrian Chadd 	 */
40069352fb7aSAdrian Chadd 	ath_tx_freebuf(sc, bf, st);
40079352fb7aSAdrian Chadd }
40089352fb7aSAdrian Chadd 
40099352fb7aSAdrian Chadd /*
4010eb6f0de0SAdrian Chadd  * Update rate control with the given completion status.
4011eb6f0de0SAdrian Chadd  */
4012eb6f0de0SAdrian Chadd void
4013eb6f0de0SAdrian Chadd ath_tx_update_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
4014eb6f0de0SAdrian Chadd     struct ath_rc_series *rc, struct ath_tx_status *ts, int frmlen,
4015eb6f0de0SAdrian Chadd     int nframes, int nbad)
4016eb6f0de0SAdrian Chadd {
4017eb6f0de0SAdrian Chadd 	struct ath_node *an;
4018eb6f0de0SAdrian Chadd 
4019eb6f0de0SAdrian Chadd 	/* Only for unicast frames */
4020eb6f0de0SAdrian Chadd 	if (ni == NULL)
4021eb6f0de0SAdrian Chadd 		return;
4022eb6f0de0SAdrian Chadd 
4023eb6f0de0SAdrian Chadd 	an = ATH_NODE(ni);
4024548a605dSAdrian Chadd 	ATH_NODE_UNLOCK_ASSERT(an);
4025eb6f0de0SAdrian Chadd 
4026eb6f0de0SAdrian Chadd 	if ((ts->ts_status & HAL_TXERR_FILT) == 0) {
4027eb6f0de0SAdrian Chadd 		ATH_NODE_LOCK(an);
4028eb6f0de0SAdrian Chadd 		ath_rate_tx_complete(sc, an, rc, ts, frmlen, nframes, nbad);
4029eb6f0de0SAdrian Chadd 		ATH_NODE_UNLOCK(an);
4030eb6f0de0SAdrian Chadd 	}
4031eb6f0de0SAdrian Chadd }
4032eb6f0de0SAdrian Chadd 
4033eb6f0de0SAdrian Chadd /*
4034bad98824SAdrian Chadd  * Process the completion of the given buffer.
4035bad98824SAdrian Chadd  *
4036bad98824SAdrian Chadd  * This calls the rate control update and then the buffer completion.
4037bad98824SAdrian Chadd  * This will either free the buffer or requeue it.  In any case, the
4038bad98824SAdrian Chadd  * bf pointer should be treated as invalid after this function is called.
4039bad98824SAdrian Chadd  */
4040bad98824SAdrian Chadd void
4041bad98824SAdrian Chadd ath_tx_process_buf_completion(struct ath_softc *sc, struct ath_txq *txq,
4042bad98824SAdrian Chadd     struct ath_tx_status *ts, struct ath_buf *bf)
4043bad98824SAdrian Chadd {
4044bad98824SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
4045bad98824SAdrian Chadd 	struct ath_node *an = NULL;
4046bad98824SAdrian Chadd 
4047375307d4SAdrian Chadd 	ATH_TX_UNLOCK_ASSERT(sc);
40485e018508SAdrian Chadd 	ATH_TXQ_UNLOCK_ASSERT(txq);
4049bad98824SAdrian Chadd 
4050bad98824SAdrian Chadd 	/* If unicast frame, update general statistics */
4051bad98824SAdrian Chadd 	if (ni != NULL) {
4052bad98824SAdrian Chadd 		an = ATH_NODE(ni);
4053bad98824SAdrian Chadd 		/* update statistics */
4054bad98824SAdrian Chadd 		ath_tx_update_stats(sc, ts, bf);
4055bad98824SAdrian Chadd 	}
4056bad98824SAdrian Chadd 
4057bad98824SAdrian Chadd 	/*
4058bad98824SAdrian Chadd 	 * Call the completion handler.
4059bad98824SAdrian Chadd 	 * The completion handler is responsible for
4060bad98824SAdrian Chadd 	 * calling the rate control code.
4061bad98824SAdrian Chadd 	 *
4062bad98824SAdrian Chadd 	 * Frames with no completion handler get the
4063bad98824SAdrian Chadd 	 * rate control code called here.
4064bad98824SAdrian Chadd 	 */
4065bad98824SAdrian Chadd 	if (bf->bf_comp == NULL) {
4066bad98824SAdrian Chadd 		if ((ts->ts_status & HAL_TXERR_FILT) == 0 &&
4067bad98824SAdrian Chadd 		    (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) {
4068bad98824SAdrian Chadd 			/*
4069bad98824SAdrian Chadd 			 * XXX assume this isn't an aggregate
4070bad98824SAdrian Chadd 			 * frame.
4071bad98824SAdrian Chadd 			 */
4072bad98824SAdrian Chadd 			ath_tx_update_ratectrl(sc, ni,
4073bad98824SAdrian Chadd 			     bf->bf_state.bfs_rc, ts,
4074bad98824SAdrian Chadd 			    bf->bf_state.bfs_pktlen, 1,
4075bad98824SAdrian Chadd 			    (ts->ts_status == 0 ? 0 : 1));
4076bad98824SAdrian Chadd 		}
4077bad98824SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4078bad98824SAdrian Chadd 	} else
4079bad98824SAdrian Chadd 		bf->bf_comp(sc, bf, 0);
4080bad98824SAdrian Chadd }
4081bad98824SAdrian Chadd 
4082bad98824SAdrian Chadd 
4083bad98824SAdrian Chadd 
4084bad98824SAdrian Chadd /*
4085c42a7b7eSSam Leffler  * Process completed xmit descriptors from the specified queue.
4086eb6f0de0SAdrian Chadd  * Kick the packet scheduler if needed. This can occur from this
4087eb6f0de0SAdrian Chadd  * particular task.
4088c42a7b7eSSam Leffler  */
4089788e6aa9SAdrian Chadd static int
4090788e6aa9SAdrian Chadd ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq, int dosched)
40915591b213SSam Leffler {
40925591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
40939352fb7aSAdrian Chadd 	struct ath_buf *bf;
40946edf1dc7SAdrian Chadd 	struct ath_desc *ds;
409565f9edeeSSam Leffler 	struct ath_tx_status *ts;
40965591b213SSam Leffler 	struct ieee80211_node *ni;
409753e98d5aSAdrian Chadd #ifdef	IEEE80211_SUPPORT_SUPERG
409843faa6b2SAdrian Chadd 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
409953e98d5aSAdrian Chadd #endif	/* IEEE80211_SUPPORT_SUPERG */
41009352fb7aSAdrian Chadd 	int nacked;
41015591b213SSam Leffler 	HAL_STATUS status;
41025591b213SSam Leffler 
4103c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
4104c42a7b7eSSam Leffler 		__func__, txq->axq_qnum,
4105c42a7b7eSSam Leffler 		(caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
4106c42a7b7eSSam Leffler 		txq->axq_link);
410703682514SAdrian Chadd 
410803682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 4,
410903682514SAdrian Chadd 	    "ath_tx_processq: txq=%u head %p link %p depth %p",
411003682514SAdrian Chadd 	    txq->axq_qnum,
411103682514SAdrian Chadd 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
411203682514SAdrian Chadd 	    txq->axq_link,
411303682514SAdrian Chadd 	    txq->axq_depth);
411403682514SAdrian Chadd 
4115d7736e13SSam Leffler 	nacked = 0;
41165591b213SSam Leffler 	for (;;) {
4117b837332dSAdrian Chadd 		ATH_TXQ_LOCK(txq);
4118c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;	/* reset periodic desc intr count */
41196b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&txq->axq_q);
41205591b213SSam Leffler 		if (bf == NULL) {
4121b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
41225591b213SSam Leffler 			break;
41235591b213SSam Leffler 		}
41246edf1dc7SAdrian Chadd 		ds = bf->bf_lastds;	/* XXX must be setup correctly! */
412565f9edeeSSam Leffler 		ts = &bf->bf_status.ds_txstat;
412603682514SAdrian Chadd 
412765f9edeeSSam Leffler 		status = ath_hal_txprocdesc(ah, ds, ts);
4128a585a9a1SSam Leffler #ifdef ATH_DEBUG
4129c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
41306902009eSSam Leffler 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
41316902009eSSam Leffler 			    status == HAL_OK);
413203682514SAdrian Chadd 		else if ((sc->sc_debug & ATH_DEBUG_RESET) && (dosched == 0))
4133d6b20023SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
4134d6b20023SAdrian Chadd 			    status == HAL_OK);
41355591b213SSam Leffler #endif
4136bb327d28SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
4137bb327d28SAdrian Chadd 		if (if_ath_alq_checkdebug(&sc->sc_alq,
4138bb327d28SAdrian Chadd 		    ATH_ALQ_EDMA_TXSTATUS)) {
4139bb327d28SAdrian Chadd 			if_ath_alq_post(&sc->sc_alq, ATH_ALQ_EDMA_TXSTATUS,
4140bb327d28SAdrian Chadd 			sc->sc_tx_statuslen,
4141bb327d28SAdrian Chadd 			(char *) ds);
4142bb327d28SAdrian Chadd 		}
4143bb327d28SAdrian Chadd #endif
414403682514SAdrian Chadd 
41455591b213SSam Leffler 		if (status == HAL_EINPROGRESS) {
414603682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_TXCOMP, 3,
414703682514SAdrian Chadd 			    "ath_tx_processq: txq=%u, bf=%p ds=%p, HAL_EINPROGRESS",
414803682514SAdrian Chadd 			    txq->axq_qnum, bf, ds);
4149b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
41505591b213SSam Leffler 			break;
41515591b213SSam Leffler 		}
41526b349e5aSAdrian Chadd 		ATH_TXQ_REMOVE(txq, bf, bf_list);
41535e018508SAdrian Chadd 
41545e018508SAdrian Chadd 		/*
41555e018508SAdrian Chadd 		 * Sanity check.
41565e018508SAdrian Chadd 		 */
41575e018508SAdrian Chadd 		if (txq->axq_qnum != bf->bf_state.bfs_tx_queue) {
41585e018508SAdrian Chadd 			device_printf(sc->sc_dev,
41595e018508SAdrian Chadd 			    "%s: TXQ=%d: bf=%p, bfs_tx_queue=%d\n",
41605e018508SAdrian Chadd 			    __func__,
41615e018508SAdrian Chadd 			    txq->axq_qnum,
41625e018508SAdrian Chadd 			    bf,
41635e018508SAdrian Chadd 			    bf->bf_state.bfs_tx_queue);
41645e018508SAdrian Chadd 		}
41655e018508SAdrian Chadd 		if (txq->axq_qnum != bf->bf_last->bf_state.bfs_tx_queue) {
41665e018508SAdrian Chadd 			device_printf(sc->sc_dev,
41675e018508SAdrian Chadd 			    "%s: TXQ=%d: bf_last=%p, bfs_tx_queue=%d\n",
41685e018508SAdrian Chadd 			    __func__,
41695e018508SAdrian Chadd 			    txq->axq_qnum,
41705e018508SAdrian Chadd 			    bf->bf_last,
41715e018508SAdrian Chadd 			    bf->bf_last->bf_state.bfs_tx_queue);
41725e018508SAdrian Chadd 		}
41735e018508SAdrian Chadd 
41745e018508SAdrian Chadd #if 0
4175d3731e4bSAdrian Chadd 		if (txq->axq_depth > 0) {
417610ad9a77SSam Leffler 			/*
4177d3731e4bSAdrian Chadd 			 * More frames follow.  Mark the buffer busy
4178d3731e4bSAdrian Chadd 			 * so it's not re-used while the hardware may
4179d3731e4bSAdrian Chadd 			 * still re-read the link field in the descriptor.
41806edf1dc7SAdrian Chadd 			 *
4181d3731e4bSAdrian Chadd 			 * Use the last buffer in an aggregate as that
4182d3731e4bSAdrian Chadd 			 * is where the hardware may be - intermediate
4183d3731e4bSAdrian Chadd 			 * descriptors won't be "busy".
418410ad9a77SSam Leffler 			 */
41856edf1dc7SAdrian Chadd 			bf->bf_last->bf_flags |= ATH_BUF_BUSY;
4186d3731e4bSAdrian Chadd 		} else
4187d3731e4bSAdrian Chadd 			txq->axq_link = NULL;
41885e018508SAdrian Chadd #else
41895e018508SAdrian Chadd 		bf->bf_last->bf_flags |= ATH_BUF_BUSY;
41905e018508SAdrian Chadd #endif
41916edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
41926edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
41935591b213SSam Leffler 
41945591b213SSam Leffler 		ni = bf->bf_node;
419503682514SAdrian Chadd 
419603682514SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TXCOMP, 5,
419703682514SAdrian Chadd 		    "ath_tx_processq: txq=%u, bf=%p, ds=%p, ni=%p, ts_status=0x%08x",
419803682514SAdrian Chadd 		    txq->axq_qnum, bf, ds, ni, ts->ts_status);
4199c42a7b7eSSam Leffler 		/*
42009352fb7aSAdrian Chadd 		 * If unicast frame was ack'd update RSSI,
420184784be1SSam Leffler 		 * including the last rx time used to
420284784be1SSam Leffler 		 * workaround phantom bmiss interrupts.
4203d7736e13SSam Leffler 		 */
42049352fb7aSAdrian Chadd 		if (ni != NULL && ts->ts_status == 0 &&
4205875a9451SAdrian Chadd 		    ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)) {
4206d7736e13SSam Leffler 			nacked++;
420784784be1SSam Leffler 			sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
420884784be1SSam Leffler 			ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi,
420984784be1SSam Leffler 				ts->ts_rssi);
421084784be1SSam Leffler 		}
4211b837332dSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
42129352fb7aSAdrian Chadd 
4213bad98824SAdrian Chadd 		/*
4214bad98824SAdrian Chadd 		 * Update statistics and call completion
4215bad98824SAdrian Chadd 		 */
4216bad98824SAdrian Chadd 		ath_tx_process_buf_completion(sc, txq, ts, bf);
4217548a605dSAdrian Chadd 
4218548a605dSAdrian Chadd 		/* XXX at this point, bf and ni may be totally invalid */
42195591b213SSam Leffler 	}
4220339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
422168e8e04eSSam Leffler 	/*
422268e8e04eSSam Leffler 	 * Flush fast-frame staging queue when traffic slows.
422368e8e04eSSam Leffler 	 */
422468e8e04eSSam Leffler 	if (txq->axq_depth <= 1)
422504f19fd6SSam Leffler 		ieee80211_ff_flush(ic, txq->axq_ac);
4226339ccfb3SSam Leffler #endif
4227eb6f0de0SAdrian Chadd 
422821bca442SAdrian Chadd 	/* Kick the software TXQ scheduler */
4229eb6f0de0SAdrian Chadd 	if (dosched) {
4230a40880adSAdrian Chadd 		ATH_TX_LOCK(sc);
4231a40880adSAdrian Chadd 		ath_txq_sched(sc, txq);
4232a40880adSAdrian Chadd 		ATH_TX_UNLOCK(sc);
4233eb6f0de0SAdrian Chadd 	}
4234eb6f0de0SAdrian Chadd 
423503682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
423603682514SAdrian Chadd 	    "ath_tx_processq: txq=%u: done",
423703682514SAdrian Chadd 	    txq->axq_qnum);
423803682514SAdrian Chadd 
4239d7736e13SSam Leffler 	return nacked;
4240d7736e13SSam Leffler }
4241d7736e13SSam Leffler 
42428f939e79SAdrian Chadd #define	TXQACTIVE(t, q)		( (t) & (1 << (q)))
4243c42a7b7eSSam Leffler 
4244c42a7b7eSSam Leffler /*
4245c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4246c42a7b7eSSam Leffler  * for a single hardware transmit queue (e.g. 5210 and 5211).
4247c42a7b7eSSam Leffler  */
4248c42a7b7eSSam Leffler static void
4249c42a7b7eSSam Leffler ath_tx_proc_q0(void *arg, int npending)
4250c42a7b7eSSam Leffler {
4251c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4252fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
42538f939e79SAdrian Chadd 	uint32_t txqs;
4254c42a7b7eSSam Leffler 
4255ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4256ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
42578f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
42588f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4259ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
42608f939e79SAdrian Chadd 
426103682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
426203682514SAdrian Chadd 	    "ath_tx_proc_q0: txqs=0x%08x", txqs);
426303682514SAdrian Chadd 
426496ff485dSAdrian Chadd 	if (TXQACTIVE(txqs, 0) && ath_tx_processq(sc, &sc->sc_txq[0], 1))
42658f939e79SAdrian Chadd 		/* XXX why is lastrx updated in tx code? */
4266d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
42678f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
426896ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
4269e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
427013f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4271e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
42722e986da5SSam Leffler 	sc->sc_wd_timer = 0;
42735591b213SSam Leffler 
42743e50ec2cSSam Leffler 	if (sc->sc_softled)
427546d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
42763e50ec2cSSam Leffler 
4277ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4278ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4279ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
42801a85141aSAdrian Chadd 
42811a85141aSAdrian Chadd 	ath_tx_kick(sc);
42825591b213SSam Leffler }
42835591b213SSam Leffler 
42845591b213SSam Leffler /*
4285c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4286c42a7b7eSSam Leffler  * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
42875591b213SSam Leffler  */
42885591b213SSam Leffler static void
4289c42a7b7eSSam Leffler ath_tx_proc_q0123(void *arg, int npending)
4290c42a7b7eSSam Leffler {
4291c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4292fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4293d7736e13SSam Leffler 	int nacked;
42948f939e79SAdrian Chadd 	uint32_t txqs;
42958f939e79SAdrian Chadd 
4296ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4297ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
42988f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
42998f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4300ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4301c42a7b7eSSam Leffler 
430203682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
430303682514SAdrian Chadd 	    "ath_tx_proc_q0123: txqs=0x%08x", txqs);
430403682514SAdrian Chadd 
4305c42a7b7eSSam Leffler 	/*
4306c42a7b7eSSam Leffler 	 * Process each active queue.
4307c42a7b7eSSam Leffler 	 */
4308d7736e13SSam Leffler 	nacked = 0;
43098f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 0))
431096ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[0], 1);
43118f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 1))
431296ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[1], 1);
43138f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 2))
431496ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[2], 1);
43158f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 3))
431696ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[3], 1);
43178f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
431896ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
4319d7736e13SSam Leffler 	if (nacked)
4320d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4321c42a7b7eSSam Leffler 
4322e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
432313f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4324e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
43252e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4326c42a7b7eSSam Leffler 
43273e50ec2cSSam Leffler 	if (sc->sc_softled)
432846d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
43293e50ec2cSSam Leffler 
4330ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4331ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4332ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
43331a85141aSAdrian Chadd 
43341a85141aSAdrian Chadd 	ath_tx_kick(sc);
4335c42a7b7eSSam Leffler }
4336c42a7b7eSSam Leffler 
4337c42a7b7eSSam Leffler /*
4338c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt.
4339c42a7b7eSSam Leffler  */
4340c42a7b7eSSam Leffler static void
4341c42a7b7eSSam Leffler ath_tx_proc(void *arg, int npending)
4342c42a7b7eSSam Leffler {
4343c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4344fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4345d7736e13SSam Leffler 	int i, nacked;
43468f939e79SAdrian Chadd 	uint32_t txqs;
43478f939e79SAdrian Chadd 
4348ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4349ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
43508f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
43518f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4352ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4353c42a7b7eSSam Leffler 
435403682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1, "ath_tx_proc: txqs=0x%08x", txqs);
435503682514SAdrian Chadd 
4356c42a7b7eSSam Leffler 	/*
4357c42a7b7eSSam Leffler 	 * Process each active queue.
4358c42a7b7eSSam Leffler 	 */
4359d7736e13SSam Leffler 	nacked = 0;
4360c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
43618f939e79SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i) && TXQACTIVE(txqs, i))
436296ff485dSAdrian Chadd 			nacked += ath_tx_processq(sc, &sc->sc_txq[i], 1);
4363d7736e13SSam Leffler 	if (nacked)
4364d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4365c42a7b7eSSam Leffler 
4366ef27340cSAdrian Chadd 	/* XXX check this inside of IF_LOCK? */
4367e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
436813f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4369e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
43702e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4371c42a7b7eSSam Leffler 
43723e50ec2cSSam Leffler 	if (sc->sc_softled)
437346d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
43743e50ec2cSSam Leffler 
4375ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4376ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4377ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
43781a85141aSAdrian Chadd 
43791a85141aSAdrian Chadd 	ath_tx_kick(sc);
4380c42a7b7eSSam Leffler }
438116d4de92SAdrian Chadd #undef	TXQACTIVE
4382c42a7b7eSSam Leffler 
43839352fb7aSAdrian Chadd /*
438403e9308fSAdrian Chadd  * Deferred processing of TXQ rescheduling.
438503e9308fSAdrian Chadd  */
438603e9308fSAdrian Chadd static void
438703e9308fSAdrian Chadd ath_txq_sched_tasklet(void *arg, int npending)
438803e9308fSAdrian Chadd {
438903e9308fSAdrian Chadd 	struct ath_softc *sc = arg;
439003e9308fSAdrian Chadd 	int i;
439103e9308fSAdrian Chadd 
439203e9308fSAdrian Chadd 	/* XXX is skipping ok? */
439303e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
439403e9308fSAdrian Chadd #if 0
439503e9308fSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
439603e9308fSAdrian Chadd 		device_printf(sc->sc_dev,
439703e9308fSAdrian Chadd 		    "%s: sc_inreset_cnt > 0; skipping\n", __func__);
439803e9308fSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
439903e9308fSAdrian Chadd 		return;
440003e9308fSAdrian Chadd 	}
440103e9308fSAdrian Chadd #endif
440203e9308fSAdrian Chadd 	sc->sc_txproc_cnt++;
440303e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
440403e9308fSAdrian Chadd 
4405375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
440603e9308fSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
4407b5a9dfd5SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
440803e9308fSAdrian Chadd 			ath_txq_sched(sc, &sc->sc_txq[i]);
4409b5a9dfd5SAdrian Chadd 		}
441003e9308fSAdrian Chadd 	}
4411375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
441203e9308fSAdrian Chadd 
441303e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
441403e9308fSAdrian Chadd 	sc->sc_txproc_cnt--;
441503e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
441603e9308fSAdrian Chadd }
441703e9308fSAdrian Chadd 
4418e1a50456SAdrian Chadd void
4419e1a50456SAdrian Chadd ath_returnbuf_tail(struct ath_softc *sc, struct ath_buf *bf)
4420e1a50456SAdrian Chadd {
4421e1a50456SAdrian Chadd 
4422e1a50456SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
4423e1a50456SAdrian Chadd 
4424af33d486SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_MGMT)
4425af33d486SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf_mgmt, bf, bf_list);
442623ced6c1SAdrian Chadd 	else {
4427e1a50456SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
442823ced6c1SAdrian Chadd 		sc->sc_txbuf_cnt++;
442923ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt > ath_txbuf) {
443023ced6c1SAdrian Chadd 			device_printf(sc->sc_dev,
443123ced6c1SAdrian Chadd 			    "%s: sc_txbuf_cnt > %d?\n",
443223ced6c1SAdrian Chadd 			    __func__,
443323ced6c1SAdrian Chadd 			    ath_txbuf);
443423ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt = ath_txbuf;
443523ced6c1SAdrian Chadd 		}
443623ced6c1SAdrian Chadd 	}
4437e1a50456SAdrian Chadd }
4438e1a50456SAdrian Chadd 
4439e1a50456SAdrian Chadd void
4440e1a50456SAdrian Chadd ath_returnbuf_head(struct ath_softc *sc, struct ath_buf *bf)
4441e1a50456SAdrian Chadd {
4442e1a50456SAdrian Chadd 
4443e1a50456SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
4444e1a50456SAdrian Chadd 
4445af33d486SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_MGMT)
4446af33d486SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf_mgmt, bf, bf_list);
444723ced6c1SAdrian Chadd 	else {
4448e1a50456SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
444923ced6c1SAdrian Chadd 		sc->sc_txbuf_cnt++;
445023ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt > ATH_TXBUF) {
445123ced6c1SAdrian Chadd 			device_printf(sc->sc_dev,
445223ced6c1SAdrian Chadd 			    "%s: sc_txbuf_cnt > %d?\n",
445323ced6c1SAdrian Chadd 			    __func__,
445423ced6c1SAdrian Chadd 			    ATH_TXBUF);
445523ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt = ATH_TXBUF;
445623ced6c1SAdrian Chadd 		}
445723ced6c1SAdrian Chadd 	}
4458e1a50456SAdrian Chadd }
4459e1a50456SAdrian Chadd 
446003e9308fSAdrian Chadd /*
4461629ce218SAdrian Chadd  * Free the holding buffer if it exists
4462629ce218SAdrian Chadd  */
44633feffbd7SAdrian Chadd void
4464629ce218SAdrian Chadd ath_txq_freeholdingbuf(struct ath_softc *sc, struct ath_txq *txq)
4465629ce218SAdrian Chadd {
44665e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK_ASSERT(sc);
44675e018508SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
4468629ce218SAdrian Chadd 
4469629ce218SAdrian Chadd 	if (txq->axq_holdingbf == NULL)
4470629ce218SAdrian Chadd 		return;
4471629ce218SAdrian Chadd 
4472629ce218SAdrian Chadd 	txq->axq_holdingbf->bf_flags &= ~ATH_BUF_BUSY;
44735e018508SAdrian Chadd 
44745e018508SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
4475629ce218SAdrian Chadd 	ath_returnbuf_tail(sc, txq->axq_holdingbf);
44765e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
44775e018508SAdrian Chadd 
4478629ce218SAdrian Chadd 	txq->axq_holdingbf = NULL;
4479629ce218SAdrian Chadd }
4480629ce218SAdrian Chadd 
4481629ce218SAdrian Chadd /*
4482629ce218SAdrian Chadd  * Add this buffer to the holding queue, freeing the previous
4483629ce218SAdrian Chadd  * one if it exists.
4484629ce218SAdrian Chadd  */
4485629ce218SAdrian Chadd static void
4486629ce218SAdrian Chadd ath_txq_addholdingbuf(struct ath_softc *sc, struct ath_buf *bf)
4487629ce218SAdrian Chadd {
4488629ce218SAdrian Chadd 	struct ath_txq *txq;
4489629ce218SAdrian Chadd 
44905e018508SAdrian Chadd 	txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue];
44915e018508SAdrian Chadd 
44925e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK_ASSERT(sc);
44935e018508SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
44945f2f0e61SAdrian Chadd 
4495629ce218SAdrian Chadd 	/* XXX assert ATH_BUF_BUSY is set */
4496629ce218SAdrian Chadd 
4497629ce218SAdrian Chadd 	/* XXX assert the tx queue is under the max number */
4498629ce218SAdrian Chadd 	if (bf->bf_state.bfs_tx_queue > HAL_NUM_TX_QUEUES) {
4499629ce218SAdrian Chadd 		device_printf(sc->sc_dev, "%s: bf=%p: invalid tx queue (%d)\n",
4500629ce218SAdrian Chadd 		    __func__,
4501629ce218SAdrian Chadd 		    bf,
4502629ce218SAdrian Chadd 		    bf->bf_state.bfs_tx_queue);
4503629ce218SAdrian Chadd 		bf->bf_flags &= ~ATH_BUF_BUSY;
4504629ce218SAdrian Chadd 		ath_returnbuf_tail(sc, bf);
4505629ce218SAdrian Chadd 		return;
4506629ce218SAdrian Chadd 	}
4507629ce218SAdrian Chadd 	ath_txq_freeholdingbuf(sc, txq);
4508629ce218SAdrian Chadd 	txq->axq_holdingbf = bf;
4509629ce218SAdrian Chadd }
4510629ce218SAdrian Chadd 
4511629ce218SAdrian Chadd /*
45129352fb7aSAdrian Chadd  * Return a buffer to the pool and update the 'busy' flag on the
45139352fb7aSAdrian Chadd  * previous 'tail' entry.
45149352fb7aSAdrian Chadd  *
45159352fb7aSAdrian Chadd  * This _must_ only be called when the buffer is involved in a completed
45169352fb7aSAdrian Chadd  * TX. The logic is that if it was part of an active TX, the previous
45179352fb7aSAdrian Chadd  * buffer on the list is now not involved in a halted TX DMA queue, waiting
45189352fb7aSAdrian Chadd  * for restart (eg for TDMA.)
45199352fb7aSAdrian Chadd  *
45209352fb7aSAdrian Chadd  * The caller must free the mbuf and recycle the node reference.
45215e018508SAdrian Chadd  *
45225e018508SAdrian Chadd  * XXX This method of handling busy / holding buffers is insanely stupid.
45235e018508SAdrian Chadd  * It requires bf_state.bfs_tx_queue to be correctly assigned.  It would
45245e018508SAdrian Chadd  * be much nicer if buffers in the processq() methods would instead be
45255e018508SAdrian Chadd  * always completed there (pushed onto a txq or ath_bufhead) so we knew
45265e018508SAdrian Chadd  * exactly what hardware queue they came from in the first place.
45279352fb7aSAdrian Chadd  */
45289352fb7aSAdrian Chadd void
45299352fb7aSAdrian Chadd ath_freebuf(struct ath_softc *sc, struct ath_buf *bf)
45309352fb7aSAdrian Chadd {
45315e018508SAdrian Chadd 	struct ath_txq *txq;
45325e018508SAdrian Chadd 
45335e018508SAdrian Chadd 	txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue];
45345e018508SAdrian Chadd 
45359352fb7aSAdrian Chadd 	KASSERT((bf->bf_node == NULL), ("%s: bf->bf_node != NULL\n", __func__));
45369352fb7aSAdrian Chadd 	KASSERT((bf->bf_m == NULL), ("%s: bf->bf_m != NULL\n", __func__));
45379352fb7aSAdrian Chadd 
4538629ce218SAdrian Chadd 	/*
45395e018508SAdrian Chadd 	 * If this buffer is busy, push it onto the holding queue.
4540629ce218SAdrian Chadd 	 */
4541629ce218SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_BUSY) {
45425e018508SAdrian Chadd 		ATH_TXQ_LOCK(txq);
4543629ce218SAdrian Chadd 		ath_txq_addholdingbuf(sc, bf);
45445e018508SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
4545629ce218SAdrian Chadd 		return;
4546629ce218SAdrian Chadd 	}
4547629ce218SAdrian Chadd 
4548629ce218SAdrian Chadd 	/*
4549629ce218SAdrian Chadd 	 * Not a busy buffer, so free normally
4550629ce218SAdrian Chadd 	 */
45519352fb7aSAdrian Chadd 	ATH_TXBUF_LOCK(sc);
4552e1a50456SAdrian Chadd 	ath_returnbuf_tail(sc, bf);
45539352fb7aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
45549352fb7aSAdrian Chadd }
45559352fb7aSAdrian Chadd 
45569352fb7aSAdrian Chadd /*
45579352fb7aSAdrian Chadd  * This is currently used by ath_tx_draintxq() and
45589352fb7aSAdrian Chadd  * ath_tx_tid_free_pkts().
45599352fb7aSAdrian Chadd  *
45609352fb7aSAdrian Chadd  * It recycles a single ath_buf.
45619352fb7aSAdrian Chadd  */
45629352fb7aSAdrian Chadd void
45639352fb7aSAdrian Chadd ath_tx_freebuf(struct ath_softc *sc, struct ath_buf *bf, int status)
45649352fb7aSAdrian Chadd {
45659352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
45669352fb7aSAdrian Chadd 	struct mbuf *m0 = bf->bf_m;
45679352fb7aSAdrian Chadd 
45683f3a5dbdSAdrian Chadd 	/*
45693f3a5dbdSAdrian Chadd 	 * Make sure that we only sync/unload if there's an mbuf.
45703f3a5dbdSAdrian Chadd 	 * If not (eg we cloned a buffer), the unload will have already
45713f3a5dbdSAdrian Chadd 	 * occured.
45723f3a5dbdSAdrian Chadd 	 */
45733f3a5dbdSAdrian Chadd 	if (bf->bf_m != NULL) {
45743f3a5dbdSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
45753f3a5dbdSAdrian Chadd 		    BUS_DMASYNC_POSTWRITE);
45763f3a5dbdSAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
45773f3a5dbdSAdrian Chadd 	}
45783f3a5dbdSAdrian Chadd 
45799352fb7aSAdrian Chadd 	bf->bf_node = NULL;
45809352fb7aSAdrian Chadd 	bf->bf_m = NULL;
45819352fb7aSAdrian Chadd 
45829352fb7aSAdrian Chadd 	/* Free the buffer, it's not needed any longer */
45839352fb7aSAdrian Chadd 	ath_freebuf(sc, bf);
45849352fb7aSAdrian Chadd 
4585e95f3424SAdrian Chadd 	/* Pass the buffer back to net80211 - completing it */
4586e95f3424SAdrian Chadd 	ieee80211_tx_complete(ni, m0, status);
45879352fb7aSAdrian Chadd }
45889352fb7aSAdrian Chadd 
45893feffbd7SAdrian Chadd static struct ath_buf *
45903feffbd7SAdrian Chadd ath_tx_draintxq_get_one(struct ath_softc *sc, struct ath_txq *txq)
45913feffbd7SAdrian Chadd {
45923feffbd7SAdrian Chadd 	struct ath_buf *bf;
45933feffbd7SAdrian Chadd 
45943feffbd7SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
45953feffbd7SAdrian Chadd 
45963feffbd7SAdrian Chadd 	/*
45973feffbd7SAdrian Chadd 	 * Drain the FIFO queue first, then if it's
45983feffbd7SAdrian Chadd 	 * empty, move to the normal frame queue.
45993feffbd7SAdrian Chadd 	 */
46003feffbd7SAdrian Chadd 	bf = TAILQ_FIRST(&txq->fifo.axq_q);
46013feffbd7SAdrian Chadd 	if (bf != NULL) {
46023feffbd7SAdrian Chadd 		/*
46033feffbd7SAdrian Chadd 		 * Is it the last buffer in this set?
46043feffbd7SAdrian Chadd 		 * Decrement the FIFO counter.
46053feffbd7SAdrian Chadd 		 */
46063feffbd7SAdrian Chadd 		if (bf->bf_flags & ATH_BUF_FIFOEND) {
46073feffbd7SAdrian Chadd 			if (txq->axq_fifo_depth == 0) {
46083feffbd7SAdrian Chadd 				device_printf(sc->sc_dev,
46093feffbd7SAdrian Chadd 				    "%s: Q%d: fifo_depth=0, fifo.axq_depth=%d?\n",
46103feffbd7SAdrian Chadd 				    __func__,
46113feffbd7SAdrian Chadd 				    txq->axq_qnum,
46123feffbd7SAdrian Chadd 				    txq->fifo.axq_depth);
46133feffbd7SAdrian Chadd 			} else
46143feffbd7SAdrian Chadd 				txq->axq_fifo_depth--;
46153feffbd7SAdrian Chadd 		}
46163feffbd7SAdrian Chadd 		ATH_TXQ_REMOVE(&txq->fifo, bf, bf_list);
46173feffbd7SAdrian Chadd 		return (bf);
46183feffbd7SAdrian Chadd 	}
46193feffbd7SAdrian Chadd 
46203feffbd7SAdrian Chadd 	/*
46213feffbd7SAdrian Chadd 	 * Debugging!
46223feffbd7SAdrian Chadd 	 */
46233feffbd7SAdrian Chadd 	if (txq->axq_fifo_depth != 0 || txq->fifo.axq_depth != 0) {
46243feffbd7SAdrian Chadd 		device_printf(sc->sc_dev,
46253feffbd7SAdrian Chadd 		    "%s: Q%d: fifo_depth=%d, fifo.axq_depth=%d\n",
46263feffbd7SAdrian Chadd 		    __func__,
46273feffbd7SAdrian Chadd 		    txq->axq_qnum,
46283feffbd7SAdrian Chadd 		    txq->axq_fifo_depth,
46293feffbd7SAdrian Chadd 		    txq->fifo.axq_depth);
46303feffbd7SAdrian Chadd 	}
46313feffbd7SAdrian Chadd 
46323feffbd7SAdrian Chadd 	/*
46333feffbd7SAdrian Chadd 	 * Now drain the pending queue.
46343feffbd7SAdrian Chadd 	 */
46353feffbd7SAdrian Chadd 	bf = TAILQ_FIRST(&txq->axq_q);
46363feffbd7SAdrian Chadd 	if (bf == NULL) {
46373feffbd7SAdrian Chadd 		txq->axq_link = NULL;
46383feffbd7SAdrian Chadd 		return (NULL);
46393feffbd7SAdrian Chadd 	}
46403feffbd7SAdrian Chadd 	ATH_TXQ_REMOVE(txq, bf, bf_list);
46413feffbd7SAdrian Chadd 	return (bf);
46423feffbd7SAdrian Chadd }
46433feffbd7SAdrian Chadd 
46449352fb7aSAdrian Chadd void
46451762ec94SAdrian Chadd ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
46465591b213SSam Leffler {
4647a585a9a1SSam Leffler #ifdef ATH_DEBUG
46485591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4649d2f6ed15SSam Leffler #endif
46505591b213SSam Leffler 	struct ath_buf *bf;
46517a4c5ed9SSam Leffler 	u_int ix;
46525591b213SSam Leffler 
4653c42a7b7eSSam Leffler 	/*
4654c42a7b7eSSam Leffler 	 * NB: this assumes output has been stopped and
46555d61b5e8SSam Leffler 	 *     we do not need to block ath_tx_proc
4656c42a7b7eSSam Leffler 	 */
46577a4c5ed9SSam Leffler 	for (ix = 0;; ix++) {
4658b837332dSAdrian Chadd 		ATH_TXQ_LOCK(txq);
46593feffbd7SAdrian Chadd 		bf = ath_tx_draintxq_get_one(sc, txq);
46605591b213SSam Leffler 		if (bf == NULL) {
4661b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
46625591b213SSam Leffler 			break;
46635591b213SSam Leffler 		}
46646edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
46656edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
4666a585a9a1SSam Leffler #ifdef ATH_DEBUG
46674a3ac3fcSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RESET) {
4668b032f27cSSam Leffler 			struct ieee80211com *ic = sc->sc_ifp->if_l2com;
46691762ec94SAdrian Chadd 			int status = 0;
4670b032f27cSSam Leffler 
46711762ec94SAdrian Chadd 			/*
46721762ec94SAdrian Chadd 			 * EDMA operation has a TX completion FIFO
46731762ec94SAdrian Chadd 			 * separate from the TX descriptor, so this
46741762ec94SAdrian Chadd 			 * method of checking the "completion" status
46751762ec94SAdrian Chadd 			 * is wrong.
46761762ec94SAdrian Chadd 			 */
46771762ec94SAdrian Chadd 			if (! sc->sc_isedma) {
46781762ec94SAdrian Chadd 				status = (ath_hal_txprocdesc(ah,
46791762ec94SAdrian Chadd 				    bf->bf_lastds,
468065f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
46811762ec94SAdrian Chadd 			}
46821762ec94SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, ix, status);
4683e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ic, mtod(bf->bf_m, const uint8_t *),
46844a3ac3fcSSam Leffler 			    bf->bf_m->m_len, 0, -1);
46854a3ac3fcSSam Leffler 		}
4686a585a9a1SSam Leffler #endif /* ATH_DEBUG */
468723428eafSSam Leffler 		/*
46889352fb7aSAdrian Chadd 		 * Since we're now doing magic in the completion
46899352fb7aSAdrian Chadd 		 * functions, we -must- call it for aggregation
46909352fb7aSAdrian Chadd 		 * destinations or BAW tracking will get upset.
469123428eafSSam Leffler 		 */
46929352fb7aSAdrian Chadd 		/*
46939352fb7aSAdrian Chadd 		 * Clear ATH_BUF_BUSY; the completion handler
46949352fb7aSAdrian Chadd 		 * will free the buffer.
46959352fb7aSAdrian Chadd 		 */
4696b837332dSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
469710ad9a77SSam Leffler 		bf->bf_flags &= ~ATH_BUF_BUSY;
46989352fb7aSAdrian Chadd 		if (bf->bf_comp)
46999352fb7aSAdrian Chadd 			bf->bf_comp(sc, bf, 1);
47009352fb7aSAdrian Chadd 		else
47019352fb7aSAdrian Chadd 			ath_tx_default_comp(sc, bf, 1);
47025591b213SSam Leffler 	}
47039352fb7aSAdrian Chadd 
4704eb6f0de0SAdrian Chadd 	/*
4705629ce218SAdrian Chadd 	 * Free the holding buffer if it exists
4706629ce218SAdrian Chadd 	 */
47075e018508SAdrian Chadd 	ATH_TXQ_LOCK(txq);
4708629ce218SAdrian Chadd 	ath_txq_freeholdingbuf(sc, txq);
47095e018508SAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
4710629ce218SAdrian Chadd 
4711629ce218SAdrian Chadd 	/*
4712eb6f0de0SAdrian Chadd 	 * Drain software queued frames which are on
4713eb6f0de0SAdrian Chadd 	 * active TIDs.
4714eb6f0de0SAdrian Chadd 	 */
4715eb6f0de0SAdrian Chadd 	ath_tx_txq_drain(sc, txq);
4716c42a7b7eSSam Leffler }
4717c42a7b7eSSam Leffler 
4718c42a7b7eSSam Leffler static void
4719c42a7b7eSSam Leffler ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
4720c42a7b7eSSam Leffler {
4721c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4722c42a7b7eSSam Leffler 
47239be82a42SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
47249be82a42SAdrian Chadd 
47259d2a962bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
4726dfaf8de9SAdrian Chadd 	    "%s: tx queue [%u] %p, active=%d, hwpending=%d, flags 0x%08x, "
4727dfaf8de9SAdrian Chadd 	    "link %p, holdingbf=%p\n",
47289d2a962bSAdrian Chadd 	    __func__,
47299d2a962bSAdrian Chadd 	    txq->axq_qnum,
47306891c875SPeter Wemm 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
47318d060542SAdrian Chadd 	    (int) (!! ath_hal_txqenabled(ah, txq->axq_qnum)),
47328d060542SAdrian Chadd 	    (int) ath_hal_numtxpending(ah, txq->axq_qnum),
47339d2a962bSAdrian Chadd 	    txq->axq_flags,
4734dfaf8de9SAdrian Chadd 	    txq->axq_link,
4735dfaf8de9SAdrian Chadd 	    txq->axq_holdingbf);
4736dfaf8de9SAdrian Chadd 
47374a3ac3fcSSam Leffler 	(void) ath_hal_stoptxdma(ah, txq->axq_qnum);
47389be82a42SAdrian Chadd 	/* We've stopped TX DMA, so mark this as stopped. */
47399be82a42SAdrian Chadd 	txq->axq_flags &= ~ATH_TXQ_PUTRUNNING;
4740dfaf8de9SAdrian Chadd 
4741dfaf8de9SAdrian Chadd #ifdef	ATH_DEBUG
4742dfaf8de9SAdrian Chadd 	if ((sc->sc_debug & ATH_DEBUG_RESET)
4743dfaf8de9SAdrian Chadd 	    && (txq->axq_holdingbf != NULL)) {
4744dfaf8de9SAdrian Chadd 		ath_printtxbuf(sc, txq->axq_holdingbf, txq->axq_qnum, 0, 0);
4745dfaf8de9SAdrian Chadd 	}
4746dfaf8de9SAdrian Chadd #endif
4747c42a7b7eSSam Leffler }
4748c42a7b7eSSam Leffler 
4749bad98824SAdrian Chadd int
47502d433424SAdrian Chadd ath_stoptxdma(struct ath_softc *sc)
4751c42a7b7eSSam Leffler {
4752c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4753c42a7b7eSSam Leffler 	int i;
4754c42a7b7eSSam Leffler 
4755c42a7b7eSSam Leffler 	/* XXX return value */
47562d433424SAdrian Chadd 	if (sc->sc_invalid)
47572d433424SAdrian Chadd 		return 0;
47582d433424SAdrian Chadd 
4759c42a7b7eSSam Leffler 	if (!sc->sc_invalid) {
4760c42a7b7eSSam Leffler 		/* don't touch the hardware if marked invalid */
47614a3ac3fcSSam Leffler 		DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
47624a3ac3fcSSam Leffler 		    __func__, sc->sc_bhalq,
47634a3ac3fcSSam Leffler 		    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq),
47644a3ac3fcSSam Leffler 		    NULL);
47659be82a42SAdrian Chadd 
47669be82a42SAdrian Chadd 		/* stop the beacon queue */
4767c42a7b7eSSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
47689be82a42SAdrian Chadd 
47699be82a42SAdrian Chadd 		/* Stop the data queues */
47709be82a42SAdrian Chadd 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
47719be82a42SAdrian Chadd 			if (ATH_TXQ_SETUP(sc, i)) {
47729be82a42SAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
4773c42a7b7eSSam Leffler 				ath_tx_stopdma(sc, &sc->sc_txq[i]);
47749be82a42SAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
47759be82a42SAdrian Chadd 			}
47769be82a42SAdrian Chadd 		}
4777c42a7b7eSSam Leffler 	}
47782d433424SAdrian Chadd 
47792d433424SAdrian Chadd 	return 1;
47802d433424SAdrian Chadd }
47812d433424SAdrian Chadd 
478207187d11SAdrian Chadd #ifdef	ATH_DEBUG
47839be82a42SAdrian Chadd void
4784ed261a61SAdrian Chadd ath_tx_dump(struct ath_softc *sc, struct ath_txq *txq)
4785ed261a61SAdrian Chadd {
4786ed261a61SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
4787ed261a61SAdrian Chadd 	struct ath_buf *bf;
4788ed261a61SAdrian Chadd 	int i = 0;
4789ed261a61SAdrian Chadd 
4790ed261a61SAdrian Chadd 	if (! (sc->sc_debug & ATH_DEBUG_RESET))
4791ed261a61SAdrian Chadd 		return;
4792ed261a61SAdrian Chadd 
4793ed261a61SAdrian Chadd 	device_printf(sc->sc_dev, "%s: Q%d: begin\n",
4794ed261a61SAdrian Chadd 	    __func__, txq->axq_qnum);
4795ed261a61SAdrian Chadd 	TAILQ_FOREACH(bf, &txq->axq_q, bf_list) {
4796ed261a61SAdrian Chadd 		ath_printtxbuf(sc, bf, txq->axq_qnum, i,
4797ed261a61SAdrian Chadd 			ath_hal_txprocdesc(ah, bf->bf_lastds,
4798ed261a61SAdrian Chadd 			    &bf->bf_status.ds_txstat) == HAL_OK);
4799ed261a61SAdrian Chadd 		i++;
4800ed261a61SAdrian Chadd 	}
4801ed261a61SAdrian Chadd 	device_printf(sc->sc_dev, "%s: Q%d: end\n",
4802ed261a61SAdrian Chadd 	    __func__, txq->axq_qnum);
4803ed261a61SAdrian Chadd }
480407187d11SAdrian Chadd #endif /* ATH_DEBUG */
4805ed261a61SAdrian Chadd 
48062d433424SAdrian Chadd /*
48072d433424SAdrian Chadd  * Drain the transmit queues and reclaim resources.
48082d433424SAdrian Chadd  */
4809788e6aa9SAdrian Chadd void
4810788e6aa9SAdrian Chadd ath_legacy_tx_drain(struct ath_softc *sc, ATH_RESET_TYPE reset_type)
48112d433424SAdrian Chadd {
48122d433424SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
48132d433424SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
48142d433424SAdrian Chadd 	int i;
48159be82a42SAdrian Chadd 	struct ath_buf *bf_last;
48162d433424SAdrian Chadd 
48172d433424SAdrian Chadd 	(void) ath_stoptxdma(sc);
48182d433424SAdrian Chadd 
4819ed261a61SAdrian Chadd 	/*
4820ed261a61SAdrian Chadd 	 * Dump the queue contents
4821ed261a61SAdrian Chadd 	 */
4822ef27340cSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
4823ef27340cSAdrian Chadd 		/*
4824ef27340cSAdrian Chadd 		 * XXX TODO: should we just handle the completed TX frames
4825ef27340cSAdrian Chadd 		 * here, whether or not the reset is a full one or not?
4826ef27340cSAdrian Chadd 		 */
4827ef27340cSAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
482807187d11SAdrian Chadd #ifdef	ATH_DEBUG
4829ed261a61SAdrian Chadd 			if (sc->sc_debug & ATH_DEBUG_RESET)
4830ed261a61SAdrian Chadd 				ath_tx_dump(sc, &sc->sc_txq[i]);
483107187d11SAdrian Chadd #endif	/* ATH_DEBUG */
48328328d6e4SAdrian Chadd 			if (reset_type == ATH_RESET_NOLOSS) {
4833ef27340cSAdrian Chadd 				ath_tx_processq(sc, &sc->sc_txq[i], 0);
48348328d6e4SAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
48358328d6e4SAdrian Chadd 				/*
48368328d6e4SAdrian Chadd 				 * Free the holding buffer; DMA is now
48378328d6e4SAdrian Chadd 				 * stopped.
48388328d6e4SAdrian Chadd 				 */
48398328d6e4SAdrian Chadd 				ath_txq_freeholdingbuf(sc, &sc->sc_txq[i]);
48408328d6e4SAdrian Chadd 				/*
48419be82a42SAdrian Chadd 				 * Setup the link pointer to be the
48429be82a42SAdrian Chadd 				 * _last_ buffer/descriptor in the list.
48439be82a42SAdrian Chadd 				 * If there's nothing in the list, set it
48449be82a42SAdrian Chadd 				 * to NULL.
48458328d6e4SAdrian Chadd 				 */
48469be82a42SAdrian Chadd 				bf_last = ATH_TXQ_LAST(&sc->sc_txq[i],
48479be82a42SAdrian Chadd 				    axq_q_s);
48489be82a42SAdrian Chadd 				if (bf_last != NULL) {
48499be82a42SAdrian Chadd 					ath_hal_gettxdesclinkptr(ah,
48509be82a42SAdrian Chadd 					    bf_last->bf_lastds,
48519be82a42SAdrian Chadd 					    &sc->sc_txq[i].axq_link);
48529be82a42SAdrian Chadd 				} else {
48538328d6e4SAdrian Chadd 					sc->sc_txq[i].axq_link = NULL;
48549be82a42SAdrian Chadd 				}
48558328d6e4SAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
48568328d6e4SAdrian Chadd 			} else
4857c42a7b7eSSam Leffler 				ath_tx_draintxq(sc, &sc->sc_txq[i]);
4858ef27340cSAdrian Chadd 		}
4859ef27340cSAdrian Chadd 	}
48604a3ac3fcSSam Leffler #ifdef ATH_DEBUG
48614a3ac3fcSSam Leffler 	if (sc->sc_debug & ATH_DEBUG_RESET) {
48626b349e5aSAdrian Chadd 		struct ath_buf *bf = TAILQ_FIRST(&sc->sc_bbuf);
48634a3ac3fcSSam Leffler 		if (bf != NULL && bf->bf_m != NULL) {
48646902009eSSam Leffler 			ath_printtxbuf(sc, bf, sc->sc_bhalq, 0,
48656edf1dc7SAdrian Chadd 				ath_hal_txprocdesc(ah, bf->bf_lastds,
486665f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
4867e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ifp->if_l2com,
4868e40b6ab1SSam Leffler 			    mtod(bf->bf_m, const uint8_t *), bf->bf_m->m_len,
4869e40b6ab1SSam Leffler 			    0, -1);
48704a3ac3fcSSam Leffler 		}
48714a3ac3fcSSam Leffler 	}
48724a3ac3fcSSam Leffler #endif /* ATH_DEBUG */
4873e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
487413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4875e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
48762e986da5SSam Leffler 	sc->sc_wd_timer = 0;
48775591b213SSam Leffler }
48785591b213SSam Leffler 
48795591b213SSam Leffler /*
4880c42a7b7eSSam Leffler  * Update internal state after a channel change.
4881c42a7b7eSSam Leffler  */
4882c42a7b7eSSam Leffler static void
4883c42a7b7eSSam Leffler ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
4884c42a7b7eSSam Leffler {
4885c42a7b7eSSam Leffler 	enum ieee80211_phymode mode;
4886c42a7b7eSSam Leffler 
4887c42a7b7eSSam Leffler 	/*
4888c42a7b7eSSam Leffler 	 * Change channels and update the h/w rate map
4889c42a7b7eSSam Leffler 	 * if we're switching; e.g. 11a to 11b/g.
4890c42a7b7eSSam Leffler 	 */
489168e8e04eSSam Leffler 	mode = ieee80211_chan2mode(chan);
4892c42a7b7eSSam Leffler 	if (mode != sc->sc_curmode)
4893c42a7b7eSSam Leffler 		ath_setcurmode(sc, mode);
489459efa8b5SSam Leffler 	sc->sc_curchan = chan;
4895c42a7b7eSSam Leffler }
4896c42a7b7eSSam Leffler 
4897c42a7b7eSSam Leffler /*
48985591b213SSam Leffler  * Set/change channels.  If the channel is really being changed,
48994fa8d4efSDaniel Eischen  * it's done by resetting the chip.  To accomplish this we must
49005591b213SSam Leffler  * first cleanup any pending DMA, then restart stuff after a la
49015591b213SSam Leffler  * ath_init.
49025591b213SSam Leffler  */
49035591b213SSam Leffler static int
49045591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
49055591b213SSam Leffler {
4906b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
4907b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
49085591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4909ef27340cSAdrian Chadd 	int ret = 0;
4910ef27340cSAdrian Chadd 
4911ef27340cSAdrian Chadd 	/* Treat this as an interface reset */
4912d52f7132SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
4913d52f7132SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
4914d52f7132SAdrian Chadd 
4915d52f7132SAdrian Chadd 	/* (Try to) stop TX/RX from occuring */
4916d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
4917d52f7132SAdrian Chadd 
4918ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4919904e385eSAdrian Chadd 
4920904e385eSAdrian Chadd 	/* Stop new RX/TX/interrupt completion */
4921ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
4922ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
4923ef27340cSAdrian Chadd 		    __func__);
4924ee321975SAdrian Chadd 	}
4925904e385eSAdrian Chadd 
4926904e385eSAdrian Chadd 	ath_hal_intrset(ah, 0);
4927904e385eSAdrian Chadd 
4928904e385eSAdrian Chadd 	/* Stop pending RX/TX completion */
4929904e385eSAdrian Chadd 	ath_txrx_stop_locked(sc);
4930904e385eSAdrian Chadd 
4931ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4932c42a7b7eSSam Leffler 
493359efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz, flags 0x%x)\n",
493459efa8b5SSam Leffler 	    __func__, ieee80211_chan2ieee(ic, chan),
493559efa8b5SSam Leffler 	    chan->ic_freq, chan->ic_flags);
493659efa8b5SSam Leffler 	if (chan != sc->sc_curchan) {
4937c42a7b7eSSam Leffler 		HAL_STATUS status;
49385591b213SSam Leffler 		/*
49395591b213SSam Leffler 		 * To switch channels clear any pending DMA operations;
49405591b213SSam Leffler 		 * wait long enough for the RX fifo to drain, reset the
49415591b213SSam Leffler 		 * hardware at the new frequency, and then re-enable
49425591b213SSam Leffler 		 * the relevant bits of the h/w.
49435591b213SSam Leffler 		 */
4944ef27340cSAdrian Chadd #if 0
49455591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
4946ef27340cSAdrian Chadd #endif
49479a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* turn off frame recv */
49489a842e8bSAdrian Chadd 		/*
49499a842e8bSAdrian Chadd 		 * First, handle completed TX/RX frames.
49509a842e8bSAdrian Chadd 		 */
4951f8cc9b09SAdrian Chadd 		ath_rx_flush(sc);
49529a842e8bSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_NOLOSS);
49539a842e8bSAdrian Chadd 		/*
49549a842e8bSAdrian Chadd 		 * Next, flush the non-scheduled frames.
49559a842e8bSAdrian Chadd 		 */
4956517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_FULL);	/* clear pending tx frames */
49579a842e8bSAdrian Chadd 
49586322256bSAdrian Chadd 		ath_update_chainmasks(sc, chan);
49596322256bSAdrian Chadd 		ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
49606322256bSAdrian Chadd 		    sc->sc_cur_rxchainmask);
496159efa8b5SSam Leffler 		if (!ath_hal_reset(ah, sc->sc_opmode, chan, AH_TRUE, &status)) {
4962b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to reset "
496379649302SGavin Atkinson 			    "channel %u (%u MHz, flags 0x%x), hal status %u\n",
496459efa8b5SSam Leffler 			    __func__, ieee80211_chan2ieee(ic, chan),
496559efa8b5SSam Leffler 			    chan->ic_freq, chan->ic_flags, status);
4966ef27340cSAdrian Chadd 			ret = EIO;
4967ef27340cSAdrian Chadd 			goto finish;
49685591b213SSam Leffler 		}
4969c59005e9SSam Leffler 		sc->sc_diversity = ath_hal_getdiversity(ah);
4970c42a7b7eSSam Leffler 
497148237774SAdrian Chadd 		/* Let DFS at it in case it's a DFS channel */
4972398bca2eSAdrian Chadd 		ath_dfs_radar_enable(sc, chan);
497348237774SAdrian Chadd 
49749af351f9SAdrian Chadd 		/* Let spectral at in case spectral is enabled */
49759af351f9SAdrian Chadd 		ath_spectral_enable(sc, chan);
49769af351f9SAdrian Chadd 
49775591b213SSam Leffler 		/*
4978b70f530bSAdrian Chadd 		 * Let bluetooth coexistence at in case it's needed for this
4979b70f530bSAdrian Chadd 		 * channel
4980b70f530bSAdrian Chadd 		 */
4981b70f530bSAdrian Chadd 		ath_btcoex_enable(sc, ic->ic_curchan);
4982b70f530bSAdrian Chadd 
4983b70f530bSAdrian Chadd 		/*
4984dd6a574eSAdrian Chadd 		 * If we're doing TDMA, enforce the TXOP limitation for chips
4985dd6a574eSAdrian Chadd 		 * that support it.
4986dd6a574eSAdrian Chadd 		 */
4987dd6a574eSAdrian Chadd 		if (sc->sc_hasenforcetxop && sc->sc_tdma)
4988dd6a574eSAdrian Chadd 			ath_hal_setenforcetxop(sc->sc_ah, 1);
4989dd6a574eSAdrian Chadd 		else
4990dd6a574eSAdrian Chadd 			ath_hal_setenforcetxop(sc->sc_ah, 0);
4991dd6a574eSAdrian Chadd 
4992dd6a574eSAdrian Chadd 		/*
49935591b213SSam Leffler 		 * Re-enable rx framework.
49945591b213SSam Leffler 		 */
49955591b213SSam Leffler 		if (ath_startrecv(sc) != 0) {
4996b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to restart recv logic\n",
4997b032f27cSSam Leffler 			    __func__);
4998ef27340cSAdrian Chadd 			ret = EIO;
4999ef27340cSAdrian Chadd 			goto finish;
50005591b213SSam Leffler 		}
50015591b213SSam Leffler 
50025591b213SSam Leffler 		/*
50035591b213SSam Leffler 		 * Change channels and update the h/w rate map
50045591b213SSam Leffler 		 * if we're switching; e.g. 11a to 11b/g.
50055591b213SSam Leffler 		 */
5006c42a7b7eSSam Leffler 		ath_chan_change(sc, chan);
50070a915fadSSam Leffler 
50080a915fadSSam Leffler 		/*
50092fd9aabbSAdrian Chadd 		 * Reset clears the beacon timers; reset them
50102fd9aabbSAdrian Chadd 		 * here if needed.
50112fd9aabbSAdrian Chadd 		 */
50122fd9aabbSAdrian Chadd 		if (sc->sc_beacons) {		/* restart beacons */
50132fd9aabbSAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
50142fd9aabbSAdrian Chadd 			if (sc->sc_tdma)
50152fd9aabbSAdrian Chadd 				ath_tdma_config(sc, NULL);
50162fd9aabbSAdrian Chadd 			else
50172fd9aabbSAdrian Chadd #endif
50182fd9aabbSAdrian Chadd 			ath_beacon_config(sc, NULL);
50192fd9aabbSAdrian Chadd 		}
50202fd9aabbSAdrian Chadd 
50212fd9aabbSAdrian Chadd 		/*
50220a915fadSSam Leffler 		 * Re-enable interrupts.
50230a915fadSSam Leffler 		 */
5024e78719adSAdrian Chadd #if 0
50250a915fadSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
5026ef27340cSAdrian Chadd #endif
50275591b213SSam Leffler 	}
5028ef27340cSAdrian Chadd 
5029ef27340cSAdrian Chadd finish:
5030ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5031ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
5032ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
5033ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
5034ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5035ef27340cSAdrian Chadd 
5036e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
5037ef27340cSAdrian Chadd 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5038e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
5039ef27340cSAdrian Chadd 	ath_txrx_start(sc);
5040ef27340cSAdrian Chadd 	/* XXX ath_start? */
5041ef27340cSAdrian Chadd 
5042ef27340cSAdrian Chadd 	return ret;
50435591b213SSam Leffler }
50445591b213SSam Leffler 
50455591b213SSam Leffler /*
50465591b213SSam Leffler  * Periodically recalibrate the PHY to account
50475591b213SSam Leffler  * for temperature/environment changes.
50485591b213SSam Leffler  */
50495591b213SSam Leffler static void
50505591b213SSam Leffler ath_calibrate(void *arg)
50515591b213SSam Leffler {
50525591b213SSam Leffler 	struct ath_softc *sc = arg;
50535591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
50542dc7fcc4SSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
50558d91de92SSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
5056943e37a1SAdrian Chadd 	HAL_BOOL longCal, isCalDone = AH_TRUE;
5057a108ab63SAdrian Chadd 	HAL_BOOL aniCal, shortCal = AH_FALSE;
50582dc7fcc4SSam Leffler 	int nextcal;
50595591b213SSam Leffler 
50608d91de92SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SCAN)	/* defer, off channel */
50618d91de92SSam Leffler 		goto restart;
50622dc7fcc4SSam Leffler 	longCal = (ticks - sc->sc_lastlongcal >= ath_longcalinterval*hz);
5063a108ab63SAdrian Chadd 	aniCal = (ticks - sc->sc_lastani >= ath_anicalinterval*hz/1000);
5064a108ab63SAdrian Chadd 	if (sc->sc_doresetcal)
5065a108ab63SAdrian Chadd 		shortCal = (ticks - sc->sc_lastshortcal >= ath_shortcalinterval*hz/1000);
5066a108ab63SAdrian Chadd 
5067a108ab63SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: shortCal=%d; longCal=%d; aniCal=%d\n", __func__, shortCal, longCal, aniCal);
5068a108ab63SAdrian Chadd 	if (aniCal) {
5069a108ab63SAdrian Chadd 		sc->sc_stats.ast_ani_cal++;
5070a108ab63SAdrian Chadd 		sc->sc_lastani = ticks;
5071a108ab63SAdrian Chadd 		ath_hal_ani_poll(ah, sc->sc_curchan);
5072a108ab63SAdrian Chadd 	}
5073a108ab63SAdrian Chadd 
50742dc7fcc4SSam Leffler 	if (longCal) {
50755591b213SSam Leffler 		sc->sc_stats.ast_per_cal++;
50768197f57eSAdrian Chadd 		sc->sc_lastlongcal = ticks;
50775591b213SSam Leffler 		if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
50785591b213SSam Leffler 			/*
50795591b213SSam Leffler 			 * Rfgain is out of bounds, reset the chip
50805591b213SSam Leffler 			 * to load new gain values.
50815591b213SSam Leffler 			 */
5082370572d9SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
5083370572d9SSam Leffler 				"%s: rfgain change\n", __func__);
50845591b213SSam Leffler 			sc->sc_stats.ast_per_rfgain++;
5085ef27340cSAdrian Chadd 			sc->sc_resetcal = 0;
5086ef27340cSAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
5087d52f7132SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
5088d52f7132SAdrian Chadd 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
5089ef27340cSAdrian Chadd 			return;
50905591b213SSam Leffler 		}
50912dc7fcc4SSam Leffler 		/*
50922dc7fcc4SSam Leffler 		 * If this long cal is after an idle period, then
50932dc7fcc4SSam Leffler 		 * reset the data collection state so we start fresh.
50942dc7fcc4SSam Leffler 		 */
50952dc7fcc4SSam Leffler 		if (sc->sc_resetcal) {
509659efa8b5SSam Leffler 			(void) ath_hal_calreset(ah, sc->sc_curchan);
50972dc7fcc4SSam Leffler 			sc->sc_lastcalreset = ticks;
5098a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
50992dc7fcc4SSam Leffler 			sc->sc_resetcal = 0;
5100a108ab63SAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
51012dc7fcc4SSam Leffler 		}
51022dc7fcc4SSam Leffler 	}
5103a108ab63SAdrian Chadd 
5104a108ab63SAdrian Chadd 	/* Only call if we're doing a short/long cal, not for ANI calibration */
5105a108ab63SAdrian Chadd 	if (shortCal || longCal) {
5106943e37a1SAdrian Chadd 		isCalDone = AH_FALSE;
510759efa8b5SSam Leffler 		if (ath_hal_calibrateN(ah, sc->sc_curchan, longCal, &isCalDone)) {
51082dc7fcc4SSam Leffler 			if (longCal) {
51092dc7fcc4SSam Leffler 				/*
51102dc7fcc4SSam Leffler 				 * Calibrate noise floor data again in case of change.
51112dc7fcc4SSam Leffler 				 */
51122dc7fcc4SSam Leffler 				ath_hal_process_noisefloor(ah);
51132dc7fcc4SSam Leffler 			}
51142dc7fcc4SSam Leffler 		} else {
5115c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
5116c42a7b7eSSam Leffler 				"%s: calibration of channel %u failed\n",
511759efa8b5SSam Leffler 				__func__, sc->sc_curchan->ic_freq);
51185591b213SSam Leffler 			sc->sc_stats.ast_per_calfail++;
51195591b213SSam Leffler 		}
5120a108ab63SAdrian Chadd 		if (shortCal)
5121a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
5122a108ab63SAdrian Chadd 	}
51232dc7fcc4SSam Leffler 	if (!isCalDone) {
51248d91de92SSam Leffler restart:
51257b0c77ecSSam Leffler 		/*
51262dc7fcc4SSam Leffler 		 * Use a shorter interval to potentially collect multiple
51272dc7fcc4SSam Leffler 		 * data samples required to complete calibration.  Once
51282dc7fcc4SSam Leffler 		 * we're told the work is done we drop back to a longer
51292dc7fcc4SSam Leffler 		 * interval between requests.  We're more aggressive doing
51302dc7fcc4SSam Leffler 		 * work when operating as an AP to improve operation right
51312dc7fcc4SSam Leffler 		 * after startup.
51327b0c77ecSSam Leffler 		 */
5133a108ab63SAdrian Chadd 		sc->sc_lastshortcal = ticks;
5134a108ab63SAdrian Chadd 		nextcal = ath_shortcalinterval*hz/1000;
51352dc7fcc4SSam Leffler 		if (sc->sc_opmode != HAL_M_HOSTAP)
51362dc7fcc4SSam Leffler 			nextcal *= 10;
5137a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_TRUE;
51382dc7fcc4SSam Leffler 	} else {
5139a108ab63SAdrian Chadd 		/* nextcal should be the shortest time for next event */
51402dc7fcc4SSam Leffler 		nextcal = ath_longcalinterval*hz;
51412dc7fcc4SSam Leffler 		if (sc->sc_lastcalreset == 0)
51422dc7fcc4SSam Leffler 			sc->sc_lastcalreset = sc->sc_lastlongcal;
51432dc7fcc4SSam Leffler 		else if (ticks - sc->sc_lastcalreset >= ath_resetcalinterval*hz)
51442dc7fcc4SSam Leffler 			sc->sc_resetcal = 1;	/* setup reset next trip */
5145a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_FALSE;
5146bd5a9920SSam Leffler 	}
5147a108ab63SAdrian Chadd 	/* ANI calibration may occur more often than short/long/resetcal */
5148a108ab63SAdrian Chadd 	if (ath_anicalinterval > 0)
5149a108ab63SAdrian Chadd 		nextcal = MIN(nextcal, ath_anicalinterval*hz/1000);
5150bd5a9920SSam Leffler 
51512dc7fcc4SSam Leffler 	if (nextcal != 0) {
51522dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: next +%u (%sisCalDone)\n",
51532dc7fcc4SSam Leffler 		    __func__, nextcal, isCalDone ? "" : "!");
51542dc7fcc4SSam Leffler 		callout_reset(&sc->sc_cal_ch, nextcal, ath_calibrate, sc);
51552dc7fcc4SSam Leffler 	} else {
51562dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: calibration disabled\n",
51572dc7fcc4SSam Leffler 		    __func__);
51582dc7fcc4SSam Leffler 		/* NB: don't rearm timer */
51592dc7fcc4SSam Leffler 	}
51605591b213SSam Leffler }
51615591b213SSam Leffler 
516268e8e04eSSam Leffler static void
516368e8e04eSSam Leffler ath_scan_start(struct ieee80211com *ic)
516468e8e04eSSam Leffler {
516568e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
516668e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
516768e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
516868e8e04eSSam Leffler 	u_int32_t rfilt;
516968e8e04eSSam Leffler 
517068e8e04eSSam Leffler 	/* XXX calibration timer? */
517168e8e04eSSam Leffler 
5172c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
517368e8e04eSSam Leffler 	sc->sc_scanning = 1;
517468e8e04eSSam Leffler 	sc->sc_syncbeacon = 0;
517568e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5176c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
5177c98cefc5SAdrian Chadd 
5178c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
517968e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
518068e8e04eSSam Leffler 	ath_hal_setassocid(ah, ifp->if_broadcastaddr, 0);
5181c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
518268e8e04eSSam Leffler 
518368e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0\n",
518468e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(ifp->if_broadcastaddr));
518568e8e04eSSam Leffler }
518668e8e04eSSam Leffler 
518768e8e04eSSam Leffler static void
518868e8e04eSSam Leffler ath_scan_end(struct ieee80211com *ic)
518968e8e04eSSam Leffler {
519068e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
519168e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
519268e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
519368e8e04eSSam Leffler 	u_int32_t rfilt;
519468e8e04eSSam Leffler 
5195c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
519668e8e04eSSam Leffler 	sc->sc_scanning = 0;
519768e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5198c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
5199c98cefc5SAdrian Chadd 
5200c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
520168e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
520268e8e04eSSam Leffler 	ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
520368e8e04eSSam Leffler 
520468e8e04eSSam Leffler 	ath_hal_process_noisefloor(ah);
5205c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
520668e8e04eSSam Leffler 
520768e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
520868e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(sc->sc_curbssid),
520968e8e04eSSam Leffler 		 sc->sc_curaid);
521068e8e04eSSam Leffler }
521168e8e04eSSam Leffler 
5212fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
5213e7200579SAdrian Chadd /*
5214e7200579SAdrian Chadd  * For now, just do a channel change.
5215e7200579SAdrian Chadd  *
5216e7200579SAdrian Chadd  * Later, we'll go through the hard slog of suspending tx/rx, changing rate
5217e7200579SAdrian Chadd  * control state and resetting the hardware without dropping frames out
5218e7200579SAdrian Chadd  * of the queue.
5219e7200579SAdrian Chadd  *
5220e7200579SAdrian Chadd  * The unfortunate trouble here is making absolutely sure that the
5221e7200579SAdrian Chadd  * channel width change has propagated enough so the hardware
5222e7200579SAdrian Chadd  * absolutely isn't handed bogus frames for it's current operating
5223e7200579SAdrian Chadd  * mode. (Eg, 40MHz frames in 20MHz mode.) Since TX and RX can and
5224e7200579SAdrian Chadd  * does occur in parallel, we need to make certain we've blocked
5225e7200579SAdrian Chadd  * any further ongoing TX (and RX, that can cause raw TX)
5226e7200579SAdrian Chadd  * before we do this.
5227e7200579SAdrian Chadd  */
5228e7200579SAdrian Chadd static void
5229e7200579SAdrian Chadd ath_update_chw(struct ieee80211com *ic)
5230e7200579SAdrian Chadd {
5231e7200579SAdrian Chadd 	struct ifnet *ifp = ic->ic_ifp;
5232e7200579SAdrian Chadd 	struct ath_softc *sc = ifp->if_softc;
5233e7200579SAdrian Chadd 
5234e7200579SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: called\n", __func__);
5235e7200579SAdrian Chadd 	ath_set_channel(ic);
5236e7200579SAdrian Chadd }
5237fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
5238e7200579SAdrian Chadd 
523968e8e04eSSam Leffler static void
524068e8e04eSSam Leffler ath_set_channel(struct ieee80211com *ic)
524168e8e04eSSam Leffler {
524268e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
524368e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
524468e8e04eSSam Leffler 
524568e8e04eSSam Leffler 	(void) ath_chan_set(sc, ic->ic_curchan);
524668e8e04eSSam Leffler 	/*
524768e8e04eSSam Leffler 	 * If we are returning to our bss channel then mark state
524868e8e04eSSam Leffler 	 * so the next recv'd beacon's tsf will be used to sync the
524968e8e04eSSam Leffler 	 * beacon timers.  Note that since we only hear beacons in
525068e8e04eSSam Leffler 	 * sta/ibss mode this has no effect in other operating modes.
525168e8e04eSSam Leffler 	 */
5252a887b1e3SAdrian Chadd 	ATH_LOCK(sc);
525368e8e04eSSam Leffler 	if (!sc->sc_scanning && ic->ic_curchan == ic->ic_bsschan)
525468e8e04eSSam Leffler 		sc->sc_syncbeacon = 1;
5255a887b1e3SAdrian Chadd 	ATH_UNLOCK(sc);
525668e8e04eSSam Leffler }
525768e8e04eSSam Leffler 
5258b032f27cSSam Leffler /*
5259b032f27cSSam Leffler  * Walk the vap list and check if there any vap's in RUN state.
5260b032f27cSSam Leffler  */
52615591b213SSam Leffler static int
5262b032f27cSSam Leffler ath_isanyrunningvaps(struct ieee80211vap *this)
52635591b213SSam Leffler {
5264b032f27cSSam Leffler 	struct ieee80211com *ic = this->iv_ic;
5265b032f27cSSam Leffler 	struct ieee80211vap *vap;
5266b032f27cSSam Leffler 
5267b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
5268b032f27cSSam Leffler 
5269b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
5270309a3e45SSam Leffler 		if (vap != this && vap->iv_state >= IEEE80211_S_RUN)
5271b032f27cSSam Leffler 			return 1;
5272b032f27cSSam Leffler 	}
5273b032f27cSSam Leffler 	return 0;
5274b032f27cSSam Leffler }
5275b032f27cSSam Leffler 
5276b032f27cSSam Leffler static int
5277b032f27cSSam Leffler ath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
5278b032f27cSSam Leffler {
5279b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
5280b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5281b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
528245bbf62fSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5283b032f27cSSam Leffler 	struct ieee80211_node *ni = NULL;
528468e8e04eSSam Leffler 	int i, error, stamode;
52855591b213SSam Leffler 	u_int32_t rfilt;
5286f52efb6dSAdrian Chadd 	int csa_run_transition = 0;
5287a74ebfe5SAdrian Chadd 
52885591b213SSam Leffler 	static const HAL_LED_STATE leds[] = {
52895591b213SSam Leffler 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
52905591b213SSam Leffler 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
52915591b213SSam Leffler 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
52925591b213SSam Leffler 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
529377d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CAC */
52945591b213SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
529577d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CSA */
529677d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_SLEEP */
52975591b213SSam Leffler 	};
52985591b213SSam Leffler 
5299c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
5300b032f27cSSam Leffler 		ieee80211_state_name[vap->iv_state],
5301c42a7b7eSSam Leffler 		ieee80211_state_name[nstate]);
53025591b213SSam Leffler 
5303107fdf96SAdrian Chadd 	/*
5304107fdf96SAdrian Chadd 	 * net80211 _should_ have the comlock asserted at this point.
5305107fdf96SAdrian Chadd 	 * There are some comments around the calls to vap->iv_newstate
5306107fdf96SAdrian Chadd 	 * which indicate that it (newstate) may end up dropping the
5307107fdf96SAdrian Chadd 	 * lock.  This and the subsequent lock assert check after newstate
5308107fdf96SAdrian Chadd 	 * are an attempt to catch these and figure out how/why.
5309107fdf96SAdrian Chadd 	 */
5310107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
5311107fdf96SAdrian Chadd 
5312f52efb6dSAdrian Chadd 	if (vap->iv_state == IEEE80211_S_CSA && nstate == IEEE80211_S_RUN)
5313f52efb6dSAdrian Chadd 		csa_run_transition = 1;
5314f52efb6dSAdrian Chadd 
53152e986da5SSam Leffler 	callout_drain(&sc->sc_cal_ch);
53165591b213SSam Leffler 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
53175591b213SSam Leffler 
5318b032f27cSSam Leffler 	if (nstate == IEEE80211_S_SCAN) {
531958769f58SSam Leffler 		/*
5320b032f27cSSam Leffler 		 * Scanning: turn off beacon miss and don't beacon.
5321b032f27cSSam Leffler 		 * Mark beacon state so when we reach RUN state we'll
5322b032f27cSSam Leffler 		 * [re]setup beacons.  Unblock the task q thread so
5323b032f27cSSam Leffler 		 * deferred interrupt processing is done.
532458769f58SSam Leffler 		 */
5325b032f27cSSam Leffler 		ath_hal_intrset(ah,
5326b032f27cSSam Leffler 		    sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
53275591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5328b032f27cSSam Leffler 		sc->sc_beacons = 0;
5329b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
53305591b213SSam Leffler 	}
53315591b213SSam Leffler 
533280767531SAdrian Chadd 	ni = ieee80211_ref_node(vap->iv_bss);
533368e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5334b032f27cSSam Leffler 	stamode = (vap->iv_opmode == IEEE80211_M_STA ||
53357b916f89SSam Leffler 		   vap->iv_opmode == IEEE80211_M_AHDEMO ||
5336b032f27cSSam Leffler 		   vap->iv_opmode == IEEE80211_M_IBSS);
533768e8e04eSSam Leffler 	if (stamode && nstate == IEEE80211_S_RUN) {
533868e8e04eSSam Leffler 		sc->sc_curaid = ni->ni_associd;
533968e8e04eSSam Leffler 		IEEE80211_ADDR_COPY(sc->sc_curbssid, ni->ni_bssid);
5340b032f27cSSam Leffler 		ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
5341b032f27cSSam Leffler 	}
534268e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
5343b032f27cSSam Leffler 	   __func__, rfilt, ether_sprintf(sc->sc_curbssid), sc->sc_curaid);
534468e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
534568e8e04eSSam Leffler 
5346b032f27cSSam Leffler 	/* XXX is this to restore keycache on resume? */
5347b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_STA &&
5348b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY)) {
53495591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
53505591b213SSam Leffler 			if (ath_hal_keyisvalid(ah, i))
535168e8e04eSSam Leffler 				ath_hal_keysetmac(ah, i, ni->ni_bssid);
53525591b213SSam Leffler 	}
5353b032f27cSSam Leffler 
5354b032f27cSSam Leffler 	/*
5355b032f27cSSam Leffler 	 * Invoke the parent method to do net80211 work.
5356b032f27cSSam Leffler 	 */
5357b032f27cSSam Leffler 	error = avp->av_newstate(vap, nstate, arg);
5358b032f27cSSam Leffler 	if (error != 0)
5359b032f27cSSam Leffler 		goto bad;
5360c42a7b7eSSam Leffler 
5361107fdf96SAdrian Chadd 	/*
5362107fdf96SAdrian Chadd 	 * See above: ensure av_newstate() doesn't drop the lock
5363107fdf96SAdrian Chadd 	 * on us.
5364107fdf96SAdrian Chadd 	 */
5365107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
5366107fdf96SAdrian Chadd 
536768e8e04eSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
5368b032f27cSSam Leffler 		/* NB: collect bss node again, it may have changed */
536980767531SAdrian Chadd 		ieee80211_free_node(ni);
537080767531SAdrian Chadd 		ni = ieee80211_ref_node(vap->iv_bss);
53715591b213SSam Leffler 
5372b032f27cSSam Leffler 		DPRINTF(sc, ATH_DEBUG_STATE,
5373b032f27cSSam Leffler 		    "%s(RUN): iv_flags 0x%08x bintvl %d bssid %s "
5374b032f27cSSam Leffler 		    "capinfo 0x%04x chan %d\n", __func__,
5375b032f27cSSam Leffler 		    vap->iv_flags, ni->ni_intval, ether_sprintf(ni->ni_bssid),
5376b032f27cSSam Leffler 		    ni->ni_capinfo, ieee80211_chan2ieee(ic, ic->ic_curchan));
5377b032f27cSSam Leffler 
5378b032f27cSSam Leffler 		switch (vap->iv_opmode) {
5379584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
538010ad9a77SSam Leffler 		case IEEE80211_M_AHDEMO:
538110ad9a77SSam Leffler 			if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
538210ad9a77SSam Leffler 				break;
538310ad9a77SSam Leffler 			/* fall thru... */
538410ad9a77SSam Leffler #endif
5385e8fd88a3SSam Leffler 		case IEEE80211_M_HOSTAP:
5386e8fd88a3SSam Leffler 		case IEEE80211_M_IBSS:
538759aa14a9SRui Paulo 		case IEEE80211_M_MBSS:
53885591b213SSam Leffler 			/*
5389e8fd88a3SSam Leffler 			 * Allocate and setup the beacon frame.
5390e8fd88a3SSam Leffler 			 *
5391f818612bSSam Leffler 			 * Stop any previous beacon DMA.  This may be
5392f818612bSSam Leffler 			 * necessary, for example, when an ibss merge
5393f818612bSSam Leffler 			 * causes reconfiguration; there will be a state
5394f818612bSSam Leffler 			 * transition from RUN->RUN that means we may
5395f818612bSSam Leffler 			 * be called with beacon transmission active.
5396f818612bSSam Leffler 			 */
5397f818612bSSam Leffler 			ath_hal_stoptxdma(ah, sc->sc_bhalq);
5398b032f27cSSam Leffler 
53995591b213SSam Leffler 			error = ath_beacon_alloc(sc, ni);
54005591b213SSam Leffler 			if (error != 0)
54015591b213SSam Leffler 				goto bad;
54027a04dc27SSam Leffler 			/*
540380d939bfSSam Leffler 			 * If joining an adhoc network defer beacon timer
540480d939bfSSam Leffler 			 * configuration to the next beacon frame so we
540580d939bfSSam Leffler 			 * have a current TSF to use.  Otherwise we're
5406b032f27cSSam Leffler 			 * starting an ibss/bss so there's no need to delay;
5407b032f27cSSam Leffler 			 * if this is the first vap moving to RUN state, then
5408b032f27cSSam Leffler 			 * beacon state needs to be [re]configured.
54097a04dc27SSam Leffler 			 */
5410b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_IBSS &&
5411b032f27cSSam Leffler 			    ni->ni_tstamp.tsf != 0) {
541280d939bfSSam Leffler 				sc->sc_syncbeacon = 1;
5413b032f27cSSam Leffler 			} else if (!sc->sc_beacons) {
5414584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
541510ad9a77SSam Leffler 				if (vap->iv_caps & IEEE80211_C_TDMA)
541610ad9a77SSam Leffler 					ath_tdma_config(sc, vap);
541710ad9a77SSam Leffler 				else
541810ad9a77SSam Leffler #endif
5419b032f27cSSam Leffler 					ath_beacon_config(sc, vap);
5420b032f27cSSam Leffler 				sc->sc_beacons = 1;
5421b032f27cSSam Leffler 			}
5422e8fd88a3SSam Leffler 			break;
5423e8fd88a3SSam Leffler 		case IEEE80211_M_STA:
5424e8fd88a3SSam Leffler 			/*
542580d939bfSSam Leffler 			 * Defer beacon timer configuration to the next
542680d939bfSSam Leffler 			 * beacon frame so we have a current TSF to use
542780d939bfSSam Leffler 			 * (any TSF collected when scanning is likely old).
5428f52efb6dSAdrian Chadd 			 * However if it's due to a CSA -> RUN transition,
5429f52efb6dSAdrian Chadd 			 * force a beacon update so we pick up a lack of
5430f52efb6dSAdrian Chadd 			 * beacons from an AP in CAC and thus force a
5431f52efb6dSAdrian Chadd 			 * scan.
5432a74ebfe5SAdrian Chadd 			 *
5433a74ebfe5SAdrian Chadd 			 * And, there's also corner cases here where
5434a74ebfe5SAdrian Chadd 			 * after a scan, the AP may have disappeared.
5435a74ebfe5SAdrian Chadd 			 * In that case, we may not receive an actual
5436a74ebfe5SAdrian Chadd 			 * beacon to update the beacon timer and thus we
5437a74ebfe5SAdrian Chadd 			 * won't get notified of the missing beacons.
54387a04dc27SSam Leffler 			 */
543980d939bfSSam Leffler 			sc->sc_syncbeacon = 1;
5440a74ebfe5SAdrian Chadd #if 0
5441f52efb6dSAdrian Chadd 			if (csa_run_transition)
5442a74ebfe5SAdrian Chadd #endif
5443f52efb6dSAdrian Chadd 				ath_beacon_config(sc, vap);
5444a74ebfe5SAdrian Chadd 
5445a74ebfe5SAdrian Chadd 			/*
5446a74ebfe5SAdrian Chadd 			 * PR: kern/175227
5447a74ebfe5SAdrian Chadd 			 *
5448a74ebfe5SAdrian Chadd 			 * Reconfigure beacons during reset; as otherwise
5449a74ebfe5SAdrian Chadd 			 * we won't get the beacon timers reprogrammed
5450a74ebfe5SAdrian Chadd 			 * after a reset and thus we won't pick up a
5451a74ebfe5SAdrian Chadd 			 * beacon miss interrupt.
5452a74ebfe5SAdrian Chadd 			 *
5453a74ebfe5SAdrian Chadd 			 * Hopefully we'll see a beacon before the BMISS
5454a74ebfe5SAdrian Chadd 			 * timer fires (too often), leading to a STA
5455a74ebfe5SAdrian Chadd 			 * disassociation.
5456a74ebfe5SAdrian Chadd 			 */
5457a74ebfe5SAdrian Chadd 			sc->sc_beacons = 1;
5458e8fd88a3SSam Leffler 			break;
5459b032f27cSSam Leffler 		case IEEE80211_M_MONITOR:
5460b032f27cSSam Leffler 			/*
5461b032f27cSSam Leffler 			 * Monitor mode vaps have only INIT->RUN and RUN->RUN
5462b032f27cSSam Leffler 			 * transitions so we must re-enable interrupts here to
5463b032f27cSSam Leffler 			 * handle the case of a single monitor mode vap.
5464b032f27cSSam Leffler 			 */
5465b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask);
5466b032f27cSSam Leffler 			break;
5467b032f27cSSam Leffler 		case IEEE80211_M_WDS:
5468b032f27cSSam Leffler 			break;
5469e8fd88a3SSam Leffler 		default:
5470e8fd88a3SSam Leffler 			break;
54715591b213SSam Leffler 		}
54725591b213SSam Leffler 		/*
54737b0c77ecSSam Leffler 		 * Let the hal process statistics collected during a
54747b0c77ecSSam Leffler 		 * scan so it can provide calibrated noise floor data.
54757b0c77ecSSam Leffler 		 */
54767b0c77ecSSam Leffler 		ath_hal_process_noisefloor(ah);
54777b0c77ecSSam Leffler 		/*
5478ffa2cab6SSam Leffler 		 * Reset rssi stats; maybe not the best place...
5479ffa2cab6SSam Leffler 		 */
5480ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
5481ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
5482ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
548345bbf62fSSam Leffler 		/*
5484b032f27cSSam Leffler 		 * Finally, start any timers and the task q thread
5485b032f27cSSam Leffler 		 * (in case we didn't go through SCAN state).
548645bbf62fSSam Leffler 		 */
54872dc7fcc4SSam Leffler 		if (ath_longcalinterval != 0) {
5488c42a7b7eSSam Leffler 			/* start periodic recalibration timer */
54892dc7fcc4SSam Leffler 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
54902dc7fcc4SSam Leffler 		} else {
54912dc7fcc4SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
54922dc7fcc4SSam Leffler 			    "%s: calibration disabled\n", __func__);
5493c42a7b7eSSam Leffler 		}
5494b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
5495b032f27cSSam Leffler 	} else if (nstate == IEEE80211_S_INIT) {
5496b032f27cSSam Leffler 		/*
5497b032f27cSSam Leffler 		 * If there are no vaps left in RUN state then
5498b032f27cSSam Leffler 		 * shutdown host/driver operation:
5499b032f27cSSam Leffler 		 * o disable interrupts
5500b032f27cSSam Leffler 		 * o disable the task queue thread
5501b032f27cSSam Leffler 		 * o mark beacon processing as stopped
5502b032f27cSSam Leffler 		 */
5503b032f27cSSam Leffler 		if (!ath_isanyrunningvaps(vap)) {
5504b032f27cSSam Leffler 			sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5505b032f27cSSam Leffler 			/* disable interrupts  */
5506b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL);
5507b032f27cSSam Leffler 			taskqueue_block(sc->sc_tq);
5508b032f27cSSam Leffler 			sc->sc_beacons = 0;
5509b032f27cSSam Leffler 		}
5510584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
551110ad9a77SSam Leffler 		ath_hal_setcca(ah, AH_TRUE);
551210ad9a77SSam Leffler #endif
5513b032f27cSSam Leffler 	}
55145591b213SSam Leffler bad:
551580767531SAdrian Chadd 	ieee80211_free_node(ni);
55165591b213SSam Leffler 	return error;
55175591b213SSam Leffler }
55185591b213SSam Leffler 
55195591b213SSam Leffler /*
5520e8fd88a3SSam Leffler  * Allocate a key cache slot to the station so we can
5521e8fd88a3SSam Leffler  * setup a mapping from key index to node. The key cache
5522e8fd88a3SSam Leffler  * slot is needed for managing antenna state and for
5523e8fd88a3SSam Leffler  * compression when stations do not use crypto.  We do
5524e8fd88a3SSam Leffler  * it uniliaterally here; if crypto is employed this slot
5525e8fd88a3SSam Leffler  * will be reassigned.
5526e8fd88a3SSam Leffler  */
5527e8fd88a3SSam Leffler static void
5528e8fd88a3SSam Leffler ath_setup_stationkey(struct ieee80211_node *ni)
5529e8fd88a3SSam Leffler {
5530b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
5531b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
5532c1225b52SSam Leffler 	ieee80211_keyix keyix, rxkeyix;
5533e8fd88a3SSam Leffler 
553480767531SAdrian Chadd 	/* XXX should take a locked ref to vap->iv_bss */
5535b032f27cSSam Leffler 	if (!ath_key_alloc(vap, &ni->ni_ucastkey, &keyix, &rxkeyix)) {
5536e8fd88a3SSam Leffler 		/*
5537e8fd88a3SSam Leffler 		 * Key cache is full; we'll fall back to doing
5538e8fd88a3SSam Leffler 		 * the more expensive lookup in software.  Note
5539e8fd88a3SSam Leffler 		 * this also means no h/w compression.
5540e8fd88a3SSam Leffler 		 */
5541e8fd88a3SSam Leffler 		/* XXX msg+statistic */
5542e8fd88a3SSam Leffler 	} else {
5543c1225b52SSam Leffler 		/* XXX locking? */
5544e8fd88a3SSam Leffler 		ni->ni_ucastkey.wk_keyix = keyix;
5545c1225b52SSam Leffler 		ni->ni_ucastkey.wk_rxkeyix = rxkeyix;
554633052833SSam Leffler 		/* NB: must mark device key to get called back on delete */
554733052833SSam Leffler 		ni->ni_ucastkey.wk_flags |= IEEE80211_KEY_DEVKEY;
5548d3ac945bSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_ucastkey.wk_macaddr, ni->ni_macaddr);
5549e8fd88a3SSam Leffler 		/* NB: this will create a pass-thru key entry */
555055c7b877SAdrian Chadd 		ath_keyset(sc, vap, &ni->ni_ucastkey, vap->iv_bss);
5551e8fd88a3SSam Leffler 	}
5552e8fd88a3SSam Leffler }
5553e8fd88a3SSam Leffler 
5554e8fd88a3SSam Leffler /*
55555591b213SSam Leffler  * Setup driver-specific state for a newly associated node.
55565591b213SSam Leffler  * Note that we're called also on a re-associate, the isnew
55575591b213SSam Leffler  * param tells us if this is the first time or not.
55585591b213SSam Leffler  */
55595591b213SSam Leffler static void
5560e9962332SSam Leffler ath_newassoc(struct ieee80211_node *ni, int isnew)
55615591b213SSam Leffler {
5562b032f27cSSam Leffler 	struct ath_node *an = ATH_NODE(ni);
5563b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
5564b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
5565c62362cbSSam Leffler 	const struct ieee80211_txparam *tp = ni->ni_txparms;
55665591b213SSam Leffler 
5567ab06fdf2SSam Leffler 	an->an_mcastrix = ath_tx_findrix(sc, tp->mcastrate);
5568ab06fdf2SSam Leffler 	an->an_mgmtrix = ath_tx_findrix(sc, tp->mgmtrate);
5569b032f27cSSam Leffler 
5570b032f27cSSam Leffler 	ath_rate_newassoc(sc, an, isnew);
557132da86a0SAdrian Chadd 
5572e8fd88a3SSam Leffler 	if (isnew &&
5573b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey &&
5574b032f27cSSam Leffler 	    ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE)
5575e8fd88a3SSam Leffler 		ath_setup_stationkey(ni);
55764bed2b67SAdrian Chadd 
55774bed2b67SAdrian Chadd 	/*
55784bed2b67SAdrian Chadd 	 * If we're reassociating, make sure that any paused queues
55794bed2b67SAdrian Chadd 	 * get unpaused.
55804bed2b67SAdrian Chadd 	 *
55814bed2b67SAdrian Chadd 	 * Now, we may hvae frames in the hardware queue for this node.
55824bed2b67SAdrian Chadd 	 * So if we are reassociating and there are frames in the queue,
55834bed2b67SAdrian Chadd 	 * we need to go through the cleanup path to ensure that they're
55844bed2b67SAdrian Chadd 	 * marked as non-aggregate.
55854bed2b67SAdrian Chadd 	 */
55864bed2b67SAdrian Chadd 	if (! isnew) {
558732da86a0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE,
55884bed2b67SAdrian Chadd 		    "%s: %6D: reassoc; is_powersave=%d\n",
55894bed2b67SAdrian Chadd 		    __func__,
55904bed2b67SAdrian Chadd 		    ni->ni_macaddr,
55914bed2b67SAdrian Chadd 		    ":",
55924bed2b67SAdrian Chadd 		    an->an_is_powersave);
55934bed2b67SAdrian Chadd 
55944bed2b67SAdrian Chadd 		/* XXX for now, we can't hold the lock across assoc */
55954bed2b67SAdrian Chadd 		ath_tx_node_reassoc(sc, an);
55964bed2b67SAdrian Chadd 
55974bed2b67SAdrian Chadd 		/* XXX for now, we can't hold the lock across wakeup */
55984bed2b67SAdrian Chadd 		if (an->an_is_powersave)
55994bed2b67SAdrian Chadd 			ath_tx_node_wakeup(sc, an);
56004bed2b67SAdrian Chadd 	}
5601e8fd88a3SSam Leffler }
56025591b213SSam Leffler 
56035591b213SSam Leffler static int
560459efa8b5SSam Leffler ath_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *reg,
5605b032f27cSSam Leffler 	int nchans, struct ieee80211_channel chans[])
5606b032f27cSSam Leffler {
5607b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5608b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
560959efa8b5SSam Leffler 	HAL_STATUS status;
5610b032f27cSSam Leffler 
5611033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
561259efa8b5SSam Leffler 	    "%s: rd %u cc %u location %c%s\n",
561359efa8b5SSam Leffler 	    __func__, reg->regdomain, reg->country, reg->location,
561459efa8b5SSam Leffler 	    reg->ecm ? " ecm" : "");
5615033022a9SSam Leffler 
561659efa8b5SSam Leffler 	status = ath_hal_set_channels(ah, chans, nchans,
561759efa8b5SSam Leffler 	    reg->country, reg->regdomain);
561859efa8b5SSam Leffler 	if (status != HAL_OK) {
561959efa8b5SSam Leffler 		DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: failed, status %u\n",
562059efa8b5SSam Leffler 		    __func__, status);
562159efa8b5SSam Leffler 		return EINVAL;		/* XXX */
5622b032f27cSSam Leffler 	}
56238db87e40SAdrian Chadd 
5624b032f27cSSam Leffler 	return 0;
5625b032f27cSSam Leffler }
5626b032f27cSSam Leffler 
5627b032f27cSSam Leffler static void
5628b032f27cSSam Leffler ath_getradiocaps(struct ieee80211com *ic,
56295fe9f044SSam Leffler 	int maxchans, int *nchans, struct ieee80211_channel chans[])
5630b032f27cSSam Leffler {
5631b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5632b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5633b032f27cSSam Leffler 
563459efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: use rd %u cc %d\n",
563559efa8b5SSam Leffler 	    __func__, SKU_DEBUG, CTRY_DEFAULT);
5636033022a9SSam Leffler 
563759efa8b5SSam Leffler 	/* XXX check return */
563859efa8b5SSam Leffler 	(void) ath_hal_getchannels(ah, chans, maxchans, nchans,
563959efa8b5SSam Leffler 	    HAL_MODE_ALL, CTRY_DEFAULT, SKU_DEBUG, AH_TRUE);
5640033022a9SSam Leffler 
5641b032f27cSSam Leffler }
5642b032f27cSSam Leffler 
5643b032f27cSSam Leffler static int
5644b032f27cSSam Leffler ath_getchannels(struct ath_softc *sc)
5645b032f27cSSam Leffler {
5646b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
5647b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
5648b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
564959efa8b5SSam Leffler 	HAL_STATUS status;
5650b032f27cSSam Leffler 
5651b032f27cSSam Leffler 	/*
565259efa8b5SSam Leffler 	 * Collect channel set based on EEPROM contents.
5653b032f27cSSam Leffler 	 */
565459efa8b5SSam Leffler 	status = ath_hal_init_channels(ah, ic->ic_channels, IEEE80211_CHAN_MAX,
565559efa8b5SSam Leffler 	    &ic->ic_nchans, HAL_MODE_ALL, CTRY_DEFAULT, SKU_NONE, AH_TRUE);
565659efa8b5SSam Leffler 	if (status != HAL_OK) {
565759efa8b5SSam Leffler 		if_printf(ifp, "%s: unable to collect channel list from hal, "
565859efa8b5SSam Leffler 		    "status %d\n", __func__, status);
565959efa8b5SSam Leffler 		return EINVAL;
566059efa8b5SSam Leffler 	}
5661ca876918SSam Leffler 	(void) ath_hal_getregdomain(ah, &sc->sc_eerd);
5662ca876918SSam Leffler 	ath_hal_getcountrycode(ah, &sc->sc_eecc);	/* NB: cannot fail */
566359efa8b5SSam Leffler 	/* XXX map Atheros sku's to net80211 SKU's */
566459efa8b5SSam Leffler 	/* XXX net80211 types too small */
566559efa8b5SSam Leffler 	ic->ic_regdomain.regdomain = (uint16_t) sc->sc_eerd;
566659efa8b5SSam Leffler 	ic->ic_regdomain.country = (uint16_t) sc->sc_eecc;
566759efa8b5SSam Leffler 	ic->ic_regdomain.isocc[0] = ' ';	/* XXX don't know */
566859efa8b5SSam Leffler 	ic->ic_regdomain.isocc[1] = ' ';
566959efa8b5SSam Leffler 
5670b032f27cSSam Leffler 	ic->ic_regdomain.ecm = 1;
5671b032f27cSSam Leffler 	ic->ic_regdomain.location = 'I';
5672033022a9SSam Leffler 
5673033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
567459efa8b5SSam Leffler 	    "%s: eeprom rd %u cc %u (mapped rd %u cc %u) location %c%s\n",
5675033022a9SSam Leffler 	    __func__, sc->sc_eerd, sc->sc_eecc,
5676033022a9SSam Leffler 	    ic->ic_regdomain.regdomain, ic->ic_regdomain.country,
567759efa8b5SSam Leffler 	    ic->ic_regdomain.location, ic->ic_regdomain.ecm ? " ecm" : "");
56785591b213SSam Leffler 	return 0;
56795591b213SSam Leffler }
56805591b213SSam Leffler 
56816c4612b9SSam Leffler static int
56826c4612b9SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode)
56836c4612b9SSam Leffler {
56846c4612b9SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
56856c4612b9SSam Leffler 	const HAL_RATE_TABLE *rt;
56866c4612b9SSam Leffler 
56876c4612b9SSam Leffler 	switch (mode) {
56886c4612b9SSam Leffler 	case IEEE80211_MODE_11A:
56896c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A);
56906c4612b9SSam Leffler 		break;
5691724c193aSSam Leffler 	case IEEE80211_MODE_HALF:
5692aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_HALF_RATE);
5693aaa70f2fSSam Leffler 		break;
5694724c193aSSam Leffler 	case IEEE80211_MODE_QUARTER:
5695aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_QUARTER_RATE);
5696aaa70f2fSSam Leffler 		break;
56976c4612b9SSam Leffler 	case IEEE80211_MODE_11B:
56986c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11B);
56996c4612b9SSam Leffler 		break;
57006c4612b9SSam Leffler 	case IEEE80211_MODE_11G:
57016c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11G);
57026c4612b9SSam Leffler 		break;
57036c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_A:
570468e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108A);
57056c4612b9SSam Leffler 		break;
57066c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_G:
57076c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108G);
57086c4612b9SSam Leffler 		break;
570968e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
571068e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_TURBO);
571168e8e04eSSam Leffler 		break;
571268e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
571368e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NA_HT20);
571468e8e04eSSam Leffler 		break;
571568e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
571668e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NG_HT20);
571768e8e04eSSam Leffler 		break;
57186c4612b9SSam Leffler 	default:
57196c4612b9SSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
57206c4612b9SSam Leffler 			__func__, mode);
57216c4612b9SSam Leffler 		return 0;
57226c4612b9SSam Leffler 	}
57236c4612b9SSam Leffler 	sc->sc_rates[mode] = rt;
5724aaa70f2fSSam Leffler 	return (rt != NULL);
57255591b213SSam Leffler }
57265591b213SSam Leffler 
57275591b213SSam Leffler static void
57285591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
57295591b213SSam Leffler {
57303e50ec2cSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
57313e50ec2cSSam Leffler 	/* NB: on/off times from the Atheros NDIS driver, w/ permission */
57323e50ec2cSSam Leffler 	static const struct {
57333e50ec2cSSam Leffler 		u_int		rate;		/* tx/rx 802.11 rate */
57343e50ec2cSSam Leffler 		u_int16_t	timeOn;		/* LED on time (ms) */
57353e50ec2cSSam Leffler 		u_int16_t	timeOff;	/* LED off time (ms) */
57363e50ec2cSSam Leffler 	} blinkrates[] = {
57373e50ec2cSSam Leffler 		{ 108,  40,  10 },
57383e50ec2cSSam Leffler 		{  96,  44,  11 },
57393e50ec2cSSam Leffler 		{  72,  50,  13 },
57403e50ec2cSSam Leffler 		{  48,  57,  14 },
57413e50ec2cSSam Leffler 		{  36,  67,  16 },
57423e50ec2cSSam Leffler 		{  24,  80,  20 },
57433e50ec2cSSam Leffler 		{  22, 100,  25 },
57443e50ec2cSSam Leffler 		{  18, 133,  34 },
57453e50ec2cSSam Leffler 		{  12, 160,  40 },
57463e50ec2cSSam Leffler 		{  10, 200,  50 },
57473e50ec2cSSam Leffler 		{   6, 240,  58 },
57483e50ec2cSSam Leffler 		{   4, 267,  66 },
57493e50ec2cSSam Leffler 		{   2, 400, 100 },
57503e50ec2cSSam Leffler 		{   0, 500, 130 },
5751724c193aSSam Leffler 		/* XXX half/quarter rates */
57523e50ec2cSSam Leffler 	};
57535591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
57543e50ec2cSSam Leffler 	int i, j;
57555591b213SSam Leffler 
57565591b213SSam Leffler 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
57575591b213SSam Leffler 	rt = sc->sc_rates[mode];
57585591b213SSam Leffler 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
5759180f268dSSam Leffler 	for (i = 0; i < rt->rateCount; i++) {
5760180f268dSSam Leffler 		uint8_t ieeerate = rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
5761180f268dSSam Leffler 		if (rt->info[i].phy != IEEE80211_T_HT)
5762180f268dSSam Leffler 			sc->sc_rixmap[ieeerate] = i;
5763180f268dSSam Leffler 		else
5764180f268dSSam Leffler 			sc->sc_rixmap[ieeerate | IEEE80211_RATE_MCS] = i;
5765180f268dSSam Leffler 	}
57661b1a8e41SSam Leffler 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
576746d4d74cSSam Leffler 	for (i = 0; i < N(sc->sc_hwmap); i++) {
576846d4d74cSSam Leffler 		if (i >= rt->rateCount) {
57693e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledon = (500 * hz) / 1000;
57703e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledoff = (130 * hz) / 1000;
577116b4851aSSam Leffler 			continue;
57723e50ec2cSSam Leffler 		}
57733e50ec2cSSam Leffler 		sc->sc_hwmap[i].ieeerate =
577446d4d74cSSam Leffler 			rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
577546d4d74cSSam Leffler 		if (rt->info[i].phy == IEEE80211_T_HT)
577626041a14SSam Leffler 			sc->sc_hwmap[i].ieeerate |= IEEE80211_RATE_MCS;
5777d3be6f5bSSam Leffler 		sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
577846d4d74cSSam Leffler 		if (rt->info[i].shortPreamble ||
577946d4d74cSSam Leffler 		    rt->info[i].phy == IEEE80211_T_OFDM)
5780d3be6f5bSSam Leffler 			sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE;
57815463c4a4SSam Leffler 		sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags;
57823e50ec2cSSam Leffler 		for (j = 0; j < N(blinkrates)-1; j++)
57833e50ec2cSSam Leffler 			if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate)
57843e50ec2cSSam Leffler 				break;
57853e50ec2cSSam Leffler 		/* NB: this uses the last entry if the rate isn't found */
57863e50ec2cSSam Leffler 		/* XXX beware of overlow */
57873e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000;
57883e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000;
5789c42a7b7eSSam Leffler 	}
57905591b213SSam Leffler 	sc->sc_currates = rt;
57915591b213SSam Leffler 	sc->sc_curmode = mode;
57925591b213SSam Leffler 	/*
5793c42a7b7eSSam Leffler 	 * All protection frames are transmited at 2Mb/s for
5794c42a7b7eSSam Leffler 	 * 11g, otherwise at 1Mb/s.
57955591b213SSam Leffler 	 */
5796913a1ba1SSam Leffler 	if (mode == IEEE80211_MODE_11G)
5797ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*2);
5798913a1ba1SSam Leffler 	else
5799ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*1);
58004fa8d4efSDaniel Eischen 	/* NB: caller is responsible for resetting rate control state */
58013e50ec2cSSam Leffler #undef N
58025591b213SSam Leffler }
58035591b213SSam Leffler 
5804c42a7b7eSSam Leffler static void
58052e986da5SSam Leffler ath_watchdog(void *arg)
5806c42a7b7eSSam Leffler {
58072e986da5SSam Leffler 	struct ath_softc *sc = arg;
5808ef27340cSAdrian Chadd 	int do_reset = 0;
5809c42a7b7eSSam Leffler 
58102e986da5SSam Leffler 	if (sc->sc_wd_timer != 0 && --sc->sc_wd_timer == 0) {
58112e986da5SSam Leffler 		struct ifnet *ifp = sc->sc_ifp;
5812459bc4f0SSam Leffler 		uint32_t hangs;
5813459bc4f0SSam Leffler 
5814459bc4f0SSam Leffler 		if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) &&
5815459bc4f0SSam Leffler 		    hangs != 0) {
5816459bc4f0SSam Leffler 			if_printf(ifp, "%s hang detected (0x%x)\n",
5817459bc4f0SSam Leffler 			    hangs & 0xff ? "bb" : "mac", hangs);
5818459bc4f0SSam Leffler 		} else
5819c42a7b7eSSam Leffler 			if_printf(ifp, "device timeout\n");
5820ef27340cSAdrian Chadd 		do_reset = 1;
5821c42a7b7eSSam Leffler 		ifp->if_oerrors++;
5822c42a7b7eSSam Leffler 		sc->sc_stats.ast_watchdog++;
5823c42a7b7eSSam Leffler 	}
5824ef27340cSAdrian Chadd 
5825ef27340cSAdrian Chadd 	/*
5826ef27340cSAdrian Chadd 	 * We can't hold the lock across the ath_reset() call.
5827d52f7132SAdrian Chadd 	 *
5828d52f7132SAdrian Chadd 	 * And since this routine can't hold a lock and sleep,
5829d52f7132SAdrian Chadd 	 * do the reset deferred.
5830ef27340cSAdrian Chadd 	 */
5831ef27340cSAdrian Chadd 	if (do_reset) {
5832d52f7132SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
5833ef27340cSAdrian Chadd 	}
5834ef27340cSAdrian Chadd 
58352e986da5SSam Leffler 	callout_schedule(&sc->sc_wd_ch, hz);
5836c42a7b7eSSam Leffler }
5837c42a7b7eSSam Leffler 
5838b8f2a853SAdrian Chadd /*
5839b8f2a853SAdrian Chadd  * Fetch the rate control statistics for the given node.
5840b8f2a853SAdrian Chadd  */
5841b8f2a853SAdrian Chadd static int
5842b8f2a853SAdrian Chadd ath_ioctl_ratestats(struct ath_softc *sc, struct ath_rateioctl *rs)
5843b8f2a853SAdrian Chadd {
5844b8f2a853SAdrian Chadd 	struct ath_node *an;
5845b8f2a853SAdrian Chadd 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
5846b8f2a853SAdrian Chadd 	struct ieee80211_node *ni;
5847b8f2a853SAdrian Chadd 	int error = 0;
5848b8f2a853SAdrian Chadd 
5849b8f2a853SAdrian Chadd 	/* Perform a lookup on the given node */
5850b8f2a853SAdrian Chadd 	ni = ieee80211_find_node(&ic->ic_sta, rs->is_u.macaddr);
5851b8f2a853SAdrian Chadd 	if (ni == NULL) {
5852b8f2a853SAdrian Chadd 		error = EINVAL;
5853b8f2a853SAdrian Chadd 		goto bad;
5854b8f2a853SAdrian Chadd 	}
5855b8f2a853SAdrian Chadd 
5856b8f2a853SAdrian Chadd 	/* Lock the ath_node */
5857b8f2a853SAdrian Chadd 	an = ATH_NODE(ni);
5858b8f2a853SAdrian Chadd 	ATH_NODE_LOCK(an);
5859b8f2a853SAdrian Chadd 
5860b8f2a853SAdrian Chadd 	/* Fetch the rate control stats for this node */
5861b8f2a853SAdrian Chadd 	error = ath_rate_fetch_node_stats(sc, an, rs);
5862b8f2a853SAdrian Chadd 
5863b8f2a853SAdrian Chadd 	/* No matter what happens here, just drop through */
5864b8f2a853SAdrian Chadd 
5865b8f2a853SAdrian Chadd 	/* Unlock the ath_node */
5866b8f2a853SAdrian Chadd 	ATH_NODE_UNLOCK(an);
5867b8f2a853SAdrian Chadd 
5868b8f2a853SAdrian Chadd 	/* Unref the node */
5869b8f2a853SAdrian Chadd 	ieee80211_node_decref(ni);
5870b8f2a853SAdrian Chadd 
5871b8f2a853SAdrian Chadd bad:
5872b8f2a853SAdrian Chadd 	return (error);
5873b8f2a853SAdrian Chadd }
5874b8f2a853SAdrian Chadd 
5875a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
5876c42a7b7eSSam Leffler /*
5877c42a7b7eSSam Leffler  * Diagnostic interface to the HAL.  This is used by various
5878c42a7b7eSSam Leffler  * tools to do things like retrieve register contents for
5879c42a7b7eSSam Leffler  * debugging.  The mechanism is intentionally opaque so that
5880c42a7b7eSSam Leffler  * it can change frequently w/o concern for compatiblity.
5881c42a7b7eSSam Leffler  */
5882c42a7b7eSSam Leffler static int
5883c42a7b7eSSam Leffler ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
5884c42a7b7eSSam Leffler {
5885c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5886c42a7b7eSSam Leffler 	u_int id = ad->ad_id & ATH_DIAG_ID;
5887c42a7b7eSSam Leffler 	void *indata = NULL;
5888c42a7b7eSSam Leffler 	void *outdata = NULL;
5889c42a7b7eSSam Leffler 	u_int32_t insize = ad->ad_in_size;
5890c42a7b7eSSam Leffler 	u_int32_t outsize = ad->ad_out_size;
5891c42a7b7eSSam Leffler 	int error = 0;
5892c42a7b7eSSam Leffler 
5893c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_IN) {
5894c42a7b7eSSam Leffler 		/*
5895c42a7b7eSSam Leffler 		 * Copy in data.
5896c42a7b7eSSam Leffler 		 */
5897c42a7b7eSSam Leffler 		indata = malloc(insize, M_TEMP, M_NOWAIT);
5898c42a7b7eSSam Leffler 		if (indata == NULL) {
5899c42a7b7eSSam Leffler 			error = ENOMEM;
5900c42a7b7eSSam Leffler 			goto bad;
5901c42a7b7eSSam Leffler 		}
5902c42a7b7eSSam Leffler 		error = copyin(ad->ad_in_data, indata, insize);
5903c42a7b7eSSam Leffler 		if (error)
5904c42a7b7eSSam Leffler 			goto bad;
5905c42a7b7eSSam Leffler 	}
5906c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_DYN) {
5907c42a7b7eSSam Leffler 		/*
5908c42a7b7eSSam Leffler 		 * Allocate a buffer for the results (otherwise the HAL
5909c42a7b7eSSam Leffler 		 * returns a pointer to a buffer where we can read the
5910c42a7b7eSSam Leffler 		 * results).  Note that we depend on the HAL leaving this
5911c42a7b7eSSam Leffler 		 * pointer for us to use below in reclaiming the buffer;
5912c42a7b7eSSam Leffler 		 * may want to be more defensive.
5913c42a7b7eSSam Leffler 		 */
5914c42a7b7eSSam Leffler 		outdata = malloc(outsize, M_TEMP, M_NOWAIT);
5915c42a7b7eSSam Leffler 		if (outdata == NULL) {
5916c42a7b7eSSam Leffler 			error = ENOMEM;
5917c42a7b7eSSam Leffler 			goto bad;
5918c42a7b7eSSam Leffler 		}
5919c42a7b7eSSam Leffler 	}
5920c42a7b7eSSam Leffler 	if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) {
5921c42a7b7eSSam Leffler 		if (outsize < ad->ad_out_size)
5922c42a7b7eSSam Leffler 			ad->ad_out_size = outsize;
5923c42a7b7eSSam Leffler 		if (outdata != NULL)
5924c42a7b7eSSam Leffler 			error = copyout(outdata, ad->ad_out_data,
5925c42a7b7eSSam Leffler 					ad->ad_out_size);
5926c42a7b7eSSam Leffler 	} else {
5927c42a7b7eSSam Leffler 		error = EINVAL;
5928c42a7b7eSSam Leffler 	}
5929c42a7b7eSSam Leffler bad:
5930c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
5931c42a7b7eSSam Leffler 		free(indata, M_TEMP);
5932c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
5933c42a7b7eSSam Leffler 		free(outdata, M_TEMP);
5934c42a7b7eSSam Leffler 	return error;
5935c42a7b7eSSam Leffler }
5936a585a9a1SSam Leffler #endif /* ATH_DIAGAPI */
5937c42a7b7eSSam Leffler 
5938c42a7b7eSSam Leffler static int
5939c42a7b7eSSam Leffler ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
5940c42a7b7eSSam Leffler {
5941c42a7b7eSSam Leffler #define	IS_RUNNING(ifp) \
594213f4c340SRobert Watson 	((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
5943c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
5944b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
5945c42a7b7eSSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
594684784be1SSam Leffler 	const HAL_RATE_TABLE *rt;
5947c42a7b7eSSam Leffler 	int error = 0;
5948c42a7b7eSSam Leffler 
5949c42a7b7eSSam Leffler 	switch (cmd) {
5950c42a7b7eSSam Leffler 	case SIOCSIFFLAGS:
595131a8c1edSAndrew Thompson 		ATH_LOCK(sc);
5952c42a7b7eSSam Leffler 		if (IS_RUNNING(ifp)) {
5953c42a7b7eSSam Leffler 			/*
5954c42a7b7eSSam Leffler 			 * To avoid rescanning another access point,
5955c42a7b7eSSam Leffler 			 * do not call ath_init() here.  Instead,
5956c42a7b7eSSam Leffler 			 * only reflect promisc mode settings.
5957c42a7b7eSSam Leffler 			 */
5958c42a7b7eSSam Leffler 			ath_mode_init(sc);
5959c42a7b7eSSam Leffler 		} else if (ifp->if_flags & IFF_UP) {
5960c42a7b7eSSam Leffler 			/*
5961c42a7b7eSSam Leffler 			 * Beware of being called during attach/detach
5962c42a7b7eSSam Leffler 			 * to reset promiscuous mode.  In that case we
5963c42a7b7eSSam Leffler 			 * will still be marked UP but not RUNNING.
5964c42a7b7eSSam Leffler 			 * However trying to re-init the interface
5965c42a7b7eSSam Leffler 			 * is the wrong thing to do as we've already
5966c42a7b7eSSam Leffler 			 * torn down much of our state.  There's
5967c42a7b7eSSam Leffler 			 * probably a better way to deal with this.
5968c42a7b7eSSam Leffler 			 */
5969b032f27cSSam Leffler 			if (!sc->sc_invalid)
5970fc74a9f9SBrooks Davis 				ath_init(sc);	/* XXX lose error */
5971d3ac945bSSam Leffler 		} else {
5972c42a7b7eSSam Leffler 			ath_stop_locked(ifp);
5973d3ac945bSSam Leffler #ifdef notyet
5974d3ac945bSSam Leffler 			/* XXX must wakeup in places like ath_vap_delete */
5975d3ac945bSSam Leffler 			if (!sc->sc_invalid)
5976d3ac945bSSam Leffler 				ath_hal_setpower(sc->sc_ah, HAL_PM_FULL_SLEEP);
5977d3ac945bSSam Leffler #endif
5978d3ac945bSSam Leffler 		}
597931a8c1edSAndrew Thompson 		ATH_UNLOCK(sc);
5980c42a7b7eSSam Leffler 		break;
5981b032f27cSSam Leffler 	case SIOCGIFMEDIA:
5982b032f27cSSam Leffler 	case SIOCSIFMEDIA:
5983b032f27cSSam Leffler 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
5984b032f27cSSam Leffler 		break;
5985c42a7b7eSSam Leffler 	case SIOCGATHSTATS:
5986c42a7b7eSSam Leffler 		/* NB: embed these numbers to get a consistent view */
5987c42a7b7eSSam Leffler 		sc->sc_stats.ast_tx_packets = ifp->if_opackets;
5988c42a7b7eSSam Leffler 		sc->sc_stats.ast_rx_packets = ifp->if_ipackets;
598984784be1SSam Leffler 		sc->sc_stats.ast_tx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgtxrssi);
599084784be1SSam Leffler 		sc->sc_stats.ast_rx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgrssi);
5991584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
599210ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsfadjp = TDMA_AVG(sc->sc_avgtsfdeltap);
599310ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsfadjm = TDMA_AVG(sc->sc_avgtsfdeltam);
599410ad9a77SSam Leffler #endif
599584784be1SSam Leffler 		rt = sc->sc_currates;
599646d4d74cSSam Leffler 		sc->sc_stats.ast_tx_rate =
599746d4d74cSSam Leffler 		    rt->info[sc->sc_txrix].dot11Rate &~ IEEE80211_RATE_BASIC;
59986aa113fdSAdrian Chadd 		if (rt->info[sc->sc_txrix].phy & IEEE80211_T_HT)
59996aa113fdSAdrian Chadd 			sc->sc_stats.ast_tx_rate |= IEEE80211_RATE_MCS;
6000c42a7b7eSSam Leffler 		return copyout(&sc->sc_stats,
6001c42a7b7eSSam Leffler 		    ifr->ifr_data, sizeof (sc->sc_stats));
600294fe37d2SAdrian Chadd 	case SIOCGATHAGSTATS:
600394fe37d2SAdrian Chadd 		return copyout(&sc->sc_aggr_stats,
600494fe37d2SAdrian Chadd 		    ifr->ifr_data, sizeof (sc->sc_aggr_stats));
60053fc21fedSSam Leffler 	case SIOCZATHSTATS:
60063fc21fedSSam Leffler 		error = priv_check(curthread, PRIV_DRIVER);
60079467e3f3SAdrian Chadd 		if (error == 0) {
60083fc21fedSSam Leffler 			memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
600941b6b507SAdrian Chadd 			memset(&sc->sc_aggr_stats, 0,
601041b6b507SAdrian Chadd 			    sizeof(sc->sc_aggr_stats));
60119467e3f3SAdrian Chadd 			memset(&sc->sc_intr_stats, 0,
60129467e3f3SAdrian Chadd 			    sizeof(sc->sc_intr_stats));
60139467e3f3SAdrian Chadd 		}
60143fc21fedSSam Leffler 		break;
6015a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
6016c42a7b7eSSam Leffler 	case SIOCGATHDIAG:
6017c42a7b7eSSam Leffler 		error = ath_ioctl_diag(sc, (struct ath_diag *) ifr);
6018c42a7b7eSSam Leffler 		break;
6019f51c84eaSAdrian Chadd 	case SIOCGATHPHYERR:
6020f51c84eaSAdrian Chadd 		error = ath_ioctl_phyerr(sc,(struct ath_diag*) ifr);
6021f51c84eaSAdrian Chadd 		break;
6022a585a9a1SSam Leffler #endif
60239af351f9SAdrian Chadd 	case SIOCGATHSPECTRAL:
60249af351f9SAdrian Chadd 		error = ath_ioctl_spectral(sc,(struct ath_diag*) ifr);
60259af351f9SAdrian Chadd 		break;
6026b8f2a853SAdrian Chadd 	case SIOCGATHNODERATESTATS:
6027b8f2a853SAdrian Chadd 		error = ath_ioctl_ratestats(sc, (struct ath_rateioctl *) ifr);
6028b8f2a853SAdrian Chadd 		break;
602931a8c1edSAndrew Thompson 	case SIOCGIFADDR:
6030b032f27cSSam Leffler 		error = ether_ioctl(ifp, cmd, data);
6031c42a7b7eSSam Leffler 		break;
603231a8c1edSAndrew Thompson 	default:
603331a8c1edSAndrew Thompson 		error = EINVAL;
603431a8c1edSAndrew Thompson 		break;
6035c42a7b7eSSam Leffler 	}
6036c42a7b7eSSam Leffler 	return error;
6037a614e076SSam Leffler #undef IS_RUNNING
6038c42a7b7eSSam Leffler }
6039c42a7b7eSSam Leffler 
6040c42a7b7eSSam Leffler /*
6041c42a7b7eSSam Leffler  * Announce various information on device/driver attach.
6042c42a7b7eSSam Leffler  */
6043c42a7b7eSSam Leffler static void
6044c42a7b7eSSam Leffler ath_announce(struct ath_softc *sc)
6045c42a7b7eSSam Leffler {
6046fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
6047c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6048c42a7b7eSSam Leffler 
6049498657cfSSam Leffler 	if_printf(ifp, "AR%s mac %d.%d RF%s phy %d.%d\n",
6050498657cfSSam Leffler 		ath_hal_mac_name(ah), ah->ah_macVersion, ah->ah_macRev,
6051498657cfSSam Leffler 		ath_hal_rf_name(ah), ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
605246a924c4SAdrian Chadd 	if_printf(ifp, "2GHz radio: 0x%.4x; 5GHz radio: 0x%.4x\n",
605346a924c4SAdrian Chadd 		ah->ah_analog2GhzRev, ah->ah_analog5GhzRev);
6054c42a7b7eSSam Leffler 	if (bootverbose) {
6055c42a7b7eSSam Leffler 		int i;
6056c42a7b7eSSam Leffler 		for (i = 0; i <= WME_AC_VO; i++) {
6057c42a7b7eSSam Leffler 			struct ath_txq *txq = sc->sc_ac2q[i];
6058c42a7b7eSSam Leffler 			if_printf(ifp, "Use hw queue %u for %s traffic\n",
6059c42a7b7eSSam Leffler 				txq->axq_qnum, ieee80211_wme_acnames[i]);
6060c42a7b7eSSam Leffler 		}
6061c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for CAB traffic\n",
6062c42a7b7eSSam Leffler 			sc->sc_cabq->axq_qnum);
6063c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for beacons\n", sc->sc_bhalq);
6064c42a7b7eSSam Leffler 	}
6065e2d787faSSam Leffler 	if (ath_rxbuf != ATH_RXBUF)
6066e2d787faSSam Leffler 		if_printf(ifp, "using %u rx buffers\n", ath_rxbuf);
6067e2d787faSSam Leffler 	if (ath_txbuf != ATH_TXBUF)
6068e2d787faSSam Leffler 		if_printf(ifp, "using %u tx buffers\n", ath_txbuf);
60699ac01d39SRui Paulo 	if (sc->sc_mcastkey && bootverbose)
60709ac01d39SRui Paulo 		if_printf(ifp, "using multicast key search\n");
6071c42a7b7eSSam Leffler }
607210ad9a77SSam Leffler 
607348237774SAdrian Chadd static void
607448237774SAdrian Chadd ath_dfs_tasklet(void *p, int npending)
607548237774SAdrian Chadd {
607648237774SAdrian Chadd 	struct ath_softc *sc = (struct ath_softc *) p;
607748237774SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
607848237774SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
607948237774SAdrian Chadd 
608048237774SAdrian Chadd 	/*
608148237774SAdrian Chadd 	 * If previous processing has found a radar event,
608248237774SAdrian Chadd 	 * signal this to the net80211 layer to begin DFS
608348237774SAdrian Chadd 	 * processing.
608448237774SAdrian Chadd 	 */
608548237774SAdrian Chadd 	if (ath_dfs_process_radar_event(sc, sc->sc_curchan)) {
608648237774SAdrian Chadd 		/* DFS event found, initiate channel change */
608706fc4a10SAdrian Chadd 		/*
608806fc4a10SAdrian Chadd 		 * XXX doesn't currently tell us whether the event
608906fc4a10SAdrian Chadd 		 * XXX was found in the primary or extension
609006fc4a10SAdrian Chadd 		 * XXX channel!
609106fc4a10SAdrian Chadd 		 */
609206fc4a10SAdrian Chadd 		IEEE80211_LOCK(ic);
609348237774SAdrian Chadd 		ieee80211_dfs_notify_radar(ic, sc->sc_curchan);
609406fc4a10SAdrian Chadd 		IEEE80211_UNLOCK(ic);
609548237774SAdrian Chadd 	}
609648237774SAdrian Chadd }
609748237774SAdrian Chadd 
60980eb81626SAdrian Chadd /*
60990eb81626SAdrian Chadd  * Enable/disable power save.  This must be called with
61000eb81626SAdrian Chadd  * no TX driver locks currently held, so it should only
61010eb81626SAdrian Chadd  * be called from the RX path (which doesn't hold any
61020eb81626SAdrian Chadd  * TX driver locks.)
61030eb81626SAdrian Chadd  */
61040eb81626SAdrian Chadd static void
61050eb81626SAdrian Chadd ath_node_powersave(struct ieee80211_node *ni, int enable)
61060eb81626SAdrian Chadd {
6107bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
61080eb81626SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
61090eb81626SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
61100eb81626SAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
61110eb81626SAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
61120eb81626SAdrian Chadd 
61130eb81626SAdrian Chadd 	/* XXX and no TXQ locks should be held here */
61140eb81626SAdrian Chadd 
61159b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, "%s: %6D: enable=%d\n",
61169b48fb4bSAdrian Chadd 	    __func__,
61179b48fb4bSAdrian Chadd 	    ni->ni_macaddr,
61189b48fb4bSAdrian Chadd 	    ":",
61199b48fb4bSAdrian Chadd 	    !! enable);
61200eb81626SAdrian Chadd 
61210eb81626SAdrian Chadd 	/* Suspend or resume software queue handling */
61220eb81626SAdrian Chadd 	if (enable)
61230eb81626SAdrian Chadd 		ath_tx_node_sleep(sc, an);
61240eb81626SAdrian Chadd 	else
61250eb81626SAdrian Chadd 		ath_tx_node_wakeup(sc, an);
61260eb81626SAdrian Chadd 
61270eb81626SAdrian Chadd 	/* Update net80211 state */
61280eb81626SAdrian Chadd 	avp->av_node_ps(ni, enable);
6129bdbb6e5bSAdrian Chadd #else
6130bdbb6e5bSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6131bdbb6e5bSAdrian Chadd 
6132bdbb6e5bSAdrian Chadd 	/* Update net80211 state */
6133bdbb6e5bSAdrian Chadd 	avp->av_node_ps(ni, enable);
6134bdbb6e5bSAdrian Chadd #endif/* ATH_SW_PSQ */
61350eb81626SAdrian Chadd }
61360eb81626SAdrian Chadd 
6137548a605dSAdrian Chadd /*
6138548a605dSAdrian Chadd  * Notification from net80211 that the powersave queue state has
6139548a605dSAdrian Chadd  * changed.
6140548a605dSAdrian Chadd  *
6141548a605dSAdrian Chadd  * Since the software queue also may have some frames:
6142548a605dSAdrian Chadd  *
6143548a605dSAdrian Chadd  * + if the node software queue has frames and the TID state
6144548a605dSAdrian Chadd  *   is 0, we set the TIM;
6145548a605dSAdrian Chadd  * + if the node and the stack are both empty, we clear the TIM bit.
6146548a605dSAdrian Chadd  * + If the stack tries to set the bit, always set it.
6147548a605dSAdrian Chadd  * + If the stack tries to clear the bit, only clear it if the
6148548a605dSAdrian Chadd  *   software queue in question is also cleared.
6149548a605dSAdrian Chadd  *
6150548a605dSAdrian Chadd  * TODO: this is called during node teardown; so let's ensure this
6151548a605dSAdrian Chadd  * is all correctly handled and that the TIM bit is cleared.
6152548a605dSAdrian Chadd  * It may be that the node flush is called _AFTER_ the net80211
6153548a605dSAdrian Chadd  * stack clears the TIM.
6154548a605dSAdrian Chadd  *
6155548a605dSAdrian Chadd  * Here is the racy part.  Since it's possible >1 concurrent,
6156548a605dSAdrian Chadd  * overlapping TXes will appear complete with a TX completion in
6157548a605dSAdrian Chadd  * another thread, it's possible that the concurrent TIM calls will
6158548a605dSAdrian Chadd  * clash.  We can't hold the node lock here because setting the
6159548a605dSAdrian Chadd  * TIM grabs the net80211 comlock and this may cause a LOR.
6160548a605dSAdrian Chadd  * The solution is either to totally serialise _everything_ at
6161548a605dSAdrian Chadd  * this point (ie, all TX, completion and any reset/flush go into
6162548a605dSAdrian Chadd  * one taskqueue) or a new "ath TIM lock" needs to be created that
6163548a605dSAdrian Chadd  * just wraps the driver state change and this call to avp->av_set_tim().
6164548a605dSAdrian Chadd  *
6165548a605dSAdrian Chadd  * The same race exists in the net80211 power save queue handling
6166548a605dSAdrian Chadd  * as well.  Since multiple transmitting threads may queue frames
6167548a605dSAdrian Chadd  * into the driver, as well as ps-poll and the driver transmitting
6168548a605dSAdrian Chadd  * frames (and thus clearing the psq), it's quite possible that
6169548a605dSAdrian Chadd  * a packet entering the PSQ and a ps-poll being handled will
6170548a605dSAdrian Chadd  * race, causing the TIM to be cleared and not re-set.
6171548a605dSAdrian Chadd  */
6172548a605dSAdrian Chadd static int
6173548a605dSAdrian Chadd ath_node_set_tim(struct ieee80211_node *ni, int enable)
6174548a605dSAdrian Chadd {
6175bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
6176548a605dSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
6177548a605dSAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
6178548a605dSAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
6179548a605dSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6180548a605dSAdrian Chadd 	int changed = 0;
6181548a605dSAdrian Chadd 
61824bed2b67SAdrian Chadd 	ATH_TX_LOCK(sc);
6183548a605dSAdrian Chadd 	an->an_stack_psq = enable;
6184548a605dSAdrian Chadd 
6185548a605dSAdrian Chadd 	/*
6186548a605dSAdrian Chadd 	 * This will get called for all operating modes,
6187548a605dSAdrian Chadd 	 * even if avp->av_set_tim is unset.
6188548a605dSAdrian Chadd 	 * It's currently set for hostap/ibss modes; but
6189548a605dSAdrian Chadd 	 * the same infrastructure is used for both STA
6190548a605dSAdrian Chadd 	 * and AP/IBSS node power save.
6191548a605dSAdrian Chadd 	 */
6192548a605dSAdrian Chadd 	if (avp->av_set_tim == NULL) {
61934bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6194548a605dSAdrian Chadd 		return (0);
6195548a605dSAdrian Chadd 	}
6196548a605dSAdrian Chadd 
6197548a605dSAdrian Chadd 	/*
6198548a605dSAdrian Chadd 	 * If setting the bit, always set it here.
6199548a605dSAdrian Chadd 	 * If clearing the bit, only clear it if the
6200548a605dSAdrian Chadd 	 * software queue is also empty.
6201548a605dSAdrian Chadd 	 *
6202548a605dSAdrian Chadd 	 * If the node has left power save, just clear the TIM
6203548a605dSAdrian Chadd 	 * bit regardless of the state of the power save queue.
6204548a605dSAdrian Chadd 	 *
6205548a605dSAdrian Chadd 	 * XXX TODO: although atomics are used, it's quite possible
6206548a605dSAdrian Chadd 	 * that a race will occur between this and setting/clearing
6207548a605dSAdrian Chadd 	 * in another thread.  TX completion will occur always in
6208548a605dSAdrian Chadd 	 * one thread, however setting/clearing the TIM bit can come
6209548a605dSAdrian Chadd 	 * from a variety of different process contexts!
6210548a605dSAdrian Chadd 	 */
6211548a605dSAdrian Chadd 	if (enable && an->an_tim_set == 1) {
6212548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
62139b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, tim_set=1, ignoring\n",
62149b48fb4bSAdrian Chadd 		    __func__,
62159b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
62169b48fb4bSAdrian Chadd 		    ":",
62179b48fb4bSAdrian Chadd 		    enable);
62184bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6219548a605dSAdrian Chadd 	} else if (enable) {
6220548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
62219b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, enabling TIM\n",
62229b48fb4bSAdrian Chadd 		    __func__,
62239b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
62249b48fb4bSAdrian Chadd 		    ":",
62259b48fb4bSAdrian Chadd 		    enable);
6226548a605dSAdrian Chadd 		an->an_tim_set = 1;
62274bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6228548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6229ba83edd4SAdrian Chadd 	} else if (an->an_swq_depth == 0) {
6230548a605dSAdrian Chadd 		/* disable */
6231548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
62329b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_swq_depth == 0, disabling\n",
62339b48fb4bSAdrian Chadd 		    __func__,
62349b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
62359b48fb4bSAdrian Chadd 		    ":",
62369b48fb4bSAdrian Chadd 		    enable);
6237548a605dSAdrian Chadd 		an->an_tim_set = 0;
62384bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6239548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6240548a605dSAdrian Chadd 	} else if (! an->an_is_powersave) {
6241548a605dSAdrian Chadd 		/*
6242548a605dSAdrian Chadd 		 * disable regardless; the node isn't in powersave now
6243548a605dSAdrian Chadd 		 */
6244548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
62459b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_pwrsave=0, disabling\n",
62469b48fb4bSAdrian Chadd 		    __func__,
62479b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
62489b48fb4bSAdrian Chadd 		    ":",
62499b48fb4bSAdrian Chadd 		    enable);
6250548a605dSAdrian Chadd 		an->an_tim_set = 0;
62514bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6252548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6253548a605dSAdrian Chadd 	} else {
6254548a605dSAdrian Chadd 		/*
6255548a605dSAdrian Chadd 		 * psq disable, node is currently in powersave, node
6256548a605dSAdrian Chadd 		 * software queue isn't empty, so don't clear the TIM bit
6257548a605dSAdrian Chadd 		 * for now.
6258548a605dSAdrian Chadd 		 */
62594bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6260548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
62619b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_swq_depth > 0, ignoring\n",
62629b48fb4bSAdrian Chadd 		    __func__,
62639b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
62649b48fb4bSAdrian Chadd 		    ":",
62659b48fb4bSAdrian Chadd 		    enable);
6266548a605dSAdrian Chadd 		changed = 0;
6267548a605dSAdrian Chadd 	}
6268548a605dSAdrian Chadd 
6269548a605dSAdrian Chadd 	return (changed);
6270bdbb6e5bSAdrian Chadd #else
6271bdbb6e5bSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6272bdbb6e5bSAdrian Chadd 
627360328038SAdrian Chadd 	/*
6274661c81c3SBaptiste Daroussin 	 * Some operating modes don't set av_set_tim(), so don't
627560328038SAdrian Chadd 	 * update it here.
627660328038SAdrian Chadd 	 */
627760328038SAdrian Chadd 	if (avp->av_set_tim == NULL)
627860328038SAdrian Chadd 		return (0);
627960328038SAdrian Chadd 
6280bdbb6e5bSAdrian Chadd 	return (avp->av_set_tim(ni, enable));
6281bdbb6e5bSAdrian Chadd #endif /* ATH_SW_PSQ */
6282548a605dSAdrian Chadd }
6283548a605dSAdrian Chadd 
6284548a605dSAdrian Chadd /*
6285548a605dSAdrian Chadd  * Set or update the TIM from the software queue.
6286548a605dSAdrian Chadd  *
6287548a605dSAdrian Chadd  * Check the software queue depth before attempting to do lock
6288548a605dSAdrian Chadd  * anything; that avoids trying to obtain the lock.  Then,
6289548a605dSAdrian Chadd  * re-check afterwards to ensure nothing has changed in the
6290548a605dSAdrian Chadd  * meantime.
6291548a605dSAdrian Chadd  *
6292548a605dSAdrian Chadd  * set:   This is designed to be called from the TX path, after
6293548a605dSAdrian Chadd  *        a frame has been queued; to see if the swq > 0.
6294548a605dSAdrian Chadd  *
6295548a605dSAdrian Chadd  * clear: This is designed to be called from the buffer completion point
6296548a605dSAdrian Chadd  *        (right now it's ath_tx_default_comp()) where the state of
6297548a605dSAdrian Chadd  *        a software queue has changed.
6298548a605dSAdrian Chadd  *
6299548a605dSAdrian Chadd  * It makes sense to place it at buffer free / completion rather
6300548a605dSAdrian Chadd  * than after each software queue operation, as there's no real
6301548a605dSAdrian Chadd  * point in churning the TIM bit as the last frames in the software
6302548a605dSAdrian Chadd  * queue are transmitted.  If they fail and we retry them, we'd
6303548a605dSAdrian Chadd  * just be setting the TIM bit again anyway.
6304548a605dSAdrian Chadd  */
6305548a605dSAdrian Chadd void
6306548a605dSAdrian Chadd ath_tx_update_tim(struct ath_softc *sc, struct ieee80211_node *ni,
6307548a605dSAdrian Chadd      int enable)
6308548a605dSAdrian Chadd {
6309bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
6310548a605dSAdrian Chadd 	struct ath_node *an;
6311548a605dSAdrian Chadd 	struct ath_vap *avp;
6312548a605dSAdrian Chadd 
6313548a605dSAdrian Chadd 	/* Don't do this for broadcast/etc frames */
6314548a605dSAdrian Chadd 	if (ni == NULL)
6315548a605dSAdrian Chadd 		return;
6316548a605dSAdrian Chadd 
6317548a605dSAdrian Chadd 	an = ATH_NODE(ni);
6318548a605dSAdrian Chadd 	avp = ATH_VAP(ni->ni_vap);
6319548a605dSAdrian Chadd 
6320548a605dSAdrian Chadd 	/*
6321548a605dSAdrian Chadd 	 * And for operating modes without the TIM handler set, let's
6322548a605dSAdrian Chadd 	 * just skip those.
6323548a605dSAdrian Chadd 	 */
6324548a605dSAdrian Chadd 	if (avp->av_set_tim == NULL)
6325548a605dSAdrian Chadd 		return;
6326548a605dSAdrian Chadd 
63274bed2b67SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
6328548a605dSAdrian Chadd 
6329548a605dSAdrian Chadd 	if (enable) {
6330548a605dSAdrian Chadd 		if (an->an_is_powersave &&
6331548a605dSAdrian Chadd 		    an->an_tim_set == 0 &&
6332ba83edd4SAdrian Chadd 		    an->an_swq_depth != 0) {
6333548a605dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
63349b48fb4bSAdrian Chadd 			    "%s: %6D: swq_depth>0, tim_set=0, set!\n",
63359b48fb4bSAdrian Chadd 			    __func__,
63369b48fb4bSAdrian Chadd 			    ni->ni_macaddr,
63379b48fb4bSAdrian Chadd 			    ":");
6338548a605dSAdrian Chadd 			an->an_tim_set = 1;
6339548a605dSAdrian Chadd 			(void) avp->av_set_tim(ni, 1);
6340548a605dSAdrian Chadd 		}
6341548a605dSAdrian Chadd 	} else {
6342548a605dSAdrian Chadd 		/*
6343548a605dSAdrian Chadd 		 * Don't bother grabbing the lock unless the queue is empty.
6344548a605dSAdrian Chadd 		 */
6345ba83edd4SAdrian Chadd 		if (&an->an_swq_depth != 0)
6346548a605dSAdrian Chadd 			return;
6347548a605dSAdrian Chadd 
6348548a605dSAdrian Chadd 		if (an->an_is_powersave &&
6349548a605dSAdrian Chadd 		    an->an_stack_psq == 0 &&
6350548a605dSAdrian Chadd 		    an->an_tim_set == 1 &&
6351ba83edd4SAdrian Chadd 		    an->an_swq_depth == 0) {
6352548a605dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
635322a3aee6SAdrian Chadd 			    "%s: %6D: swq_depth=0, tim_set=1, psq_set=0,"
6354548a605dSAdrian Chadd 			    " clear!\n",
635522a3aee6SAdrian Chadd 			    __func__,
635622a3aee6SAdrian Chadd 			    ni->ni_macaddr,
635722a3aee6SAdrian Chadd 			    ":");
6358548a605dSAdrian Chadd 			an->an_tim_set = 0;
6359548a605dSAdrian Chadd 			(void) avp->av_set_tim(ni, 0);
6360548a605dSAdrian Chadd 		}
6361548a605dSAdrian Chadd 	}
6362bdbb6e5bSAdrian Chadd #else
6363bdbb6e5bSAdrian Chadd 	return;
6364bdbb6e5bSAdrian Chadd #endif	/* ATH_SW_PSQ */
6365548a605dSAdrian Chadd }
63660eb81626SAdrian Chadd 
636722a3aee6SAdrian Chadd /*
636822a3aee6SAdrian Chadd  * Received a ps-poll frame from net80211.
636922a3aee6SAdrian Chadd  *
637022a3aee6SAdrian Chadd  * Here we get a chance to serve out a software-queued frame ourselves
637122a3aee6SAdrian Chadd  * before we punt it to net80211 to transmit us one itself - either
637222a3aee6SAdrian Chadd  * because there's traffic in the net80211 psq, or a NULL frame to
637322a3aee6SAdrian Chadd  * indicate there's nothing else.
637422a3aee6SAdrian Chadd  */
637522a3aee6SAdrian Chadd static void
637622a3aee6SAdrian Chadd ath_node_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m)
637722a3aee6SAdrian Chadd {
637822a3aee6SAdrian Chadd #ifdef	ATH_SW_PSQ
637922a3aee6SAdrian Chadd 	struct ath_node *an;
638022a3aee6SAdrian Chadd 	struct ath_vap *avp;
638122a3aee6SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
638222a3aee6SAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
638322a3aee6SAdrian Chadd 	int tid;
638422a3aee6SAdrian Chadd 
638522a3aee6SAdrian Chadd 	/* Just paranoia */
638622a3aee6SAdrian Chadd 	if (ni == NULL)
638722a3aee6SAdrian Chadd 		return;
638822a3aee6SAdrian Chadd 
638922a3aee6SAdrian Chadd 	/*
639022a3aee6SAdrian Chadd 	 * Unassociated (temporary node) station.
639122a3aee6SAdrian Chadd 	 */
639222a3aee6SAdrian Chadd 	if (ni->ni_associd == 0)
639322a3aee6SAdrian Chadd 		return;
639422a3aee6SAdrian Chadd 
639522a3aee6SAdrian Chadd 	/*
639622a3aee6SAdrian Chadd 	 * We do have an active node, so let's begin looking into it.
639722a3aee6SAdrian Chadd 	 */
639822a3aee6SAdrian Chadd 	an = ATH_NODE(ni);
639922a3aee6SAdrian Chadd 	avp = ATH_VAP(ni->ni_vap);
640022a3aee6SAdrian Chadd 
640122a3aee6SAdrian Chadd 	/*
640222a3aee6SAdrian Chadd 	 * For now, we just call the original ps-poll method.
640322a3aee6SAdrian Chadd 	 * Once we're ready to flip this on:
640422a3aee6SAdrian Chadd 	 *
640522a3aee6SAdrian Chadd 	 * + Set leak to 1, as no matter what we're going to have
640622a3aee6SAdrian Chadd 	 *   to send a frame;
640722a3aee6SAdrian Chadd 	 * + Check the software queue and if there's something in it,
640822a3aee6SAdrian Chadd 	 *   schedule the highest TID thas has traffic from this node.
640922a3aee6SAdrian Chadd 	 *   Then make sure we schedule the software scheduler to
641022a3aee6SAdrian Chadd 	 *   run so it picks up said frame.
641122a3aee6SAdrian Chadd 	 *
641222a3aee6SAdrian Chadd 	 * That way whatever happens, we'll at least send _a_ frame
641322a3aee6SAdrian Chadd 	 * to the given node.
641422a3aee6SAdrian Chadd 	 *
641522a3aee6SAdrian Chadd 	 * Again, yes, it's crappy QoS if the node has multiple
641622a3aee6SAdrian Chadd 	 * TIDs worth of traffic - but let's get it working first
641722a3aee6SAdrian Chadd 	 * before we optimise it.
641822a3aee6SAdrian Chadd 	 *
641922a3aee6SAdrian Chadd 	 * Also yes, there's definitely latency here - we're not
642022a3aee6SAdrian Chadd 	 * direct dispatching to the hardware in this path (and
642122a3aee6SAdrian Chadd 	 * we're likely being called from the packet receive path,
642222a3aee6SAdrian Chadd 	 * so going back into TX may be a little hairy!) but again
642322a3aee6SAdrian Chadd 	 * I'd like to get this working first before optimising
642422a3aee6SAdrian Chadd 	 * turn-around time.
642522a3aee6SAdrian Chadd 	 */
642622a3aee6SAdrian Chadd 
642722a3aee6SAdrian Chadd 	ATH_TX_LOCK(sc);
642822a3aee6SAdrian Chadd 
642922a3aee6SAdrian Chadd 	/*
643022a3aee6SAdrian Chadd 	 * Legacy - we're called and the node isn't asleep.
643122a3aee6SAdrian Chadd 	 * Immediately punt.
643222a3aee6SAdrian Chadd 	 */
643322a3aee6SAdrian Chadd 	if (! an->an_is_powersave) {
643483bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
643522a3aee6SAdrian Chadd 		    "%s: %6D: not in powersave?\n",
643622a3aee6SAdrian Chadd 		    __func__,
643722a3aee6SAdrian Chadd 		    ni->ni_macaddr,
643822a3aee6SAdrian Chadd 		    ":");
643922a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
644022a3aee6SAdrian Chadd 		avp->av_recv_pspoll(ni, m);
644122a3aee6SAdrian Chadd 		return;
644222a3aee6SAdrian Chadd 	}
644322a3aee6SAdrian Chadd 
644422a3aee6SAdrian Chadd 	/*
644522a3aee6SAdrian Chadd 	 * We're in powersave.
644622a3aee6SAdrian Chadd 	 *
644722a3aee6SAdrian Chadd 	 * Leak a frame.
644822a3aee6SAdrian Chadd 	 */
644922a3aee6SAdrian Chadd 	an->an_leak_count = 1;
645022a3aee6SAdrian Chadd 
645122a3aee6SAdrian Chadd 	/*
645222a3aee6SAdrian Chadd 	 * Now, if there's no frames in the node, just punt to
645322a3aee6SAdrian Chadd 	 * recv_pspoll.
645422a3aee6SAdrian Chadd 	 *
645522a3aee6SAdrian Chadd 	 * Don't bother checking if the TIM bit is set, we really
645622a3aee6SAdrian Chadd 	 * only care if there are any frames here!
645722a3aee6SAdrian Chadd 	 */
645822a3aee6SAdrian Chadd 	if (an->an_swq_depth == 0) {
645922a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
646022a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
646122a3aee6SAdrian Chadd 		    "%s: %6D: SWQ empty; punting to net80211\n",
646222a3aee6SAdrian Chadd 		    __func__,
646322a3aee6SAdrian Chadd 		    ni->ni_macaddr,
646422a3aee6SAdrian Chadd 		    ":");
646522a3aee6SAdrian Chadd 		avp->av_recv_pspoll(ni, m);
646622a3aee6SAdrian Chadd 		return;
646722a3aee6SAdrian Chadd 	}
646822a3aee6SAdrian Chadd 
646922a3aee6SAdrian Chadd 	/*
647022a3aee6SAdrian Chadd 	 * Ok, let's schedule the highest TID that has traffic
647122a3aee6SAdrian Chadd 	 * and then schedule something.
647222a3aee6SAdrian Chadd 	 */
647322a3aee6SAdrian Chadd 	for (tid = IEEE80211_TID_SIZE - 1; tid >= 0; tid--) {
647422a3aee6SAdrian Chadd 		struct ath_tid *atid = &an->an_tid[tid];
647522a3aee6SAdrian Chadd 		/*
647622a3aee6SAdrian Chadd 		 * No frames? Skip.
647722a3aee6SAdrian Chadd 		 */
647822a3aee6SAdrian Chadd 		if (atid->axq_depth == 0)
647922a3aee6SAdrian Chadd 			continue;
648022a3aee6SAdrian Chadd 		ath_tx_tid_sched(sc, atid);
648122a3aee6SAdrian Chadd 		/*
648222a3aee6SAdrian Chadd 		 * XXX we could do a direct call to the TXQ
648322a3aee6SAdrian Chadd 		 * scheduler code here to optimise latency
648422a3aee6SAdrian Chadd 		 * at the expense of a REALLY deep callstack.
648522a3aee6SAdrian Chadd 		 */
648622a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
648722a3aee6SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_txqtask);
648822a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
648922a3aee6SAdrian Chadd 		    "%s: %6D: leaking frame to TID %d\n",
649022a3aee6SAdrian Chadd 		    __func__,
649122a3aee6SAdrian Chadd 		    ni->ni_macaddr,
649222a3aee6SAdrian Chadd 		    ":",
649322a3aee6SAdrian Chadd 		    tid);
649422a3aee6SAdrian Chadd 		return;
649522a3aee6SAdrian Chadd 	}
649622a3aee6SAdrian Chadd 
649722a3aee6SAdrian Chadd 	ATH_TX_UNLOCK(sc);
649822a3aee6SAdrian Chadd 
649922a3aee6SAdrian Chadd 	/*
650022a3aee6SAdrian Chadd 	 * XXX nothing in the TIDs at this point? Eek.
650122a3aee6SAdrian Chadd 	 */
650283bbd5ebSRui Paulo 	DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
650383bbd5ebSRui Paulo 	    "%s: %6D: TIDs empty, but ath_node showed traffic?!\n",
650422a3aee6SAdrian Chadd 	    __func__,
650522a3aee6SAdrian Chadd 	    ni->ni_macaddr,
650622a3aee6SAdrian Chadd 	    ":");
650722a3aee6SAdrian Chadd 	avp->av_recv_pspoll(ni, m);
650822a3aee6SAdrian Chadd #else
650922a3aee6SAdrian Chadd 	avp->av_recv_pspoll(ni, m);
651022a3aee6SAdrian Chadd #endif	/* ATH_SW_PSQ */
651122a3aee6SAdrian Chadd }
651222a3aee6SAdrian Chadd 
6513dba9c859SAdrian Chadd MODULE_VERSION(if_ath, 1);
6514dba9c859SAdrian Chadd MODULE_DEPEND(if_ath, wlan, 1, 1, 1);          /* 802.11 media layer */
651558816f3fSAdrian Chadd #if	defined(IEEE80211_ALQ) || defined(AH_DEBUG_ALQ)
651658816f3fSAdrian Chadd MODULE_DEPEND(if_ath, alq, 1, 1, 1);
651758816f3fSAdrian Chadd #endif
6518