xref: /freebsd/sys/dev/ath/if_ath.c (revision 7e97436b0ed53d1f264b8d586b0b0b24aede8c10)
1 /*-
2  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 /*
34  * Driver for the Atheros Wireless LAN controller.
35  *
36  * This software is derived from work of Atsushi Onoe; his contribution
37  * is greatly appreciated.
38  */
39 
40 #include "opt_inet.h"
41 #include "opt_ath.h"
42 /*
43  * This is needed for register operations which are performed
44  * by the driver - eg, calls to ath_hal_gettsf32().
45  */
46 #include "opt_ah.h"
47 #include "opt_wlan.h"
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/sysctl.h>
52 #include <sys/mbuf.h>
53 #include <sys/malloc.h>
54 #include <sys/lock.h>
55 #include <sys/mutex.h>
56 #include <sys/kernel.h>
57 #include <sys/socket.h>
58 #include <sys/sockio.h>
59 #include <sys/errno.h>
60 #include <sys/callout.h>
61 #include <sys/bus.h>
62 #include <sys/endian.h>
63 #include <sys/kthread.h>
64 #include <sys/taskqueue.h>
65 #include <sys/priv.h>
66 #include <sys/module.h>
67 #include <sys/ktr.h>
68 #include <sys/smp.h>	/* for mp_ncpus */
69 
70 #include <machine/bus.h>
71 
72 #include <net/if.h>
73 #include <net/if_dl.h>
74 #include <net/if_media.h>
75 #include <net/if_types.h>
76 #include <net/if_arp.h>
77 #include <net/ethernet.h>
78 #include <net/if_llc.h>
79 
80 #include <net80211/ieee80211_var.h>
81 #include <net80211/ieee80211_regdomain.h>
82 #ifdef IEEE80211_SUPPORT_SUPERG
83 #include <net80211/ieee80211_superg.h>
84 #endif
85 #ifdef IEEE80211_SUPPORT_TDMA
86 #include <net80211/ieee80211_tdma.h>
87 #endif
88 
89 #include <net/bpf.h>
90 
91 #ifdef INET
92 #include <netinet/in.h>
93 #include <netinet/if_ether.h>
94 #endif
95 
96 #include <dev/ath/if_athvar.h>
97 #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
98 #include <dev/ath/ath_hal/ah_diagcodes.h>
99 
100 #include <dev/ath/if_ath_debug.h>
101 #include <dev/ath/if_ath_misc.h>
102 #include <dev/ath/if_ath_tx.h>
103 #include <dev/ath/if_ath_sysctl.h>
104 #include <dev/ath/if_ath_keycache.h>
105 #include <dev/ath/if_athdfs.h>
106 
107 #ifdef ATH_TX99_DIAG
108 #include <dev/ath/ath_tx99/ath_tx99.h>
109 #endif
110 
111 #define	ATH_KTR_INTR	KTR_SPARE4
112 #define	ATH_KTR_ERR	KTR_SPARE3
113 
114 /*
115  * ATH_BCBUF determines the number of vap's that can transmit
116  * beacons and also (currently) the number of vap's that can
117  * have unique mac addresses/bssid.  When staggering beacons
118  * 4 is probably a good max as otherwise the beacons become
119  * very closely spaced and there is limited time for cab q traffic
120  * to go out.  You can burst beacons instead but that is not good
121  * for stations in power save and at some point you really want
122  * another radio (and channel).
123  *
124  * The limit on the number of mac addresses is tied to our use of
125  * the U/L bit and tracking addresses in a byte; it would be
126  * worthwhile to allow more for applications like proxy sta.
127  */
128 CTASSERT(ATH_BCBUF <= 8);
129 
130 static struct ieee80211vap *ath_vap_create(struct ieee80211com *,
131 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
132 		    const uint8_t [IEEE80211_ADDR_LEN],
133 		    const uint8_t [IEEE80211_ADDR_LEN]);
134 static void	ath_vap_delete(struct ieee80211vap *);
135 static void	ath_init(void *);
136 static void	ath_stop_locked(struct ifnet *);
137 static void	ath_stop(struct ifnet *);
138 static void	ath_start(struct ifnet *);
139 static int	ath_reset_vap(struct ieee80211vap *, u_long);
140 static int	ath_media_change(struct ifnet *);
141 static void	ath_watchdog(void *);
142 static int	ath_ioctl(struct ifnet *, u_long, caddr_t);
143 static void	ath_fatal_proc(void *, int);
144 static void	ath_bmiss_vap(struct ieee80211vap *);
145 static void	ath_bmiss_proc(void *, int);
146 static void	ath_key_update_begin(struct ieee80211vap *);
147 static void	ath_key_update_end(struct ieee80211vap *);
148 static void	ath_update_mcast(struct ifnet *);
149 static void	ath_update_promisc(struct ifnet *);
150 static void	ath_mode_init(struct ath_softc *);
151 static void	ath_setslottime(struct ath_softc *);
152 static void	ath_updateslot(struct ifnet *);
153 static int	ath_beaconq_setup(struct ath_hal *);
154 static int	ath_beacon_alloc(struct ath_softc *, struct ieee80211_node *);
155 static void	ath_beacon_update(struct ieee80211vap *, int item);
156 static void	ath_beacon_setup(struct ath_softc *, struct ath_buf *);
157 static void	ath_beacon_proc(void *, int);
158 static struct ath_buf *ath_beacon_generate(struct ath_softc *,
159 			struct ieee80211vap *);
160 static void	ath_bstuck_proc(void *, int);
161 static void	ath_beacon_return(struct ath_softc *, struct ath_buf *);
162 static void	ath_beacon_free(struct ath_softc *);
163 static void	ath_beacon_config(struct ath_softc *, struct ieee80211vap *);
164 static void	ath_descdma_cleanup(struct ath_softc *sc,
165 			struct ath_descdma *, ath_bufhead *);
166 static int	ath_desc_alloc(struct ath_softc *);
167 static void	ath_desc_free(struct ath_softc *);
168 static struct ieee80211_node *ath_node_alloc(struct ieee80211vap *,
169 			const uint8_t [IEEE80211_ADDR_LEN]);
170 static void	ath_node_cleanup(struct ieee80211_node *);
171 static void	ath_node_free(struct ieee80211_node *);
172 static void	ath_node_getsignal(const struct ieee80211_node *,
173 			int8_t *, int8_t *);
174 static int	ath_rxbuf_init(struct ath_softc *, struct ath_buf *);
175 static void	ath_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
176 			int subtype, int rssi, int nf);
177 static void	ath_setdefantenna(struct ath_softc *, u_int);
178 static void	ath_rx_proc(struct ath_softc *sc, int);
179 static void	ath_rx_tasklet(void *, int);
180 static void	ath_txq_init(struct ath_softc *sc, struct ath_txq *, int);
181 static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype);
182 static int	ath_tx_setup(struct ath_softc *, int, int);
183 static int	ath_wme_update(struct ieee80211com *);
184 static void	ath_tx_cleanupq(struct ath_softc *, struct ath_txq *);
185 static void	ath_tx_cleanup(struct ath_softc *);
186 static void	ath_tx_proc_q0(void *, int);
187 static void	ath_tx_proc_q0123(void *, int);
188 static void	ath_tx_proc(void *, int);
189 static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
190 static void	ath_draintxq(struct ath_softc *, ATH_RESET_TYPE reset_type);
191 static void	ath_stoprecv(struct ath_softc *, int);
192 static int	ath_startrecv(struct ath_softc *);
193 static void	ath_chan_change(struct ath_softc *, struct ieee80211_channel *);
194 static void	ath_scan_start(struct ieee80211com *);
195 static void	ath_scan_end(struct ieee80211com *);
196 static void	ath_set_channel(struct ieee80211com *);
197 static void	ath_calibrate(void *);
198 static int	ath_newstate(struct ieee80211vap *, enum ieee80211_state, int);
199 static void	ath_setup_stationkey(struct ieee80211_node *);
200 static void	ath_newassoc(struct ieee80211_node *, int);
201 static int	ath_setregdomain(struct ieee80211com *,
202 		    struct ieee80211_regdomain *, int,
203 		    struct ieee80211_channel []);
204 static void	ath_getradiocaps(struct ieee80211com *, int, int *,
205 		    struct ieee80211_channel []);
206 static int	ath_getchannels(struct ath_softc *);
207 static void	ath_led_event(struct ath_softc *, int);
208 
209 static int	ath_rate_setup(struct ath_softc *, u_int mode);
210 static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
211 
212 static void	ath_announce(struct ath_softc *);
213 
214 static void	ath_dfs_tasklet(void *, int);
215 
216 #ifdef IEEE80211_SUPPORT_TDMA
217 static void	ath_tdma_settimers(struct ath_softc *sc, u_int32_t nexttbtt,
218 		    u_int32_t bintval);
219 static void	ath_tdma_bintvalsetup(struct ath_softc *sc,
220 		    const struct ieee80211_tdma_state *tdma);
221 static void	ath_tdma_config(struct ath_softc *sc, struct ieee80211vap *vap);
222 static void	ath_tdma_update(struct ieee80211_node *ni,
223 		    const struct ieee80211_tdma_param *tdma, int);
224 static void	ath_tdma_beacon_send(struct ath_softc *sc,
225 		    struct ieee80211vap *vap);
226 
227 #define	TDMA_EP_MULTIPLIER	(1<<10) /* pow2 to optimize out * and / */
228 #define	TDMA_LPF_LEN		6
229 #define	TDMA_DUMMY_MARKER	0x127
230 #define	TDMA_EP_MUL(x, mul)	((x) * (mul))
231 #define	TDMA_IN(x)		(TDMA_EP_MUL((x), TDMA_EP_MULTIPLIER))
232 #define	TDMA_LPF(x, y, len) \
233     ((x != TDMA_DUMMY_MARKER) ? (((x) * ((len)-1) + (y)) / (len)) : (y))
234 #define	TDMA_SAMPLE(x, y) do {					\
235 	x = TDMA_LPF((x), TDMA_IN(y), TDMA_LPF_LEN);		\
236 } while (0)
237 #define	TDMA_EP_RND(x,mul) \
238 	((((x)%(mul)) >= ((mul)/2)) ? ((x) + ((mul) - 1)) / (mul) : (x)/(mul))
239 #define	TDMA_AVG(x)		TDMA_EP_RND(x, TDMA_EP_MULTIPLIER)
240 #endif /* IEEE80211_SUPPORT_TDMA */
241 
242 SYSCTL_DECL(_hw_ath);
243 
244 /* XXX validate sysctl values */
245 static	int ath_longcalinterval = 30;		/* long cals every 30 secs */
246 SYSCTL_INT(_hw_ath, OID_AUTO, longcal, CTLFLAG_RW, &ath_longcalinterval,
247 	    0, "long chip calibration interval (secs)");
248 static	int ath_shortcalinterval = 100;		/* short cals every 100 ms */
249 SYSCTL_INT(_hw_ath, OID_AUTO, shortcal, CTLFLAG_RW, &ath_shortcalinterval,
250 	    0, "short chip calibration interval (msecs)");
251 static	int ath_resetcalinterval = 20*60;	/* reset cal state 20 mins */
252 SYSCTL_INT(_hw_ath, OID_AUTO, resetcal, CTLFLAG_RW, &ath_resetcalinterval,
253 	    0, "reset chip calibration results (secs)");
254 static	int ath_anicalinterval = 100;		/* ANI calibration - 100 msec */
255 SYSCTL_INT(_hw_ath, OID_AUTO, anical, CTLFLAG_RW, &ath_anicalinterval,
256 	    0, "ANI calibration (msecs)");
257 
258 static	int ath_rxbuf = ATH_RXBUF;		/* # rx buffers to allocate */
259 SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RW, &ath_rxbuf,
260 	    0, "rx buffers allocated");
261 TUNABLE_INT("hw.ath.rxbuf", &ath_rxbuf);
262 static	int ath_txbuf = ATH_TXBUF;		/* # tx buffers to allocate */
263 SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RW, &ath_txbuf,
264 	    0, "tx buffers allocated");
265 TUNABLE_INT("hw.ath.txbuf", &ath_txbuf);
266 
267 static	int ath_bstuck_threshold = 4;		/* max missed beacons */
268 SYSCTL_INT(_hw_ath, OID_AUTO, bstuck, CTLFLAG_RW, &ath_bstuck_threshold,
269 	    0, "max missed beacon xmits before chip reset");
270 
271 MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
272 
273 #define	HAL_MODE_HT20 (HAL_MODE_11NG_HT20 | HAL_MODE_11NA_HT20)
274 #define	HAL_MODE_HT40 \
275 	(HAL_MODE_11NG_HT40PLUS | HAL_MODE_11NG_HT40MINUS | \
276 	HAL_MODE_11NA_HT40PLUS | HAL_MODE_11NA_HT40MINUS)
277 int
278 ath_attach(u_int16_t devid, struct ath_softc *sc)
279 {
280 	struct ifnet *ifp;
281 	struct ieee80211com *ic;
282 	struct ath_hal *ah = NULL;
283 	HAL_STATUS status;
284 	int error = 0, i;
285 	u_int wmodes;
286 	uint8_t macaddr[IEEE80211_ADDR_LEN];
287 
288 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
289 
290 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
291 	if (ifp == NULL) {
292 		device_printf(sc->sc_dev, "can not if_alloc()\n");
293 		error = ENOSPC;
294 		goto bad;
295 	}
296 	ic = ifp->if_l2com;
297 
298 	/* set these up early for if_printf use */
299 	if_initname(ifp, device_get_name(sc->sc_dev),
300 		device_get_unit(sc->sc_dev));
301 
302 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh,
303 	    sc->sc_eepromdata, &status);
304 	if (ah == NULL) {
305 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
306 			status);
307 		error = ENXIO;
308 		goto bad;
309 	}
310 	sc->sc_ah = ah;
311 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
312 #ifdef	ATH_DEBUG
313 	sc->sc_debug = ath_debug;
314 #endif
315 
316 	/*
317 	 * Check if the MAC has multi-rate retry support.
318 	 * We do this by trying to setup a fake extended
319 	 * descriptor.  MAC's that don't have support will
320 	 * return false w/o doing anything.  MAC's that do
321 	 * support it will return true w/o doing anything.
322 	 */
323 	sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
324 
325 	/*
326 	 * Check if the device has hardware counters for PHY
327 	 * errors.  If so we need to enable the MIB interrupt
328 	 * so we can act on stat triggers.
329 	 */
330 	if (ath_hal_hwphycounters(ah))
331 		sc->sc_needmib = 1;
332 
333 	/*
334 	 * Get the hardware key cache size.
335 	 */
336 	sc->sc_keymax = ath_hal_keycachesize(ah);
337 	if (sc->sc_keymax > ATH_KEYMAX) {
338 		if_printf(ifp, "Warning, using only %u of %u key cache slots\n",
339 			ATH_KEYMAX, sc->sc_keymax);
340 		sc->sc_keymax = ATH_KEYMAX;
341 	}
342 	/*
343 	 * Reset the key cache since some parts do not
344 	 * reset the contents on initial power up.
345 	 */
346 	for (i = 0; i < sc->sc_keymax; i++)
347 		ath_hal_keyreset(ah, i);
348 
349 	/*
350 	 * Collect the default channel list.
351 	 */
352 	error = ath_getchannels(sc);
353 	if (error != 0)
354 		goto bad;
355 
356 	/*
357 	 * Setup rate tables for all potential media types.
358 	 */
359 	ath_rate_setup(sc, IEEE80211_MODE_11A);
360 	ath_rate_setup(sc, IEEE80211_MODE_11B);
361 	ath_rate_setup(sc, IEEE80211_MODE_11G);
362 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
363 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
364 	ath_rate_setup(sc, IEEE80211_MODE_STURBO_A);
365 	ath_rate_setup(sc, IEEE80211_MODE_11NA);
366 	ath_rate_setup(sc, IEEE80211_MODE_11NG);
367 	ath_rate_setup(sc, IEEE80211_MODE_HALF);
368 	ath_rate_setup(sc, IEEE80211_MODE_QUARTER);
369 
370 	/* NB: setup here so ath_rate_update is happy */
371 	ath_setcurmode(sc, IEEE80211_MODE_11A);
372 
373 	/*
374 	 * Allocate tx+rx descriptors and populate the lists.
375 	 */
376 	error = ath_desc_alloc(sc);
377 	if (error != 0) {
378 		if_printf(ifp, "failed to allocate descriptors: %d\n", error);
379 		goto bad;
380 	}
381 	callout_init_mtx(&sc->sc_cal_ch, &sc->sc_mtx, 0);
382 	callout_init_mtx(&sc->sc_wd_ch, &sc->sc_mtx, 0);
383 
384 	ATH_TXBUF_LOCK_INIT(sc);
385 
386 	sc->sc_tq = taskqueue_create("ath_taskq", M_NOWAIT,
387 		taskqueue_thread_enqueue, &sc->sc_tq);
388 	taskqueue_start_threads(&sc->sc_tq, 1, PI_NET,
389 		"%s taskq", ifp->if_xname);
390 
391 	TASK_INIT(&sc->sc_rxtask, 0, ath_rx_tasklet, sc);
392 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
393 	TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc);
394 
395 	/*
396 	 * Allocate hardware transmit queues: one queue for
397 	 * beacon frames and one data queue for each QoS
398 	 * priority.  Note that the hal handles resetting
399 	 * these queues at the needed time.
400 	 *
401 	 * XXX PS-Poll
402 	 */
403 	sc->sc_bhalq = ath_beaconq_setup(ah);
404 	if (sc->sc_bhalq == (u_int) -1) {
405 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
406 		error = EIO;
407 		goto bad2;
408 	}
409 	sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
410 	if (sc->sc_cabq == NULL) {
411 		if_printf(ifp, "unable to setup CAB xmit queue!\n");
412 		error = EIO;
413 		goto bad2;
414 	}
415 	/* NB: insure BK queue is the lowest priority h/w queue */
416 	if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
417 		if_printf(ifp, "unable to setup xmit queue for %s traffic!\n",
418 			ieee80211_wme_acnames[WME_AC_BK]);
419 		error = EIO;
420 		goto bad2;
421 	}
422 	if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
423 	    !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
424 	    !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
425 		/*
426 		 * Not enough hardware tx queues to properly do WME;
427 		 * just punt and assign them all to the same h/w queue.
428 		 * We could do a better job of this if, for example,
429 		 * we allocate queues when we switch from station to
430 		 * AP mode.
431 		 */
432 		if (sc->sc_ac2q[WME_AC_VI] != NULL)
433 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
434 		if (sc->sc_ac2q[WME_AC_BE] != NULL)
435 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
436 		sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
437 		sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
438 		sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
439 	}
440 
441 	/*
442 	 * Special case certain configurations.  Note the
443 	 * CAB queue is handled by these specially so don't
444 	 * include them when checking the txq setup mask.
445 	 */
446 	switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
447 	case 0x01:
448 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
449 		break;
450 	case 0x0f:
451 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
452 		break;
453 	default:
454 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
455 		break;
456 	}
457 
458 	/*
459 	 * Setup rate control.  Some rate control modules
460 	 * call back to change the anntena state so expose
461 	 * the necessary entry points.
462 	 * XXX maybe belongs in struct ath_ratectrl?
463 	 */
464 	sc->sc_setdefantenna = ath_setdefantenna;
465 	sc->sc_rc = ath_rate_attach(sc);
466 	if (sc->sc_rc == NULL) {
467 		error = EIO;
468 		goto bad2;
469 	}
470 
471 	/* Attach DFS module */
472 	if (! ath_dfs_attach(sc)) {
473 		device_printf(sc->sc_dev,
474 		    "%s: unable to attach DFS\n", __func__);
475 		error = EIO;
476 		goto bad2;
477 	}
478 
479 	/* Start DFS processing tasklet */
480 	TASK_INIT(&sc->sc_dfstask, 0, ath_dfs_tasklet, sc);
481 
482 	sc->sc_blinking = 0;
483 	sc->sc_ledstate = 1;
484 	sc->sc_ledon = 0;			/* low true */
485 	sc->sc_ledidle = (2700*hz)/1000;	/* 2.7sec */
486 	callout_init(&sc->sc_ledtimer, CALLOUT_MPSAFE);
487 	/*
488 	 * Auto-enable soft led processing for IBM cards and for
489 	 * 5211 minipci cards.  Users can also manually enable/disable
490 	 * support with a sysctl.
491 	 */
492 	sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
493 	if (sc->sc_softled) {
494 		ath_hal_gpioCfgOutput(ah, sc->sc_ledpin,
495 		    HAL_GPIO_MUX_MAC_NETWORK_LED);
496 		ath_hal_gpioset(ah, sc->sc_ledpin, !sc->sc_ledon);
497 	}
498 
499 	ifp->if_softc = sc;
500 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
501 	ifp->if_start = ath_start;
502 	ifp->if_ioctl = ath_ioctl;
503 	ifp->if_init = ath_init;
504 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
505 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
506 	IFQ_SET_READY(&ifp->if_snd);
507 
508 	ic->ic_ifp = ifp;
509 	/* XXX not right but it's not used anywhere important */
510 	ic->ic_phytype = IEEE80211_T_OFDM;
511 	ic->ic_opmode = IEEE80211_M_STA;
512 	ic->ic_caps =
513 		  IEEE80211_C_STA		/* station mode */
514 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
515 		| IEEE80211_C_HOSTAP		/* hostap mode */
516 		| IEEE80211_C_MONITOR		/* monitor mode */
517 		| IEEE80211_C_AHDEMO		/* adhoc demo mode */
518 		| IEEE80211_C_WDS		/* 4-address traffic works */
519 		| IEEE80211_C_MBSS		/* mesh point link mode */
520 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
521 		| IEEE80211_C_SHSLOT		/* short slot time supported */
522 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
523 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
524 		| IEEE80211_C_TXFRAG		/* handle tx frags */
525 #ifdef	ATH_ENABLE_DFS
526 		| IEEE80211_C_DFS		/* Enable radar detection */
527 #endif
528 		;
529 	/*
530 	 * Query the hal to figure out h/w crypto support.
531 	 */
532 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
533 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
534 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
535 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_OCB;
536 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
537 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_CCM;
538 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
539 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_CKIP;
540 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
541 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIP;
542 		/*
543 		 * Check if h/w does the MIC and/or whether the
544 		 * separate key cache entries are required to
545 		 * handle both tx+rx MIC keys.
546 		 */
547 		if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
548 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
549 		/*
550 		 * If the h/w supports storing tx+rx MIC keys
551 		 * in one cache slot automatically enable use.
552 		 */
553 		if (ath_hal_hastkipsplit(ah) ||
554 		    !ath_hal_settkipsplit(ah, AH_FALSE))
555 			sc->sc_splitmic = 1;
556 		/*
557 		 * If the h/w can do TKIP MIC together with WME then
558 		 * we use it; otherwise we force the MIC to be done
559 		 * in software by the net80211 layer.
560 		 */
561 		if (ath_hal_haswmetkipmic(ah))
562 			sc->sc_wmetkipmic = 1;
563 	}
564 	sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR);
565 	/*
566 	 * Check for multicast key search support.
567 	 */
568 	if (ath_hal_hasmcastkeysearch(sc->sc_ah) &&
569 	    !ath_hal_getmcastkeysearch(sc->sc_ah)) {
570 		ath_hal_setmcastkeysearch(sc->sc_ah, 1);
571 	}
572 	sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah);
573 	/*
574 	 * Mark key cache slots associated with global keys
575 	 * as in use.  If we knew TKIP was not to be used we
576 	 * could leave the +32, +64, and +32+64 slots free.
577 	 */
578 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
579 		setbit(sc->sc_keymap, i);
580 		setbit(sc->sc_keymap, i+64);
581 		if (sc->sc_splitmic) {
582 			setbit(sc->sc_keymap, i+32);
583 			setbit(sc->sc_keymap, i+32+64);
584 		}
585 	}
586 	/*
587 	 * TPC support can be done either with a global cap or
588 	 * per-packet support.  The latter is not available on
589 	 * all parts.  We're a bit pedantic here as all parts
590 	 * support a global cap.
591 	 */
592 	if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah))
593 		ic->ic_caps |= IEEE80211_C_TXPMGT;
594 
595 	/*
596 	 * Mark WME capability only if we have sufficient
597 	 * hardware queues to do proper priority scheduling.
598 	 */
599 	if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
600 		ic->ic_caps |= IEEE80211_C_WME;
601 	/*
602 	 * Check for misc other capabilities.
603 	 */
604 	if (ath_hal_hasbursting(ah))
605 		ic->ic_caps |= IEEE80211_C_BURST;
606 	sc->sc_hasbmask = ath_hal_hasbssidmask(ah);
607 	sc->sc_hasbmatch = ath_hal_hasbssidmatch(ah);
608 	sc->sc_hastsfadd = ath_hal_hastsfadjust(ah);
609 	sc->sc_rxslink = ath_hal_self_linked_final_rxdesc(ah);
610 	sc->sc_rxtsf32 = ath_hal_has_long_rxdesc_tsf(ah);
611 	if (ath_hal_hasfastframes(ah))
612 		ic->ic_caps |= IEEE80211_C_FF;
613 	wmodes = ath_hal_getwirelessmodes(ah);
614 	if (wmodes & (HAL_MODE_108G|HAL_MODE_TURBO))
615 		ic->ic_caps |= IEEE80211_C_TURBOP;
616 #ifdef IEEE80211_SUPPORT_TDMA
617 	if (ath_hal_macversion(ah) > 0x78) {
618 		ic->ic_caps |= IEEE80211_C_TDMA; /* capable of TDMA */
619 		ic->ic_tdma_update = ath_tdma_update;
620 	}
621 #endif
622 
623 	/*
624 	 * The if_ath 11n support is completely not ready for normal use.
625 	 * Enabling this option will likely break everything and everything.
626 	 * Don't think of doing that unless you know what you're doing.
627 	 */
628 
629 #ifdef	ATH_ENABLE_11N
630 	/*
631 	 * Query HT capabilities
632 	 */
633 	if (ath_hal_getcapability(ah, HAL_CAP_HT, 0, NULL) == HAL_OK &&
634 	    (wmodes & (HAL_MODE_HT20 | HAL_MODE_HT40))) {
635 		int rxs, txs;
636 
637 		device_printf(sc->sc_dev, "[HT] enabling HT modes\n");
638 		ic->ic_htcaps = IEEE80211_HTC_HT	/* HT operation */
639 			    | IEEE80211_HTC_AMPDU	/* A-MPDU tx/rx */
640 			    | IEEE80211_HTC_AMSDU	/* A-MSDU tx/rx */
641 			    | IEEE80211_HTCAP_MAXAMSDU_3839
642 			    				/* max A-MSDU length */
643 			    | IEEE80211_HTCAP_SMPS_OFF;	/* SM power save off */
644 			;
645 
646 		/*
647 		 * Enable short-GI for HT20 only if the hardware
648 		 * advertises support.
649 		 * Notably, anything earlier than the AR9287 doesn't.
650 		 */
651 		if ((ath_hal_getcapability(ah,
652 		    HAL_CAP_HT20_SGI, 0, NULL) == HAL_OK) &&
653 		    (wmodes & HAL_MODE_HT20)) {
654 			device_printf(sc->sc_dev,
655 			    "[HT] enabling short-GI in 20MHz mode\n");
656 			ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
657 		}
658 
659 		if (wmodes & HAL_MODE_HT40)
660 			ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40
661 			    |  IEEE80211_HTCAP_SHORTGI40;
662 
663 		/*
664 		 * TX/RX streams need to be taken into account when
665 		 * negotiating which MCS rates it'll receive and
666 		 * what MCS rates are available for TX.
667 		 */
668 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 0, &rxs);
669 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 1, &txs);
670 
671 		ath_hal_getrxchainmask(ah, &sc->sc_rxchainmask);
672 		ath_hal_gettxchainmask(ah, &sc->sc_txchainmask);
673 
674 		ic->ic_txstream = txs;
675 		ic->ic_rxstream = rxs;
676 
677 		device_printf(sc->sc_dev,
678 		    "[HT] %d RX streams; %d TX streams\n", rxs, txs);
679 	}
680 #endif
681 
682 	/*
683 	 * Check if the hardware requires PCI register serialisation.
684 	 * Some of the Owl based MACs require this.
685 	 */
686 	if (mp_ncpus > 1 &&
687 	    ath_hal_getcapability(ah, HAL_CAP_SERIALISE_WAR,
688 	     0, NULL) == HAL_OK) {
689 		sc->sc_ah->ah_config.ah_serialise_reg_war = 1;
690 		device_printf(sc->sc_dev,
691 		    "Enabling register serialisation\n");
692 	}
693 
694 	/*
695 	 * Indicate we need the 802.11 header padded to a
696 	 * 32-bit boundary for 4-address and QoS frames.
697 	 */
698 	ic->ic_flags |= IEEE80211_F_DATAPAD;
699 
700 	/*
701 	 * Query the hal about antenna support.
702 	 */
703 	sc->sc_defant = ath_hal_getdefantenna(ah);
704 
705 	/*
706 	 * Not all chips have the VEOL support we want to
707 	 * use with IBSS beacons; check here for it.
708 	 */
709 	sc->sc_hasveol = ath_hal_hasveol(ah);
710 
711 	/* get mac address from hardware */
712 	ath_hal_getmac(ah, macaddr);
713 	if (sc->sc_hasbmask)
714 		ath_hal_getbssidmask(ah, sc->sc_hwbssidmask);
715 
716 	/* NB: used to size node table key mapping array */
717 	ic->ic_max_keyix = sc->sc_keymax;
718 	/* call MI attach routine. */
719 	ieee80211_ifattach(ic, macaddr);
720 	ic->ic_setregdomain = ath_setregdomain;
721 	ic->ic_getradiocaps = ath_getradiocaps;
722 	sc->sc_opmode = HAL_M_STA;
723 
724 	/* override default methods */
725 	ic->ic_newassoc = ath_newassoc;
726 	ic->ic_updateslot = ath_updateslot;
727 	ic->ic_wme.wme_update = ath_wme_update;
728 	ic->ic_vap_create = ath_vap_create;
729 	ic->ic_vap_delete = ath_vap_delete;
730 	ic->ic_raw_xmit = ath_raw_xmit;
731 	ic->ic_update_mcast = ath_update_mcast;
732 	ic->ic_update_promisc = ath_update_promisc;
733 	ic->ic_node_alloc = ath_node_alloc;
734 	sc->sc_node_free = ic->ic_node_free;
735 	ic->ic_node_free = ath_node_free;
736 	sc->sc_node_cleanup = ic->ic_node_cleanup;
737 	ic->ic_node_cleanup = ath_node_cleanup;
738 	ic->ic_node_getsignal = ath_node_getsignal;
739 	ic->ic_scan_start = ath_scan_start;
740 	ic->ic_scan_end = ath_scan_end;
741 	ic->ic_set_channel = ath_set_channel;
742 
743 	/* 802.11n specific - but just override anyway */
744 	sc->sc_addba_request = ic->ic_addba_request;
745 	sc->sc_addba_response = ic->ic_addba_response;
746 	sc->sc_addba_stop = ic->ic_addba_stop;
747 	sc->sc_bar_response = ic->ic_bar_response;
748 	sc->sc_addba_response_timeout = ic->ic_addba_response_timeout;
749 
750 	ic->ic_addba_request = ath_addba_request;
751 	ic->ic_addba_response = ath_addba_response;
752 	ic->ic_addba_response_timeout = ath_addba_response_timeout;
753 	ic->ic_addba_stop = ath_addba_stop;
754 	ic->ic_bar_response = ath_bar_response;
755 
756 	ieee80211_radiotap_attach(ic,
757 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
758 		ATH_TX_RADIOTAP_PRESENT,
759 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
760 		ATH_RX_RADIOTAP_PRESENT);
761 
762 	/*
763 	 * Setup dynamic sysctl's now that country code and
764 	 * regdomain are available from the hal.
765 	 */
766 	ath_sysctlattach(sc);
767 	ath_sysctl_stats_attach(sc);
768 	ath_sysctl_hal_attach(sc);
769 
770 	if (bootverbose)
771 		ieee80211_announce(ic);
772 	ath_announce(sc);
773 	return 0;
774 bad2:
775 	ath_tx_cleanup(sc);
776 	ath_desc_free(sc);
777 bad:
778 	if (ah)
779 		ath_hal_detach(ah);
780 	if (ifp != NULL)
781 		if_free(ifp);
782 	sc->sc_invalid = 1;
783 	return error;
784 }
785 
786 int
787 ath_detach(struct ath_softc *sc)
788 {
789 	struct ifnet *ifp = sc->sc_ifp;
790 
791 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
792 		__func__, ifp->if_flags);
793 
794 	/*
795 	 * NB: the order of these is important:
796 	 * o stop the chip so no more interrupts will fire
797 	 * o call the 802.11 layer before detaching the hal to
798 	 *   insure callbacks into the driver to delete global
799 	 *   key cache entries can be handled
800 	 * o free the taskqueue which drains any pending tasks
801 	 * o reclaim the tx queue data structures after calling
802 	 *   the 802.11 layer as we'll get called back to reclaim
803 	 *   node state and potentially want to use them
804 	 * o to cleanup the tx queues the hal is called, so detach
805 	 *   it last
806 	 * Other than that, it's straightforward...
807 	 */
808 	ath_stop(ifp);
809 	ieee80211_ifdetach(ifp->if_l2com);
810 	taskqueue_free(sc->sc_tq);
811 #ifdef ATH_TX99_DIAG
812 	if (sc->sc_tx99 != NULL)
813 		sc->sc_tx99->detach(sc->sc_tx99);
814 #endif
815 	ath_rate_detach(sc->sc_rc);
816 
817 	ath_dfs_detach(sc);
818 	ath_desc_free(sc);
819 	ath_tx_cleanup(sc);
820 	ath_hal_detach(sc->sc_ah);	/* NB: sets chip in full sleep */
821 	if_free(ifp);
822 
823 	return 0;
824 }
825 
826 /*
827  * MAC address handling for multiple BSS on the same radio.
828  * The first vap uses the MAC address from the EEPROM.  For
829  * subsequent vap's we set the U/L bit (bit 1) in the MAC
830  * address and use the next six bits as an index.
831  */
832 static void
833 assign_address(struct ath_softc *sc, uint8_t mac[IEEE80211_ADDR_LEN], int clone)
834 {
835 	int i;
836 
837 	if (clone && sc->sc_hasbmask) {
838 		/* NB: we only do this if h/w supports multiple bssid */
839 		for (i = 0; i < 8; i++)
840 			if ((sc->sc_bssidmask & (1<<i)) == 0)
841 				break;
842 		if (i != 0)
843 			mac[0] |= (i << 2)|0x2;
844 	} else
845 		i = 0;
846 	sc->sc_bssidmask |= 1<<i;
847 	sc->sc_hwbssidmask[0] &= ~mac[0];
848 	if (i == 0)
849 		sc->sc_nbssid0++;
850 }
851 
852 static void
853 reclaim_address(struct ath_softc *sc, const uint8_t mac[IEEE80211_ADDR_LEN])
854 {
855 	int i = mac[0] >> 2;
856 	uint8_t mask;
857 
858 	if (i != 0 || --sc->sc_nbssid0 == 0) {
859 		sc->sc_bssidmask &= ~(1<<i);
860 		/* recalculate bssid mask from remaining addresses */
861 		mask = 0xff;
862 		for (i = 1; i < 8; i++)
863 			if (sc->sc_bssidmask & (1<<i))
864 				mask &= ~((i<<2)|0x2);
865 		sc->sc_hwbssidmask[0] |= mask;
866 	}
867 }
868 
869 /*
870  * Assign a beacon xmit slot.  We try to space out
871  * assignments so when beacons are staggered the
872  * traffic coming out of the cab q has maximal time
873  * to go out before the next beacon is scheduled.
874  */
875 static int
876 assign_bslot(struct ath_softc *sc)
877 {
878 	u_int slot, free;
879 
880 	free = 0;
881 	for (slot = 0; slot < ATH_BCBUF; slot++)
882 		if (sc->sc_bslot[slot] == NULL) {
883 			if (sc->sc_bslot[(slot+1)%ATH_BCBUF] == NULL &&
884 			    sc->sc_bslot[(slot-1)%ATH_BCBUF] == NULL)
885 				return slot;
886 			free = slot;
887 			/* NB: keep looking for a double slot */
888 		}
889 	return free;
890 }
891 
892 static struct ieee80211vap *
893 ath_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
894     enum ieee80211_opmode opmode, int flags,
895     const uint8_t bssid[IEEE80211_ADDR_LEN],
896     const uint8_t mac0[IEEE80211_ADDR_LEN])
897 {
898 	struct ath_softc *sc = ic->ic_ifp->if_softc;
899 	struct ath_vap *avp;
900 	struct ieee80211vap *vap;
901 	uint8_t mac[IEEE80211_ADDR_LEN];
902 	int needbeacon, error;
903 	enum ieee80211_opmode ic_opmode;
904 
905 	avp = (struct ath_vap *) malloc(sizeof(struct ath_vap),
906 	    M_80211_VAP, M_WAITOK | M_ZERO);
907 	needbeacon = 0;
908 	IEEE80211_ADDR_COPY(mac, mac0);
909 
910 	ATH_LOCK(sc);
911 	ic_opmode = opmode;		/* default to opmode of new vap */
912 	switch (opmode) {
913 	case IEEE80211_M_STA:
914 		if (sc->sc_nstavaps != 0) {	/* XXX only 1 for now */
915 			device_printf(sc->sc_dev, "only 1 sta vap supported\n");
916 			goto bad;
917 		}
918 		if (sc->sc_nvaps) {
919 			/*
920 			 * With multiple vaps we must fall back
921 			 * to s/w beacon miss handling.
922 			 */
923 			flags |= IEEE80211_CLONE_NOBEACONS;
924 		}
925 		if (flags & IEEE80211_CLONE_NOBEACONS) {
926 			/*
927 			 * Station mode w/o beacons are implemented w/ AP mode.
928 			 */
929 			ic_opmode = IEEE80211_M_HOSTAP;
930 		}
931 		break;
932 	case IEEE80211_M_IBSS:
933 		if (sc->sc_nvaps != 0) {	/* XXX only 1 for now */
934 			device_printf(sc->sc_dev,
935 			    "only 1 ibss vap supported\n");
936 			goto bad;
937 		}
938 		needbeacon = 1;
939 		break;
940 	case IEEE80211_M_AHDEMO:
941 #ifdef IEEE80211_SUPPORT_TDMA
942 		if (flags & IEEE80211_CLONE_TDMA) {
943 			if (sc->sc_nvaps != 0) {
944 				device_printf(sc->sc_dev,
945 				    "only 1 tdma vap supported\n");
946 				goto bad;
947 			}
948 			needbeacon = 1;
949 			flags |= IEEE80211_CLONE_NOBEACONS;
950 		}
951 		/* fall thru... */
952 #endif
953 	case IEEE80211_M_MONITOR:
954 		if (sc->sc_nvaps != 0 && ic->ic_opmode != opmode) {
955 			/*
956 			 * Adopt existing mode.  Adding a monitor or ahdemo
957 			 * vap to an existing configuration is of dubious
958 			 * value but should be ok.
959 			 */
960 			/* XXX not right for monitor mode */
961 			ic_opmode = ic->ic_opmode;
962 		}
963 		break;
964 	case IEEE80211_M_HOSTAP:
965 	case IEEE80211_M_MBSS:
966 		needbeacon = 1;
967 		break;
968 	case IEEE80211_M_WDS:
969 		if (sc->sc_nvaps != 0 && ic->ic_opmode == IEEE80211_M_STA) {
970 			device_printf(sc->sc_dev,
971 			    "wds not supported in sta mode\n");
972 			goto bad;
973 		}
974 		/*
975 		 * Silently remove any request for a unique
976 		 * bssid; WDS vap's always share the local
977 		 * mac address.
978 		 */
979 		flags &= ~IEEE80211_CLONE_BSSID;
980 		if (sc->sc_nvaps == 0)
981 			ic_opmode = IEEE80211_M_HOSTAP;
982 		else
983 			ic_opmode = ic->ic_opmode;
984 		break;
985 	default:
986 		device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
987 		goto bad;
988 	}
989 	/*
990 	 * Check that a beacon buffer is available; the code below assumes it.
991 	 */
992 	if (needbeacon & TAILQ_EMPTY(&sc->sc_bbuf)) {
993 		device_printf(sc->sc_dev, "no beacon buffer available\n");
994 		goto bad;
995 	}
996 
997 	/* STA, AHDEMO? */
998 	if (opmode == IEEE80211_M_HOSTAP || opmode == IEEE80211_M_MBSS) {
999 		assign_address(sc, mac, flags & IEEE80211_CLONE_BSSID);
1000 		ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1001 	}
1002 
1003 	vap = &avp->av_vap;
1004 	/* XXX can't hold mutex across if_alloc */
1005 	ATH_UNLOCK(sc);
1006 	error = ieee80211_vap_setup(ic, vap, name, unit, opmode, flags,
1007 	    bssid, mac);
1008 	ATH_LOCK(sc);
1009 	if (error != 0) {
1010 		device_printf(sc->sc_dev, "%s: error %d creating vap\n",
1011 		    __func__, error);
1012 		goto bad2;
1013 	}
1014 
1015 	/* h/w crypto support */
1016 	vap->iv_key_alloc = ath_key_alloc;
1017 	vap->iv_key_delete = ath_key_delete;
1018 	vap->iv_key_set = ath_key_set;
1019 	vap->iv_key_update_begin = ath_key_update_begin;
1020 	vap->iv_key_update_end = ath_key_update_end;
1021 
1022 	/* override various methods */
1023 	avp->av_recv_mgmt = vap->iv_recv_mgmt;
1024 	vap->iv_recv_mgmt = ath_recv_mgmt;
1025 	vap->iv_reset = ath_reset_vap;
1026 	vap->iv_update_beacon = ath_beacon_update;
1027 	avp->av_newstate = vap->iv_newstate;
1028 	vap->iv_newstate = ath_newstate;
1029 	avp->av_bmiss = vap->iv_bmiss;
1030 	vap->iv_bmiss = ath_bmiss_vap;
1031 
1032 	/* Set default parameters */
1033 
1034 	/*
1035 	 * Anything earlier than some AR9300 series MACs don't
1036 	 * support a smaller MPDU density.
1037 	 */
1038 	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_8;
1039 	/*
1040 	 * All NICs can handle the maximum size, however
1041 	 * AR5416 based MACs can only TX aggregates w/ RTS
1042 	 * protection when the total aggregate size is <= 8k.
1043 	 * However, for now that's enforced by the TX path.
1044 	 */
1045 	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K;
1046 
1047 	avp->av_bslot = -1;
1048 	if (needbeacon) {
1049 		/*
1050 		 * Allocate beacon state and setup the q for buffered
1051 		 * multicast frames.  We know a beacon buffer is
1052 		 * available because we checked above.
1053 		 */
1054 		avp->av_bcbuf = TAILQ_FIRST(&sc->sc_bbuf);
1055 		TAILQ_REMOVE(&sc->sc_bbuf, avp->av_bcbuf, bf_list);
1056 		if (opmode != IEEE80211_M_IBSS || !sc->sc_hasveol) {
1057 			/*
1058 			 * Assign the vap to a beacon xmit slot.  As above
1059 			 * this cannot fail to find a free one.
1060 			 */
1061 			avp->av_bslot = assign_bslot(sc);
1062 			KASSERT(sc->sc_bslot[avp->av_bslot] == NULL,
1063 			    ("beacon slot %u not empty", avp->av_bslot));
1064 			sc->sc_bslot[avp->av_bslot] = vap;
1065 			sc->sc_nbcnvaps++;
1066 		}
1067 		if (sc->sc_hastsfadd && sc->sc_nbcnvaps > 0) {
1068 			/*
1069 			 * Multple vaps are to transmit beacons and we
1070 			 * have h/w support for TSF adjusting; enable
1071 			 * use of staggered beacons.
1072 			 */
1073 			sc->sc_stagbeacons = 1;
1074 		}
1075 		ath_txq_init(sc, &avp->av_mcastq, ATH_TXQ_SWQ);
1076 	}
1077 
1078 	ic->ic_opmode = ic_opmode;
1079 	if (opmode != IEEE80211_M_WDS) {
1080 		sc->sc_nvaps++;
1081 		if (opmode == IEEE80211_M_STA)
1082 			sc->sc_nstavaps++;
1083 		if (opmode == IEEE80211_M_MBSS)
1084 			sc->sc_nmeshvaps++;
1085 	}
1086 	switch (ic_opmode) {
1087 	case IEEE80211_M_IBSS:
1088 		sc->sc_opmode = HAL_M_IBSS;
1089 		break;
1090 	case IEEE80211_M_STA:
1091 		sc->sc_opmode = HAL_M_STA;
1092 		break;
1093 	case IEEE80211_M_AHDEMO:
1094 #ifdef IEEE80211_SUPPORT_TDMA
1095 		if (vap->iv_caps & IEEE80211_C_TDMA) {
1096 			sc->sc_tdma = 1;
1097 			/* NB: disable tsf adjust */
1098 			sc->sc_stagbeacons = 0;
1099 		}
1100 		/*
1101 		 * NB: adhoc demo mode is a pseudo mode; to the hal it's
1102 		 * just ap mode.
1103 		 */
1104 		/* fall thru... */
1105 #endif
1106 	case IEEE80211_M_HOSTAP:
1107 	case IEEE80211_M_MBSS:
1108 		sc->sc_opmode = HAL_M_HOSTAP;
1109 		break;
1110 	case IEEE80211_M_MONITOR:
1111 		sc->sc_opmode = HAL_M_MONITOR;
1112 		break;
1113 	default:
1114 		/* XXX should not happen */
1115 		break;
1116 	}
1117 	if (sc->sc_hastsfadd) {
1118 		/*
1119 		 * Configure whether or not TSF adjust should be done.
1120 		 */
1121 		ath_hal_settsfadjust(sc->sc_ah, sc->sc_stagbeacons);
1122 	}
1123 	if (flags & IEEE80211_CLONE_NOBEACONS) {
1124 		/*
1125 		 * Enable s/w beacon miss handling.
1126 		 */
1127 		sc->sc_swbmiss = 1;
1128 	}
1129 	ATH_UNLOCK(sc);
1130 
1131 	/* complete setup */
1132 	ieee80211_vap_attach(vap, ath_media_change, ieee80211_media_status);
1133 	return vap;
1134 bad2:
1135 	reclaim_address(sc, mac);
1136 	ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1137 bad:
1138 	free(avp, M_80211_VAP);
1139 	ATH_UNLOCK(sc);
1140 	return NULL;
1141 }
1142 
1143 static void
1144 ath_vap_delete(struct ieee80211vap *vap)
1145 {
1146 	struct ieee80211com *ic = vap->iv_ic;
1147 	struct ifnet *ifp = ic->ic_ifp;
1148 	struct ath_softc *sc = ifp->if_softc;
1149 	struct ath_hal *ah = sc->sc_ah;
1150 	struct ath_vap *avp = ATH_VAP(vap);
1151 
1152 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
1153 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1154 		/*
1155 		 * Quiesce the hardware while we remove the vap.  In
1156 		 * particular we need to reclaim all references to
1157 		 * the vap state by any frames pending on the tx queues.
1158 		 */
1159 		ath_hal_intrset(ah, 0);		/* disable interrupts */
1160 		ath_draintxq(sc, ATH_RESET_DEFAULT);		/* stop hw xmit side */
1161 		/* XXX Do all frames from all vaps/nodes need draining here? */
1162 		ath_stoprecv(sc, 1);		/* stop recv side */
1163 	}
1164 
1165 	ieee80211_vap_detach(vap);
1166 
1167 	/*
1168 	 * XXX Danger Will Robinson! Danger!
1169 	 *
1170 	 * Because ieee80211_vap_detach() can queue a frame (the station
1171 	 * diassociate message?) after we've drained the TXQ and
1172 	 * flushed the software TXQ, we will end up with a frame queued
1173 	 * to a node whose vap is about to be freed.
1174 	 *
1175 	 * To work around this, flush the hardware/software again.
1176 	 * This may be racy - the ath task may be running and the packet
1177 	 * may be being scheduled between sw->hw txq. Tsk.
1178 	 *
1179 	 * TODO: figure out why a new node gets allocated somewhere around
1180 	 * here (after the ath_tx_swq() call; and after an ath_stop_locked()
1181 	 * call!)
1182 	 */
1183 
1184 	ath_draintxq(sc, ATH_RESET_DEFAULT);
1185 
1186 	ATH_LOCK(sc);
1187 	/*
1188 	 * Reclaim beacon state.  Note this must be done before
1189 	 * the vap instance is reclaimed as we may have a reference
1190 	 * to it in the buffer for the beacon frame.
1191 	 */
1192 	if (avp->av_bcbuf != NULL) {
1193 		if (avp->av_bslot != -1) {
1194 			sc->sc_bslot[avp->av_bslot] = NULL;
1195 			sc->sc_nbcnvaps--;
1196 		}
1197 		ath_beacon_return(sc, avp->av_bcbuf);
1198 		avp->av_bcbuf = NULL;
1199 		if (sc->sc_nbcnvaps == 0) {
1200 			sc->sc_stagbeacons = 0;
1201 			if (sc->sc_hastsfadd)
1202 				ath_hal_settsfadjust(sc->sc_ah, 0);
1203 		}
1204 		/*
1205 		 * Reclaim any pending mcast frames for the vap.
1206 		 */
1207 		ath_tx_draintxq(sc, &avp->av_mcastq);
1208 		ATH_TXQ_LOCK_DESTROY(&avp->av_mcastq);
1209 	}
1210 	/*
1211 	 * Update bookkeeping.
1212 	 */
1213 	if (vap->iv_opmode == IEEE80211_M_STA) {
1214 		sc->sc_nstavaps--;
1215 		if (sc->sc_nstavaps == 0 && sc->sc_swbmiss)
1216 			sc->sc_swbmiss = 0;
1217 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1218 	    vap->iv_opmode == IEEE80211_M_MBSS) {
1219 		reclaim_address(sc, vap->iv_myaddr);
1220 		ath_hal_setbssidmask(ah, sc->sc_hwbssidmask);
1221 		if (vap->iv_opmode == IEEE80211_M_MBSS)
1222 			sc->sc_nmeshvaps--;
1223 	}
1224 	if (vap->iv_opmode != IEEE80211_M_WDS)
1225 		sc->sc_nvaps--;
1226 #ifdef IEEE80211_SUPPORT_TDMA
1227 	/* TDMA operation ceases when the last vap is destroyed */
1228 	if (sc->sc_tdma && sc->sc_nvaps == 0) {
1229 		sc->sc_tdma = 0;
1230 		sc->sc_swbmiss = 0;
1231 	}
1232 #endif
1233 	free(avp, M_80211_VAP);
1234 
1235 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1236 		/*
1237 		 * Restart rx+tx machines if still running (RUNNING will
1238 		 * be reset if we just destroyed the last vap).
1239 		 */
1240 		if (ath_startrecv(sc) != 0)
1241 			if_printf(ifp, "%s: unable to restart recv logic\n",
1242 			    __func__);
1243 		if (sc->sc_beacons) {		/* restart beacons */
1244 #ifdef IEEE80211_SUPPORT_TDMA
1245 			if (sc->sc_tdma)
1246 				ath_tdma_config(sc, NULL);
1247 			else
1248 #endif
1249 				ath_beacon_config(sc, NULL);
1250 		}
1251 		ath_hal_intrset(ah, sc->sc_imask);
1252 	}
1253 	ATH_UNLOCK(sc);
1254 }
1255 
1256 void
1257 ath_suspend(struct ath_softc *sc)
1258 {
1259 	struct ifnet *ifp = sc->sc_ifp;
1260 	struct ieee80211com *ic = ifp->if_l2com;
1261 
1262 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1263 		__func__, ifp->if_flags);
1264 
1265 	sc->sc_resume_up = (ifp->if_flags & IFF_UP) != 0;
1266 	if (ic->ic_opmode == IEEE80211_M_STA)
1267 		ath_stop(ifp);
1268 	else
1269 		ieee80211_suspend_all(ic);
1270 	/*
1271 	 * NB: don't worry about putting the chip in low power
1272 	 * mode; pci will power off our socket on suspend and
1273 	 * CardBus detaches the device.
1274 	 */
1275 }
1276 
1277 /*
1278  * Reset the key cache since some parts do not reset the
1279  * contents on resume.  First we clear all entries, then
1280  * re-load keys that the 802.11 layer assumes are setup
1281  * in h/w.
1282  */
1283 static void
1284 ath_reset_keycache(struct ath_softc *sc)
1285 {
1286 	struct ifnet *ifp = sc->sc_ifp;
1287 	struct ieee80211com *ic = ifp->if_l2com;
1288 	struct ath_hal *ah = sc->sc_ah;
1289 	int i;
1290 
1291 	for (i = 0; i < sc->sc_keymax; i++)
1292 		ath_hal_keyreset(ah, i);
1293 	ieee80211_crypto_reload_keys(ic);
1294 }
1295 
1296 void
1297 ath_resume(struct ath_softc *sc)
1298 {
1299 	struct ifnet *ifp = sc->sc_ifp;
1300 	struct ieee80211com *ic = ifp->if_l2com;
1301 	struct ath_hal *ah = sc->sc_ah;
1302 	HAL_STATUS status;
1303 
1304 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1305 		__func__, ifp->if_flags);
1306 
1307 	/*
1308 	 * Must reset the chip before we reload the
1309 	 * keycache as we were powered down on suspend.
1310 	 */
1311 	ath_hal_reset(ah, sc->sc_opmode,
1312 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan,
1313 	    AH_FALSE, &status);
1314 	ath_reset_keycache(sc);
1315 
1316 	/* Let DFS at it in case it's a DFS channel */
1317 	ath_dfs_radar_enable(sc, ic->ic_curchan);
1318 
1319 	if (sc->sc_resume_up) {
1320 		if (ic->ic_opmode == IEEE80211_M_STA) {
1321 			ath_init(sc);
1322 			/*
1323 			 * Program the beacon registers using the last rx'd
1324 			 * beacon frame and enable sync on the next beacon
1325 			 * we see.  This should handle the case where we
1326 			 * wakeup and find the same AP and also the case where
1327 			 * we wakeup and need to roam.  For the latter we
1328 			 * should get bmiss events that trigger a roam.
1329 			 */
1330 			ath_beacon_config(sc, NULL);
1331 			sc->sc_syncbeacon = 1;
1332 		} else
1333 			ieee80211_resume_all(ic);
1334 	}
1335 	if (sc->sc_softled) {
1336 		ath_hal_gpioCfgOutput(ah, sc->sc_ledpin,
1337 		    HAL_GPIO_MUX_MAC_NETWORK_LED);
1338 		ath_hal_gpioset(ah, sc->sc_ledpin, !sc->sc_ledon);
1339 	}
1340 
1341 	/* XXX beacons ? */
1342 }
1343 
1344 void
1345 ath_shutdown(struct ath_softc *sc)
1346 {
1347 	struct ifnet *ifp = sc->sc_ifp;
1348 
1349 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1350 		__func__, ifp->if_flags);
1351 
1352 	ath_stop(ifp);
1353 	/* NB: no point powering down chip as we're about to reboot */
1354 }
1355 
1356 /*
1357  * Interrupt handler.  Most of the actual processing is deferred.
1358  */
1359 void
1360 ath_intr(void *arg)
1361 {
1362 	struct ath_softc *sc = arg;
1363 	struct ifnet *ifp = sc->sc_ifp;
1364 	struct ath_hal *ah = sc->sc_ah;
1365 	HAL_INT status = 0;
1366 	uint32_t txqs;
1367 
1368 	/*
1369 	 * If we're inside a reset path, just print a warning and
1370 	 * clear the ISR. The reset routine will finish it for us.
1371 	 */
1372 	ATH_PCU_LOCK(sc);
1373 	if (sc->sc_inreset_cnt) {
1374 		HAL_INT status;
1375 		ath_hal_getisr(ah, &status);	/* clear ISR */
1376 		ath_hal_intrset(ah, 0);		/* disable further intr's */
1377 		DPRINTF(sc, ATH_DEBUG_ANY,
1378 		    "%s: in reset, ignoring: status=0x%x\n",
1379 		    __func__, status);
1380 		ATH_PCU_UNLOCK(sc);
1381 		return;
1382 	}
1383 
1384 	if (sc->sc_invalid) {
1385 		/*
1386 		 * The hardware is not ready/present, don't touch anything.
1387 		 * Note this can happen early on if the IRQ is shared.
1388 		 */
1389 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
1390 		ATH_PCU_UNLOCK(sc);
1391 		return;
1392 	}
1393 	if (!ath_hal_intrpend(ah)) {		/* shared irq, not for us */
1394 		ATH_PCU_UNLOCK(sc);
1395 		return;
1396 	}
1397 
1398 	if ((ifp->if_flags & IFF_UP) == 0 ||
1399 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1400 		HAL_INT status;
1401 
1402 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1403 			__func__, ifp->if_flags);
1404 		ath_hal_getisr(ah, &status);	/* clear ISR */
1405 		ath_hal_intrset(ah, 0);		/* disable further intr's */
1406 		ATH_PCU_UNLOCK(sc);
1407 		return;
1408 	}
1409 
1410 	/*
1411 	 * Figure out the reason(s) for the interrupt.  Note
1412 	 * that the hal returns a pseudo-ISR that may include
1413 	 * bits we haven't explicitly enabled so we mask the
1414 	 * value to insure we only process bits we requested.
1415 	 */
1416 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
1417 	DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
1418 	CTR1(ATH_KTR_INTR, "ath_intr: mask=0x%.8x", status);
1419 #ifdef	ATH_KTR_INTR_DEBUG
1420 	CTR5(ATH_KTR_INTR,
1421 	    "ath_intr: ISR=0x%.8x, ISR_S0=0x%.8x, ISR_S1=0x%.8x, ISR_S2=0x%.8x, ISR_S5=0x%.8x",
1422 	    ah->ah_intrstate[0],
1423 	    ah->ah_intrstate[1],
1424 	    ah->ah_intrstate[2],
1425 	    ah->ah_intrstate[3],
1426 	    ah->ah_intrstate[6]);
1427 #endif
1428 	status &= sc->sc_imask;			/* discard unasked for bits */
1429 
1430 	/* Short-circuit un-handled interrupts */
1431 	if (status == 0x0) {
1432 		ATH_PCU_UNLOCK(sc);
1433 		return;
1434 	}
1435 
1436 	/*
1437 	 * Take a note that we're inside the interrupt handler, so
1438 	 * the reset routines know to wait.
1439 	 */
1440 	sc->sc_intr_cnt++;
1441 	ATH_PCU_UNLOCK(sc);
1442 
1443 	/*
1444 	 * Handle the interrupt. We won't run concurrent with the reset
1445 	 * or channel change routines as they'll wait for sc_intr_cnt
1446 	 * to be 0 before continuing.
1447 	 */
1448 	if (status & HAL_INT_FATAL) {
1449 		sc->sc_stats.ast_hardware++;
1450 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
1451 		ath_fatal_proc(sc, 0);
1452 	} else {
1453 		if (status & HAL_INT_SWBA) {
1454 			/*
1455 			 * Software beacon alert--time to send a beacon.
1456 			 * Handle beacon transmission directly; deferring
1457 			 * this is too slow to meet timing constraints
1458 			 * under load.
1459 			 */
1460 #ifdef IEEE80211_SUPPORT_TDMA
1461 			if (sc->sc_tdma) {
1462 				if (sc->sc_tdmaswba == 0) {
1463 					struct ieee80211com *ic = ifp->if_l2com;
1464 					struct ieee80211vap *vap =
1465 					    TAILQ_FIRST(&ic->ic_vaps);
1466 					ath_tdma_beacon_send(sc, vap);
1467 					sc->sc_tdmaswba =
1468 					    vap->iv_tdma->tdma_bintval;
1469 				} else
1470 					sc->sc_tdmaswba--;
1471 			} else
1472 #endif
1473 			{
1474 				ath_beacon_proc(sc, 0);
1475 #ifdef IEEE80211_SUPPORT_SUPERG
1476 				/*
1477 				 * Schedule the rx taskq in case there's no
1478 				 * traffic so any frames held on the staging
1479 				 * queue are aged and potentially flushed.
1480 				 */
1481 				taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1482 #endif
1483 			}
1484 		}
1485 		if (status & HAL_INT_RXEOL) {
1486 			int imask;
1487 			CTR0(ATH_KTR_ERR, "ath_intr: RXEOL");
1488 			ATH_PCU_LOCK(sc);
1489 			/*
1490 			 * NB: the hardware should re-read the link when
1491 			 *     RXE bit is written, but it doesn't work at
1492 			 *     least on older hardware revs.
1493 			 */
1494 			sc->sc_stats.ast_rxeol++;
1495 			/*
1496 			 * Disable RXEOL/RXORN - prevent an interrupt
1497 			 * storm until the PCU logic can be reset.
1498 			 * In case the interface is reset some other
1499 			 * way before "sc_kickpcu" is called, don't
1500 			 * modify sc_imask - that way if it is reset
1501 			 * by a call to ath_reset() somehow, the
1502 			 * interrupt mask will be correctly reprogrammed.
1503 			 */
1504 			imask = sc->sc_imask;
1505 			imask &= ~(HAL_INT_RXEOL | HAL_INT_RXORN);
1506 			ath_hal_intrset(ah, imask);
1507 			/*
1508 			 * Only blank sc_rxlink if we've not yet kicked
1509 			 * the PCU.
1510 			 *
1511 			 * This isn't entirely correct - the correct solution
1512 			 * would be to have a PCU lock and engage that for
1513 			 * the duration of the PCU fiddling; which would include
1514 			 * running the RX process. Otherwise we could end up
1515 			 * messing up the RX descriptor chain and making the
1516 			 * RX desc list much shorter.
1517 			 */
1518 			if (! sc->sc_kickpcu)
1519 				sc->sc_rxlink = NULL;
1520 			sc->sc_kickpcu = 1;
1521 			/*
1522 			 * Enqueue an RX proc, to handled whatever
1523 			 * is in the RX queue.
1524 			 * This will then kick the PCU.
1525 			 */
1526 			taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1527 			ATH_PCU_UNLOCK(sc);
1528 		}
1529 		if (status & HAL_INT_TXURN) {
1530 			sc->sc_stats.ast_txurn++;
1531 			/* bump tx trigger level */
1532 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
1533 		}
1534 		if (status & HAL_INT_RX) {
1535 			sc->sc_stats.ast_rx_intr++;
1536 			taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1537 		}
1538 		if (status & HAL_INT_TX) {
1539 			sc->sc_stats.ast_tx_intr++;
1540 			/*
1541 			 * Grab all the currently set bits in the HAL txq bitmap
1542 			 * and blank them. This is the only place we should be
1543 			 * doing this.
1544 			 */
1545 			ATH_PCU_LOCK(sc);
1546 			txqs = 0xffffffff;
1547 			ath_hal_gettxintrtxqs(sc->sc_ah, &txqs);
1548 			sc->sc_txq_active |= txqs;
1549 			taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask);
1550 			ATH_PCU_UNLOCK(sc);
1551 		}
1552 		if (status & HAL_INT_BMISS) {
1553 			sc->sc_stats.ast_bmiss++;
1554 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask);
1555 		}
1556 		if (status & HAL_INT_GTT)
1557 			sc->sc_stats.ast_tx_timeout++;
1558 		if (status & HAL_INT_CST)
1559 			sc->sc_stats.ast_tx_cst++;
1560 		if (status & HAL_INT_MIB) {
1561 			sc->sc_stats.ast_mib++;
1562 			ATH_PCU_LOCK(sc);
1563 			/*
1564 			 * Disable interrupts until we service the MIB
1565 			 * interrupt; otherwise it will continue to fire.
1566 			 */
1567 			ath_hal_intrset(ah, 0);
1568 			/*
1569 			 * Let the hal handle the event.  We assume it will
1570 			 * clear whatever condition caused the interrupt.
1571 			 */
1572 			ath_hal_mibevent(ah, &sc->sc_halstats);
1573 			/*
1574 			 * Don't reset the interrupt if we've just
1575 			 * kicked the PCU, or we may get a nested
1576 			 * RXEOL before the rxproc has had a chance
1577 			 * to run.
1578 			 */
1579 			if (sc->sc_kickpcu == 0)
1580 				ath_hal_intrset(ah, sc->sc_imask);
1581 			ATH_PCU_UNLOCK(sc);
1582 		}
1583 		if (status & HAL_INT_RXORN) {
1584 			/* NB: hal marks HAL_INT_FATAL when RXORN is fatal */
1585 			CTR0(ATH_KTR_ERR, "ath_intr: RXORN");
1586 			sc->sc_stats.ast_rxorn++;
1587 		}
1588 	}
1589 	ATH_PCU_LOCK(sc);
1590 	sc->sc_intr_cnt--;
1591 	ATH_PCU_UNLOCK(sc);
1592 }
1593 
1594 static void
1595 ath_fatal_proc(void *arg, int pending)
1596 {
1597 	struct ath_softc *sc = arg;
1598 	struct ifnet *ifp = sc->sc_ifp;
1599 	u_int32_t *state;
1600 	u_int32_t len;
1601 	void *sp;
1602 
1603 	if_printf(ifp, "hardware error; resetting\n");
1604 	/*
1605 	 * Fatal errors are unrecoverable.  Typically these
1606 	 * are caused by DMA errors.  Collect h/w state from
1607 	 * the hal so we can diagnose what's going on.
1608 	 */
1609 	if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) {
1610 		KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len));
1611 		state = sp;
1612 		if_printf(ifp, "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n",
1613 		    state[0], state[1] , state[2], state[3],
1614 		    state[4], state[5]);
1615 	}
1616 	ath_reset(ifp, ATH_RESET_NOLOSS);
1617 }
1618 
1619 static void
1620 ath_bmiss_vap(struct ieee80211vap *vap)
1621 {
1622 	/*
1623 	 * Workaround phantom bmiss interrupts by sanity-checking
1624 	 * the time of our last rx'd frame.  If it is within the
1625 	 * beacon miss interval then ignore the interrupt.  If it's
1626 	 * truly a bmiss we'll get another interrupt soon and that'll
1627 	 * be dispatched up for processing.  Note this applies only
1628 	 * for h/w beacon miss events.
1629 	 */
1630 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
1631 		struct ifnet *ifp = vap->iv_ic->ic_ifp;
1632 		struct ath_softc *sc = ifp->if_softc;
1633 		u_int64_t lastrx = sc->sc_lastrx;
1634 		u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah);
1635 		u_int bmisstimeout =
1636 			vap->iv_bmissthreshold * vap->iv_bss->ni_intval * 1024;
1637 
1638 		DPRINTF(sc, ATH_DEBUG_BEACON,
1639 		    "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n",
1640 		    __func__, (unsigned long long) tsf,
1641 		    (unsigned long long)(tsf - lastrx),
1642 		    (unsigned long long) lastrx, bmisstimeout);
1643 
1644 		if (tsf - lastrx <= bmisstimeout) {
1645 			sc->sc_stats.ast_bmiss_phantom++;
1646 			return;
1647 		}
1648 	}
1649 	ATH_VAP(vap)->av_bmiss(vap);
1650 }
1651 
1652 static int
1653 ath_hal_gethangstate(struct ath_hal *ah, uint32_t mask, uint32_t *hangs)
1654 {
1655 	uint32_t rsize;
1656 	void *sp;
1657 
1658 	if (!ath_hal_getdiagstate(ah, HAL_DIAG_CHECK_HANGS, &mask, sizeof(mask), &sp, &rsize))
1659 		return 0;
1660 	KASSERT(rsize == sizeof(uint32_t), ("resultsize %u", rsize));
1661 	*hangs = *(uint32_t *)sp;
1662 	return 1;
1663 }
1664 
1665 static void
1666 ath_bmiss_proc(void *arg, int pending)
1667 {
1668 	struct ath_softc *sc = arg;
1669 	struct ifnet *ifp = sc->sc_ifp;
1670 	uint32_t hangs;
1671 
1672 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
1673 
1674 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) {
1675 		if_printf(ifp, "bb hang detected (0x%x), resetting\n", hangs);
1676 		ath_reset(ifp, ATH_RESET_NOLOSS);
1677 	} else
1678 		ieee80211_beacon_miss(ifp->if_l2com);
1679 }
1680 
1681 /*
1682  * Handle TKIP MIC setup to deal hardware that doesn't do MIC
1683  * calcs together with WME.  If necessary disable the crypto
1684  * hardware and mark the 802.11 state so keys will be setup
1685  * with the MIC work done in software.
1686  */
1687 static void
1688 ath_settkipmic(struct ath_softc *sc)
1689 {
1690 	struct ifnet *ifp = sc->sc_ifp;
1691 	struct ieee80211com *ic = ifp->if_l2com;
1692 
1693 	if ((ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIP) && !sc->sc_wmetkipmic) {
1694 		if (ic->ic_flags & IEEE80211_F_WME) {
1695 			ath_hal_settkipmic(sc->sc_ah, AH_FALSE);
1696 			ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC;
1697 		} else {
1698 			ath_hal_settkipmic(sc->sc_ah, AH_TRUE);
1699 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
1700 		}
1701 	}
1702 }
1703 
1704 static void
1705 ath_init(void *arg)
1706 {
1707 	struct ath_softc *sc = (struct ath_softc *) arg;
1708 	struct ifnet *ifp = sc->sc_ifp;
1709 	struct ieee80211com *ic = ifp->if_l2com;
1710 	struct ath_hal *ah = sc->sc_ah;
1711 	HAL_STATUS status;
1712 
1713 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1714 		__func__, ifp->if_flags);
1715 
1716 	ATH_LOCK(sc);
1717 	/*
1718 	 * Stop anything previously setup.  This is safe
1719 	 * whether this is the first time through or not.
1720 	 */
1721 	ath_stop_locked(ifp);
1722 
1723 	/*
1724 	 * The basic interface to setting the hardware in a good
1725 	 * state is ``reset''.  On return the hardware is known to
1726 	 * be powered up and with interrupts disabled.  This must
1727 	 * be followed by initialization of the appropriate bits
1728 	 * and then setup of the interrupt mask.
1729 	 */
1730 	ath_settkipmic(sc);
1731 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_FALSE, &status)) {
1732 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
1733 			status);
1734 		ATH_UNLOCK(sc);
1735 		return;
1736 	}
1737 	ath_chan_change(sc, ic->ic_curchan);
1738 
1739 	/* Let DFS at it in case it's a DFS channel */
1740 	ath_dfs_radar_enable(sc, ic->ic_curchan);
1741 
1742 	/*
1743 	 * Likewise this is set during reset so update
1744 	 * state cached in the driver.
1745 	 */
1746 	sc->sc_diversity = ath_hal_getdiversity(ah);
1747 	sc->sc_lastlongcal = 0;
1748 	sc->sc_resetcal = 1;
1749 	sc->sc_lastcalreset = 0;
1750 	sc->sc_lastani = 0;
1751 	sc->sc_lastshortcal = 0;
1752 	sc->sc_doresetcal = AH_FALSE;
1753 	/*
1754 	 * Beacon timers were cleared here; give ath_newstate()
1755 	 * a hint that the beacon timers should be poked when
1756 	 * things transition to the RUN state.
1757 	 */
1758 	sc->sc_beacons = 0;
1759 
1760 	/*
1761 	 * Initial aggregation settings.
1762 	 */
1763 	sc->sc_hwq_limit = ATH_AGGR_MIN_QDEPTH;
1764 	sc->sc_tid_hwq_lo = ATH_AGGR_SCHED_LOW;
1765 	sc->sc_tid_hwq_hi = ATH_AGGR_SCHED_HIGH;
1766 
1767 	/*
1768 	 * Setup the hardware after reset: the key cache
1769 	 * is filled as needed and the receive engine is
1770 	 * set going.  Frame transmit is handled entirely
1771 	 * in the frame output path; there's nothing to do
1772 	 * here except setup the interrupt mask.
1773 	 */
1774 	if (ath_startrecv(sc) != 0) {
1775 		if_printf(ifp, "unable to start recv logic\n");
1776 		ATH_UNLOCK(sc);
1777 		return;
1778 	}
1779 
1780 	/*
1781 	 * Enable interrupts.
1782 	 */
1783 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
1784 		  | HAL_INT_RXEOL | HAL_INT_RXORN
1785 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
1786 	/*
1787 	 * Enable MIB interrupts when there are hardware phy counters.
1788 	 * Note we only do this (at the moment) for station mode.
1789 	 */
1790 	if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
1791 		sc->sc_imask |= HAL_INT_MIB;
1792 
1793 	/* Enable global TX timeout and carrier sense timeout if available */
1794 	if (ath_hal_gtxto_supported(ah))
1795 		sc->sc_imask |= HAL_INT_GTT;
1796 
1797 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: imask=0x%x\n",
1798 		__func__, sc->sc_imask);
1799 
1800 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1801 	callout_reset(&sc->sc_wd_ch, hz, ath_watchdog, sc);
1802 	ath_hal_intrset(ah, sc->sc_imask);
1803 
1804 	ATH_UNLOCK(sc);
1805 
1806 #ifdef ATH_TX99_DIAG
1807 	if (sc->sc_tx99 != NULL)
1808 		sc->sc_tx99->start(sc->sc_tx99);
1809 	else
1810 #endif
1811 	ieee80211_start_all(ic);		/* start all vap's */
1812 }
1813 
1814 static void
1815 ath_stop_locked(struct ifnet *ifp)
1816 {
1817 	struct ath_softc *sc = ifp->if_softc;
1818 	struct ath_hal *ah = sc->sc_ah;
1819 
1820 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
1821 		__func__, sc->sc_invalid, ifp->if_flags);
1822 
1823 	ATH_LOCK_ASSERT(sc);
1824 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1825 		/*
1826 		 * Shutdown the hardware and driver:
1827 		 *    reset 802.11 state machine
1828 		 *    turn off timers
1829 		 *    disable interrupts
1830 		 *    turn off the radio
1831 		 *    clear transmit machinery
1832 		 *    clear receive machinery
1833 		 *    drain and release tx queues
1834 		 *    reclaim beacon resources
1835 		 *    power down hardware
1836 		 *
1837 		 * Note that some of this work is not possible if the
1838 		 * hardware is gone (invalid).
1839 		 */
1840 #ifdef ATH_TX99_DIAG
1841 		if (sc->sc_tx99 != NULL)
1842 			sc->sc_tx99->stop(sc->sc_tx99);
1843 #endif
1844 		callout_stop(&sc->sc_wd_ch);
1845 		sc->sc_wd_timer = 0;
1846 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1847 		if (!sc->sc_invalid) {
1848 			if (sc->sc_softled) {
1849 				callout_stop(&sc->sc_ledtimer);
1850 				ath_hal_gpioset(ah, sc->sc_ledpin,
1851 					!sc->sc_ledon);
1852 				sc->sc_blinking = 0;
1853 			}
1854 			ath_hal_intrset(ah, 0);
1855 		}
1856 		ath_draintxq(sc, ATH_RESET_DEFAULT);
1857 		if (!sc->sc_invalid) {
1858 			ath_stoprecv(sc, 1);
1859 			ath_hal_phydisable(ah);
1860 		} else
1861 			sc->sc_rxlink = NULL;
1862 		ath_beacon_free(sc);	/* XXX not needed */
1863 	}
1864 }
1865 
1866 #define	MAX_TXRX_ITERATIONS	1000
1867 static void
1868 ath_txrx_stop(struct ath_softc *sc)
1869 {
1870 	int i = MAX_TXRX_ITERATIONS;
1871 
1872 	ATH_UNLOCK_ASSERT(sc);
1873 	/* Stop any new TX/RX from occuring */
1874 	taskqueue_block(sc->sc_tq);
1875 
1876 	ATH_PCU_LOCK(sc);
1877 	/*
1878 	 * Sleep until all the pending operations have completed.
1879 	 *
1880 	 * The caller must ensure that reset has been incremented
1881 	 * or the pending operations may continue being queued.
1882 	 */
1883 	while (sc->sc_rxproc_cnt || sc->sc_txproc_cnt ||
1884 	    sc->sc_txstart_cnt || sc->sc_intr_cnt) {
1885 		if (i <= 0)
1886 			break;
1887 		msleep(sc, &sc->sc_pcu_mtx, 0, "ath_txrx_stop", 1);
1888 		i--;
1889 	}
1890 	ATH_PCU_UNLOCK(sc);
1891 
1892 	if (i <= 0)
1893 		device_printf(sc->sc_dev,
1894 		    "%s: didn't finish after %d iterations\n",
1895 		    __func__, MAX_TXRX_ITERATIONS);
1896 }
1897 #undef	MAX_TXRX_ITERATIONS
1898 
1899 static void
1900 ath_txrx_start(struct ath_softc *sc)
1901 {
1902 
1903 	taskqueue_unblock(sc->sc_tq);
1904 }
1905 
1906 /*
1907  * Grab the reset lock, and wait around until noone else
1908  * is trying to do anything with it.
1909  *
1910  * This is totally horrible but we can't hold this lock for
1911  * long enough to do TX/RX or we end up with net80211/ip stack
1912  * LORs and eventual deadlock.
1913  *
1914  * "dowait" signals whether to spin, waiting for the reset
1915  * lock count to reach 0. This should (for now) only be used
1916  * during the reset path, as the rest of the code may not
1917  * be locking-reentrant enough to behave correctly.
1918  *
1919  * Another, cleaner way should be found to serialise all of
1920  * these operations.
1921  */
1922 #define	MAX_RESET_ITERATIONS	10
1923 static int
1924 ath_reset_grablock(struct ath_softc *sc, int dowait)
1925 {
1926 	int w = 0;
1927 	int i = MAX_RESET_ITERATIONS;
1928 
1929 	ATH_PCU_LOCK_ASSERT(sc);
1930 	do {
1931 		if (sc->sc_inreset_cnt == 0) {
1932 			w = 1;
1933 			break;
1934 		}
1935 		if (dowait == 0) {
1936 			w = 0;
1937 			break;
1938 		}
1939 		ATH_PCU_UNLOCK(sc);
1940 		pause("ath_reset_grablock", 1);
1941 		i--;
1942 		ATH_PCU_LOCK(sc);
1943 	} while (i > 0);
1944 
1945 	/*
1946 	 * We always increment the refcounter, regardless
1947 	 * of whether we succeeded to get it in an exclusive
1948 	 * way.
1949 	 */
1950 	sc->sc_inreset_cnt++;
1951 
1952 	if (i <= 0)
1953 		device_printf(sc->sc_dev,
1954 		    "%s: didn't finish after %d iterations\n",
1955 		    __func__, MAX_RESET_ITERATIONS);
1956 
1957 	if (w == 0)
1958 		device_printf(sc->sc_dev,
1959 		    "%s: warning, recursive reset path!\n",
1960 		    __func__);
1961 
1962 	return w;
1963 }
1964 #undef MAX_RESET_ITERATIONS
1965 
1966 /*
1967  * XXX TODO: write ath_reset_releaselock
1968  */
1969 
1970 static void
1971 ath_stop(struct ifnet *ifp)
1972 {
1973 	struct ath_softc *sc = ifp->if_softc;
1974 
1975 	ATH_LOCK(sc);
1976 	ath_stop_locked(ifp);
1977 	ATH_UNLOCK(sc);
1978 }
1979 
1980 /*
1981  * Reset the hardware w/o losing operational state.  This is
1982  * basically a more efficient way of doing ath_stop, ath_init,
1983  * followed by state transitions to the current 802.11
1984  * operational state.  Used to recover from various errors and
1985  * to reset or reload hardware state.
1986  */
1987 int
1988 ath_reset(struct ifnet *ifp, ATH_RESET_TYPE reset_type)
1989 {
1990 	struct ath_softc *sc = ifp->if_softc;
1991 	struct ieee80211com *ic = ifp->if_l2com;
1992 	struct ath_hal *ah = sc->sc_ah;
1993 	HAL_STATUS status;
1994 	int i;
1995 
1996 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
1997 
1998 	/* Ensure ATH_LOCK isn't held; ath_rx_proc can't be locked */
1999 	ATH_PCU_UNLOCK_ASSERT(sc);
2000 	ATH_UNLOCK_ASSERT(sc);
2001 
2002 	ATH_PCU_LOCK(sc);
2003 	if (ath_reset_grablock(sc, 1) == 0) {
2004 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
2005 		    __func__);
2006 	}
2007 	ath_hal_intrset(ah, 0);		/* disable interrupts */
2008 	ATH_PCU_UNLOCK(sc);
2009 
2010 	/*
2011 	 * Should now wait for pending TX/RX to complete
2012 	 * and block future ones from occuring. This needs to be
2013 	 * done before the TX queue is drained.
2014 	 */
2015 	ath_txrx_stop(sc);
2016 	ath_draintxq(sc, reset_type);	/* stop xmit side */
2017 
2018 	/*
2019 	 * Regardless of whether we're doing a no-loss flush or
2020 	 * not, stop the PCU and handle what's in the RX queue.
2021 	 * That way frames aren't dropped which shouldn't be.
2022 	 */
2023 	ath_stoprecv(sc, (reset_type != ATH_RESET_NOLOSS));
2024 	ath_rx_proc(sc, 0);
2025 
2026 	ath_settkipmic(sc);		/* configure TKIP MIC handling */
2027 	/* NB: indicate channel change so we do a full reset */
2028 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_TRUE, &status))
2029 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
2030 			__func__, status);
2031 	sc->sc_diversity = ath_hal_getdiversity(ah);
2032 
2033 	/* Let DFS at it in case it's a DFS channel */
2034 	ath_dfs_radar_enable(sc, ic->ic_curchan);
2035 
2036 	if (ath_startrecv(sc) != 0)	/* restart recv */
2037 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
2038 	/*
2039 	 * We may be doing a reset in response to an ioctl
2040 	 * that changes the channel so update any state that
2041 	 * might change as a result.
2042 	 */
2043 	ath_chan_change(sc, ic->ic_curchan);
2044 	if (sc->sc_beacons) {		/* restart beacons */
2045 #ifdef IEEE80211_SUPPORT_TDMA
2046 		if (sc->sc_tdma)
2047 			ath_tdma_config(sc, NULL);
2048 		else
2049 #endif
2050 			ath_beacon_config(sc, NULL);
2051 	}
2052 
2053 	/*
2054 	 * Release the reset lock and re-enable interrupts here.
2055 	 * If an interrupt was being processed in ath_intr(),
2056 	 * it would disable interrupts at this point. So we have
2057 	 * to atomically enable interrupts and decrement the
2058 	 * reset counter - this way ath_intr() doesn't end up
2059 	 * disabling interrupts without a corresponding enable
2060 	 * in the rest or channel change path.
2061 	 */
2062 	ATH_PCU_LOCK(sc);
2063 	sc->sc_inreset_cnt--;
2064 	/* XXX only do this if sc_inreset_cnt == 0? */
2065 	ath_hal_intrset(ah, sc->sc_imask);
2066 	ATH_PCU_UNLOCK(sc);
2067 
2068 	/*
2069 	 * TX and RX can be started here. If it were started with
2070 	 * sc_inreset_cnt > 0, the TX and RX path would abort.
2071 	 * Thus if this is a nested call through the reset or
2072 	 * channel change code, TX completion will occur but
2073 	 * RX completion and ath_start / ath_tx_start will not
2074 	 * run.
2075 	 */
2076 
2077 	/* Restart TX/RX as needed */
2078 	ath_txrx_start(sc);
2079 
2080 	/* XXX Restart TX completion and pending TX */
2081 	if (reset_type == ATH_RESET_NOLOSS) {
2082 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
2083 			if (ATH_TXQ_SETUP(sc, i)) {
2084 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
2085 				ath_txq_restart_dma(sc, &sc->sc_txq[i]);
2086 				ath_txq_sched(sc, &sc->sc_txq[i]);
2087 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
2088 			}
2089 		}
2090 	}
2091 
2092 	/*
2093 	 * This may have been set during an ath_start() call which
2094 	 * set this once it detected a concurrent TX was going on.
2095 	 * So, clear it.
2096 	 */
2097 	/* XXX do this inside of IF_LOCK? */
2098 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2099 
2100 	/* Handle any frames in the TX queue */
2101 	/*
2102 	 * XXX should this be done by the caller, rather than
2103 	 * ath_reset() ?
2104 	 */
2105 	ath_start(ifp);			/* restart xmit */
2106 	return 0;
2107 }
2108 
2109 static int
2110 ath_reset_vap(struct ieee80211vap *vap, u_long cmd)
2111 {
2112 	struct ieee80211com *ic = vap->iv_ic;
2113 	struct ifnet *ifp = ic->ic_ifp;
2114 	struct ath_softc *sc = ifp->if_softc;
2115 	struct ath_hal *ah = sc->sc_ah;
2116 
2117 	switch (cmd) {
2118 	case IEEE80211_IOC_TXPOWER:
2119 		/*
2120 		 * If per-packet TPC is enabled, then we have nothing
2121 		 * to do; otherwise we need to force the global limit.
2122 		 * All this can happen directly; no need to reset.
2123 		 */
2124 		if (!ath_hal_gettpc(ah))
2125 			ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
2126 		return 0;
2127 	}
2128 	/* XXX? Full or NOLOSS? */
2129 	return ath_reset(ifp, ATH_RESET_FULL);
2130 }
2131 
2132 struct ath_buf *
2133 _ath_getbuf_locked(struct ath_softc *sc)
2134 {
2135 	struct ath_buf *bf;
2136 
2137 	ATH_TXBUF_LOCK_ASSERT(sc);
2138 
2139 	bf = TAILQ_FIRST(&sc->sc_txbuf);
2140 	if (bf == NULL) {
2141 		sc->sc_stats.ast_tx_getnobuf++;
2142 	} else {
2143 		if (bf->bf_flags & ATH_BUF_BUSY) {
2144 			sc->sc_stats.ast_tx_getbusybuf++;
2145 			bf = NULL;
2146 		}
2147 	}
2148 
2149 	if (bf != NULL && (bf->bf_flags & ATH_BUF_BUSY) == 0)
2150 		TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
2151 	else
2152 		bf = NULL;
2153 
2154 	if (bf == NULL) {
2155 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: %s\n", __func__,
2156 		    TAILQ_FIRST(&sc->sc_txbuf) == NULL ?
2157 			"out of xmit buffers" : "xmit buffer busy");
2158 		return NULL;
2159 	}
2160 
2161 	/* Valid bf here; clear some basic fields */
2162 	bf->bf_next = NULL;	/* XXX just to be sure */
2163 	bf->bf_last = NULL;	/* XXX again, just to be sure */
2164 	bf->bf_comp = NULL;	/* XXX again, just to be sure */
2165 	bzero(&bf->bf_state, sizeof(bf->bf_state));
2166 
2167 	return bf;
2168 }
2169 
2170 /*
2171  * When retrying a software frame, buffers marked ATH_BUF_BUSY
2172  * can't be thrown back on the queue as they could still be
2173  * in use by the hardware.
2174  *
2175  * This duplicates the buffer, or returns NULL.
2176  *
2177  * The descriptor is also copied but the link pointers and
2178  * the DMA segments aren't copied; this frame should thus
2179  * be again passed through the descriptor setup/chain routines
2180  * so the link is correct.
2181  *
2182  * The caller must free the buffer using ath_freebuf().
2183  *
2184  * XXX TODO: this call shouldn't fail as it'll cause packet loss
2185  * XXX in the TX pathway when retries are needed.
2186  * XXX Figure out how to keep some buffers free, or factor the
2187  * XXX number of busy buffers into the xmit path (ath_start())
2188  * XXX so we don't over-commit.
2189  */
2190 struct ath_buf *
2191 ath_buf_clone(struct ath_softc *sc, const struct ath_buf *bf)
2192 {
2193 	struct ath_buf *tbf;
2194 
2195 	tbf = ath_getbuf(sc);
2196 	if (tbf == NULL)
2197 		return NULL;	/* XXX failure? Why? */
2198 
2199 	/* Copy basics */
2200 	tbf->bf_next = NULL;
2201 	tbf->bf_nseg = bf->bf_nseg;
2202 	tbf->bf_txflags = bf->bf_txflags;
2203 	tbf->bf_flags = bf->bf_flags & ~ATH_BUF_BUSY;
2204 	tbf->bf_status = bf->bf_status;
2205 	tbf->bf_m = bf->bf_m;
2206 	tbf->bf_node = bf->bf_node;
2207 	/* will be setup by the chain/setup function */
2208 	tbf->bf_lastds = NULL;
2209 	/* for now, last == self */
2210 	tbf->bf_last = tbf;
2211 	tbf->bf_comp = bf->bf_comp;
2212 
2213 	/* NOTE: DMA segments will be setup by the setup/chain functions */
2214 
2215 	/* The caller has to re-init the descriptor + links */
2216 
2217 	/* Copy state */
2218 	memcpy(&tbf->bf_state, &bf->bf_state, sizeof(bf->bf_state));
2219 
2220 	return tbf;
2221 }
2222 
2223 struct ath_buf *
2224 ath_getbuf(struct ath_softc *sc)
2225 {
2226 	struct ath_buf *bf;
2227 
2228 	ATH_TXBUF_LOCK(sc);
2229 	bf = _ath_getbuf_locked(sc);
2230 	if (bf == NULL) {
2231 		struct ifnet *ifp = sc->sc_ifp;
2232 
2233 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: stop queue\n", __func__);
2234 		sc->sc_stats.ast_tx_qstop++;
2235 		/* XXX do this inside of IF_LOCK? */
2236 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2237 	}
2238 	ATH_TXBUF_UNLOCK(sc);
2239 	return bf;
2240 }
2241 
2242 static void
2243 ath_start(struct ifnet *ifp)
2244 {
2245 	struct ath_softc *sc = ifp->if_softc;
2246 	struct ieee80211_node *ni;
2247 	struct ath_buf *bf;
2248 	struct mbuf *m, *next;
2249 	ath_bufhead frags;
2250 
2251 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid)
2252 		return;
2253 
2254 	/* XXX is it ok to hold the ATH_LOCK here? */
2255 	ATH_PCU_LOCK(sc);
2256 	if (sc->sc_inreset_cnt > 0) {
2257 		device_printf(sc->sc_dev,
2258 		    "%s: sc_inreset_cnt > 0; bailing\n", __func__);
2259 		/* XXX do this inside of IF_LOCK? */
2260 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2261 		ATH_PCU_UNLOCK(sc);
2262 		return;
2263 	}
2264 	sc->sc_txstart_cnt++;
2265 	ATH_PCU_UNLOCK(sc);
2266 
2267 	for (;;) {
2268 		/*
2269 		 * Grab a TX buffer and associated resources.
2270 		 */
2271 		bf = ath_getbuf(sc);
2272 		if (bf == NULL)
2273 			break;
2274 
2275 		IFQ_DEQUEUE(&ifp->if_snd, m);
2276 		if (m == NULL) {
2277 			ATH_TXBUF_LOCK(sc);
2278 			TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
2279 			ATH_TXBUF_UNLOCK(sc);
2280 			break;
2281 		}
2282 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
2283 		/*
2284 		 * Check for fragmentation.  If this frame
2285 		 * has been broken up verify we have enough
2286 		 * buffers to send all the fragments so all
2287 		 * go out or none...
2288 		 */
2289 		TAILQ_INIT(&frags);
2290 		if ((m->m_flags & M_FRAG) &&
2291 		    !ath_txfrag_setup(sc, &frags, m, ni)) {
2292 			DPRINTF(sc, ATH_DEBUG_XMIT,
2293 			    "%s: out of txfrag buffers\n", __func__);
2294 			sc->sc_stats.ast_tx_nofrag++;
2295 			ifp->if_oerrors++;
2296 			ath_freetx(m);
2297 			goto bad;
2298 		}
2299 		ifp->if_opackets++;
2300 	nextfrag:
2301 		/*
2302 		 * Pass the frame to the h/w for transmission.
2303 		 * Fragmented frames have each frag chained together
2304 		 * with m_nextpkt.  We know there are sufficient ath_buf's
2305 		 * to send all the frags because of work done by
2306 		 * ath_txfrag_setup.  We leave m_nextpkt set while
2307 		 * calling ath_tx_start so it can use it to extend the
2308 		 * the tx duration to cover the subsequent frag and
2309 		 * so it can reclaim all the mbufs in case of an error;
2310 		 * ath_tx_start clears m_nextpkt once it commits to
2311 		 * handing the frame to the hardware.
2312 		 */
2313 		next = m->m_nextpkt;
2314 		if (ath_tx_start(sc, ni, bf, m)) {
2315 	bad:
2316 			ifp->if_oerrors++;
2317 	reclaim:
2318 			bf->bf_m = NULL;
2319 			bf->bf_node = NULL;
2320 			ATH_TXBUF_LOCK(sc);
2321 			TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
2322 			ath_txfrag_cleanup(sc, &frags, ni);
2323 			ATH_TXBUF_UNLOCK(sc);
2324 			if (ni != NULL)
2325 				ieee80211_free_node(ni);
2326 			continue;
2327 		}
2328 		if (next != NULL) {
2329 			/*
2330 			 * Beware of state changing between frags.
2331 			 * XXX check sta power-save state?
2332 			 */
2333 			if (ni->ni_vap->iv_state != IEEE80211_S_RUN) {
2334 				DPRINTF(sc, ATH_DEBUG_XMIT,
2335 				    "%s: flush fragmented packet, state %s\n",
2336 				    __func__,
2337 				    ieee80211_state_name[ni->ni_vap->iv_state]);
2338 				ath_freetx(next);
2339 				goto reclaim;
2340 			}
2341 			m = next;
2342 			bf = TAILQ_FIRST(&frags);
2343 			KASSERT(bf != NULL, ("no buf for txfrag"));
2344 			TAILQ_REMOVE(&frags, bf, bf_list);
2345 			goto nextfrag;
2346 		}
2347 
2348 		sc->sc_wd_timer = 5;
2349 	}
2350 
2351 	ATH_PCU_LOCK(sc);
2352 	sc->sc_txstart_cnt--;
2353 	ATH_PCU_UNLOCK(sc);
2354 }
2355 
2356 static int
2357 ath_media_change(struct ifnet *ifp)
2358 {
2359 	int error = ieee80211_media_change(ifp);
2360 	/* NB: only the fixed rate can change and that doesn't need a reset */
2361 	return (error == ENETRESET ? 0 : error);
2362 }
2363 
2364 /*
2365  * Block/unblock tx+rx processing while a key change is done.
2366  * We assume the caller serializes key management operations
2367  * so we only need to worry about synchronization with other
2368  * uses that originate in the driver.
2369  */
2370 static void
2371 ath_key_update_begin(struct ieee80211vap *vap)
2372 {
2373 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2374 	struct ath_softc *sc = ifp->if_softc;
2375 
2376 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
2377 	taskqueue_block(sc->sc_tq);
2378 	IF_LOCK(&ifp->if_snd);		/* NB: doesn't block mgmt frames */
2379 }
2380 
2381 static void
2382 ath_key_update_end(struct ieee80211vap *vap)
2383 {
2384 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2385 	struct ath_softc *sc = ifp->if_softc;
2386 
2387 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
2388 	IF_UNLOCK(&ifp->if_snd);
2389 	taskqueue_unblock(sc->sc_tq);
2390 }
2391 
2392 /*
2393  * Calculate the receive filter according to the
2394  * operating mode and state:
2395  *
2396  * o always accept unicast, broadcast, and multicast traffic
2397  * o accept PHY error frames when hardware doesn't have MIB support
2398  *   to count and we need them for ANI (sta mode only until recently)
2399  *   and we are not scanning (ANI is disabled)
2400  *   NB: older hal's add rx filter bits out of sight and we need to
2401  *	 blindly preserve them
2402  * o probe request frames are accepted only when operating in
2403  *   hostap, adhoc, mesh, or monitor modes
2404  * o enable promiscuous mode
2405  *   - when in monitor mode
2406  *   - if interface marked PROMISC (assumes bridge setting is filtered)
2407  * o accept beacons:
2408  *   - when operating in station mode for collecting rssi data when
2409  *     the station is otherwise quiet, or
2410  *   - when operating in adhoc mode so the 802.11 layer creates
2411  *     node table entries for peers,
2412  *   - when scanning
2413  *   - when doing s/w beacon miss (e.g. for ap+sta)
2414  *   - when operating in ap mode in 11g to detect overlapping bss that
2415  *     require protection
2416  *   - when operating in mesh mode to detect neighbors
2417  * o accept control frames:
2418  *   - when in monitor mode
2419  * XXX HT protection for 11n
2420  */
2421 static u_int32_t
2422 ath_calcrxfilter(struct ath_softc *sc)
2423 {
2424 	struct ifnet *ifp = sc->sc_ifp;
2425 	struct ieee80211com *ic = ifp->if_l2com;
2426 	u_int32_t rfilt;
2427 
2428 	rfilt = HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
2429 	if (!sc->sc_needmib && !sc->sc_scanning)
2430 		rfilt |= HAL_RX_FILTER_PHYERR;
2431 	if (ic->ic_opmode != IEEE80211_M_STA)
2432 		rfilt |= HAL_RX_FILTER_PROBEREQ;
2433 	/* XXX ic->ic_monvaps != 0? */
2434 	if (ic->ic_opmode == IEEE80211_M_MONITOR || (ifp->if_flags & IFF_PROMISC))
2435 		rfilt |= HAL_RX_FILTER_PROM;
2436 	if (ic->ic_opmode == IEEE80211_M_STA ||
2437 	    ic->ic_opmode == IEEE80211_M_IBSS ||
2438 	    sc->sc_swbmiss || sc->sc_scanning)
2439 		rfilt |= HAL_RX_FILTER_BEACON;
2440 	/*
2441 	 * NB: We don't recalculate the rx filter when
2442 	 * ic_protmode changes; otherwise we could do
2443 	 * this only when ic_protmode != NONE.
2444 	 */
2445 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
2446 	    IEEE80211_IS_CHAN_ANYG(ic->ic_curchan))
2447 		rfilt |= HAL_RX_FILTER_BEACON;
2448 
2449 	/*
2450 	 * Enable hardware PS-POLL RX only for hostap mode;
2451 	 * STA mode sends PS-POLL frames but never
2452 	 * receives them.
2453 	 */
2454 	if (ath_hal_getcapability(sc->sc_ah, HAL_CAP_PSPOLL,
2455 	    0, NULL) == HAL_OK &&
2456 	    ic->ic_opmode == IEEE80211_M_HOSTAP)
2457 		rfilt |= HAL_RX_FILTER_PSPOLL;
2458 
2459 	if (sc->sc_nmeshvaps) {
2460 		rfilt |= HAL_RX_FILTER_BEACON;
2461 		if (sc->sc_hasbmatch)
2462 			rfilt |= HAL_RX_FILTER_BSSID;
2463 		else
2464 			rfilt |= HAL_RX_FILTER_PROM;
2465 	}
2466 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
2467 		rfilt |= HAL_RX_FILTER_CONTROL;
2468 
2469 	/*
2470 	 * Enable RX of compressed BAR frames only when doing
2471 	 * 802.11n. Required for A-MPDU.
2472 	 */
2473 	if (IEEE80211_IS_CHAN_HT(ic->ic_curchan))
2474 		rfilt |= HAL_RX_FILTER_COMPBAR;
2475 
2476 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x, %s if_flags 0x%x\n",
2477 	    __func__, rfilt, ieee80211_opmode_name[ic->ic_opmode], ifp->if_flags);
2478 	return rfilt;
2479 }
2480 
2481 static void
2482 ath_update_promisc(struct ifnet *ifp)
2483 {
2484 	struct ath_softc *sc = ifp->if_softc;
2485 	u_int32_t rfilt;
2486 
2487 	/* configure rx filter */
2488 	rfilt = ath_calcrxfilter(sc);
2489 	ath_hal_setrxfilter(sc->sc_ah, rfilt);
2490 
2491 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x\n", __func__, rfilt);
2492 }
2493 
2494 static void
2495 ath_update_mcast(struct ifnet *ifp)
2496 {
2497 	struct ath_softc *sc = ifp->if_softc;
2498 	u_int32_t mfilt[2];
2499 
2500 	/* calculate and install multicast filter */
2501 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
2502 		struct ifmultiaddr *ifma;
2503 		/*
2504 		 * Merge multicast addresses to form the hardware filter.
2505 		 */
2506 		mfilt[0] = mfilt[1] = 0;
2507 		if_maddr_rlock(ifp);	/* XXX need some fiddling to remove? */
2508 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2509 			caddr_t dl;
2510 			u_int32_t val;
2511 			u_int8_t pos;
2512 
2513 			/* calculate XOR of eight 6bit values */
2514 			dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
2515 			val = LE_READ_4(dl + 0);
2516 			pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2517 			val = LE_READ_4(dl + 3);
2518 			pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2519 			pos &= 0x3f;
2520 			mfilt[pos / 32] |= (1 << (pos % 32));
2521 		}
2522 		if_maddr_runlock(ifp);
2523 	} else
2524 		mfilt[0] = mfilt[1] = ~0;
2525 	ath_hal_setmcastfilter(sc->sc_ah, mfilt[0], mfilt[1]);
2526 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: MC filter %08x:%08x\n",
2527 		__func__, mfilt[0], mfilt[1]);
2528 }
2529 
2530 static void
2531 ath_mode_init(struct ath_softc *sc)
2532 {
2533 	struct ifnet *ifp = sc->sc_ifp;
2534 	struct ath_hal *ah = sc->sc_ah;
2535 	u_int32_t rfilt;
2536 
2537 	/* configure rx filter */
2538 	rfilt = ath_calcrxfilter(sc);
2539 	ath_hal_setrxfilter(ah, rfilt);
2540 
2541 	/* configure operational mode */
2542 	ath_hal_setopmode(ah);
2543 
2544 	/* handle any link-level address change */
2545 	ath_hal_setmac(ah, IF_LLADDR(ifp));
2546 
2547 	/* calculate and install multicast filter */
2548 	ath_update_mcast(ifp);
2549 }
2550 
2551 /*
2552  * Set the slot time based on the current setting.
2553  */
2554 static void
2555 ath_setslottime(struct ath_softc *sc)
2556 {
2557 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2558 	struct ath_hal *ah = sc->sc_ah;
2559 	u_int usec;
2560 
2561 	if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan))
2562 		usec = 13;
2563 	else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan))
2564 		usec = 21;
2565 	else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) {
2566 		/* honor short/long slot time only in 11g */
2567 		/* XXX shouldn't honor on pure g or turbo g channel */
2568 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
2569 			usec = HAL_SLOT_TIME_9;
2570 		else
2571 			usec = HAL_SLOT_TIME_20;
2572 	} else
2573 		usec = HAL_SLOT_TIME_9;
2574 
2575 	DPRINTF(sc, ATH_DEBUG_RESET,
2576 	    "%s: chan %u MHz flags 0x%x %s slot, %u usec\n",
2577 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
2578 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec);
2579 
2580 	ath_hal_setslottime(ah, usec);
2581 	sc->sc_updateslot = OK;
2582 }
2583 
2584 /*
2585  * Callback from the 802.11 layer to update the
2586  * slot time based on the current setting.
2587  */
2588 static void
2589 ath_updateslot(struct ifnet *ifp)
2590 {
2591 	struct ath_softc *sc = ifp->if_softc;
2592 	struct ieee80211com *ic = ifp->if_l2com;
2593 
2594 	/*
2595 	 * When not coordinating the BSS, change the hardware
2596 	 * immediately.  For other operation we defer the change
2597 	 * until beacon updates have propagated to the stations.
2598 	 */
2599 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
2600 	    ic->ic_opmode == IEEE80211_M_MBSS)
2601 		sc->sc_updateslot = UPDATE;
2602 	else
2603 		ath_setslottime(sc);
2604 }
2605 
2606 /*
2607  * Setup a h/w transmit queue for beacons.
2608  */
2609 static int
2610 ath_beaconq_setup(struct ath_hal *ah)
2611 {
2612 	HAL_TXQ_INFO qi;
2613 
2614 	memset(&qi, 0, sizeof(qi));
2615 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
2616 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
2617 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
2618 	/* NB: for dynamic turbo, don't enable any other interrupts */
2619 	qi.tqi_qflags = HAL_TXQ_TXDESCINT_ENABLE;
2620 	return ath_hal_setuptxqueue(ah, HAL_TX_QUEUE_BEACON, &qi);
2621 }
2622 
2623 /*
2624  * Setup the transmit queue parameters for the beacon queue.
2625  */
2626 static int
2627 ath_beaconq_config(struct ath_softc *sc)
2628 {
2629 #define	ATH_EXPONENT_TO_VALUE(v)	((1<<(v))-1)
2630 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2631 	struct ath_hal *ah = sc->sc_ah;
2632 	HAL_TXQ_INFO qi;
2633 
2634 	ath_hal_gettxqueueprops(ah, sc->sc_bhalq, &qi);
2635 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
2636 	    ic->ic_opmode == IEEE80211_M_MBSS) {
2637 		/*
2638 		 * Always burst out beacon and CAB traffic.
2639 		 */
2640 		qi.tqi_aifs = ATH_BEACON_AIFS_DEFAULT;
2641 		qi.tqi_cwmin = ATH_BEACON_CWMIN_DEFAULT;
2642 		qi.tqi_cwmax = ATH_BEACON_CWMAX_DEFAULT;
2643 	} else {
2644 		struct wmeParams *wmep =
2645 			&ic->ic_wme.wme_chanParams.cap_wmeParams[WME_AC_BE];
2646 		/*
2647 		 * Adhoc mode; important thing is to use 2x cwmin.
2648 		 */
2649 		qi.tqi_aifs = wmep->wmep_aifsn;
2650 		qi.tqi_cwmin = 2*ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
2651 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
2652 	}
2653 
2654 	if (!ath_hal_settxqueueprops(ah, sc->sc_bhalq, &qi)) {
2655 		device_printf(sc->sc_dev, "unable to update parameters for "
2656 			"beacon hardware queue!\n");
2657 		return 0;
2658 	} else {
2659 		ath_hal_resettxqueue(ah, sc->sc_bhalq); /* push to h/w */
2660 		return 1;
2661 	}
2662 #undef ATH_EXPONENT_TO_VALUE
2663 }
2664 
2665 /*
2666  * Allocate and setup an initial beacon frame.
2667  */
2668 static int
2669 ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni)
2670 {
2671 	struct ieee80211vap *vap = ni->ni_vap;
2672 	struct ath_vap *avp = ATH_VAP(vap);
2673 	struct ath_buf *bf;
2674 	struct mbuf *m;
2675 	int error;
2676 
2677 	bf = avp->av_bcbuf;
2678 	if (bf->bf_m != NULL) {
2679 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2680 		m_freem(bf->bf_m);
2681 		bf->bf_m = NULL;
2682 	}
2683 	if (bf->bf_node != NULL) {
2684 		ieee80211_free_node(bf->bf_node);
2685 		bf->bf_node = NULL;
2686 	}
2687 
2688 	/*
2689 	 * NB: the beacon data buffer must be 32-bit aligned;
2690 	 * we assume the mbuf routines will return us something
2691 	 * with this alignment (perhaps should assert).
2692 	 */
2693 	m = ieee80211_beacon_alloc(ni, &avp->av_boff);
2694 	if (m == NULL) {
2695 		device_printf(sc->sc_dev, "%s: cannot get mbuf\n", __func__);
2696 		sc->sc_stats.ast_be_nombuf++;
2697 		return ENOMEM;
2698 	}
2699 	error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
2700 				     bf->bf_segs, &bf->bf_nseg,
2701 				     BUS_DMA_NOWAIT);
2702 	if (error != 0) {
2703 		device_printf(sc->sc_dev,
2704 		    "%s: cannot map mbuf, bus_dmamap_load_mbuf_sg returns %d\n",
2705 		    __func__, error);
2706 		m_freem(m);
2707 		return error;
2708 	}
2709 
2710 	/*
2711 	 * Calculate a TSF adjustment factor required for staggered
2712 	 * beacons.  Note that we assume the format of the beacon
2713 	 * frame leaves the tstamp field immediately following the
2714 	 * header.
2715 	 */
2716 	if (sc->sc_stagbeacons && avp->av_bslot > 0) {
2717 		uint64_t tsfadjust;
2718 		struct ieee80211_frame *wh;
2719 
2720 		/*
2721 		 * The beacon interval is in TU's; the TSF is in usecs.
2722 		 * We figure out how many TU's to add to align the timestamp
2723 		 * then convert to TSF units and handle byte swapping before
2724 		 * inserting it in the frame.  The hardware will then add this
2725 		 * each time a beacon frame is sent.  Note that we align vap's
2726 		 * 1..N and leave vap 0 untouched.  This means vap 0 has a
2727 		 * timestamp in one beacon interval while the others get a
2728 		 * timstamp aligned to the next interval.
2729 		 */
2730 		tsfadjust = ni->ni_intval *
2731 		    (ATH_BCBUF - avp->av_bslot) / ATH_BCBUF;
2732 		tsfadjust = htole64(tsfadjust << 10);	/* TU -> TSF */
2733 
2734 		DPRINTF(sc, ATH_DEBUG_BEACON,
2735 		    "%s: %s beacons bslot %d intval %u tsfadjust %llu\n",
2736 		    __func__, sc->sc_stagbeacons ? "stagger" : "burst",
2737 		    avp->av_bslot, ni->ni_intval,
2738 		    (long long unsigned) le64toh(tsfadjust));
2739 
2740 		wh = mtod(m, struct ieee80211_frame *);
2741 		memcpy(&wh[1], &tsfadjust, sizeof(tsfadjust));
2742 	}
2743 	bf->bf_m = m;
2744 	bf->bf_node = ieee80211_ref_node(ni);
2745 
2746 	return 0;
2747 }
2748 
2749 /*
2750  * Setup the beacon frame for transmit.
2751  */
2752 static void
2753 ath_beacon_setup(struct ath_softc *sc, struct ath_buf *bf)
2754 {
2755 #define	USE_SHPREAMBLE(_ic) \
2756 	(((_ic)->ic_flags & (IEEE80211_F_SHPREAMBLE | IEEE80211_F_USEBARKER))\
2757 		== IEEE80211_F_SHPREAMBLE)
2758 	struct ieee80211_node *ni = bf->bf_node;
2759 	struct ieee80211com *ic = ni->ni_ic;
2760 	struct mbuf *m = bf->bf_m;
2761 	struct ath_hal *ah = sc->sc_ah;
2762 	struct ath_desc *ds;
2763 	int flags, antenna;
2764 	const HAL_RATE_TABLE *rt;
2765 	u_int8_t rix, rate;
2766 
2767 	DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: m %p len %u\n",
2768 		__func__, m, m->m_len);
2769 
2770 	/* setup descriptors */
2771 	ds = bf->bf_desc;
2772 	bf->bf_last = bf;
2773 	bf->bf_lastds = ds;
2774 
2775 	flags = HAL_TXDESC_NOACK;
2776 	if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol) {
2777 		ds->ds_link = bf->bf_daddr;	/* self-linked */
2778 		flags |= HAL_TXDESC_VEOL;
2779 		/*
2780 		 * Let hardware handle antenna switching.
2781 		 */
2782 		antenna = sc->sc_txantenna;
2783 	} else {
2784 		ds->ds_link = 0;
2785 		/*
2786 		 * Switch antenna every 4 beacons.
2787 		 * XXX assumes two antenna
2788 		 */
2789 		if (sc->sc_txantenna != 0)
2790 			antenna = sc->sc_txantenna;
2791 		else if (sc->sc_stagbeacons && sc->sc_nbcnvaps != 0)
2792 			antenna = ((sc->sc_stats.ast_be_xmit / sc->sc_nbcnvaps) & 4 ? 2 : 1);
2793 		else
2794 			antenna = (sc->sc_stats.ast_be_xmit & 4 ? 2 : 1);
2795 	}
2796 
2797 	KASSERT(bf->bf_nseg == 1,
2798 		("multi-segment beacon frame; nseg %u", bf->bf_nseg));
2799 	ds->ds_data = bf->bf_segs[0].ds_addr;
2800 	/*
2801 	 * Calculate rate code.
2802 	 * XXX everything at min xmit rate
2803 	 */
2804 	rix = 0;
2805 	rt = sc->sc_currates;
2806 	rate = rt->info[rix].rateCode;
2807 	if (USE_SHPREAMBLE(ic))
2808 		rate |= rt->info[rix].shortPreamble;
2809 	ath_hal_setuptxdesc(ah, ds
2810 		, m->m_len + IEEE80211_CRC_LEN	/* frame length */
2811 		, sizeof(struct ieee80211_frame)/* header length */
2812 		, HAL_PKT_TYPE_BEACON		/* Atheros packet type */
2813 		, ni->ni_txpower		/* txpower XXX */
2814 		, rate, 1			/* series 0 rate/tries */
2815 		, HAL_TXKEYIX_INVALID		/* no encryption */
2816 		, antenna			/* antenna mode */
2817 		, flags				/* no ack, veol for beacons */
2818 		, 0				/* rts/cts rate */
2819 		, 0				/* rts/cts duration */
2820 	);
2821 	/* NB: beacon's BufLen must be a multiple of 4 bytes */
2822 	ath_hal_filltxdesc(ah, ds
2823 		, roundup(m->m_len, 4)		/* buffer length */
2824 		, AH_TRUE			/* first segment */
2825 		, AH_TRUE			/* last segment */
2826 		, ds				/* first descriptor */
2827 	);
2828 #if 0
2829 	ath_desc_swap(ds);
2830 #endif
2831 #undef USE_SHPREAMBLE
2832 }
2833 
2834 static void
2835 ath_beacon_update(struct ieee80211vap *vap, int item)
2836 {
2837 	struct ieee80211_beacon_offsets *bo = &ATH_VAP(vap)->av_boff;
2838 
2839 	setbit(bo->bo_flags, item);
2840 }
2841 
2842 /*
2843  * Append the contents of src to dst; both queues
2844  * are assumed to be locked.
2845  */
2846 static void
2847 ath_txqmove(struct ath_txq *dst, struct ath_txq *src)
2848 {
2849 	TAILQ_CONCAT(&dst->axq_q, &src->axq_q, bf_list);
2850 	dst->axq_link = src->axq_link;
2851 	src->axq_link = NULL;
2852 	dst->axq_depth += src->axq_depth;
2853 	dst->axq_aggr_depth += src->axq_aggr_depth;
2854 	src->axq_depth = 0;
2855 	src->axq_aggr_depth = 0;
2856 }
2857 
2858 /*
2859  * Transmit a beacon frame at SWBA.  Dynamic updates to the
2860  * frame contents are done as needed and the slot time is
2861  * also adjusted based on current state.
2862  */
2863 static void
2864 ath_beacon_proc(void *arg, int pending)
2865 {
2866 	struct ath_softc *sc = arg;
2867 	struct ath_hal *ah = sc->sc_ah;
2868 	struct ieee80211vap *vap;
2869 	struct ath_buf *bf;
2870 	int slot, otherant;
2871 	uint32_t bfaddr;
2872 
2873 	DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: pending %u\n",
2874 		__func__, pending);
2875 	/*
2876 	 * Check if the previous beacon has gone out.  If
2877 	 * not don't try to post another, skip this period
2878 	 * and wait for the next.  Missed beacons indicate
2879 	 * a problem and should not occur.  If we miss too
2880 	 * many consecutive beacons reset the device.
2881 	 */
2882 	if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) {
2883 		sc->sc_bmisscount++;
2884 		sc->sc_stats.ast_be_missed++;
2885 		DPRINTF(sc, ATH_DEBUG_BEACON,
2886 			"%s: missed %u consecutive beacons\n",
2887 			__func__, sc->sc_bmisscount);
2888 		if (sc->sc_bmisscount >= ath_bstuck_threshold)
2889 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask);
2890 		return;
2891 	}
2892 	if (sc->sc_bmisscount != 0) {
2893 		DPRINTF(sc, ATH_DEBUG_BEACON,
2894 			"%s: resume beacon xmit after %u misses\n",
2895 			__func__, sc->sc_bmisscount);
2896 		sc->sc_bmisscount = 0;
2897 	}
2898 
2899 	if (sc->sc_stagbeacons) {			/* staggered beacons */
2900 		struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2901 		uint32_t tsftu;
2902 
2903 		tsftu = ath_hal_gettsf32(ah) >> 10;
2904 		/* XXX lintval */
2905 		slot = ((tsftu % ic->ic_lintval) * ATH_BCBUF) / ic->ic_lintval;
2906 		vap = sc->sc_bslot[(slot+1) % ATH_BCBUF];
2907 		bfaddr = 0;
2908 		if (vap != NULL && vap->iv_state >= IEEE80211_S_RUN) {
2909 			bf = ath_beacon_generate(sc, vap);
2910 			if (bf != NULL)
2911 				bfaddr = bf->bf_daddr;
2912 		}
2913 	} else {					/* burst'd beacons */
2914 		uint32_t *bflink = &bfaddr;
2915 
2916 		for (slot = 0; slot < ATH_BCBUF; slot++) {
2917 			vap = sc->sc_bslot[slot];
2918 			if (vap != NULL && vap->iv_state >= IEEE80211_S_RUN) {
2919 				bf = ath_beacon_generate(sc, vap);
2920 				if (bf != NULL) {
2921 					*bflink = bf->bf_daddr;
2922 					bflink = &bf->bf_desc->ds_link;
2923 				}
2924 			}
2925 		}
2926 		*bflink = 0;				/* terminate list */
2927 	}
2928 
2929 	/*
2930 	 * Handle slot time change when a non-ERP station joins/leaves
2931 	 * an 11g network.  The 802.11 layer notifies us via callback,
2932 	 * we mark updateslot, then wait one beacon before effecting
2933 	 * the change.  This gives associated stations at least one
2934 	 * beacon interval to note the state change.
2935 	 */
2936 	/* XXX locking */
2937 	if (sc->sc_updateslot == UPDATE) {
2938 		sc->sc_updateslot = COMMIT;	/* commit next beacon */
2939 		sc->sc_slotupdate = slot;
2940 	} else if (sc->sc_updateslot == COMMIT && sc->sc_slotupdate == slot)
2941 		ath_setslottime(sc);		/* commit change to h/w */
2942 
2943 	/*
2944 	 * Check recent per-antenna transmit statistics and flip
2945 	 * the default antenna if noticeably more frames went out
2946 	 * on the non-default antenna.
2947 	 * XXX assumes 2 anntenae
2948 	 */
2949 	if (!sc->sc_diversity && (!sc->sc_stagbeacons || slot == 0)) {
2950 		otherant = sc->sc_defant & 1 ? 2 : 1;
2951 		if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2)
2952 			ath_setdefantenna(sc, otherant);
2953 		sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0;
2954 	}
2955 
2956 	if (bfaddr != 0) {
2957 		/*
2958 		 * Stop any current dma and put the new frame on the queue.
2959 		 * This should never fail since we check above that no frames
2960 		 * are still pending on the queue.
2961 		 */
2962 		if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
2963 			DPRINTF(sc, ATH_DEBUG_ANY,
2964 				"%s: beacon queue %u did not stop?\n",
2965 				__func__, sc->sc_bhalq);
2966 		}
2967 		/* NB: cabq traffic should already be queued and primed */
2968 		ath_hal_puttxbuf(ah, sc->sc_bhalq, bfaddr);
2969 		ath_hal_txstart(ah, sc->sc_bhalq);
2970 
2971 		sc->sc_stats.ast_be_xmit++;
2972 	}
2973 }
2974 
2975 static struct ath_buf *
2976 ath_beacon_generate(struct ath_softc *sc, struct ieee80211vap *vap)
2977 {
2978 	struct ath_vap *avp = ATH_VAP(vap);
2979 	struct ath_txq *cabq = sc->sc_cabq;
2980 	struct ath_buf *bf;
2981 	struct mbuf *m;
2982 	int nmcastq, error;
2983 
2984 	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
2985 	    ("not running, state %d", vap->iv_state));
2986 	KASSERT(avp->av_bcbuf != NULL, ("no beacon buffer"));
2987 
2988 	/*
2989 	 * Update dynamic beacon contents.  If this returns
2990 	 * non-zero then we need to remap the memory because
2991 	 * the beacon frame changed size (probably because
2992 	 * of the TIM bitmap).
2993 	 */
2994 	bf = avp->av_bcbuf;
2995 	m = bf->bf_m;
2996 	nmcastq = avp->av_mcastq.axq_depth;
2997 	if (ieee80211_beacon_update(bf->bf_node, &avp->av_boff, m, nmcastq)) {
2998 		/* XXX too conservative? */
2999 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3000 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
3001 					     bf->bf_segs, &bf->bf_nseg,
3002 					     BUS_DMA_NOWAIT);
3003 		if (error != 0) {
3004 			if_printf(vap->iv_ifp,
3005 			    "%s: bus_dmamap_load_mbuf_sg failed, error %u\n",
3006 			    __func__, error);
3007 			return NULL;
3008 		}
3009 	}
3010 	if ((avp->av_boff.bo_tim[4] & 1) && cabq->axq_depth) {
3011 		DPRINTF(sc, ATH_DEBUG_BEACON,
3012 		    "%s: cabq did not drain, mcastq %u cabq %u\n",
3013 		    __func__, nmcastq, cabq->axq_depth);
3014 		sc->sc_stats.ast_cabq_busy++;
3015 		if (sc->sc_nvaps > 1 && sc->sc_stagbeacons) {
3016 			/*
3017 			 * CABQ traffic from a previous vap is still pending.
3018 			 * We must drain the q before this beacon frame goes
3019 			 * out as otherwise this vap's stations will get cab
3020 			 * frames from a different vap.
3021 			 * XXX could be slow causing us to miss DBA
3022 			 */
3023 			ath_tx_draintxq(sc, cabq);
3024 		}
3025 	}
3026 	ath_beacon_setup(sc, bf);
3027 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
3028 
3029 	/*
3030 	 * Enable the CAB queue before the beacon queue to
3031 	 * insure cab frames are triggered by this beacon.
3032 	 */
3033 	if (avp->av_boff.bo_tim[4] & 1) {
3034 		struct ath_hal *ah = sc->sc_ah;
3035 
3036 		/* NB: only at DTIM */
3037 		ATH_TXQ_LOCK(cabq);
3038 		ATH_TXQ_LOCK(&avp->av_mcastq);
3039 		if (nmcastq) {
3040 			struct ath_buf *bfm;
3041 
3042 			/*
3043 			 * Move frames from the s/w mcast q to the h/w cab q.
3044 			 * XXX MORE_DATA bit
3045 			 */
3046 			bfm = TAILQ_FIRST(&avp->av_mcastq.axq_q);
3047 			if (cabq->axq_link != NULL) {
3048 				*cabq->axq_link = bfm->bf_daddr;
3049 			} else
3050 				ath_hal_puttxbuf(ah, cabq->axq_qnum,
3051 					bfm->bf_daddr);
3052 			ath_txqmove(cabq, &avp->av_mcastq);
3053 
3054 			sc->sc_stats.ast_cabq_xmit += nmcastq;
3055 		}
3056 		/* NB: gated by beacon so safe to start here */
3057 		if (! TAILQ_EMPTY(&(cabq->axq_q)))
3058 			ath_hal_txstart(ah, cabq->axq_qnum);
3059 		ATH_TXQ_UNLOCK(&avp->av_mcastq);
3060 		ATH_TXQ_UNLOCK(cabq);
3061 	}
3062 	return bf;
3063 }
3064 
3065 static void
3066 ath_beacon_start_adhoc(struct ath_softc *sc, struct ieee80211vap *vap)
3067 {
3068 	struct ath_vap *avp = ATH_VAP(vap);
3069 	struct ath_hal *ah = sc->sc_ah;
3070 	struct ath_buf *bf;
3071 	struct mbuf *m;
3072 	int error;
3073 
3074 	KASSERT(avp->av_bcbuf != NULL, ("no beacon buffer"));
3075 
3076 	/*
3077 	 * Update dynamic beacon contents.  If this returns
3078 	 * non-zero then we need to remap the memory because
3079 	 * the beacon frame changed size (probably because
3080 	 * of the TIM bitmap).
3081 	 */
3082 	bf = avp->av_bcbuf;
3083 	m = bf->bf_m;
3084 	if (ieee80211_beacon_update(bf->bf_node, &avp->av_boff, m, 0)) {
3085 		/* XXX too conservative? */
3086 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3087 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
3088 					     bf->bf_segs, &bf->bf_nseg,
3089 					     BUS_DMA_NOWAIT);
3090 		if (error != 0) {
3091 			if_printf(vap->iv_ifp,
3092 			    "%s: bus_dmamap_load_mbuf_sg failed, error %u\n",
3093 			    __func__, error);
3094 			return;
3095 		}
3096 	}
3097 	ath_beacon_setup(sc, bf);
3098 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
3099 
3100 	/* NB: caller is known to have already stopped tx dma */
3101 	ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
3102 	ath_hal_txstart(ah, sc->sc_bhalq);
3103 }
3104 
3105 /*
3106  * Reset the hardware after detecting beacons have stopped.
3107  */
3108 static void
3109 ath_bstuck_proc(void *arg, int pending)
3110 {
3111 	struct ath_softc *sc = arg;
3112 	struct ifnet *ifp = sc->sc_ifp;
3113 	uint32_t hangs = 0;
3114 
3115 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0)
3116 		if_printf(ifp, "bb hang detected (0x%x)\n", hangs);
3117 
3118 	if_printf(ifp, "stuck beacon; resetting (bmiss count %u)\n",
3119 		sc->sc_bmisscount);
3120 	sc->sc_stats.ast_bstuck++;
3121 	/*
3122 	 * This assumes that there's no simultaneous channel mode change
3123 	 * occuring.
3124 	 */
3125 	ath_reset(ifp, ATH_RESET_NOLOSS);
3126 }
3127 
3128 /*
3129  * Reclaim beacon resources and return buffer to the pool.
3130  */
3131 static void
3132 ath_beacon_return(struct ath_softc *sc, struct ath_buf *bf)
3133 {
3134 
3135 	if (bf->bf_m != NULL) {
3136 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3137 		m_freem(bf->bf_m);
3138 		bf->bf_m = NULL;
3139 	}
3140 	if (bf->bf_node != NULL) {
3141 		ieee80211_free_node(bf->bf_node);
3142 		bf->bf_node = NULL;
3143 	}
3144 	TAILQ_INSERT_TAIL(&sc->sc_bbuf, bf, bf_list);
3145 }
3146 
3147 /*
3148  * Reclaim beacon resources.
3149  */
3150 static void
3151 ath_beacon_free(struct ath_softc *sc)
3152 {
3153 	struct ath_buf *bf;
3154 
3155 	TAILQ_FOREACH(bf, &sc->sc_bbuf, bf_list) {
3156 		if (bf->bf_m != NULL) {
3157 			bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3158 			m_freem(bf->bf_m);
3159 			bf->bf_m = NULL;
3160 		}
3161 		if (bf->bf_node != NULL) {
3162 			ieee80211_free_node(bf->bf_node);
3163 			bf->bf_node = NULL;
3164 		}
3165 	}
3166 }
3167 
3168 /*
3169  * Configure the beacon and sleep timers.
3170  *
3171  * When operating as an AP this resets the TSF and sets
3172  * up the hardware to notify us when we need to issue beacons.
3173  *
3174  * When operating in station mode this sets up the beacon
3175  * timers according to the timestamp of the last received
3176  * beacon and the current TSF, configures PCF and DTIM
3177  * handling, programs the sleep registers so the hardware
3178  * will wakeup in time to receive beacons, and configures
3179  * the beacon miss handling so we'll receive a BMISS
3180  * interrupt when we stop seeing beacons from the AP
3181  * we've associated with.
3182  */
3183 static void
3184 ath_beacon_config(struct ath_softc *sc, struct ieee80211vap *vap)
3185 {
3186 #define	TSF_TO_TU(_h,_l) \
3187 	((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10))
3188 #define	FUDGE	2
3189 	struct ath_hal *ah = sc->sc_ah;
3190 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3191 	struct ieee80211_node *ni;
3192 	u_int32_t nexttbtt, intval, tsftu;
3193 	u_int64_t tsf;
3194 
3195 	if (vap == NULL)
3196 		vap = TAILQ_FIRST(&ic->ic_vaps);	/* XXX */
3197 	ni = vap->iv_bss;
3198 
3199 	/* extract tstamp from last beacon and convert to TU */
3200 	nexttbtt = TSF_TO_TU(LE_READ_4(ni->ni_tstamp.data + 4),
3201 			     LE_READ_4(ni->ni_tstamp.data));
3202 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
3203 	    ic->ic_opmode == IEEE80211_M_MBSS) {
3204 		/*
3205 		 * For multi-bss ap/mesh support beacons are either staggered
3206 		 * evenly over N slots or burst together.  For the former
3207 		 * arrange for the SWBA to be delivered for each slot.
3208 		 * Slots that are not occupied will generate nothing.
3209 		 */
3210 		/* NB: the beacon interval is kept internally in TU's */
3211 		intval = ni->ni_intval & HAL_BEACON_PERIOD;
3212 		if (sc->sc_stagbeacons)
3213 			intval /= ATH_BCBUF;
3214 	} else {
3215 		/* NB: the beacon interval is kept internally in TU's */
3216 		intval = ni->ni_intval & HAL_BEACON_PERIOD;
3217 	}
3218 	if (nexttbtt == 0)		/* e.g. for ap mode */
3219 		nexttbtt = intval;
3220 	else if (intval)		/* NB: can be 0 for monitor mode */
3221 		nexttbtt = roundup(nexttbtt, intval);
3222 	DPRINTF(sc, ATH_DEBUG_BEACON, "%s: nexttbtt %u intval %u (%u)\n",
3223 		__func__, nexttbtt, intval, ni->ni_intval);
3224 	if (ic->ic_opmode == IEEE80211_M_STA && !sc->sc_swbmiss) {
3225 		HAL_BEACON_STATE bs;
3226 		int dtimperiod, dtimcount;
3227 		int cfpperiod, cfpcount;
3228 
3229 		/*
3230 		 * Setup dtim and cfp parameters according to
3231 		 * last beacon we received (which may be none).
3232 		 */
3233 		dtimperiod = ni->ni_dtim_period;
3234 		if (dtimperiod <= 0)		/* NB: 0 if not known */
3235 			dtimperiod = 1;
3236 		dtimcount = ni->ni_dtim_count;
3237 		if (dtimcount >= dtimperiod)	/* NB: sanity check */
3238 			dtimcount = 0;		/* XXX? */
3239 		cfpperiod = 1;			/* NB: no PCF support yet */
3240 		cfpcount = 0;
3241 		/*
3242 		 * Pull nexttbtt forward to reflect the current
3243 		 * TSF and calculate dtim+cfp state for the result.
3244 		 */
3245 		tsf = ath_hal_gettsf64(ah);
3246 		tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
3247 		do {
3248 			nexttbtt += intval;
3249 			if (--dtimcount < 0) {
3250 				dtimcount = dtimperiod - 1;
3251 				if (--cfpcount < 0)
3252 					cfpcount = cfpperiod - 1;
3253 			}
3254 		} while (nexttbtt < tsftu);
3255 		memset(&bs, 0, sizeof(bs));
3256 		bs.bs_intval = intval;
3257 		bs.bs_nexttbtt = nexttbtt;
3258 		bs.bs_dtimperiod = dtimperiod*intval;
3259 		bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
3260 		bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
3261 		bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
3262 		bs.bs_cfpmaxduration = 0;
3263 #if 0
3264 		/*
3265 		 * The 802.11 layer records the offset to the DTIM
3266 		 * bitmap while receiving beacons; use it here to
3267 		 * enable h/w detection of our AID being marked in
3268 		 * the bitmap vector (to indicate frames for us are
3269 		 * pending at the AP).
3270 		 * XXX do DTIM handling in s/w to WAR old h/w bugs
3271 		 * XXX enable based on h/w rev for newer chips
3272 		 */
3273 		bs.bs_timoffset = ni->ni_timoff;
3274 #endif
3275 		/*
3276 		 * Calculate the number of consecutive beacons to miss
3277 		 * before taking a BMISS interrupt.
3278 		 * Note that we clamp the result to at most 10 beacons.
3279 		 */
3280 		bs.bs_bmissthreshold = vap->iv_bmissthreshold;
3281 		if (bs.bs_bmissthreshold > 10)
3282 			bs.bs_bmissthreshold = 10;
3283 		else if (bs.bs_bmissthreshold <= 0)
3284 			bs.bs_bmissthreshold = 1;
3285 
3286 		/*
3287 		 * Calculate sleep duration.  The configuration is
3288 		 * given in ms.  We insure a multiple of the beacon
3289 		 * period is used.  Also, if the sleep duration is
3290 		 * greater than the DTIM period then it makes senses
3291 		 * to make it a multiple of that.
3292 		 *
3293 		 * XXX fixed at 100ms
3294 		 */
3295 		bs.bs_sleepduration =
3296 			roundup(IEEE80211_MS_TO_TU(100), bs.bs_intval);
3297 		if (bs.bs_sleepduration > bs.bs_dtimperiod)
3298 			bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod);
3299 
3300 		DPRINTF(sc, ATH_DEBUG_BEACON,
3301 			"%s: tsf %ju tsf:tu %u intval %u nexttbtt %u dtim %u nextdtim %u bmiss %u sleep %u cfp:period %u maxdur %u next %u timoffset %u\n"
3302 			, __func__
3303 			, tsf, tsftu
3304 			, bs.bs_intval
3305 			, bs.bs_nexttbtt
3306 			, bs.bs_dtimperiod
3307 			, bs.bs_nextdtim
3308 			, bs.bs_bmissthreshold
3309 			, bs.bs_sleepduration
3310 			, bs.bs_cfpperiod
3311 			, bs.bs_cfpmaxduration
3312 			, bs.bs_cfpnext
3313 			, bs.bs_timoffset
3314 		);
3315 		ath_hal_intrset(ah, 0);
3316 		ath_hal_beacontimers(ah, &bs);
3317 		sc->sc_imask |= HAL_INT_BMISS;
3318 		ath_hal_intrset(ah, sc->sc_imask);
3319 	} else {
3320 		ath_hal_intrset(ah, 0);
3321 		if (nexttbtt == intval)
3322 			intval |= HAL_BEACON_RESET_TSF;
3323 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
3324 			/*
3325 			 * In IBSS mode enable the beacon timers but only
3326 			 * enable SWBA interrupts if we need to manually
3327 			 * prepare beacon frames.  Otherwise we use a
3328 			 * self-linked tx descriptor and let the hardware
3329 			 * deal with things.
3330 			 */
3331 			intval |= HAL_BEACON_ENA;
3332 			if (!sc->sc_hasveol)
3333 				sc->sc_imask |= HAL_INT_SWBA;
3334 			if ((intval & HAL_BEACON_RESET_TSF) == 0) {
3335 				/*
3336 				 * Pull nexttbtt forward to reflect
3337 				 * the current TSF.
3338 				 */
3339 				tsf = ath_hal_gettsf64(ah);
3340 				tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
3341 				do {
3342 					nexttbtt += intval;
3343 				} while (nexttbtt < tsftu);
3344 			}
3345 			ath_beaconq_config(sc);
3346 		} else if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
3347 		    ic->ic_opmode == IEEE80211_M_MBSS) {
3348 			/*
3349 			 * In AP/mesh mode we enable the beacon timers
3350 			 * and SWBA interrupts to prepare beacon frames.
3351 			 */
3352 			intval |= HAL_BEACON_ENA;
3353 			sc->sc_imask |= HAL_INT_SWBA;	/* beacon prepare */
3354 			ath_beaconq_config(sc);
3355 		}
3356 		ath_hal_beaconinit(ah, nexttbtt, intval);
3357 		sc->sc_bmisscount = 0;
3358 		ath_hal_intrset(ah, sc->sc_imask);
3359 		/*
3360 		 * When using a self-linked beacon descriptor in
3361 		 * ibss mode load it once here.
3362 		 */
3363 		if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol)
3364 			ath_beacon_start_adhoc(sc, vap);
3365 	}
3366 	sc->sc_syncbeacon = 0;
3367 #undef FUDGE
3368 #undef TSF_TO_TU
3369 }
3370 
3371 static void
3372 ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
3373 {
3374 	bus_addr_t *paddr = (bus_addr_t*) arg;
3375 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
3376 	*paddr = segs->ds_addr;
3377 }
3378 
3379 static int
3380 ath_descdma_setup(struct ath_softc *sc,
3381 	struct ath_descdma *dd, ath_bufhead *head,
3382 	const char *name, int nbuf, int ndesc)
3383 {
3384 #define	DS2PHYS(_dd, _ds) \
3385 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
3386 #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
3387 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
3388 	struct ifnet *ifp = sc->sc_ifp;
3389 	uint8_t *ds;
3390 	struct ath_buf *bf;
3391 	int i, bsize, error;
3392 	int desc_len;
3393 
3394 	desc_len = sizeof(struct ath_desc);
3395 
3396 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers %u desc/buf\n",
3397 	    __func__, name, nbuf, ndesc);
3398 
3399 	dd->dd_name = name;
3400 	dd->dd_desc_len = desc_len * nbuf * ndesc;
3401 
3402 	/*
3403 	 * Merlin work-around:
3404 	 * Descriptors that cross the 4KB boundary can't be used.
3405 	 * Assume one skipped descriptor per 4KB page.
3406 	 */
3407 	if (! ath_hal_split4ktrans(sc->sc_ah)) {
3408 		int numdescpage = 4096 / (desc_len * ndesc);
3409 		dd->dd_desc_len = (nbuf / numdescpage + 1) * 4096;
3410 	}
3411 
3412 	/*
3413 	 * Setup DMA descriptor area.
3414 	 */
3415 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev),	/* parent */
3416 		       PAGE_SIZE, 0,		/* alignment, bounds */
3417 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
3418 		       BUS_SPACE_MAXADDR,	/* highaddr */
3419 		       NULL, NULL,		/* filter, filterarg */
3420 		       dd->dd_desc_len,		/* maxsize */
3421 		       1,			/* nsegments */
3422 		       dd->dd_desc_len,		/* maxsegsize */
3423 		       BUS_DMA_ALLOCNOW,	/* flags */
3424 		       NULL,			/* lockfunc */
3425 		       NULL,			/* lockarg */
3426 		       &dd->dd_dmat);
3427 	if (error != 0) {
3428 		if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name);
3429 		return error;
3430 	}
3431 
3432 	/* allocate descriptors */
3433 	error = bus_dmamap_create(dd->dd_dmat, BUS_DMA_NOWAIT, &dd->dd_dmamap);
3434 	if (error != 0) {
3435 		if_printf(ifp, "unable to create dmamap for %s descriptors, "
3436 			"error %u\n", dd->dd_name, error);
3437 		goto fail0;
3438 	}
3439 
3440 	error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
3441 				 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
3442 				 &dd->dd_dmamap);
3443 	if (error != 0) {
3444 		if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
3445 			"error %u\n", nbuf * ndesc, dd->dd_name, error);
3446 		goto fail1;
3447 	}
3448 
3449 	error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
3450 				dd->dd_desc, dd->dd_desc_len,
3451 				ath_load_cb, &dd->dd_desc_paddr,
3452 				BUS_DMA_NOWAIT);
3453 	if (error != 0) {
3454 		if_printf(ifp, "unable to map %s descriptors, error %u\n",
3455 			dd->dd_name, error);
3456 		goto fail2;
3457 	}
3458 
3459 	ds = (uint8_t *) dd->dd_desc;
3460 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
3461 	    __func__, dd->dd_name, ds, (u_long) dd->dd_desc_len,
3462 	    (caddr_t) dd->dd_desc_paddr, /*XXX*/ (u_long) dd->dd_desc_len);
3463 
3464 	/* allocate rx buffers */
3465 	bsize = sizeof(struct ath_buf) * nbuf;
3466 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
3467 	if (bf == NULL) {
3468 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
3469 			dd->dd_name, bsize);
3470 		goto fail3;
3471 	}
3472 	dd->dd_bufptr = bf;
3473 
3474 	TAILQ_INIT(head);
3475 	for (i = 0; i < nbuf; i++, bf++, ds += (ndesc * desc_len)) {
3476 		bf->bf_desc = (struct ath_desc *) ds;
3477 		bf->bf_daddr = DS2PHYS(dd, ds);
3478 		if (! ath_hal_split4ktrans(sc->sc_ah)) {
3479 			/*
3480 			 * Merlin WAR: Skip descriptor addresses which
3481 			 * cause 4KB boundary crossing along any point
3482 			 * in the descriptor.
3483 			 */
3484 			 if (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr,
3485 			     desc_len * ndesc)) {
3486 				/* Start at the next page */
3487 				ds += 0x1000 - (bf->bf_daddr & 0xFFF);
3488 				bf->bf_desc = (struct ath_desc *) ds;
3489 				bf->bf_daddr = DS2PHYS(dd, ds);
3490 			}
3491 		}
3492 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
3493 				&bf->bf_dmamap);
3494 		if (error != 0) {
3495 			if_printf(ifp, "unable to create dmamap for %s "
3496 				"buffer %u, error %u\n", dd->dd_name, i, error);
3497 			ath_descdma_cleanup(sc, dd, head);
3498 			return error;
3499 		}
3500 		bf->bf_lastds = bf->bf_desc;	/* Just an initial value */
3501 		TAILQ_INSERT_TAIL(head, bf, bf_list);
3502 	}
3503 	return 0;
3504 fail3:
3505 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3506 fail2:
3507 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3508 fail1:
3509 	bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
3510 fail0:
3511 	bus_dma_tag_destroy(dd->dd_dmat);
3512 	memset(dd, 0, sizeof(*dd));
3513 	return error;
3514 #undef DS2PHYS
3515 #undef ATH_DESC_4KB_BOUND_CHECK
3516 }
3517 
3518 static void
3519 ath_descdma_cleanup(struct ath_softc *sc,
3520 	struct ath_descdma *dd, ath_bufhead *head)
3521 {
3522 	struct ath_buf *bf;
3523 	struct ieee80211_node *ni;
3524 
3525 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3526 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3527 	bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
3528 	bus_dma_tag_destroy(dd->dd_dmat);
3529 
3530 	TAILQ_FOREACH(bf, head, bf_list) {
3531 		if (bf->bf_m) {
3532 			m_freem(bf->bf_m);
3533 			bf->bf_m = NULL;
3534 		}
3535 		if (bf->bf_dmamap != NULL) {
3536 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
3537 			bf->bf_dmamap = NULL;
3538 		}
3539 		ni = bf->bf_node;
3540 		bf->bf_node = NULL;
3541 		if (ni != NULL) {
3542 			/*
3543 			 * Reclaim node reference.
3544 			 */
3545 			ieee80211_free_node(ni);
3546 		}
3547 	}
3548 
3549 	TAILQ_INIT(head);
3550 	free(dd->dd_bufptr, M_ATHDEV);
3551 	memset(dd, 0, sizeof(*dd));
3552 }
3553 
3554 static int
3555 ath_desc_alloc(struct ath_softc *sc)
3556 {
3557 	int error;
3558 
3559 	error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
3560 			"rx", ath_rxbuf, 1);
3561 	if (error != 0)
3562 		return error;
3563 
3564 	error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
3565 			"tx", ath_txbuf, ATH_TXDESC);
3566 	if (error != 0) {
3567 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
3568 		return error;
3569 	}
3570 
3571 	error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
3572 			"beacon", ATH_BCBUF, 1);
3573 	if (error != 0) {
3574 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3575 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
3576 		return error;
3577 	}
3578 	return 0;
3579 }
3580 
3581 static void
3582 ath_desc_free(struct ath_softc *sc)
3583 {
3584 
3585 	if (sc->sc_bdma.dd_desc_len != 0)
3586 		ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
3587 	if (sc->sc_txdma.dd_desc_len != 0)
3588 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3589 	if (sc->sc_rxdma.dd_desc_len != 0)
3590 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
3591 }
3592 
3593 static struct ieee80211_node *
3594 ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
3595 {
3596 	struct ieee80211com *ic = vap->iv_ic;
3597 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3598 	const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
3599 	struct ath_node *an;
3600 
3601 	an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
3602 	if (an == NULL) {
3603 		/* XXX stat+msg */
3604 		return NULL;
3605 	}
3606 	ath_rate_node_init(sc, an);
3607 
3608 	/* Setup the mutex - there's no associd yet so set the name to NULL */
3609 	snprintf(an->an_name, sizeof(an->an_name), "%s: node %p",
3610 	    device_get_nameunit(sc->sc_dev), an);
3611 	mtx_init(&an->an_mtx, an->an_name, NULL, MTX_DEF);
3612 
3613 	/* XXX setup ath_tid */
3614 	ath_tx_tid_init(sc, an);
3615 
3616 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: an %p\n", __func__, an);
3617 	return &an->an_node;
3618 }
3619 
3620 static void
3621 ath_node_cleanup(struct ieee80211_node *ni)
3622 {
3623 	struct ieee80211com *ic = ni->ni_ic;
3624 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3625 
3626 	/* Cleanup ath_tid, free unused bufs, unlink bufs in TXQ */
3627 	ath_tx_node_flush(sc, ATH_NODE(ni));
3628 	ath_rate_node_cleanup(sc, ATH_NODE(ni));
3629 	sc->sc_node_cleanup(ni);
3630 }
3631 
3632 static void
3633 ath_node_free(struct ieee80211_node *ni)
3634 {
3635 	struct ieee80211com *ic = ni->ni_ic;
3636 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3637 
3638 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: ni %p\n", __func__, ni);
3639 	mtx_destroy(&ATH_NODE(ni)->an_mtx);
3640 	sc->sc_node_free(ni);
3641 }
3642 
3643 static void
3644 ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
3645 {
3646 	struct ieee80211com *ic = ni->ni_ic;
3647 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3648 	struct ath_hal *ah = sc->sc_ah;
3649 
3650 	*rssi = ic->ic_node_getrssi(ni);
3651 	if (ni->ni_chan != IEEE80211_CHAN_ANYC)
3652 		*noise = ath_hal_getchannoise(ah, ni->ni_chan);
3653 	else
3654 		*noise = -95;		/* nominally correct */
3655 }
3656 
3657 static int
3658 ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
3659 {
3660 	struct ath_hal *ah = sc->sc_ah;
3661 	int error;
3662 	struct mbuf *m;
3663 	struct ath_desc *ds;
3664 
3665 	m = bf->bf_m;
3666 	if (m == NULL) {
3667 		/*
3668 		 * NB: by assigning a page to the rx dma buffer we
3669 		 * implicitly satisfy the Atheros requirement that
3670 		 * this buffer be cache-line-aligned and sized to be
3671 		 * multiple of the cache line size.  Not doing this
3672 		 * causes weird stuff to happen (for the 5210 at least).
3673 		 */
3674 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
3675 		if (m == NULL) {
3676 			DPRINTF(sc, ATH_DEBUG_ANY,
3677 				"%s: no mbuf/cluster\n", __func__);
3678 			sc->sc_stats.ast_rx_nombuf++;
3679 			return ENOMEM;
3680 		}
3681 		m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
3682 
3683 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat,
3684 					     bf->bf_dmamap, m,
3685 					     bf->bf_segs, &bf->bf_nseg,
3686 					     BUS_DMA_NOWAIT);
3687 		if (error != 0) {
3688 			DPRINTF(sc, ATH_DEBUG_ANY,
3689 			    "%s: bus_dmamap_load_mbuf_sg failed; error %d\n",
3690 			    __func__, error);
3691 			sc->sc_stats.ast_rx_busdma++;
3692 			m_freem(m);
3693 			return error;
3694 		}
3695 		KASSERT(bf->bf_nseg == 1,
3696 			("multi-segment packet; nseg %u", bf->bf_nseg));
3697 		bf->bf_m = m;
3698 	}
3699 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD);
3700 
3701 	/*
3702 	 * Setup descriptors.  For receive we always terminate
3703 	 * the descriptor list with a self-linked entry so we'll
3704 	 * not get overrun under high load (as can happen with a
3705 	 * 5212 when ANI processing enables PHY error frames).
3706 	 *
3707 	 * To insure the last descriptor is self-linked we create
3708 	 * each descriptor as self-linked and add it to the end.  As
3709 	 * each additional descriptor is added the previous self-linked
3710 	 * entry is ``fixed'' naturally.  This should be safe even
3711 	 * if DMA is happening.  When processing RX interrupts we
3712 	 * never remove/process the last, self-linked, entry on the
3713 	 * descriptor list.  This insures the hardware always has
3714 	 * someplace to write a new frame.
3715 	 */
3716 	/*
3717 	 * 11N: we can no longer afford to self link the last descriptor.
3718 	 * MAC acknowledges BA status as long as it copies frames to host
3719 	 * buffer (or rx fifo). This can incorrectly acknowledge packets
3720 	 * to a sender if last desc is self-linked.
3721 	 */
3722 	ds = bf->bf_desc;
3723 	if (sc->sc_rxslink)
3724 		ds->ds_link = bf->bf_daddr;	/* link to self */
3725 	else
3726 		ds->ds_link = 0;		/* terminate the list */
3727 	ds->ds_data = bf->bf_segs[0].ds_addr;
3728 	ath_hal_setuprxdesc(ah, ds
3729 		, m->m_len		/* buffer size */
3730 		, 0
3731 	);
3732 
3733 	if (sc->sc_rxlink != NULL)
3734 		*sc->sc_rxlink = bf->bf_daddr;
3735 	sc->sc_rxlink = &ds->ds_link;
3736 	return 0;
3737 }
3738 
3739 /*
3740  * Extend 15-bit time stamp from rx descriptor to
3741  * a full 64-bit TSF using the specified TSF.
3742  */
3743 static __inline u_int64_t
3744 ath_extend_tsf15(u_int32_t rstamp, u_int64_t tsf)
3745 {
3746 	if ((tsf & 0x7fff) < rstamp)
3747 		tsf -= 0x8000;
3748 
3749 	return ((tsf &~ 0x7fff) | rstamp);
3750 }
3751 
3752 /*
3753  * Extend 32-bit time stamp from rx descriptor to
3754  * a full 64-bit TSF using the specified TSF.
3755  */
3756 static __inline u_int64_t
3757 ath_extend_tsf32(u_int32_t rstamp, u_int64_t tsf)
3758 {
3759 	u_int32_t tsf_low = tsf & 0xffffffff;
3760 	u_int64_t tsf64 = (tsf & ~0xffffffffULL) | rstamp;
3761 
3762 	if (rstamp > tsf_low && (rstamp - tsf_low > 0x10000000))
3763 		tsf64 -= 0x100000000ULL;
3764 
3765 	if (rstamp < tsf_low && (tsf_low - rstamp > 0x10000000))
3766 		tsf64 += 0x100000000ULL;
3767 
3768 	return tsf64;
3769 }
3770 
3771 /*
3772  * Extend the TSF from the RX descriptor to a full 64 bit TSF.
3773  * Earlier hardware versions only wrote the low 15 bits of the
3774  * TSF into the RX descriptor; later versions (AR5416 and up)
3775  * include the 32 bit TSF value.
3776  */
3777 static __inline u_int64_t
3778 ath_extend_tsf(struct ath_softc *sc, u_int32_t rstamp, u_int64_t tsf)
3779 {
3780 	if (sc->sc_rxtsf32)
3781 		return ath_extend_tsf32(rstamp, tsf);
3782 	else
3783 		return ath_extend_tsf15(rstamp, tsf);
3784 }
3785 
3786 /*
3787  * Intercept management frames to collect beacon rssi data
3788  * and to do ibss merges.
3789  */
3790 static void
3791 ath_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
3792 	int subtype, int rssi, int nf)
3793 {
3794 	struct ieee80211vap *vap = ni->ni_vap;
3795 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
3796 
3797 	/*
3798 	 * Call up first so subsequent work can use information
3799 	 * potentially stored in the node (e.g. for ibss merge).
3800 	 */
3801 	ATH_VAP(vap)->av_recv_mgmt(ni, m, subtype, rssi, nf);
3802 	switch (subtype) {
3803 	case IEEE80211_FC0_SUBTYPE_BEACON:
3804 		/* update rssi statistics for use by the hal */
3805 		ATH_RSSI_LPF(sc->sc_halstats.ns_avgbrssi, rssi);
3806 		if (sc->sc_syncbeacon &&
3807 		    ni == vap->iv_bss && vap->iv_state == IEEE80211_S_RUN) {
3808 			/*
3809 			 * Resync beacon timers using the tsf of the beacon
3810 			 * frame we just received.
3811 			 */
3812 			ath_beacon_config(sc, vap);
3813 		}
3814 		/* fall thru... */
3815 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
3816 		if (vap->iv_opmode == IEEE80211_M_IBSS &&
3817 		    vap->iv_state == IEEE80211_S_RUN) {
3818 			uint32_t rstamp = sc->sc_lastrs->rs_tstamp;
3819 			uint64_t tsf = ath_extend_tsf(sc, rstamp,
3820 				ath_hal_gettsf64(sc->sc_ah));
3821 			/*
3822 			 * Handle ibss merge as needed; check the tsf on the
3823 			 * frame before attempting the merge.  The 802.11 spec
3824 			 * says the station should change it's bssid to match
3825 			 * the oldest station with the same ssid, where oldest
3826 			 * is determined by the tsf.  Note that hardware
3827 			 * reconfiguration happens through callback to
3828 			 * ath_newstate as the state machine will go from
3829 			 * RUN -> RUN when this happens.
3830 			 */
3831 			if (le64toh(ni->ni_tstamp.tsf) >= tsf) {
3832 				DPRINTF(sc, ATH_DEBUG_STATE,
3833 				    "ibss merge, rstamp %u tsf %ju "
3834 				    "tstamp %ju\n", rstamp, (uintmax_t)tsf,
3835 				    (uintmax_t)ni->ni_tstamp.tsf);
3836 				(void) ieee80211_ibss_merge(ni);
3837 			}
3838 		}
3839 		break;
3840 	}
3841 }
3842 
3843 /*
3844  * Set the default antenna.
3845  */
3846 static void
3847 ath_setdefantenna(struct ath_softc *sc, u_int antenna)
3848 {
3849 	struct ath_hal *ah = sc->sc_ah;
3850 
3851 	/* XXX block beacon interrupts */
3852 	ath_hal_setdefantenna(ah, antenna);
3853 	if (sc->sc_defant != antenna)
3854 		sc->sc_stats.ast_ant_defswitch++;
3855 	sc->sc_defant = antenna;
3856 	sc->sc_rxotherant = 0;
3857 }
3858 
3859 static void
3860 ath_rx_tap(struct ifnet *ifp, struct mbuf *m,
3861 	const struct ath_rx_status *rs, u_int64_t tsf, int16_t nf)
3862 {
3863 #define	CHAN_HT20	htole32(IEEE80211_CHAN_HT20)
3864 #define	CHAN_HT40U	htole32(IEEE80211_CHAN_HT40U)
3865 #define	CHAN_HT40D	htole32(IEEE80211_CHAN_HT40D)
3866 #define	CHAN_HT		(CHAN_HT20|CHAN_HT40U|CHAN_HT40D)
3867 	struct ath_softc *sc = ifp->if_softc;
3868 	const HAL_RATE_TABLE *rt;
3869 	uint8_t rix;
3870 
3871 	rt = sc->sc_currates;
3872 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
3873 	rix = rt->rateCodeToIndex[rs->rs_rate];
3874 	sc->sc_rx_th.wr_rate = sc->sc_hwmap[rix].ieeerate;
3875 	sc->sc_rx_th.wr_flags = sc->sc_hwmap[rix].rxflags;
3876 #ifdef AH_SUPPORT_AR5416
3877 	sc->sc_rx_th.wr_chan_flags &= ~CHAN_HT;
3878 	if (sc->sc_rx_th.wr_rate & IEEE80211_RATE_MCS) {	/* HT rate */
3879 		struct ieee80211com *ic = ifp->if_l2com;
3880 
3881 		if ((rs->rs_flags & HAL_RX_2040) == 0)
3882 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT20;
3883 		else if (IEEE80211_IS_CHAN_HT40U(ic->ic_curchan))
3884 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT40U;
3885 		else
3886 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT40D;
3887 		if ((rs->rs_flags & HAL_RX_GI) == 0)
3888 			sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
3889 	}
3890 #endif
3891 	sc->sc_rx_th.wr_tsf = htole64(ath_extend_tsf(sc, rs->rs_tstamp, tsf));
3892 	if (rs->rs_status & HAL_RXERR_CRC)
3893 		sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
3894 	/* XXX propagate other error flags from descriptor */
3895 	sc->sc_rx_th.wr_antnoise = nf;
3896 	sc->sc_rx_th.wr_antsignal = nf + rs->rs_rssi;
3897 	sc->sc_rx_th.wr_antenna = rs->rs_antenna;
3898 #undef CHAN_HT
3899 #undef CHAN_HT20
3900 #undef CHAN_HT40U
3901 #undef CHAN_HT40D
3902 }
3903 
3904 static void
3905 ath_handle_micerror(struct ieee80211com *ic,
3906 	struct ieee80211_frame *wh, int keyix)
3907 {
3908 	struct ieee80211_node *ni;
3909 
3910 	/* XXX recheck MIC to deal w/ chips that lie */
3911 	/* XXX discard MIC errors on !data frames */
3912 	ni = ieee80211_find_rxnode(ic, (const struct ieee80211_frame_min *) wh);
3913 	if (ni != NULL) {
3914 		ieee80211_notify_michael_failure(ni->ni_vap, wh, keyix);
3915 		ieee80211_free_node(ni);
3916 	}
3917 }
3918 
3919 /*
3920  * Only run the RX proc if it's not already running.
3921  * Since this may get run as part of the reset/flush path,
3922  * the task can't clash with an existing, running tasklet.
3923  */
3924 static void
3925 ath_rx_tasklet(void *arg, int npending)
3926 {
3927 	struct ath_softc *sc = arg;
3928 
3929 	CTR1(ATH_KTR_INTR, "ath_rx_proc: pending=%d", npending);
3930 	DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: pending %u\n", __func__, npending);
3931 	ATH_PCU_LOCK(sc);
3932 	if (sc->sc_inreset_cnt > 0) {
3933 		device_printf(sc->sc_dev,
3934 		    "%s: sc_inreset_cnt > 0; skipping\n", __func__);
3935 		ATH_PCU_UNLOCK(sc);
3936 		return;
3937 	}
3938 	ATH_PCU_UNLOCK(sc);
3939 	ath_rx_proc(sc, 1);
3940 }
3941 
3942 static void
3943 ath_rx_proc(struct ath_softc *sc, int resched)
3944 {
3945 #define	PA2DESC(_sc, _pa) \
3946 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
3947 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
3948 	struct ath_buf *bf;
3949 	struct ifnet *ifp = sc->sc_ifp;
3950 	struct ieee80211com *ic = ifp->if_l2com;
3951 	struct ath_hal *ah = sc->sc_ah;
3952 	struct ath_desc *ds;
3953 	struct ath_rx_status *rs;
3954 	struct mbuf *m;
3955 	struct ieee80211_node *ni;
3956 	int len, type, ngood;
3957 	HAL_STATUS status;
3958 	int16_t nf;
3959 	u_int64_t tsf;
3960 	int npkts = 0;
3961 
3962 	/* XXX we must not hold the ATH_LOCK here */
3963 	ATH_UNLOCK_ASSERT(sc);
3964 	ATH_PCU_UNLOCK_ASSERT(sc);
3965 
3966 	ATH_PCU_LOCK(sc);
3967 	sc->sc_rxproc_cnt++;
3968 	ATH_PCU_UNLOCK(sc);
3969 
3970 	DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: called\n", __func__);
3971 	ngood = 0;
3972 	nf = ath_hal_getchannoise(ah, sc->sc_curchan);
3973 	sc->sc_stats.ast_rx_noise = nf;
3974 	tsf = ath_hal_gettsf64(ah);
3975 	do {
3976 		bf = TAILQ_FIRST(&sc->sc_rxbuf);
3977 		if (sc->sc_rxslink && bf == NULL) {	/* NB: shouldn't happen */
3978 			if_printf(ifp, "%s: no buffer!\n", __func__);
3979 			break;
3980 		} else if (bf == NULL) {
3981 			/*
3982 			 * End of List:
3983 			 * this can happen for non-self-linked RX chains
3984 			 */
3985 			sc->sc_stats.ast_rx_hitqueueend++;
3986 			break;
3987 		}
3988 		m = bf->bf_m;
3989 		if (m == NULL) {		/* NB: shouldn't happen */
3990 			/*
3991 			 * If mbuf allocation failed previously there
3992 			 * will be no mbuf; try again to re-populate it.
3993 			 */
3994 			/* XXX make debug msg */
3995 			if_printf(ifp, "%s: no mbuf!\n", __func__);
3996 			TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
3997 			goto rx_next;
3998 		}
3999 		ds = bf->bf_desc;
4000 		if (ds->ds_link == bf->bf_daddr) {
4001 			/* NB: never process the self-linked entry at the end */
4002 			sc->sc_stats.ast_rx_hitqueueend++;
4003 			break;
4004 		}
4005 		/* XXX sync descriptor memory */
4006 		/*
4007 		 * Must provide the virtual address of the current
4008 		 * descriptor, the physical address, and the virtual
4009 		 * address of the next descriptor in the h/w chain.
4010 		 * This allows the HAL to look ahead to see if the
4011 		 * hardware is done with a descriptor by checking the
4012 		 * done bit in the following descriptor and the address
4013 		 * of the current descriptor the DMA engine is working
4014 		 * on.  All this is necessary because of our use of
4015 		 * a self-linked list to avoid rx overruns.
4016 		 */
4017 		rs = &bf->bf_status.ds_rxstat;
4018 		status = ath_hal_rxprocdesc(ah, ds,
4019 				bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
4020 #ifdef ATH_DEBUG
4021 		if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
4022 			ath_printrxbuf(sc, bf, 0, status == HAL_OK);
4023 #endif
4024 		if (status == HAL_EINPROGRESS)
4025 			break;
4026 
4027 		TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
4028 		npkts++;
4029 
4030 		/* These aren't specifically errors */
4031 #ifdef	AH_SUPPORT_AR5416
4032 		if (rs->rs_flags & HAL_RX_GI)
4033 			sc->sc_stats.ast_rx_halfgi++;
4034 		if (rs->rs_flags & HAL_RX_2040)
4035 			sc->sc_stats.ast_rx_2040++;
4036 		if (rs->rs_flags & HAL_RX_DELIM_CRC_PRE)
4037 			sc->sc_stats.ast_rx_pre_crc_err++;
4038 		if (rs->rs_flags & HAL_RX_DELIM_CRC_POST)
4039 			sc->sc_stats.ast_rx_post_crc_err++;
4040 		if (rs->rs_flags & HAL_RX_DECRYPT_BUSY)
4041 			sc->sc_stats.ast_rx_decrypt_busy_err++;
4042 		if (rs->rs_flags & HAL_RX_HI_RX_CHAIN)
4043 			sc->sc_stats.ast_rx_hi_rx_chain++;
4044 #endif /* AH_SUPPORT_AR5416 */
4045 
4046 		if (rs->rs_status != 0) {
4047 			if (rs->rs_status & HAL_RXERR_CRC)
4048 				sc->sc_stats.ast_rx_crcerr++;
4049 			if (rs->rs_status & HAL_RXERR_FIFO)
4050 				sc->sc_stats.ast_rx_fifoerr++;
4051 			if (rs->rs_status & HAL_RXERR_PHY) {
4052 				sc->sc_stats.ast_rx_phyerr++;
4053 				/* Process DFS radar events */
4054 				if ((rs->rs_phyerr == HAL_PHYERR_RADAR) ||
4055 				    (rs->rs_phyerr == HAL_PHYERR_FALSE_RADAR_EXT)) {
4056 					/* Since we're touching the frame data, sync it */
4057 					bus_dmamap_sync(sc->sc_dmat,
4058 					    bf->bf_dmamap,
4059 					    BUS_DMASYNC_POSTREAD);
4060 					/* Now pass it to the radar processing code */
4061 					ath_dfs_process_phy_err(sc, mtod(m, char *), tsf, rs);
4062 				}
4063 
4064 				/* Be suitably paranoid about receiving phy errors out of the stats array bounds */
4065 				if (rs->rs_phyerr < 64)
4066 					sc->sc_stats.ast_rx_phy[rs->rs_phyerr]++;
4067 				goto rx_error;	/* NB: don't count in ierrors */
4068 			}
4069 			if (rs->rs_status & HAL_RXERR_DECRYPT) {
4070 				/*
4071 				 * Decrypt error.  If the error occurred
4072 				 * because there was no hardware key, then
4073 				 * let the frame through so the upper layers
4074 				 * can process it.  This is necessary for 5210
4075 				 * parts which have no way to setup a ``clear''
4076 				 * key cache entry.
4077 				 *
4078 				 * XXX do key cache faulting
4079 				 */
4080 				if (rs->rs_keyix == HAL_RXKEYIX_INVALID)
4081 					goto rx_accept;
4082 				sc->sc_stats.ast_rx_badcrypt++;
4083 			}
4084 			if (rs->rs_status & HAL_RXERR_MIC) {
4085 				sc->sc_stats.ast_rx_badmic++;
4086 				/*
4087 				 * Do minimal work required to hand off
4088 				 * the 802.11 header for notification.
4089 				 */
4090 				/* XXX frag's and qos frames */
4091 				len = rs->rs_datalen;
4092 				if (len >= sizeof (struct ieee80211_frame)) {
4093 					bus_dmamap_sync(sc->sc_dmat,
4094 					    bf->bf_dmamap,
4095 					    BUS_DMASYNC_POSTREAD);
4096 					ath_handle_micerror(ic,
4097 					    mtod(m, struct ieee80211_frame *),
4098 					    sc->sc_splitmic ?
4099 						rs->rs_keyix-32 : rs->rs_keyix);
4100 				}
4101 			}
4102 			ifp->if_ierrors++;
4103 rx_error:
4104 			/*
4105 			 * Cleanup any pending partial frame.
4106 			 */
4107 			if (sc->sc_rxpending != NULL) {
4108 				m_freem(sc->sc_rxpending);
4109 				sc->sc_rxpending = NULL;
4110 			}
4111 			/*
4112 			 * When a tap is present pass error frames
4113 			 * that have been requested.  By default we
4114 			 * pass decrypt+mic errors but others may be
4115 			 * interesting (e.g. crc).
4116 			 */
4117 			if (ieee80211_radiotap_active(ic) &&
4118 			    (rs->rs_status & sc->sc_monpass)) {
4119 				bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
4120 				    BUS_DMASYNC_POSTREAD);
4121 				/* NB: bpf needs the mbuf length setup */
4122 				len = rs->rs_datalen;
4123 				m->m_pkthdr.len = m->m_len = len;
4124 				bf->bf_m = NULL;
4125 				ath_rx_tap(ifp, m, rs, tsf, nf);
4126 				ieee80211_radiotap_rx_all(ic, m);
4127 				m_freem(m);
4128 			}
4129 			/* XXX pass MIC errors up for s/w reclaculation */
4130 			goto rx_next;
4131 		}
4132 rx_accept:
4133 		/*
4134 		 * Sync and unmap the frame.  At this point we're
4135 		 * committed to passing the mbuf somewhere so clear
4136 		 * bf_m; this means a new mbuf must be allocated
4137 		 * when the rx descriptor is setup again to receive
4138 		 * another frame.
4139 		 */
4140 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
4141 		    BUS_DMASYNC_POSTREAD);
4142 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
4143 		bf->bf_m = NULL;
4144 
4145 		len = rs->rs_datalen;
4146 		m->m_len = len;
4147 
4148 		if (rs->rs_more) {
4149 			/*
4150 			 * Frame spans multiple descriptors; save
4151 			 * it for the next completed descriptor, it
4152 			 * will be used to construct a jumbogram.
4153 			 */
4154 			if (sc->sc_rxpending != NULL) {
4155 				/* NB: max frame size is currently 2 clusters */
4156 				sc->sc_stats.ast_rx_toobig++;
4157 				m_freem(sc->sc_rxpending);
4158 			}
4159 			m->m_pkthdr.rcvif = ifp;
4160 			m->m_pkthdr.len = len;
4161 			sc->sc_rxpending = m;
4162 			goto rx_next;
4163 		} else if (sc->sc_rxpending != NULL) {
4164 			/*
4165 			 * This is the second part of a jumbogram,
4166 			 * chain it to the first mbuf, adjust the
4167 			 * frame length, and clear the rxpending state.
4168 			 */
4169 			sc->sc_rxpending->m_next = m;
4170 			sc->sc_rxpending->m_pkthdr.len += len;
4171 			m = sc->sc_rxpending;
4172 			sc->sc_rxpending = NULL;
4173 		} else {
4174 			/*
4175 			 * Normal single-descriptor receive; setup
4176 			 * the rcvif and packet length.
4177 			 */
4178 			m->m_pkthdr.rcvif = ifp;
4179 			m->m_pkthdr.len = len;
4180 		}
4181 
4182 		/*
4183 		 * Validate rs->rs_antenna.
4184 		 *
4185 		 * Some users w/ AR9285 NICs have reported crashes
4186 		 * here because rs_antenna field is bogusly large.
4187 		 * Let's enforce the maximum antenna limit of 8
4188 		 * (and it shouldn't be hard coded, but that's a
4189 		 * separate problem) and if there's an issue, print
4190 		 * out an error and adjust rs_antenna to something
4191 		 * sensible.
4192 		 *
4193 		 * This code should be removed once the actual
4194 		 * root cause of the issue has been identified.
4195 		 * For example, it may be that the rs_antenna
4196 		 * field is only valid for the lsat frame of
4197 		 * an aggregate and it just happens that it is
4198 		 * "mostly" right. (This is a general statement -
4199 		 * the majority of the statistics are only valid
4200 		 * for the last frame in an aggregate.
4201 		 */
4202 		if (rs->rs_antenna > 7) {
4203 			device_printf(sc->sc_dev, "%s: rs_antenna > 7 (%d)\n",
4204 			    __func__, rs->rs_antenna);
4205 #ifdef	ATH_DEBUG
4206 			ath_printrxbuf(sc, bf, 0, status == HAL_OK);
4207 #endif /* ATH_DEBUG */
4208 			rs->rs_antenna = 0;	/* XXX better than nothing */
4209 		}
4210 
4211 		ifp->if_ipackets++;
4212 		sc->sc_stats.ast_ant_rx[rs->rs_antenna]++;
4213 
4214 		/*
4215 		 * Populate the rx status block.  When there are bpf
4216 		 * listeners we do the additional work to provide
4217 		 * complete status.  Otherwise we fill in only the
4218 		 * material required by ieee80211_input.  Note that
4219 		 * noise setting is filled in above.
4220 		 */
4221 		if (ieee80211_radiotap_active(ic))
4222 			ath_rx_tap(ifp, m, rs, tsf, nf);
4223 
4224 		/*
4225 		 * From this point on we assume the frame is at least
4226 		 * as large as ieee80211_frame_min; verify that.
4227 		 */
4228 		if (len < IEEE80211_MIN_LEN) {
4229 			if (!ieee80211_radiotap_active(ic)) {
4230 				DPRINTF(sc, ATH_DEBUG_RECV,
4231 				    "%s: short packet %d\n", __func__, len);
4232 				sc->sc_stats.ast_rx_tooshort++;
4233 			} else {
4234 				/* NB: in particular this captures ack's */
4235 				ieee80211_radiotap_rx_all(ic, m);
4236 			}
4237 			m_freem(m);
4238 			goto rx_next;
4239 		}
4240 
4241 		if (IFF_DUMPPKTS(sc, ATH_DEBUG_RECV)) {
4242 			const HAL_RATE_TABLE *rt = sc->sc_currates;
4243 			uint8_t rix = rt->rateCodeToIndex[rs->rs_rate];
4244 
4245 			ieee80211_dump_pkt(ic, mtod(m, caddr_t), len,
4246 			    sc->sc_hwmap[rix].ieeerate, rs->rs_rssi);
4247 		}
4248 
4249 		m_adj(m, -IEEE80211_CRC_LEN);
4250 
4251 		/*
4252 		 * Locate the node for sender, track state, and then
4253 		 * pass the (referenced) node up to the 802.11 layer
4254 		 * for its use.
4255 		 */
4256 		ni = ieee80211_find_rxnode_withkey(ic,
4257 			mtod(m, const struct ieee80211_frame_min *),
4258 			rs->rs_keyix == HAL_RXKEYIX_INVALID ?
4259 				IEEE80211_KEYIX_NONE : rs->rs_keyix);
4260 		sc->sc_lastrs = rs;
4261 
4262 #ifdef	AH_SUPPORT_AR5416
4263 		if (rs->rs_isaggr)
4264 			sc->sc_stats.ast_rx_agg++;
4265 #endif /* AH_SUPPORT_AR5416 */
4266 
4267 		if (ni != NULL) {
4268 			/*
4269  			 * Only punt packets for ampdu reorder processing for
4270 			 * 11n nodes; net80211 enforces that M_AMPDU is only
4271 			 * set for 11n nodes.
4272  			 */
4273 			if (ni->ni_flags & IEEE80211_NODE_HT)
4274 				m->m_flags |= M_AMPDU;
4275 
4276 			/*
4277 			 * Sending station is known, dispatch directly.
4278 			 */
4279 			type = ieee80211_input(ni, m, rs->rs_rssi, nf);
4280 			ieee80211_free_node(ni);
4281 			/*
4282 			 * Arrange to update the last rx timestamp only for
4283 			 * frames from our ap when operating in station mode.
4284 			 * This assumes the rx key is always setup when
4285 			 * associated.
4286 			 */
4287 			if (ic->ic_opmode == IEEE80211_M_STA &&
4288 			    rs->rs_keyix != HAL_RXKEYIX_INVALID)
4289 				ngood++;
4290 		} else {
4291 			type = ieee80211_input_all(ic, m, rs->rs_rssi, nf);
4292 		}
4293 		/*
4294 		 * Track rx rssi and do any rx antenna management.
4295 		 */
4296 		ATH_RSSI_LPF(sc->sc_halstats.ns_avgrssi, rs->rs_rssi);
4297 		if (sc->sc_diversity) {
4298 			/*
4299 			 * When using fast diversity, change the default rx
4300 			 * antenna if diversity chooses the other antenna 3
4301 			 * times in a row.
4302 			 */
4303 			if (sc->sc_defant != rs->rs_antenna) {
4304 				if (++sc->sc_rxotherant >= 3)
4305 					ath_setdefantenna(sc, rs->rs_antenna);
4306 			} else
4307 				sc->sc_rxotherant = 0;
4308 		}
4309 
4310 		/* Newer school diversity - kite specific for now */
4311 		/* XXX perhaps migrate the normal diversity code to this? */
4312 		if ((ah)->ah_rxAntCombDiversity)
4313 			(*(ah)->ah_rxAntCombDiversity)(ah, rs, ticks, hz);
4314 
4315 		if (sc->sc_softled) {
4316 			/*
4317 			 * Blink for any data frame.  Otherwise do a
4318 			 * heartbeat-style blink when idle.  The latter
4319 			 * is mainly for station mode where we depend on
4320 			 * periodic beacon frames to trigger the poll event.
4321 			 */
4322 			if (type == IEEE80211_FC0_TYPE_DATA) {
4323 				const HAL_RATE_TABLE *rt = sc->sc_currates;
4324 				ath_led_event(sc,
4325 				    rt->rateCodeToIndex[rs->rs_rate]);
4326 			} else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
4327 				ath_led_event(sc, 0);
4328 		}
4329 rx_next:
4330 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
4331 	} while (ath_rxbuf_init(sc, bf) == 0);
4332 
4333 	/* rx signal state monitoring */
4334 	ath_hal_rxmonitor(ah, &sc->sc_halstats, sc->sc_curchan);
4335 	if (ngood)
4336 		sc->sc_lastrx = tsf;
4337 
4338 	CTR2(ATH_KTR_INTR, "ath_rx_proc: npkts=%d, ngood=%d", npkts, ngood);
4339 	/* Queue DFS tasklet if needed */
4340 	if (resched && ath_dfs_tasklet_needed(sc, sc->sc_curchan))
4341 		taskqueue_enqueue(sc->sc_tq, &sc->sc_dfstask);
4342 
4343 	/*
4344 	 * Now that all the RX frames were handled that
4345 	 * need to be handled, kick the PCU if there's
4346 	 * been an RXEOL condition.
4347 	 */
4348 	ATH_PCU_LOCK(sc);
4349 	if (resched && sc->sc_kickpcu) {
4350 		CTR0(ATH_KTR_ERR, "ath_rx_proc: kickpcu");
4351 		device_printf(sc->sc_dev, "%s: kickpcu; handled %d packets\n",
4352 		    __func__, npkts);
4353 
4354 		/* XXX rxslink? */
4355 		/*
4356 		 * XXX can we hold the PCU lock here?
4357 		 * Are there any net80211 buffer calls involved?
4358 		 */
4359 		bf = TAILQ_FIRST(&sc->sc_rxbuf);
4360 		ath_hal_putrxbuf(ah, bf->bf_daddr);
4361 		ath_hal_rxena(ah);		/* enable recv descriptors */
4362 		ath_mode_init(sc);		/* set filters, etc. */
4363 		ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
4364 
4365 		ath_hal_intrset(ah, sc->sc_imask);
4366 		sc->sc_kickpcu = 0;
4367 	}
4368 	ATH_PCU_UNLOCK(sc);
4369 
4370 	/* XXX check this inside of IF_LOCK? */
4371 	if (resched && (ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0) {
4372 #ifdef IEEE80211_SUPPORT_SUPERG
4373 		ieee80211_ff_age_all(ic, 100);
4374 #endif
4375 		if (!IFQ_IS_EMPTY(&ifp->if_snd))
4376 			ath_start(ifp);
4377 	}
4378 #undef PA2DESC
4379 
4380 	ATH_PCU_LOCK(sc);
4381 	sc->sc_rxproc_cnt--;
4382 	ATH_PCU_UNLOCK(sc);
4383 }
4384 
4385 static void
4386 ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum)
4387 {
4388 	txq->axq_qnum = qnum;
4389 	txq->axq_ac = 0;
4390 	txq->axq_depth = 0;
4391 	txq->axq_aggr_depth = 0;
4392 	txq->axq_intrcnt = 0;
4393 	txq->axq_link = NULL;
4394 	txq->axq_softc = sc;
4395 	TAILQ_INIT(&txq->axq_q);
4396 	TAILQ_INIT(&txq->axq_tidq);
4397 	ATH_TXQ_LOCK_INIT(sc, txq);
4398 }
4399 
4400 /*
4401  * Setup a h/w transmit queue.
4402  */
4403 static struct ath_txq *
4404 ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
4405 {
4406 #define	N(a)	(sizeof(a)/sizeof(a[0]))
4407 	struct ath_hal *ah = sc->sc_ah;
4408 	HAL_TXQ_INFO qi;
4409 	int qnum;
4410 
4411 	memset(&qi, 0, sizeof(qi));
4412 	qi.tqi_subtype = subtype;
4413 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
4414 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
4415 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
4416 	/*
4417 	 * Enable interrupts only for EOL and DESC conditions.
4418 	 * We mark tx descriptors to receive a DESC interrupt
4419 	 * when a tx queue gets deep; otherwise waiting for the
4420 	 * EOL to reap descriptors.  Note that this is done to
4421 	 * reduce interrupt load and this only defers reaping
4422 	 * descriptors, never transmitting frames.  Aside from
4423 	 * reducing interrupts this also permits more concurrency.
4424 	 * The only potential downside is if the tx queue backs
4425 	 * up in which case the top half of the kernel may backup
4426 	 * due to a lack of tx descriptors.
4427 	 */
4428 	qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE | HAL_TXQ_TXDESCINT_ENABLE;
4429 	qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
4430 	if (qnum == -1) {
4431 		/*
4432 		 * NB: don't print a message, this happens
4433 		 * normally on parts with too few tx queues
4434 		 */
4435 		return NULL;
4436 	}
4437 	if (qnum >= N(sc->sc_txq)) {
4438 		device_printf(sc->sc_dev,
4439 			"hal qnum %u out of range, max %zu!\n",
4440 			qnum, N(sc->sc_txq));
4441 		ath_hal_releasetxqueue(ah, qnum);
4442 		return NULL;
4443 	}
4444 	if (!ATH_TXQ_SETUP(sc, qnum)) {
4445 		ath_txq_init(sc, &sc->sc_txq[qnum], qnum);
4446 		sc->sc_txqsetup |= 1<<qnum;
4447 	}
4448 	return &sc->sc_txq[qnum];
4449 #undef N
4450 }
4451 
4452 /*
4453  * Setup a hardware data transmit queue for the specified
4454  * access control.  The hal may not support all requested
4455  * queues in which case it will return a reference to a
4456  * previously setup queue.  We record the mapping from ac's
4457  * to h/w queues for use by ath_tx_start and also track
4458  * the set of h/w queues being used to optimize work in the
4459  * transmit interrupt handler and related routines.
4460  */
4461 static int
4462 ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
4463 {
4464 #define	N(a)	(sizeof(a)/sizeof(a[0]))
4465 	struct ath_txq *txq;
4466 
4467 	if (ac >= N(sc->sc_ac2q)) {
4468 		device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
4469 			ac, N(sc->sc_ac2q));
4470 		return 0;
4471 	}
4472 	txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
4473 	if (txq != NULL) {
4474 		txq->axq_ac = ac;
4475 		sc->sc_ac2q[ac] = txq;
4476 		return 1;
4477 	} else
4478 		return 0;
4479 #undef N
4480 }
4481 
4482 /*
4483  * Update WME parameters for a transmit queue.
4484  */
4485 static int
4486 ath_txq_update(struct ath_softc *sc, int ac)
4487 {
4488 #define	ATH_EXPONENT_TO_VALUE(v)	((1<<v)-1)
4489 #define	ATH_TXOP_TO_US(v)		(v<<5)
4490 	struct ifnet *ifp = sc->sc_ifp;
4491 	struct ieee80211com *ic = ifp->if_l2com;
4492 	struct ath_txq *txq = sc->sc_ac2q[ac];
4493 	struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
4494 	struct ath_hal *ah = sc->sc_ah;
4495 	HAL_TXQ_INFO qi;
4496 
4497 	ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
4498 #ifdef IEEE80211_SUPPORT_TDMA
4499 	if (sc->sc_tdma) {
4500 		/*
4501 		 * AIFS is zero so there's no pre-transmit wait.  The
4502 		 * burst time defines the slot duration and is configured
4503 		 * through net80211.  The QCU is setup to not do post-xmit
4504 		 * back off, lockout all lower-priority QCU's, and fire
4505 		 * off the DMA beacon alert timer which is setup based
4506 		 * on the slot configuration.
4507 		 */
4508 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
4509 			      | HAL_TXQ_TXERRINT_ENABLE
4510 			      | HAL_TXQ_TXURNINT_ENABLE
4511 			      | HAL_TXQ_TXEOLINT_ENABLE
4512 			      | HAL_TXQ_DBA_GATED
4513 			      | HAL_TXQ_BACKOFF_DISABLE
4514 			      | HAL_TXQ_ARB_LOCKOUT_GLOBAL
4515 			      ;
4516 		qi.tqi_aifs = 0;
4517 		/* XXX +dbaprep? */
4518 		qi.tqi_readyTime = sc->sc_tdmaslotlen;
4519 		qi.tqi_burstTime = qi.tqi_readyTime;
4520 	} else {
4521 #endif
4522 		/*
4523 		 * XXX shouldn't this just use the default flags
4524 		 * used in the previous queue setup?
4525 		 */
4526 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
4527 			      | HAL_TXQ_TXERRINT_ENABLE
4528 			      | HAL_TXQ_TXDESCINT_ENABLE
4529 			      | HAL_TXQ_TXURNINT_ENABLE
4530 			      | HAL_TXQ_TXEOLINT_ENABLE
4531 			      ;
4532 		qi.tqi_aifs = wmep->wmep_aifsn;
4533 		qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
4534 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
4535 		qi.tqi_readyTime = 0;
4536 		qi.tqi_burstTime = ATH_TXOP_TO_US(wmep->wmep_txopLimit);
4537 #ifdef IEEE80211_SUPPORT_TDMA
4538 	}
4539 #endif
4540 
4541 	DPRINTF(sc, ATH_DEBUG_RESET,
4542 	    "%s: Q%u qflags 0x%x aifs %u cwmin %u cwmax %u burstTime %u\n",
4543 	    __func__, txq->axq_qnum, qi.tqi_qflags,
4544 	    qi.tqi_aifs, qi.tqi_cwmin, qi.tqi_cwmax, qi.tqi_burstTime);
4545 
4546 	if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
4547 		if_printf(ifp, "unable to update hardware queue "
4548 			"parameters for %s traffic!\n",
4549 			ieee80211_wme_acnames[ac]);
4550 		return 0;
4551 	} else {
4552 		ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
4553 		return 1;
4554 	}
4555 #undef ATH_TXOP_TO_US
4556 #undef ATH_EXPONENT_TO_VALUE
4557 }
4558 
4559 /*
4560  * Callback from the 802.11 layer to update WME parameters.
4561  */
4562 static int
4563 ath_wme_update(struct ieee80211com *ic)
4564 {
4565 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4566 
4567 	return !ath_txq_update(sc, WME_AC_BE) ||
4568 	    !ath_txq_update(sc, WME_AC_BK) ||
4569 	    !ath_txq_update(sc, WME_AC_VI) ||
4570 	    !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
4571 }
4572 
4573 /*
4574  * Reclaim resources for a setup queue.
4575  */
4576 static void
4577 ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
4578 {
4579 
4580 	ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
4581 	ATH_TXQ_LOCK_DESTROY(txq);
4582 	sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
4583 }
4584 
4585 /*
4586  * Reclaim all tx queue resources.
4587  */
4588 static void
4589 ath_tx_cleanup(struct ath_softc *sc)
4590 {
4591 	int i;
4592 
4593 	ATH_TXBUF_LOCK_DESTROY(sc);
4594 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4595 		if (ATH_TXQ_SETUP(sc, i))
4596 			ath_tx_cleanupq(sc, &sc->sc_txq[i]);
4597 }
4598 
4599 /*
4600  * Return h/w rate index for an IEEE rate (w/o basic rate bit)
4601  * using the current rates in sc_rixmap.
4602  */
4603 int
4604 ath_tx_findrix(const struct ath_softc *sc, uint8_t rate)
4605 {
4606 	int rix = sc->sc_rixmap[rate];
4607 	/* NB: return lowest rix for invalid rate */
4608 	return (rix == 0xff ? 0 : rix);
4609 }
4610 
4611 static void
4612 ath_tx_update_stats(struct ath_softc *sc, struct ath_tx_status *ts,
4613     struct ath_buf *bf)
4614 {
4615 	struct ieee80211_node *ni = bf->bf_node;
4616 	struct ifnet *ifp = sc->sc_ifp;
4617 	struct ieee80211com *ic = ifp->if_l2com;
4618 	int sr, lr, pri;
4619 
4620 	if (ts->ts_status == 0) {
4621 		u_int8_t txant = ts->ts_antenna;
4622 		sc->sc_stats.ast_ant_tx[txant]++;
4623 		sc->sc_ant_tx[txant]++;
4624 		if (ts->ts_finaltsi != 0)
4625 			sc->sc_stats.ast_tx_altrate++;
4626 		pri = M_WME_GETAC(bf->bf_m);
4627 		if (pri >= WME_AC_VO)
4628 			ic->ic_wme.wme_hipri_traffic++;
4629 		if ((bf->bf_txflags & HAL_TXDESC_NOACK) == 0)
4630 			ni->ni_inact = ni->ni_inact_reload;
4631 	} else {
4632 		if (ts->ts_status & HAL_TXERR_XRETRY)
4633 			sc->sc_stats.ast_tx_xretries++;
4634 		if (ts->ts_status & HAL_TXERR_FIFO)
4635 			sc->sc_stats.ast_tx_fifoerr++;
4636 		if (ts->ts_status & HAL_TXERR_FILT)
4637 			sc->sc_stats.ast_tx_filtered++;
4638 		if (ts->ts_status & HAL_TXERR_XTXOP)
4639 			sc->sc_stats.ast_tx_xtxop++;
4640 		if (ts->ts_status & HAL_TXERR_TIMER_EXPIRED)
4641 			sc->sc_stats.ast_tx_timerexpired++;
4642 
4643 		if (ts->ts_status & HAL_TX_DATA_UNDERRUN)
4644 			sc->sc_stats.ast_tx_data_underrun++;
4645 		if (ts->ts_status & HAL_TX_DELIM_UNDERRUN)
4646 			sc->sc_stats.ast_tx_delim_underrun++;
4647 
4648 		if (bf->bf_m->m_flags & M_FF)
4649 			sc->sc_stats.ast_ff_txerr++;
4650 	}
4651 	/* XXX when is this valid? */
4652 	if (ts->ts_status & HAL_TX_DESC_CFG_ERR)
4653 		sc->sc_stats.ast_tx_desccfgerr++;
4654 
4655 	sr = ts->ts_shortretry;
4656 	lr = ts->ts_longretry;
4657 	sc->sc_stats.ast_tx_shortretry += sr;
4658 	sc->sc_stats.ast_tx_longretry += lr;
4659 
4660 }
4661 
4662 /*
4663  * The default completion. If fail is 1, this means
4664  * "please don't retry the frame, and just return -1 status
4665  * to the net80211 stack.
4666  */
4667 void
4668 ath_tx_default_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
4669 {
4670 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
4671 	int st;
4672 
4673 	if (fail == 1)
4674 		st = -1;
4675 	else
4676 		st = ((bf->bf_txflags & HAL_TXDESC_NOACK) == 0) ?
4677 		    ts->ts_status : HAL_TXERR_XRETRY;
4678 
4679 	if (bf->bf_state.bfs_dobaw)
4680 		device_printf(sc->sc_dev,
4681 		    "%s: dobaw should've been cleared!\n", __func__);
4682 	if (bf->bf_next != NULL)
4683 		device_printf(sc->sc_dev,
4684 		    "%s: bf_next not NULL!\n", __func__);
4685 
4686 	/*
4687 	 * Do any tx complete callback.  Note this must
4688 	 * be done before releasing the node reference.
4689 	 * This will free the mbuf, release the net80211
4690 	 * node and recycle the ath_buf.
4691 	 */
4692 	ath_tx_freebuf(sc, bf, st);
4693 }
4694 
4695 /*
4696  * Update rate control with the given completion status.
4697  */
4698 void
4699 ath_tx_update_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
4700     struct ath_rc_series *rc, struct ath_tx_status *ts, int frmlen,
4701     int nframes, int nbad)
4702 {
4703 	struct ath_node *an;
4704 
4705 	/* Only for unicast frames */
4706 	if (ni == NULL)
4707 		return;
4708 
4709 	an = ATH_NODE(ni);
4710 
4711 	if ((ts->ts_status & HAL_TXERR_FILT) == 0) {
4712 		ATH_NODE_LOCK(an);
4713 		ath_rate_tx_complete(sc, an, rc, ts, frmlen, nframes, nbad);
4714 		ATH_NODE_UNLOCK(an);
4715 	}
4716 }
4717 
4718 /*
4719  * Update the busy status of the last frame on the free list.
4720  * When doing TDMA, the busy flag tracks whether the hardware
4721  * currently points to this buffer or not, and thus gated DMA
4722  * may restart by re-reading the last descriptor in this
4723  * buffer.
4724  *
4725  * This should be called in the completion function once one
4726  * of the buffers has been used.
4727  */
4728 static void
4729 ath_tx_update_busy(struct ath_softc *sc)
4730 {
4731 	struct ath_buf *last;
4732 
4733 	/*
4734 	 * Since the last frame may still be marked
4735 	 * as ATH_BUF_BUSY, unmark it here before
4736 	 * finishing the frame processing.
4737 	 * Since we've completed a frame (aggregate
4738 	 * or otherwise), the hardware has moved on
4739 	 * and is no longer referencing the previous
4740 	 * descriptor.
4741 	 */
4742 	ATH_TXBUF_LOCK_ASSERT(sc);
4743 	last = TAILQ_LAST(&sc->sc_txbuf, ath_bufhead_s);
4744 	if (last != NULL)
4745 		last->bf_flags &= ~ATH_BUF_BUSY;
4746 }
4747 
4748 
4749 /*
4750  * Process completed xmit descriptors from the specified queue.
4751  * Kick the packet scheduler if needed. This can occur from this
4752  * particular task.
4753  */
4754 static int
4755 ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq, int dosched)
4756 {
4757 	struct ath_hal *ah = sc->sc_ah;
4758 	struct ath_buf *bf;
4759 	struct ath_desc *ds;
4760 	struct ath_tx_status *ts;
4761 	struct ieee80211_node *ni;
4762 	struct ath_node *an;
4763 	int nacked;
4764 	HAL_STATUS status;
4765 
4766 	DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
4767 		__func__, txq->axq_qnum,
4768 		(caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
4769 		txq->axq_link);
4770 	nacked = 0;
4771 	for (;;) {
4772 		ATH_TXQ_LOCK(txq);
4773 		txq->axq_intrcnt = 0;	/* reset periodic desc intr count */
4774 		bf = TAILQ_FIRST(&txq->axq_q);
4775 		if (bf == NULL) {
4776 			ATH_TXQ_UNLOCK(txq);
4777 			break;
4778 		}
4779 		ds = bf->bf_lastds;	/* XXX must be setup correctly! */
4780 		ts = &bf->bf_status.ds_txstat;
4781 		status = ath_hal_txprocdesc(ah, ds, ts);
4782 #ifdef ATH_DEBUG
4783 		if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
4784 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
4785 			    status == HAL_OK);
4786 #endif
4787 		if (status == HAL_EINPROGRESS) {
4788 			ATH_TXQ_UNLOCK(txq);
4789 			break;
4790 		}
4791 		ATH_TXQ_REMOVE(txq, bf, bf_list);
4792 #ifdef IEEE80211_SUPPORT_TDMA
4793 		if (txq->axq_depth > 0) {
4794 			/*
4795 			 * More frames follow.  Mark the buffer busy
4796 			 * so it's not re-used while the hardware may
4797 			 * still re-read the link field in the descriptor.
4798 			 *
4799 			 * Use the last buffer in an aggregate as that
4800 			 * is where the hardware may be - intermediate
4801 			 * descriptors won't be "busy".
4802 			 */
4803 			bf->bf_last->bf_flags |= ATH_BUF_BUSY;
4804 		} else
4805 #else
4806 		if (txq->axq_depth == 0)
4807 #endif
4808 			txq->axq_link = NULL;
4809 		if (bf->bf_state.bfs_aggr)
4810 			txq->axq_aggr_depth--;
4811 
4812 		ni = bf->bf_node;
4813 		/*
4814 		 * If unicast frame was ack'd update RSSI,
4815 		 * including the last rx time used to
4816 		 * workaround phantom bmiss interrupts.
4817 		 */
4818 		if (ni != NULL && ts->ts_status == 0 &&
4819 		    ((bf->bf_txflags & HAL_TXDESC_NOACK) == 0)) {
4820 			nacked++;
4821 			sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
4822 			ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi,
4823 				ts->ts_rssi);
4824 		}
4825 		ATH_TXQ_UNLOCK(txq);
4826 
4827 		/* If unicast frame, update general statistics */
4828 		if (ni != NULL) {
4829 			an = ATH_NODE(ni);
4830 			/* update statistics */
4831 			ath_tx_update_stats(sc, ts, bf);
4832 		}
4833 
4834 		/*
4835 		 * Call the completion handler.
4836 		 * The completion handler is responsible for
4837 		 * calling the rate control code.
4838 		 *
4839 		 * Frames with no completion handler get the
4840 		 * rate control code called here.
4841 		 */
4842 		if (bf->bf_comp == NULL) {
4843 			if ((ts->ts_status & HAL_TXERR_FILT) == 0 &&
4844 			    (bf->bf_txflags & HAL_TXDESC_NOACK) == 0) {
4845 				/*
4846 				 * XXX assume this isn't an aggregate
4847 				 * frame.
4848 				 */
4849 				ath_tx_update_ratectrl(sc, ni,
4850 				     bf->bf_state.bfs_rc, ts,
4851 				    bf->bf_state.bfs_pktlen, 1,
4852 				    (ts->ts_status == 0 ? 0 : 1));
4853 			}
4854 			ath_tx_default_comp(sc, bf, 0);
4855 		} else
4856 			bf->bf_comp(sc, bf, 0);
4857 	}
4858 #ifdef IEEE80211_SUPPORT_SUPERG
4859 	/*
4860 	 * Flush fast-frame staging queue when traffic slows.
4861 	 */
4862 	if (txq->axq_depth <= 1)
4863 		ieee80211_ff_flush(ic, txq->axq_ac);
4864 #endif
4865 
4866 	/* Kick the TXQ scheduler */
4867 	if (dosched) {
4868 		ATH_TXQ_LOCK(txq);
4869 		ath_txq_sched(sc, txq);
4870 		ATH_TXQ_UNLOCK(txq);
4871 	}
4872 
4873 	return nacked;
4874 }
4875 
4876 #define	TXQACTIVE(t, q)		( (t) & (1 << (q)))
4877 
4878 /*
4879  * Deferred processing of transmit interrupt; special-cased
4880  * for a single hardware transmit queue (e.g. 5210 and 5211).
4881  */
4882 static void
4883 ath_tx_proc_q0(void *arg, int npending)
4884 {
4885 	struct ath_softc *sc = arg;
4886 	struct ifnet *ifp = sc->sc_ifp;
4887 	uint32_t txqs;
4888 
4889 	ATH_PCU_LOCK(sc);
4890 	sc->sc_txproc_cnt++;
4891 	txqs = sc->sc_txq_active;
4892 	sc->sc_txq_active &= ~txqs;
4893 	ATH_PCU_UNLOCK(sc);
4894 
4895 	if (TXQACTIVE(txqs, 0) && ath_tx_processq(sc, &sc->sc_txq[0], 1))
4896 		/* XXX why is lastrx updated in tx code? */
4897 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4898 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
4899 		ath_tx_processq(sc, sc->sc_cabq, 1);
4900 	/* XXX check this inside of IF_LOCK? */
4901 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4902 	sc->sc_wd_timer = 0;
4903 
4904 	if (sc->sc_softled)
4905 		ath_led_event(sc, sc->sc_txrix);
4906 
4907 	ATH_PCU_LOCK(sc);
4908 	sc->sc_txproc_cnt--;
4909 	ATH_PCU_UNLOCK(sc);
4910 
4911 	ath_start(ifp);
4912 }
4913 
4914 /*
4915  * Deferred processing of transmit interrupt; special-cased
4916  * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
4917  */
4918 static void
4919 ath_tx_proc_q0123(void *arg, int npending)
4920 {
4921 	struct ath_softc *sc = arg;
4922 	struct ifnet *ifp = sc->sc_ifp;
4923 	int nacked;
4924 	uint32_t txqs;
4925 
4926 	ATH_PCU_LOCK(sc);
4927 	sc->sc_txproc_cnt++;
4928 	txqs = sc->sc_txq_active;
4929 	sc->sc_txq_active &= ~txqs;
4930 	ATH_PCU_UNLOCK(sc);
4931 
4932 	/*
4933 	 * Process each active queue.
4934 	 */
4935 	nacked = 0;
4936 	if (TXQACTIVE(txqs, 0))
4937 		nacked += ath_tx_processq(sc, &sc->sc_txq[0], 1);
4938 	if (TXQACTIVE(txqs, 1))
4939 		nacked += ath_tx_processq(sc, &sc->sc_txq[1], 1);
4940 	if (TXQACTIVE(txqs, 2))
4941 		nacked += ath_tx_processq(sc, &sc->sc_txq[2], 1);
4942 	if (TXQACTIVE(txqs, 3))
4943 		nacked += ath_tx_processq(sc, &sc->sc_txq[3], 1);
4944 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
4945 		ath_tx_processq(sc, sc->sc_cabq, 1);
4946 	if (nacked)
4947 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4948 
4949 	/* XXX check this inside of IF_LOCK? */
4950 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4951 	sc->sc_wd_timer = 0;
4952 
4953 	if (sc->sc_softled)
4954 		ath_led_event(sc, sc->sc_txrix);
4955 
4956 	ATH_PCU_LOCK(sc);
4957 	sc->sc_txproc_cnt--;
4958 	ATH_PCU_UNLOCK(sc);
4959 
4960 	ath_start(ifp);
4961 }
4962 
4963 /*
4964  * Deferred processing of transmit interrupt.
4965  */
4966 static void
4967 ath_tx_proc(void *arg, int npending)
4968 {
4969 	struct ath_softc *sc = arg;
4970 	struct ifnet *ifp = sc->sc_ifp;
4971 	int i, nacked;
4972 	uint32_t txqs;
4973 
4974 	ATH_PCU_LOCK(sc);
4975 	sc->sc_txproc_cnt++;
4976 	txqs = sc->sc_txq_active;
4977 	sc->sc_txq_active &= ~txqs;
4978 	ATH_PCU_UNLOCK(sc);
4979 
4980 	/*
4981 	 * Process each active queue.
4982 	 */
4983 	nacked = 0;
4984 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4985 		if (ATH_TXQ_SETUP(sc, i) && TXQACTIVE(txqs, i))
4986 			nacked += ath_tx_processq(sc, &sc->sc_txq[i], 1);
4987 	if (nacked)
4988 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4989 
4990 	/* XXX check this inside of IF_LOCK? */
4991 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4992 	sc->sc_wd_timer = 0;
4993 
4994 	if (sc->sc_softled)
4995 		ath_led_event(sc, sc->sc_txrix);
4996 
4997 	ATH_PCU_LOCK(sc);
4998 	sc->sc_txproc_cnt--;
4999 	ATH_PCU_UNLOCK(sc);
5000 
5001 	ath_start(ifp);
5002 }
5003 #undef	TXQACTIVE
5004 
5005 /*
5006  * Return a buffer to the pool and update the 'busy' flag on the
5007  * previous 'tail' entry.
5008  *
5009  * This _must_ only be called when the buffer is involved in a completed
5010  * TX. The logic is that if it was part of an active TX, the previous
5011  * buffer on the list is now not involved in a halted TX DMA queue, waiting
5012  * for restart (eg for TDMA.)
5013  *
5014  * The caller must free the mbuf and recycle the node reference.
5015  */
5016 void
5017 ath_freebuf(struct ath_softc *sc, struct ath_buf *bf)
5018 {
5019 	bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
5020 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_POSTWRITE);
5021 
5022 	KASSERT((bf->bf_node == NULL), ("%s: bf->bf_node != NULL\n", __func__));
5023 	KASSERT((bf->bf_m == NULL), ("%s: bf->bf_m != NULL\n", __func__));
5024 
5025 	ATH_TXBUF_LOCK(sc);
5026 	ath_tx_update_busy(sc);
5027 	TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
5028 	ATH_TXBUF_UNLOCK(sc);
5029 }
5030 
5031 /*
5032  * This is currently used by ath_tx_draintxq() and
5033  * ath_tx_tid_free_pkts().
5034  *
5035  * It recycles a single ath_buf.
5036  */
5037 void
5038 ath_tx_freebuf(struct ath_softc *sc, struct ath_buf *bf, int status)
5039 {
5040 	struct ieee80211_node *ni = bf->bf_node;
5041 	struct mbuf *m0 = bf->bf_m;
5042 
5043 	bf->bf_node = NULL;
5044 	bf->bf_m = NULL;
5045 
5046 	/* Free the buffer, it's not needed any longer */
5047 	ath_freebuf(sc, bf);
5048 
5049 	if (ni != NULL) {
5050 		/*
5051 		 * Do any callback and reclaim the node reference.
5052 		 */
5053 		if (m0->m_flags & M_TXCB)
5054 			ieee80211_process_callback(ni, m0, status);
5055 		ieee80211_free_node(ni);
5056 	}
5057 	m_freem(m0);
5058 
5059 	/*
5060 	 * XXX the buffer used to be freed -after-, but the DMA map was
5061 	 * freed where ath_freebuf() now is. I've no idea what this
5062 	 * will do.
5063 	 */
5064 }
5065 
5066 void
5067 ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
5068 {
5069 #ifdef ATH_DEBUG
5070 	struct ath_hal *ah = sc->sc_ah;
5071 #endif
5072 	struct ath_buf *bf;
5073 	u_int ix;
5074 
5075 	/*
5076 	 * NB: this assumes output has been stopped and
5077 	 *     we do not need to block ath_tx_proc
5078 	 */
5079 	ATH_TXBUF_LOCK(sc);
5080 	bf = TAILQ_LAST(&sc->sc_txbuf, ath_bufhead_s);
5081 	if (bf != NULL)
5082 		bf->bf_flags &= ~ATH_BUF_BUSY;
5083 	ATH_TXBUF_UNLOCK(sc);
5084 
5085 	for (ix = 0;; ix++) {
5086 		ATH_TXQ_LOCK(txq);
5087 		bf = TAILQ_FIRST(&txq->axq_q);
5088 		if (bf == NULL) {
5089 			txq->axq_link = NULL;
5090 			ATH_TXQ_UNLOCK(txq);
5091 			break;
5092 		}
5093 		ATH_TXQ_REMOVE(txq, bf, bf_list);
5094 		if (bf->bf_state.bfs_aggr)
5095 			txq->axq_aggr_depth--;
5096 #ifdef ATH_DEBUG
5097 		if (sc->sc_debug & ATH_DEBUG_RESET) {
5098 			struct ieee80211com *ic = sc->sc_ifp->if_l2com;
5099 
5100 			ath_printtxbuf(sc, bf, txq->axq_qnum, ix,
5101 				ath_hal_txprocdesc(ah, bf->bf_lastds,
5102 				    &bf->bf_status.ds_txstat) == HAL_OK);
5103 			ieee80211_dump_pkt(ic, mtod(bf->bf_m, const uint8_t *),
5104 			    bf->bf_m->m_len, 0, -1);
5105 		}
5106 #endif /* ATH_DEBUG */
5107 		/*
5108 		 * Since we're now doing magic in the completion
5109 		 * functions, we -must- call it for aggregation
5110 		 * destinations or BAW tracking will get upset.
5111 		 */
5112 		/*
5113 		 * Clear ATH_BUF_BUSY; the completion handler
5114 		 * will free the buffer.
5115 		 */
5116 		ATH_TXQ_UNLOCK(txq);
5117 		bf->bf_flags &= ~ATH_BUF_BUSY;
5118 		if (bf->bf_comp)
5119 			bf->bf_comp(sc, bf, 1);
5120 		else
5121 			ath_tx_default_comp(sc, bf, 1);
5122 	}
5123 
5124 	/*
5125 	 * Drain software queued frames which are on
5126 	 * active TIDs.
5127 	 */
5128 	ath_tx_txq_drain(sc, txq);
5129 }
5130 
5131 static void
5132 ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
5133 {
5134 	struct ath_hal *ah = sc->sc_ah;
5135 
5136 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
5137 	    __func__, txq->axq_qnum,
5138 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
5139 	    txq->axq_link);
5140 	(void) ath_hal_stoptxdma(ah, txq->axq_qnum);
5141 }
5142 
5143 static int
5144 ath_stoptxdma(struct ath_softc *sc)
5145 {
5146 	struct ath_hal *ah = sc->sc_ah;
5147 	int i;
5148 
5149 	/* XXX return value */
5150 	if (sc->sc_invalid)
5151 		return 0;
5152 
5153 	if (!sc->sc_invalid) {
5154 		/* don't touch the hardware if marked invalid */
5155 		DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
5156 		    __func__, sc->sc_bhalq,
5157 		    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq),
5158 		    NULL);
5159 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
5160 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
5161 			if (ATH_TXQ_SETUP(sc, i))
5162 				ath_tx_stopdma(sc, &sc->sc_txq[i]);
5163 	}
5164 
5165 	return 1;
5166 }
5167 
5168 /*
5169  * Drain the transmit queues and reclaim resources.
5170  */
5171 static void
5172 ath_draintxq(struct ath_softc *sc, ATH_RESET_TYPE reset_type)
5173 {
5174 #ifdef	ATH_DEBUG
5175 	struct ath_hal *ah = sc->sc_ah;
5176 #endif
5177 	struct ifnet *ifp = sc->sc_ifp;
5178 	int i;
5179 
5180 	(void) ath_stoptxdma(sc);
5181 
5182 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
5183 		/*
5184 		 * XXX TODO: should we just handle the completed TX frames
5185 		 * here, whether or not the reset is a full one or not?
5186 		 */
5187 		if (ATH_TXQ_SETUP(sc, i)) {
5188 			if (reset_type == ATH_RESET_NOLOSS)
5189 				ath_tx_processq(sc, &sc->sc_txq[i], 0);
5190 			else
5191 				ath_tx_draintxq(sc, &sc->sc_txq[i]);
5192 		}
5193 	}
5194 #ifdef ATH_DEBUG
5195 	if (sc->sc_debug & ATH_DEBUG_RESET) {
5196 		struct ath_buf *bf = TAILQ_FIRST(&sc->sc_bbuf);
5197 		if (bf != NULL && bf->bf_m != NULL) {
5198 			ath_printtxbuf(sc, bf, sc->sc_bhalq, 0,
5199 				ath_hal_txprocdesc(ah, bf->bf_lastds,
5200 				    &bf->bf_status.ds_txstat) == HAL_OK);
5201 			ieee80211_dump_pkt(ifp->if_l2com,
5202 			    mtod(bf->bf_m, const uint8_t *), bf->bf_m->m_len,
5203 			    0, -1);
5204 		}
5205 	}
5206 #endif /* ATH_DEBUG */
5207 	/* XXX check this inside of IF_LOCK? */
5208 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5209 	sc->sc_wd_timer = 0;
5210 }
5211 
5212 /*
5213  * Disable the receive h/w in preparation for a reset.
5214  */
5215 static void
5216 ath_stoprecv(struct ath_softc *sc, int dodelay)
5217 {
5218 #define	PA2DESC(_sc, _pa) \
5219 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
5220 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
5221 	struct ath_hal *ah = sc->sc_ah;
5222 
5223 	ath_hal_stoppcurecv(ah);	/* disable PCU */
5224 	ath_hal_setrxfilter(ah, 0);	/* clear recv filter */
5225 	ath_hal_stopdmarecv(ah);	/* disable DMA engine */
5226 	if (dodelay)
5227 		DELAY(3000);		/* 3ms is long enough for 1 frame */
5228 #ifdef ATH_DEBUG
5229 	if (sc->sc_debug & (ATH_DEBUG_RESET | ATH_DEBUG_FATAL)) {
5230 		struct ath_buf *bf;
5231 		u_int ix;
5232 
5233 		printf("%s: rx queue %p, link %p\n", __func__,
5234 			(caddr_t)(uintptr_t) ath_hal_getrxbuf(ah), sc->sc_rxlink);
5235 		ix = 0;
5236 		TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
5237 			struct ath_desc *ds = bf->bf_desc;
5238 			struct ath_rx_status *rs = &bf->bf_status.ds_rxstat;
5239 			HAL_STATUS status = ath_hal_rxprocdesc(ah, ds,
5240 				bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
5241 			if (status == HAL_OK || (sc->sc_debug & ATH_DEBUG_FATAL))
5242 				ath_printrxbuf(sc, bf, ix, status == HAL_OK);
5243 			ix++;
5244 		}
5245 	}
5246 #endif
5247 	if (sc->sc_rxpending != NULL) {
5248 		m_freem(sc->sc_rxpending);
5249 		sc->sc_rxpending = NULL;
5250 	}
5251 	sc->sc_rxlink = NULL;		/* just in case */
5252 #undef PA2DESC
5253 }
5254 
5255 /*
5256  * Enable the receive h/w following a reset.
5257  */
5258 static int
5259 ath_startrecv(struct ath_softc *sc)
5260 {
5261 	struct ath_hal *ah = sc->sc_ah;
5262 	struct ath_buf *bf;
5263 
5264 	sc->sc_rxlink = NULL;
5265 	sc->sc_rxpending = NULL;
5266 	TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
5267 		int error = ath_rxbuf_init(sc, bf);
5268 		if (error != 0) {
5269 			DPRINTF(sc, ATH_DEBUG_RECV,
5270 				"%s: ath_rxbuf_init failed %d\n",
5271 				__func__, error);
5272 			return error;
5273 		}
5274 	}
5275 
5276 	bf = TAILQ_FIRST(&sc->sc_rxbuf);
5277 	ath_hal_putrxbuf(ah, bf->bf_daddr);
5278 	ath_hal_rxena(ah);		/* enable recv descriptors */
5279 	ath_mode_init(sc);		/* set filters, etc. */
5280 	ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
5281 	return 0;
5282 }
5283 
5284 /*
5285  * Update internal state after a channel change.
5286  */
5287 static void
5288 ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
5289 {
5290 	enum ieee80211_phymode mode;
5291 
5292 	/*
5293 	 * Change channels and update the h/w rate map
5294 	 * if we're switching; e.g. 11a to 11b/g.
5295 	 */
5296 	mode = ieee80211_chan2mode(chan);
5297 	if (mode != sc->sc_curmode)
5298 		ath_setcurmode(sc, mode);
5299 	sc->sc_curchan = chan;
5300 }
5301 
5302 /*
5303  * Set/change channels.  If the channel is really being changed,
5304  * it's done by resetting the chip.  To accomplish this we must
5305  * first cleanup any pending DMA, then restart stuff after a la
5306  * ath_init.
5307  */
5308 static int
5309 ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
5310 {
5311 	struct ifnet *ifp = sc->sc_ifp;
5312 	struct ieee80211com *ic = ifp->if_l2com;
5313 	struct ath_hal *ah = sc->sc_ah;
5314 	int ret = 0;
5315 	int dointr = 0;
5316 
5317 	/* Treat this as an interface reset */
5318 	ATH_PCU_LOCK(sc);
5319 	if (ath_reset_grablock(sc, 1) == 0) {
5320 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
5321 		    __func__);
5322 	}
5323 	if (chan != sc->sc_curchan) {
5324 		dointr = 1;
5325 		/* XXX only do this if inreset_cnt is 1? */
5326 		ath_hal_intrset(ah, 0);
5327 	}
5328 	ATH_PCU_UNLOCK(sc);
5329 	ath_txrx_stop(sc);
5330 
5331 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz, flags 0x%x)\n",
5332 	    __func__, ieee80211_chan2ieee(ic, chan),
5333 	    chan->ic_freq, chan->ic_flags);
5334 	if (chan != sc->sc_curchan) {
5335 		HAL_STATUS status;
5336 		/*
5337 		 * To switch channels clear any pending DMA operations;
5338 		 * wait long enough for the RX fifo to drain, reset the
5339 		 * hardware at the new frequency, and then re-enable
5340 		 * the relevant bits of the h/w.
5341 		 */
5342 #if 0
5343 		ath_hal_intrset(ah, 0);		/* disable interrupts */
5344 #endif
5345 		ath_stoprecv(sc, 1);		/* turn off frame recv */
5346 		/*
5347 		 * First, handle completed TX/RX frames.
5348 		 */
5349 		ath_rx_proc(sc, 0);
5350 		ath_draintxq(sc, ATH_RESET_NOLOSS);
5351 		/*
5352 		 * Next, flush the non-scheduled frames.
5353 		 */
5354 		ath_draintxq(sc, ATH_RESET_FULL);	/* clear pending tx frames */
5355 
5356 		if (!ath_hal_reset(ah, sc->sc_opmode, chan, AH_TRUE, &status)) {
5357 			if_printf(ifp, "%s: unable to reset "
5358 			    "channel %u (%u MHz, flags 0x%x), hal status %u\n",
5359 			    __func__, ieee80211_chan2ieee(ic, chan),
5360 			    chan->ic_freq, chan->ic_flags, status);
5361 			ret = EIO;
5362 			goto finish;
5363 		}
5364 		sc->sc_diversity = ath_hal_getdiversity(ah);
5365 
5366 		/* Let DFS at it in case it's a DFS channel */
5367 		ath_dfs_radar_enable(sc, ic->ic_curchan);
5368 
5369 		/*
5370 		 * Re-enable rx framework.
5371 		 */
5372 		if (ath_startrecv(sc) != 0) {
5373 			if_printf(ifp, "%s: unable to restart recv logic\n",
5374 			    __func__);
5375 			ret = EIO;
5376 			goto finish;
5377 		}
5378 
5379 		/*
5380 		 * Change channels and update the h/w rate map
5381 		 * if we're switching; e.g. 11a to 11b/g.
5382 		 */
5383 		ath_chan_change(sc, chan);
5384 
5385 		/*
5386 		 * Reset clears the beacon timers; reset them
5387 		 * here if needed.
5388 		 */
5389 		if (sc->sc_beacons) {		/* restart beacons */
5390 #ifdef IEEE80211_SUPPORT_TDMA
5391 			if (sc->sc_tdma)
5392 				ath_tdma_config(sc, NULL);
5393 			else
5394 #endif
5395 			ath_beacon_config(sc, NULL);
5396 		}
5397 
5398 #if 0
5399 		/*
5400 		 * Re-enable interrupts.
5401 		 */
5402 		ath_hal_intrset(ah, sc->sc_imask);
5403 #endif
5404 	}
5405 
5406 finish:
5407 	ATH_PCU_LOCK(sc);
5408 	sc->sc_inreset_cnt--;
5409 	/* XXX only do this if sc_inreset_cnt == 0? */
5410 	if (dointr)
5411 		ath_hal_intrset(ah, sc->sc_imask);
5412 	ATH_PCU_UNLOCK(sc);
5413 
5414 	/* XXX do this inside of IF_LOCK? */
5415 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5416 	ath_txrx_start(sc);
5417 	/* XXX ath_start? */
5418 
5419 	return ret;
5420 }
5421 
5422 /*
5423  * Periodically recalibrate the PHY to account
5424  * for temperature/environment changes.
5425  */
5426 static void
5427 ath_calibrate(void *arg)
5428 {
5429 	struct ath_softc *sc = arg;
5430 	struct ath_hal *ah = sc->sc_ah;
5431 	struct ifnet *ifp = sc->sc_ifp;
5432 	struct ieee80211com *ic = ifp->if_l2com;
5433 	HAL_BOOL longCal, isCalDone;
5434 	HAL_BOOL aniCal, shortCal = AH_FALSE;
5435 	int nextcal;
5436 
5437 	if (ic->ic_flags & IEEE80211_F_SCAN)	/* defer, off channel */
5438 		goto restart;
5439 	longCal = (ticks - sc->sc_lastlongcal >= ath_longcalinterval*hz);
5440 	aniCal = (ticks - sc->sc_lastani >= ath_anicalinterval*hz/1000);
5441 	if (sc->sc_doresetcal)
5442 		shortCal = (ticks - sc->sc_lastshortcal >= ath_shortcalinterval*hz/1000);
5443 
5444 	DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: shortCal=%d; longCal=%d; aniCal=%d\n", __func__, shortCal, longCal, aniCal);
5445 	if (aniCal) {
5446 		sc->sc_stats.ast_ani_cal++;
5447 		sc->sc_lastani = ticks;
5448 		ath_hal_ani_poll(ah, sc->sc_curchan);
5449 	}
5450 
5451 	if (longCal) {
5452 		sc->sc_stats.ast_per_cal++;
5453 		sc->sc_lastlongcal = ticks;
5454 		if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
5455 			/*
5456 			 * Rfgain is out of bounds, reset the chip
5457 			 * to load new gain values.
5458 			 */
5459 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
5460 				"%s: rfgain change\n", __func__);
5461 			sc->sc_stats.ast_per_rfgain++;
5462 			/*
5463 			 * Drop lock - we can't hold it across the
5464 			 * ath_reset() call. Instead, we'll drop
5465 			 * out here, do a reset, then reschedule
5466 			 * the callout.
5467 			 */
5468 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
5469 			sc->sc_resetcal = 0;
5470 			sc->sc_doresetcal = AH_TRUE;
5471 			ATH_UNLOCK(sc);
5472 			ath_reset(ifp, ATH_RESET_NOLOSS);
5473 			ATH_LOCK(sc);
5474 			return;
5475 		}
5476 		/*
5477 		 * If this long cal is after an idle period, then
5478 		 * reset the data collection state so we start fresh.
5479 		 */
5480 		if (sc->sc_resetcal) {
5481 			(void) ath_hal_calreset(ah, sc->sc_curchan);
5482 			sc->sc_lastcalreset = ticks;
5483 			sc->sc_lastshortcal = ticks;
5484 			sc->sc_resetcal = 0;
5485 			sc->sc_doresetcal = AH_TRUE;
5486 		}
5487 	}
5488 
5489 	/* Only call if we're doing a short/long cal, not for ANI calibration */
5490 	if (shortCal || longCal) {
5491 		if (ath_hal_calibrateN(ah, sc->sc_curchan, longCal, &isCalDone)) {
5492 			if (longCal) {
5493 				/*
5494 				 * Calibrate noise floor data again in case of change.
5495 				 */
5496 				ath_hal_process_noisefloor(ah);
5497 			}
5498 		} else {
5499 			DPRINTF(sc, ATH_DEBUG_ANY,
5500 				"%s: calibration of channel %u failed\n",
5501 				__func__, sc->sc_curchan->ic_freq);
5502 			sc->sc_stats.ast_per_calfail++;
5503 		}
5504 		if (shortCal)
5505 			sc->sc_lastshortcal = ticks;
5506 	}
5507 	if (!isCalDone) {
5508 restart:
5509 		/*
5510 		 * Use a shorter interval to potentially collect multiple
5511 		 * data samples required to complete calibration.  Once
5512 		 * we're told the work is done we drop back to a longer
5513 		 * interval between requests.  We're more aggressive doing
5514 		 * work when operating as an AP to improve operation right
5515 		 * after startup.
5516 		 */
5517 		sc->sc_lastshortcal = ticks;
5518 		nextcal = ath_shortcalinterval*hz/1000;
5519 		if (sc->sc_opmode != HAL_M_HOSTAP)
5520 			nextcal *= 10;
5521 		sc->sc_doresetcal = AH_TRUE;
5522 	} else {
5523 		/* nextcal should be the shortest time for next event */
5524 		nextcal = ath_longcalinterval*hz;
5525 		if (sc->sc_lastcalreset == 0)
5526 			sc->sc_lastcalreset = sc->sc_lastlongcal;
5527 		else if (ticks - sc->sc_lastcalreset >= ath_resetcalinterval*hz)
5528 			sc->sc_resetcal = 1;	/* setup reset next trip */
5529 		sc->sc_doresetcal = AH_FALSE;
5530 	}
5531 	/* ANI calibration may occur more often than short/long/resetcal */
5532 	if (ath_anicalinterval > 0)
5533 		nextcal = MIN(nextcal, ath_anicalinterval*hz/1000);
5534 
5535 	if (nextcal != 0) {
5536 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: next +%u (%sisCalDone)\n",
5537 		    __func__, nextcal, isCalDone ? "" : "!");
5538 		callout_reset(&sc->sc_cal_ch, nextcal, ath_calibrate, sc);
5539 	} else {
5540 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: calibration disabled\n",
5541 		    __func__);
5542 		/* NB: don't rearm timer */
5543 	}
5544 }
5545 
5546 static void
5547 ath_scan_start(struct ieee80211com *ic)
5548 {
5549 	struct ifnet *ifp = ic->ic_ifp;
5550 	struct ath_softc *sc = ifp->if_softc;
5551 	struct ath_hal *ah = sc->sc_ah;
5552 	u_int32_t rfilt;
5553 
5554 	/* XXX calibration timer? */
5555 
5556 	sc->sc_scanning = 1;
5557 	sc->sc_syncbeacon = 0;
5558 	rfilt = ath_calcrxfilter(sc);
5559 	ath_hal_setrxfilter(ah, rfilt);
5560 	ath_hal_setassocid(ah, ifp->if_broadcastaddr, 0);
5561 
5562 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0\n",
5563 		 __func__, rfilt, ether_sprintf(ifp->if_broadcastaddr));
5564 }
5565 
5566 static void
5567 ath_scan_end(struct ieee80211com *ic)
5568 {
5569 	struct ifnet *ifp = ic->ic_ifp;
5570 	struct ath_softc *sc = ifp->if_softc;
5571 	struct ath_hal *ah = sc->sc_ah;
5572 	u_int32_t rfilt;
5573 
5574 	sc->sc_scanning = 0;
5575 	rfilt = ath_calcrxfilter(sc);
5576 	ath_hal_setrxfilter(ah, rfilt);
5577 	ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
5578 
5579 	ath_hal_process_noisefloor(ah);
5580 
5581 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
5582 		 __func__, rfilt, ether_sprintf(sc->sc_curbssid),
5583 		 sc->sc_curaid);
5584 }
5585 
5586 static void
5587 ath_set_channel(struct ieee80211com *ic)
5588 {
5589 	struct ifnet *ifp = ic->ic_ifp;
5590 	struct ath_softc *sc = ifp->if_softc;
5591 
5592 	(void) ath_chan_set(sc, ic->ic_curchan);
5593 	/*
5594 	 * If we are returning to our bss channel then mark state
5595 	 * so the next recv'd beacon's tsf will be used to sync the
5596 	 * beacon timers.  Note that since we only hear beacons in
5597 	 * sta/ibss mode this has no effect in other operating modes.
5598 	 */
5599 	if (!sc->sc_scanning && ic->ic_curchan == ic->ic_bsschan)
5600 		sc->sc_syncbeacon = 1;
5601 }
5602 
5603 /*
5604  * Walk the vap list and check if there any vap's in RUN state.
5605  */
5606 static int
5607 ath_isanyrunningvaps(struct ieee80211vap *this)
5608 {
5609 	struct ieee80211com *ic = this->iv_ic;
5610 	struct ieee80211vap *vap;
5611 
5612 	IEEE80211_LOCK_ASSERT(ic);
5613 
5614 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
5615 		if (vap != this && vap->iv_state >= IEEE80211_S_RUN)
5616 			return 1;
5617 	}
5618 	return 0;
5619 }
5620 
5621 static int
5622 ath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
5623 {
5624 	struct ieee80211com *ic = vap->iv_ic;
5625 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5626 	struct ath_vap *avp = ATH_VAP(vap);
5627 	struct ath_hal *ah = sc->sc_ah;
5628 	struct ieee80211_node *ni = NULL;
5629 	int i, error, stamode;
5630 	u_int32_t rfilt;
5631 	int csa_run_transition = 0;
5632 	static const HAL_LED_STATE leds[] = {
5633 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
5634 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
5635 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
5636 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
5637 	    HAL_LED_RUN, 	/* IEEE80211_S_CAC */
5638 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
5639 	    HAL_LED_RUN, 	/* IEEE80211_S_CSA */
5640 	    HAL_LED_RUN, 	/* IEEE80211_S_SLEEP */
5641 	};
5642 
5643 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
5644 		ieee80211_state_name[vap->iv_state],
5645 		ieee80211_state_name[nstate]);
5646 
5647 	if (vap->iv_state == IEEE80211_S_CSA && nstate == IEEE80211_S_RUN)
5648 		csa_run_transition = 1;
5649 
5650 	callout_drain(&sc->sc_cal_ch);
5651 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
5652 
5653 	if (nstate == IEEE80211_S_SCAN) {
5654 		/*
5655 		 * Scanning: turn off beacon miss and don't beacon.
5656 		 * Mark beacon state so when we reach RUN state we'll
5657 		 * [re]setup beacons.  Unblock the task q thread so
5658 		 * deferred interrupt processing is done.
5659 		 */
5660 		ath_hal_intrset(ah,
5661 		    sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
5662 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5663 		sc->sc_beacons = 0;
5664 		taskqueue_unblock(sc->sc_tq);
5665 	}
5666 
5667 	ni = vap->iv_bss;
5668 	rfilt = ath_calcrxfilter(sc);
5669 	stamode = (vap->iv_opmode == IEEE80211_M_STA ||
5670 		   vap->iv_opmode == IEEE80211_M_AHDEMO ||
5671 		   vap->iv_opmode == IEEE80211_M_IBSS);
5672 	if (stamode && nstate == IEEE80211_S_RUN) {
5673 		sc->sc_curaid = ni->ni_associd;
5674 		IEEE80211_ADDR_COPY(sc->sc_curbssid, ni->ni_bssid);
5675 		ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
5676 	}
5677 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
5678 	   __func__, rfilt, ether_sprintf(sc->sc_curbssid), sc->sc_curaid);
5679 	ath_hal_setrxfilter(ah, rfilt);
5680 
5681 	/* XXX is this to restore keycache on resume? */
5682 	if (vap->iv_opmode != IEEE80211_M_STA &&
5683 	    (vap->iv_flags & IEEE80211_F_PRIVACY)) {
5684 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
5685 			if (ath_hal_keyisvalid(ah, i))
5686 				ath_hal_keysetmac(ah, i, ni->ni_bssid);
5687 	}
5688 
5689 	/*
5690 	 * Invoke the parent method to do net80211 work.
5691 	 */
5692 	error = avp->av_newstate(vap, nstate, arg);
5693 	if (error != 0)
5694 		goto bad;
5695 
5696 	if (nstate == IEEE80211_S_RUN) {
5697 		/* NB: collect bss node again, it may have changed */
5698 		ni = vap->iv_bss;
5699 
5700 		DPRINTF(sc, ATH_DEBUG_STATE,
5701 		    "%s(RUN): iv_flags 0x%08x bintvl %d bssid %s "
5702 		    "capinfo 0x%04x chan %d\n", __func__,
5703 		    vap->iv_flags, ni->ni_intval, ether_sprintf(ni->ni_bssid),
5704 		    ni->ni_capinfo, ieee80211_chan2ieee(ic, ic->ic_curchan));
5705 
5706 		switch (vap->iv_opmode) {
5707 #ifdef IEEE80211_SUPPORT_TDMA
5708 		case IEEE80211_M_AHDEMO:
5709 			if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
5710 				break;
5711 			/* fall thru... */
5712 #endif
5713 		case IEEE80211_M_HOSTAP:
5714 		case IEEE80211_M_IBSS:
5715 		case IEEE80211_M_MBSS:
5716 			/*
5717 			 * Allocate and setup the beacon frame.
5718 			 *
5719 			 * Stop any previous beacon DMA.  This may be
5720 			 * necessary, for example, when an ibss merge
5721 			 * causes reconfiguration; there will be a state
5722 			 * transition from RUN->RUN that means we may
5723 			 * be called with beacon transmission active.
5724 			 */
5725 			ath_hal_stoptxdma(ah, sc->sc_bhalq);
5726 
5727 			error = ath_beacon_alloc(sc, ni);
5728 			if (error != 0)
5729 				goto bad;
5730 			/*
5731 			 * If joining an adhoc network defer beacon timer
5732 			 * configuration to the next beacon frame so we
5733 			 * have a current TSF to use.  Otherwise we're
5734 			 * starting an ibss/bss so there's no need to delay;
5735 			 * if this is the first vap moving to RUN state, then
5736 			 * beacon state needs to be [re]configured.
5737 			 */
5738 			if (vap->iv_opmode == IEEE80211_M_IBSS &&
5739 			    ni->ni_tstamp.tsf != 0) {
5740 				sc->sc_syncbeacon = 1;
5741 			} else if (!sc->sc_beacons) {
5742 #ifdef IEEE80211_SUPPORT_TDMA
5743 				if (vap->iv_caps & IEEE80211_C_TDMA)
5744 					ath_tdma_config(sc, vap);
5745 				else
5746 #endif
5747 					ath_beacon_config(sc, vap);
5748 				sc->sc_beacons = 1;
5749 			}
5750 			break;
5751 		case IEEE80211_M_STA:
5752 			/*
5753 			 * Defer beacon timer configuration to the next
5754 			 * beacon frame so we have a current TSF to use
5755 			 * (any TSF collected when scanning is likely old).
5756 			 * However if it's due to a CSA -> RUN transition,
5757 			 * force a beacon update so we pick up a lack of
5758 			 * beacons from an AP in CAC and thus force a
5759 			 * scan.
5760 			 */
5761 			sc->sc_syncbeacon = 1;
5762 			if (csa_run_transition)
5763 				ath_beacon_config(sc, vap);
5764 			break;
5765 		case IEEE80211_M_MONITOR:
5766 			/*
5767 			 * Monitor mode vaps have only INIT->RUN and RUN->RUN
5768 			 * transitions so we must re-enable interrupts here to
5769 			 * handle the case of a single monitor mode vap.
5770 			 */
5771 			ath_hal_intrset(ah, sc->sc_imask);
5772 			break;
5773 		case IEEE80211_M_WDS:
5774 			break;
5775 		default:
5776 			break;
5777 		}
5778 		/*
5779 		 * Let the hal process statistics collected during a
5780 		 * scan so it can provide calibrated noise floor data.
5781 		 */
5782 		ath_hal_process_noisefloor(ah);
5783 		/*
5784 		 * Reset rssi stats; maybe not the best place...
5785 		 */
5786 		sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
5787 		sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
5788 		sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
5789 		/*
5790 		 * Finally, start any timers and the task q thread
5791 		 * (in case we didn't go through SCAN state).
5792 		 */
5793 		if (ath_longcalinterval != 0) {
5794 			/* start periodic recalibration timer */
5795 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
5796 		} else {
5797 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
5798 			    "%s: calibration disabled\n", __func__);
5799 		}
5800 		taskqueue_unblock(sc->sc_tq);
5801 	} else if (nstate == IEEE80211_S_INIT) {
5802 		/*
5803 		 * If there are no vaps left in RUN state then
5804 		 * shutdown host/driver operation:
5805 		 * o disable interrupts
5806 		 * o disable the task queue thread
5807 		 * o mark beacon processing as stopped
5808 		 */
5809 		if (!ath_isanyrunningvaps(vap)) {
5810 			sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5811 			/* disable interrupts  */
5812 			ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL);
5813 			taskqueue_block(sc->sc_tq);
5814 			sc->sc_beacons = 0;
5815 		}
5816 #ifdef IEEE80211_SUPPORT_TDMA
5817 		ath_hal_setcca(ah, AH_TRUE);
5818 #endif
5819 	}
5820 bad:
5821 	return error;
5822 }
5823 
5824 /*
5825  * Allocate a key cache slot to the station so we can
5826  * setup a mapping from key index to node. The key cache
5827  * slot is needed for managing antenna state and for
5828  * compression when stations do not use crypto.  We do
5829  * it uniliaterally here; if crypto is employed this slot
5830  * will be reassigned.
5831  */
5832 static void
5833 ath_setup_stationkey(struct ieee80211_node *ni)
5834 {
5835 	struct ieee80211vap *vap = ni->ni_vap;
5836 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
5837 	ieee80211_keyix keyix, rxkeyix;
5838 
5839 	if (!ath_key_alloc(vap, &ni->ni_ucastkey, &keyix, &rxkeyix)) {
5840 		/*
5841 		 * Key cache is full; we'll fall back to doing
5842 		 * the more expensive lookup in software.  Note
5843 		 * this also means no h/w compression.
5844 		 */
5845 		/* XXX msg+statistic */
5846 	} else {
5847 		/* XXX locking? */
5848 		ni->ni_ucastkey.wk_keyix = keyix;
5849 		ni->ni_ucastkey.wk_rxkeyix = rxkeyix;
5850 		/* NB: must mark device key to get called back on delete */
5851 		ni->ni_ucastkey.wk_flags |= IEEE80211_KEY_DEVKEY;
5852 		IEEE80211_ADDR_COPY(ni->ni_ucastkey.wk_macaddr, ni->ni_macaddr);
5853 		/* NB: this will create a pass-thru key entry */
5854 		ath_keyset(sc, vap, &ni->ni_ucastkey, vap->iv_bss);
5855 	}
5856 }
5857 
5858 /*
5859  * Setup driver-specific state for a newly associated node.
5860  * Note that we're called also on a re-associate, the isnew
5861  * param tells us if this is the first time or not.
5862  */
5863 static void
5864 ath_newassoc(struct ieee80211_node *ni, int isnew)
5865 {
5866 	struct ath_node *an = ATH_NODE(ni);
5867 	struct ieee80211vap *vap = ni->ni_vap;
5868 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
5869 	const struct ieee80211_txparam *tp = ni->ni_txparms;
5870 
5871 	an->an_mcastrix = ath_tx_findrix(sc, tp->mcastrate);
5872 	an->an_mgmtrix = ath_tx_findrix(sc, tp->mgmtrate);
5873 
5874 	ath_rate_newassoc(sc, an, isnew);
5875 	if (isnew &&
5876 	    (vap->iv_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey &&
5877 	    ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE)
5878 		ath_setup_stationkey(ni);
5879 }
5880 
5881 static int
5882 ath_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *reg,
5883 	int nchans, struct ieee80211_channel chans[])
5884 {
5885 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5886 	struct ath_hal *ah = sc->sc_ah;
5887 	HAL_STATUS status;
5888 
5889 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
5890 	    "%s: rd %u cc %u location %c%s\n",
5891 	    __func__, reg->regdomain, reg->country, reg->location,
5892 	    reg->ecm ? " ecm" : "");
5893 
5894 	status = ath_hal_set_channels(ah, chans, nchans,
5895 	    reg->country, reg->regdomain);
5896 	if (status != HAL_OK) {
5897 		DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: failed, status %u\n",
5898 		    __func__, status);
5899 		return EINVAL;		/* XXX */
5900 	}
5901 
5902 	return 0;
5903 }
5904 
5905 static void
5906 ath_getradiocaps(struct ieee80211com *ic,
5907 	int maxchans, int *nchans, struct ieee80211_channel chans[])
5908 {
5909 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5910 	struct ath_hal *ah = sc->sc_ah;
5911 
5912 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: use rd %u cc %d\n",
5913 	    __func__, SKU_DEBUG, CTRY_DEFAULT);
5914 
5915 	/* XXX check return */
5916 	(void) ath_hal_getchannels(ah, chans, maxchans, nchans,
5917 	    HAL_MODE_ALL, CTRY_DEFAULT, SKU_DEBUG, AH_TRUE);
5918 
5919 }
5920 
5921 static int
5922 ath_getchannels(struct ath_softc *sc)
5923 {
5924 	struct ifnet *ifp = sc->sc_ifp;
5925 	struct ieee80211com *ic = ifp->if_l2com;
5926 	struct ath_hal *ah = sc->sc_ah;
5927 	HAL_STATUS status;
5928 
5929 	/*
5930 	 * Collect channel set based on EEPROM contents.
5931 	 */
5932 	status = ath_hal_init_channels(ah, ic->ic_channels, IEEE80211_CHAN_MAX,
5933 	    &ic->ic_nchans, HAL_MODE_ALL, CTRY_DEFAULT, SKU_NONE, AH_TRUE);
5934 	if (status != HAL_OK) {
5935 		if_printf(ifp, "%s: unable to collect channel list from hal, "
5936 		    "status %d\n", __func__, status);
5937 		return EINVAL;
5938 	}
5939 	(void) ath_hal_getregdomain(ah, &sc->sc_eerd);
5940 	ath_hal_getcountrycode(ah, &sc->sc_eecc);	/* NB: cannot fail */
5941 	/* XXX map Atheros sku's to net80211 SKU's */
5942 	/* XXX net80211 types too small */
5943 	ic->ic_regdomain.regdomain = (uint16_t) sc->sc_eerd;
5944 	ic->ic_regdomain.country = (uint16_t) sc->sc_eecc;
5945 	ic->ic_regdomain.isocc[0] = ' ';	/* XXX don't know */
5946 	ic->ic_regdomain.isocc[1] = ' ';
5947 
5948 	ic->ic_regdomain.ecm = 1;
5949 	ic->ic_regdomain.location = 'I';
5950 
5951 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
5952 	    "%s: eeprom rd %u cc %u (mapped rd %u cc %u) location %c%s\n",
5953 	    __func__, sc->sc_eerd, sc->sc_eecc,
5954 	    ic->ic_regdomain.regdomain, ic->ic_regdomain.country,
5955 	    ic->ic_regdomain.location, ic->ic_regdomain.ecm ? " ecm" : "");
5956 	return 0;
5957 }
5958 
5959 static void
5960 ath_led_done(void *arg)
5961 {
5962 	struct ath_softc *sc = arg;
5963 
5964 	sc->sc_blinking = 0;
5965 }
5966 
5967 /*
5968  * Turn the LED off: flip the pin and then set a timer so no
5969  * update will happen for the specified duration.
5970  */
5971 static void
5972 ath_led_off(void *arg)
5973 {
5974 	struct ath_softc *sc = arg;
5975 
5976 	ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, !sc->sc_ledon);
5977 	callout_reset(&sc->sc_ledtimer, sc->sc_ledoff, ath_led_done, sc);
5978 }
5979 
5980 /*
5981  * Blink the LED according to the specified on/off times.
5982  */
5983 static void
5984 ath_led_blink(struct ath_softc *sc, int on, int off)
5985 {
5986 	DPRINTF(sc, ATH_DEBUG_LED, "%s: on %u off %u\n", __func__, on, off);
5987 	ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, sc->sc_ledon);
5988 	sc->sc_blinking = 1;
5989 	sc->sc_ledoff = off;
5990 	callout_reset(&sc->sc_ledtimer, on, ath_led_off, sc);
5991 }
5992 
5993 static void
5994 ath_led_event(struct ath_softc *sc, int rix)
5995 {
5996 	sc->sc_ledevent = ticks;	/* time of last event */
5997 	if (sc->sc_blinking)		/* don't interrupt active blink */
5998 		return;
5999 	ath_led_blink(sc, sc->sc_hwmap[rix].ledon, sc->sc_hwmap[rix].ledoff);
6000 }
6001 
6002 static int
6003 ath_rate_setup(struct ath_softc *sc, u_int mode)
6004 {
6005 	struct ath_hal *ah = sc->sc_ah;
6006 	const HAL_RATE_TABLE *rt;
6007 
6008 	switch (mode) {
6009 	case IEEE80211_MODE_11A:
6010 		rt = ath_hal_getratetable(ah, HAL_MODE_11A);
6011 		break;
6012 	case IEEE80211_MODE_HALF:
6013 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_HALF_RATE);
6014 		break;
6015 	case IEEE80211_MODE_QUARTER:
6016 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_QUARTER_RATE);
6017 		break;
6018 	case IEEE80211_MODE_11B:
6019 		rt = ath_hal_getratetable(ah, HAL_MODE_11B);
6020 		break;
6021 	case IEEE80211_MODE_11G:
6022 		rt = ath_hal_getratetable(ah, HAL_MODE_11G);
6023 		break;
6024 	case IEEE80211_MODE_TURBO_A:
6025 		rt = ath_hal_getratetable(ah, HAL_MODE_108A);
6026 		break;
6027 	case IEEE80211_MODE_TURBO_G:
6028 		rt = ath_hal_getratetable(ah, HAL_MODE_108G);
6029 		break;
6030 	case IEEE80211_MODE_STURBO_A:
6031 		rt = ath_hal_getratetable(ah, HAL_MODE_TURBO);
6032 		break;
6033 	case IEEE80211_MODE_11NA:
6034 		rt = ath_hal_getratetable(ah, HAL_MODE_11NA_HT20);
6035 		break;
6036 	case IEEE80211_MODE_11NG:
6037 		rt = ath_hal_getratetable(ah, HAL_MODE_11NG_HT20);
6038 		break;
6039 	default:
6040 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
6041 			__func__, mode);
6042 		return 0;
6043 	}
6044 	sc->sc_rates[mode] = rt;
6045 	return (rt != NULL);
6046 }
6047 
6048 static void
6049 ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
6050 {
6051 #define	N(a)	(sizeof(a)/sizeof(a[0]))
6052 	/* NB: on/off times from the Atheros NDIS driver, w/ permission */
6053 	static const struct {
6054 		u_int		rate;		/* tx/rx 802.11 rate */
6055 		u_int16_t	timeOn;		/* LED on time (ms) */
6056 		u_int16_t	timeOff;	/* LED off time (ms) */
6057 	} blinkrates[] = {
6058 		{ 108,  40,  10 },
6059 		{  96,  44,  11 },
6060 		{  72,  50,  13 },
6061 		{  48,  57,  14 },
6062 		{  36,  67,  16 },
6063 		{  24,  80,  20 },
6064 		{  22, 100,  25 },
6065 		{  18, 133,  34 },
6066 		{  12, 160,  40 },
6067 		{  10, 200,  50 },
6068 		{   6, 240,  58 },
6069 		{   4, 267,  66 },
6070 		{   2, 400, 100 },
6071 		{   0, 500, 130 },
6072 		/* XXX half/quarter rates */
6073 	};
6074 	const HAL_RATE_TABLE *rt;
6075 	int i, j;
6076 
6077 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
6078 	rt = sc->sc_rates[mode];
6079 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
6080 	for (i = 0; i < rt->rateCount; i++) {
6081 		uint8_t ieeerate = rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
6082 		if (rt->info[i].phy != IEEE80211_T_HT)
6083 			sc->sc_rixmap[ieeerate] = i;
6084 		else
6085 			sc->sc_rixmap[ieeerate | IEEE80211_RATE_MCS] = i;
6086 	}
6087 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
6088 	for (i = 0; i < N(sc->sc_hwmap); i++) {
6089 		if (i >= rt->rateCount) {
6090 			sc->sc_hwmap[i].ledon = (500 * hz) / 1000;
6091 			sc->sc_hwmap[i].ledoff = (130 * hz) / 1000;
6092 			continue;
6093 		}
6094 		sc->sc_hwmap[i].ieeerate =
6095 			rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
6096 		if (rt->info[i].phy == IEEE80211_T_HT)
6097 			sc->sc_hwmap[i].ieeerate |= IEEE80211_RATE_MCS;
6098 		sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
6099 		if (rt->info[i].shortPreamble ||
6100 		    rt->info[i].phy == IEEE80211_T_OFDM)
6101 			sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE;
6102 		sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags;
6103 		for (j = 0; j < N(blinkrates)-1; j++)
6104 			if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate)
6105 				break;
6106 		/* NB: this uses the last entry if the rate isn't found */
6107 		/* XXX beware of overlow */
6108 		sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000;
6109 		sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000;
6110 	}
6111 	sc->sc_currates = rt;
6112 	sc->sc_curmode = mode;
6113 	/*
6114 	 * All protection frames are transmited at 2Mb/s for
6115 	 * 11g, otherwise at 1Mb/s.
6116 	 */
6117 	if (mode == IEEE80211_MODE_11G)
6118 		sc->sc_protrix = ath_tx_findrix(sc, 2*2);
6119 	else
6120 		sc->sc_protrix = ath_tx_findrix(sc, 2*1);
6121 	/* NB: caller is responsible for resetting rate control state */
6122 #undef N
6123 }
6124 
6125 static void
6126 ath_watchdog(void *arg)
6127 {
6128 	struct ath_softc *sc = arg;
6129 	int do_reset = 0;
6130 
6131 	if (sc->sc_wd_timer != 0 && --sc->sc_wd_timer == 0) {
6132 		struct ifnet *ifp = sc->sc_ifp;
6133 		uint32_t hangs;
6134 
6135 		if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) &&
6136 		    hangs != 0) {
6137 			if_printf(ifp, "%s hang detected (0x%x)\n",
6138 			    hangs & 0xff ? "bb" : "mac", hangs);
6139 		} else
6140 			if_printf(ifp, "device timeout\n");
6141 		do_reset = 1;
6142 		ifp->if_oerrors++;
6143 		sc->sc_stats.ast_watchdog++;
6144 	}
6145 
6146 	/*
6147 	 * We can't hold the lock across the ath_reset() call.
6148 	 */
6149 	if (do_reset) {
6150 		ATH_UNLOCK(sc);
6151 		ath_reset(sc->sc_ifp, ATH_RESET_NOLOSS);
6152 		ATH_LOCK(sc);
6153 	}
6154 
6155 	callout_schedule(&sc->sc_wd_ch, hz);
6156 }
6157 
6158 #ifdef ATH_DIAGAPI
6159 /*
6160  * Diagnostic interface to the HAL.  This is used by various
6161  * tools to do things like retrieve register contents for
6162  * debugging.  The mechanism is intentionally opaque so that
6163  * it can change frequently w/o concern for compatiblity.
6164  */
6165 static int
6166 ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
6167 {
6168 	struct ath_hal *ah = sc->sc_ah;
6169 	u_int id = ad->ad_id & ATH_DIAG_ID;
6170 	void *indata = NULL;
6171 	void *outdata = NULL;
6172 	u_int32_t insize = ad->ad_in_size;
6173 	u_int32_t outsize = ad->ad_out_size;
6174 	int error = 0;
6175 
6176 	if (ad->ad_id & ATH_DIAG_IN) {
6177 		/*
6178 		 * Copy in data.
6179 		 */
6180 		indata = malloc(insize, M_TEMP, M_NOWAIT);
6181 		if (indata == NULL) {
6182 			error = ENOMEM;
6183 			goto bad;
6184 		}
6185 		error = copyin(ad->ad_in_data, indata, insize);
6186 		if (error)
6187 			goto bad;
6188 	}
6189 	if (ad->ad_id & ATH_DIAG_DYN) {
6190 		/*
6191 		 * Allocate a buffer for the results (otherwise the HAL
6192 		 * returns a pointer to a buffer where we can read the
6193 		 * results).  Note that we depend on the HAL leaving this
6194 		 * pointer for us to use below in reclaiming the buffer;
6195 		 * may want to be more defensive.
6196 		 */
6197 		outdata = malloc(outsize, M_TEMP, M_NOWAIT);
6198 		if (outdata == NULL) {
6199 			error = ENOMEM;
6200 			goto bad;
6201 		}
6202 	}
6203 	if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) {
6204 		if (outsize < ad->ad_out_size)
6205 			ad->ad_out_size = outsize;
6206 		if (outdata != NULL)
6207 			error = copyout(outdata, ad->ad_out_data,
6208 					ad->ad_out_size);
6209 	} else {
6210 		error = EINVAL;
6211 	}
6212 bad:
6213 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
6214 		free(indata, M_TEMP);
6215 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
6216 		free(outdata, M_TEMP);
6217 	return error;
6218 }
6219 #endif /* ATH_DIAGAPI */
6220 
6221 static int
6222 ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
6223 {
6224 #define	IS_RUNNING(ifp) \
6225 	((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
6226 	struct ath_softc *sc = ifp->if_softc;
6227 	struct ieee80211com *ic = ifp->if_l2com;
6228 	struct ifreq *ifr = (struct ifreq *)data;
6229 	const HAL_RATE_TABLE *rt;
6230 	int error = 0;
6231 
6232 	switch (cmd) {
6233 	case SIOCSIFFLAGS:
6234 		ATH_LOCK(sc);
6235 		if (IS_RUNNING(ifp)) {
6236 			/*
6237 			 * To avoid rescanning another access point,
6238 			 * do not call ath_init() here.  Instead,
6239 			 * only reflect promisc mode settings.
6240 			 */
6241 			ath_mode_init(sc);
6242 		} else if (ifp->if_flags & IFF_UP) {
6243 			/*
6244 			 * Beware of being called during attach/detach
6245 			 * to reset promiscuous mode.  In that case we
6246 			 * will still be marked UP but not RUNNING.
6247 			 * However trying to re-init the interface
6248 			 * is the wrong thing to do as we've already
6249 			 * torn down much of our state.  There's
6250 			 * probably a better way to deal with this.
6251 			 */
6252 			if (!sc->sc_invalid)
6253 				ath_init(sc);	/* XXX lose error */
6254 		} else {
6255 			ath_stop_locked(ifp);
6256 #ifdef notyet
6257 			/* XXX must wakeup in places like ath_vap_delete */
6258 			if (!sc->sc_invalid)
6259 				ath_hal_setpower(sc->sc_ah, HAL_PM_FULL_SLEEP);
6260 #endif
6261 		}
6262 		ATH_UNLOCK(sc);
6263 		break;
6264 	case SIOCGIFMEDIA:
6265 	case SIOCSIFMEDIA:
6266 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
6267 		break;
6268 	case SIOCGATHSTATS:
6269 		/* NB: embed these numbers to get a consistent view */
6270 		sc->sc_stats.ast_tx_packets = ifp->if_opackets;
6271 		sc->sc_stats.ast_rx_packets = ifp->if_ipackets;
6272 		sc->sc_stats.ast_tx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgtxrssi);
6273 		sc->sc_stats.ast_rx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgrssi);
6274 #ifdef IEEE80211_SUPPORT_TDMA
6275 		sc->sc_stats.ast_tdma_tsfadjp = TDMA_AVG(sc->sc_avgtsfdeltap);
6276 		sc->sc_stats.ast_tdma_tsfadjm = TDMA_AVG(sc->sc_avgtsfdeltam);
6277 #endif
6278 		rt = sc->sc_currates;
6279 		sc->sc_stats.ast_tx_rate =
6280 		    rt->info[sc->sc_txrix].dot11Rate &~ IEEE80211_RATE_BASIC;
6281 		if (rt->info[sc->sc_txrix].phy & IEEE80211_T_HT)
6282 			sc->sc_stats.ast_tx_rate |= IEEE80211_RATE_MCS;
6283 		return copyout(&sc->sc_stats,
6284 		    ifr->ifr_data, sizeof (sc->sc_stats));
6285 	case SIOCZATHSTATS:
6286 		error = priv_check(curthread, PRIV_DRIVER);
6287 		if (error == 0)
6288 			memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
6289 		break;
6290 #ifdef ATH_DIAGAPI
6291 	case SIOCGATHDIAG:
6292 		error = ath_ioctl_diag(sc, (struct ath_diag *) ifr);
6293 		break;
6294 	case SIOCGATHPHYERR:
6295 		error = ath_ioctl_phyerr(sc,(struct ath_diag*) ifr);
6296 		break;
6297 #endif
6298 	case SIOCGIFADDR:
6299 		error = ether_ioctl(ifp, cmd, data);
6300 		break;
6301 	default:
6302 		error = EINVAL;
6303 		break;
6304 	}
6305 	return error;
6306 #undef IS_RUNNING
6307 }
6308 
6309 /*
6310  * Announce various information on device/driver attach.
6311  */
6312 static void
6313 ath_announce(struct ath_softc *sc)
6314 {
6315 	struct ifnet *ifp = sc->sc_ifp;
6316 	struct ath_hal *ah = sc->sc_ah;
6317 
6318 	if_printf(ifp, "AR%s mac %d.%d RF%s phy %d.%d\n",
6319 		ath_hal_mac_name(ah), ah->ah_macVersion, ah->ah_macRev,
6320 		ath_hal_rf_name(ah), ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
6321 	if_printf(ifp, "2GHz radio: 0x%.4x; 5GHz radio: 0x%.4x\n",
6322 		ah->ah_analog2GhzRev, ah->ah_analog5GhzRev);
6323 	if (bootverbose) {
6324 		int i;
6325 		for (i = 0; i <= WME_AC_VO; i++) {
6326 			struct ath_txq *txq = sc->sc_ac2q[i];
6327 			if_printf(ifp, "Use hw queue %u for %s traffic\n",
6328 				txq->axq_qnum, ieee80211_wme_acnames[i]);
6329 		}
6330 		if_printf(ifp, "Use hw queue %u for CAB traffic\n",
6331 			sc->sc_cabq->axq_qnum);
6332 		if_printf(ifp, "Use hw queue %u for beacons\n", sc->sc_bhalq);
6333 	}
6334 	if (ath_rxbuf != ATH_RXBUF)
6335 		if_printf(ifp, "using %u rx buffers\n", ath_rxbuf);
6336 	if (ath_txbuf != ATH_TXBUF)
6337 		if_printf(ifp, "using %u tx buffers\n", ath_txbuf);
6338 	if (sc->sc_mcastkey && bootverbose)
6339 		if_printf(ifp, "using multicast key search\n");
6340 }
6341 
6342 #ifdef IEEE80211_SUPPORT_TDMA
6343 static void
6344 ath_tdma_settimers(struct ath_softc *sc, u_int32_t nexttbtt, u_int32_t bintval)
6345 {
6346 	struct ath_hal *ah = sc->sc_ah;
6347 	HAL_BEACON_TIMERS bt;
6348 
6349 	bt.bt_intval = bintval | HAL_BEACON_ENA;
6350 	bt.bt_nexttbtt = nexttbtt;
6351 	bt.bt_nextdba = (nexttbtt<<3) - sc->sc_tdmadbaprep;
6352 	bt.bt_nextswba = (nexttbtt<<3) - sc->sc_tdmaswbaprep;
6353 	bt.bt_nextatim = nexttbtt+1;
6354 	/* Enables TBTT, DBA, SWBA timers by default */
6355 	bt.bt_flags = 0;
6356 	ath_hal_beaconsettimers(ah, &bt);
6357 }
6358 
6359 /*
6360  * Calculate the beacon interval.  This is periodic in the
6361  * superframe for the bss.  We assume each station is configured
6362  * identically wrt transmit rate so the guard time we calculate
6363  * above will be the same on all stations.  Note we need to
6364  * factor in the xmit time because the hardware will schedule
6365  * a frame for transmit if the start of the frame is within
6366  * the burst time.  When we get hardware that properly kills
6367  * frames in the PCU we can reduce/eliminate the guard time.
6368  *
6369  * Roundup to 1024 is so we have 1 TU buffer in the guard time
6370  * to deal with the granularity of the nexttbtt timer.  11n MAC's
6371  * with 1us timer granularity should allow us to reduce/eliminate
6372  * this.
6373  */
6374 static void
6375 ath_tdma_bintvalsetup(struct ath_softc *sc,
6376 	const struct ieee80211_tdma_state *tdma)
6377 {
6378 	/* copy from vap state (XXX check all vaps have same value?) */
6379 	sc->sc_tdmaslotlen = tdma->tdma_slotlen;
6380 
6381 	sc->sc_tdmabintval = roundup((sc->sc_tdmaslotlen+sc->sc_tdmaguard) *
6382 		tdma->tdma_slotcnt, 1024);
6383 	sc->sc_tdmabintval >>= 10;		/* TSF -> TU */
6384 	if (sc->sc_tdmabintval & 1)
6385 		sc->sc_tdmabintval++;
6386 
6387 	if (tdma->tdma_slot == 0) {
6388 		/*
6389 		 * Only slot 0 beacons; other slots respond.
6390 		 */
6391 		sc->sc_imask |= HAL_INT_SWBA;
6392 		sc->sc_tdmaswba = 0;		/* beacon immediately */
6393 	} else {
6394 		/* XXX all vaps must be slot 0 or slot !0 */
6395 		sc->sc_imask &= ~HAL_INT_SWBA;
6396 	}
6397 }
6398 
6399 /*
6400  * Max 802.11 overhead.  This assumes no 4-address frames and
6401  * the encapsulation done by ieee80211_encap (llc).  We also
6402  * include potential crypto overhead.
6403  */
6404 #define	IEEE80211_MAXOVERHEAD \
6405 	(sizeof(struct ieee80211_qosframe) \
6406 	 + sizeof(struct llc) \
6407 	 + IEEE80211_ADDR_LEN \
6408 	 + IEEE80211_WEP_IVLEN \
6409 	 + IEEE80211_WEP_KIDLEN \
6410 	 + IEEE80211_WEP_CRCLEN \
6411 	 + IEEE80211_WEP_MICLEN \
6412 	 + IEEE80211_CRC_LEN)
6413 
6414 /*
6415  * Setup initially for tdma operation.  Start the beacon
6416  * timers and enable SWBA if we are slot 0.  Otherwise
6417  * we wait for slot 0 to arrive so we can sync up before
6418  * starting to transmit.
6419  */
6420 static void
6421 ath_tdma_config(struct ath_softc *sc, struct ieee80211vap *vap)
6422 {
6423 	struct ath_hal *ah = sc->sc_ah;
6424 	struct ifnet *ifp = sc->sc_ifp;
6425 	struct ieee80211com *ic = ifp->if_l2com;
6426 	const struct ieee80211_txparam *tp;
6427 	const struct ieee80211_tdma_state *tdma = NULL;
6428 	int rix;
6429 
6430 	if (vap == NULL) {
6431 		vap = TAILQ_FIRST(&ic->ic_vaps);   /* XXX */
6432 		if (vap == NULL) {
6433 			if_printf(ifp, "%s: no vaps?\n", __func__);
6434 			return;
6435 		}
6436 	}
6437 	tp = vap->iv_bss->ni_txparms;
6438 	/*
6439 	 * Calculate the guard time for each slot.  This is the
6440 	 * time to send a maximal-size frame according to the
6441 	 * fixed/lowest transmit rate.  Note that the interface
6442 	 * mtu does not include the 802.11 overhead so we must
6443 	 * tack that on (ath_hal_computetxtime includes the
6444 	 * preamble and plcp in it's calculation).
6445 	 */
6446 	tdma = vap->iv_tdma;
6447 	if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
6448 		rix = ath_tx_findrix(sc, tp->ucastrate);
6449 	else
6450 		rix = ath_tx_findrix(sc, tp->mcastrate);
6451 	/* XXX short preamble assumed */
6452 	sc->sc_tdmaguard = ath_hal_computetxtime(ah, sc->sc_currates,
6453 		ifp->if_mtu + IEEE80211_MAXOVERHEAD, rix, AH_TRUE);
6454 
6455 	ath_hal_intrset(ah, 0);
6456 
6457 	ath_beaconq_config(sc);			/* setup h/w beacon q */
6458 	if (sc->sc_setcca)
6459 		ath_hal_setcca(ah, AH_FALSE);	/* disable CCA */
6460 	ath_tdma_bintvalsetup(sc, tdma);	/* calculate beacon interval */
6461 	ath_tdma_settimers(sc, sc->sc_tdmabintval,
6462 		sc->sc_tdmabintval | HAL_BEACON_RESET_TSF);
6463 	sc->sc_syncbeacon = 0;
6464 
6465 	sc->sc_avgtsfdeltap = TDMA_DUMMY_MARKER;
6466 	sc->sc_avgtsfdeltam = TDMA_DUMMY_MARKER;
6467 
6468 	ath_hal_intrset(ah, sc->sc_imask);
6469 
6470 	DPRINTF(sc, ATH_DEBUG_TDMA, "%s: slot %u len %uus cnt %u "
6471 	    "bsched %u guard %uus bintval %u TU dba prep %u\n", __func__,
6472 	    tdma->tdma_slot, tdma->tdma_slotlen, tdma->tdma_slotcnt,
6473 	    tdma->tdma_bintval, sc->sc_tdmaguard, sc->sc_tdmabintval,
6474 	    sc->sc_tdmadbaprep);
6475 }
6476 
6477 /*
6478  * Update tdma operation.  Called from the 802.11 layer
6479  * when a beacon is received from the TDMA station operating
6480  * in the slot immediately preceding us in the bss.  Use
6481  * the rx timestamp for the beacon frame to update our
6482  * beacon timers so we follow their schedule.  Note that
6483  * by using the rx timestamp we implicitly include the
6484  * propagation delay in our schedule.
6485  */
6486 static void
6487 ath_tdma_update(struct ieee80211_node *ni,
6488 	const struct ieee80211_tdma_param *tdma, int changed)
6489 {
6490 #define	TSF_TO_TU(_h,_l) \
6491 	((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10))
6492 #define	TU_TO_TSF(_tu)	(((u_int64_t)(_tu)) << 10)
6493 	struct ieee80211vap *vap = ni->ni_vap;
6494 	struct ieee80211com *ic = ni->ni_ic;
6495 	struct ath_softc *sc = ic->ic_ifp->if_softc;
6496 	struct ath_hal *ah = sc->sc_ah;
6497 	const HAL_RATE_TABLE *rt = sc->sc_currates;
6498 	u_int64_t tsf, rstamp, nextslot, nexttbtt;
6499 	u_int32_t txtime, nextslottu;
6500 	int32_t tudelta, tsfdelta;
6501 	const struct ath_rx_status *rs;
6502 	int rix;
6503 
6504 	sc->sc_stats.ast_tdma_update++;
6505 
6506 	/*
6507 	 * Check for and adopt configuration changes.
6508 	 */
6509 	if (changed != 0) {
6510 		const struct ieee80211_tdma_state *ts = vap->iv_tdma;
6511 
6512 		ath_tdma_bintvalsetup(sc, ts);
6513 		if (changed & TDMA_UPDATE_SLOTLEN)
6514 			ath_wme_update(ic);
6515 
6516 		DPRINTF(sc, ATH_DEBUG_TDMA,
6517 		    "%s: adopt slot %u slotcnt %u slotlen %u us "
6518 		    "bintval %u TU\n", __func__,
6519 		    ts->tdma_slot, ts->tdma_slotcnt, ts->tdma_slotlen,
6520 		    sc->sc_tdmabintval);
6521 
6522 		/* XXX right? */
6523 		ath_hal_intrset(ah, sc->sc_imask);
6524 		/* NB: beacon timers programmed below */
6525 	}
6526 
6527 	/* extend rx timestamp to 64 bits */
6528 	rs = sc->sc_lastrs;
6529 	tsf = ath_hal_gettsf64(ah);
6530 	rstamp = ath_extend_tsf(sc, rs->rs_tstamp, tsf);
6531 	/*
6532 	 * The rx timestamp is set by the hardware on completing
6533 	 * reception (at the point where the rx descriptor is DMA'd
6534 	 * to the host).  To find the start of our next slot we
6535 	 * must adjust this time by the time required to send
6536 	 * the packet just received.
6537 	 */
6538 	rix = rt->rateCodeToIndex[rs->rs_rate];
6539 	txtime = ath_hal_computetxtime(ah, rt, rs->rs_datalen, rix,
6540 	    rt->info[rix].shortPreamble);
6541 	/* NB: << 9 is to cvt to TU and /2 */
6542 	nextslot = (rstamp - txtime) + (sc->sc_tdmabintval << 9);
6543 	nextslottu = TSF_TO_TU(nextslot>>32, nextslot) & HAL_BEACON_PERIOD;
6544 
6545 	/*
6546 	 * Retrieve the hardware NextTBTT in usecs
6547 	 * and calculate the difference between what the
6548 	 * other station thinks and what we have programmed.  This
6549 	 * lets us figure how to adjust our timers to match.  The
6550 	 * adjustments are done by pulling the TSF forward and possibly
6551 	 * rewriting the beacon timers.
6552 	 */
6553 	nexttbtt = ath_hal_getnexttbtt(ah);
6554 	tsfdelta = (int32_t)((nextslot % TU_TO_TSF(HAL_BEACON_PERIOD + 1)) - nexttbtt);
6555 
6556 	DPRINTF(sc, ATH_DEBUG_TDMA_TIMER,
6557 	    "tsfdelta %d avg +%d/-%d\n", tsfdelta,
6558 	    TDMA_AVG(sc->sc_avgtsfdeltap), TDMA_AVG(sc->sc_avgtsfdeltam));
6559 
6560 	if (tsfdelta < 0) {
6561 		TDMA_SAMPLE(sc->sc_avgtsfdeltap, 0);
6562 		TDMA_SAMPLE(sc->sc_avgtsfdeltam, -tsfdelta);
6563 		tsfdelta = -tsfdelta % 1024;
6564 		nextslottu++;
6565 	} else if (tsfdelta > 0) {
6566 		TDMA_SAMPLE(sc->sc_avgtsfdeltap, tsfdelta);
6567 		TDMA_SAMPLE(sc->sc_avgtsfdeltam, 0);
6568 		tsfdelta = 1024 - (tsfdelta % 1024);
6569 		nextslottu++;
6570 	} else {
6571 		TDMA_SAMPLE(sc->sc_avgtsfdeltap, 0);
6572 		TDMA_SAMPLE(sc->sc_avgtsfdeltam, 0);
6573 	}
6574 	tudelta = nextslottu - TSF_TO_TU(nexttbtt >> 32, nexttbtt);
6575 
6576 	/*
6577 	 * Copy sender's timetstamp into tdma ie so they can
6578 	 * calculate roundtrip time.  We submit a beacon frame
6579 	 * below after any timer adjustment.  The frame goes out
6580 	 * at the next TBTT so the sender can calculate the
6581 	 * roundtrip by inspecting the tdma ie in our beacon frame.
6582 	 *
6583 	 * NB: This tstamp is subtlely preserved when
6584 	 *     IEEE80211_BEACON_TDMA is marked (e.g. when the
6585 	 *     slot position changes) because ieee80211_add_tdma
6586 	 *     skips over the data.
6587 	 */
6588 	memcpy(ATH_VAP(vap)->av_boff.bo_tdma +
6589 		__offsetof(struct ieee80211_tdma_param, tdma_tstamp),
6590 		&ni->ni_tstamp.data, 8);
6591 #if 0
6592 	DPRINTF(sc, ATH_DEBUG_TDMA_TIMER,
6593 	    "tsf %llu nextslot %llu (%d, %d) nextslottu %u nexttbtt %llu (%d)\n",
6594 	    (unsigned long long) tsf, (unsigned long long) nextslot,
6595 	    (int)(nextslot - tsf), tsfdelta, nextslottu, nexttbtt, tudelta);
6596 #endif
6597 	/*
6598 	 * Adjust the beacon timers only when pulling them forward
6599 	 * or when going back by less than the beacon interval.
6600 	 * Negative jumps larger than the beacon interval seem to
6601 	 * cause the timers to stop and generally cause instability.
6602 	 * This basically filters out jumps due to missed beacons.
6603 	 */
6604 	if (tudelta != 0 && (tudelta > 0 || -tudelta < sc->sc_tdmabintval)) {
6605 		ath_tdma_settimers(sc, nextslottu, sc->sc_tdmabintval);
6606 		sc->sc_stats.ast_tdma_timers++;
6607 	}
6608 	if (tsfdelta > 0) {
6609 		ath_hal_adjusttsf(ah, tsfdelta);
6610 		sc->sc_stats.ast_tdma_tsf++;
6611 	}
6612 	ath_tdma_beacon_send(sc, vap);		/* prepare response */
6613 #undef TU_TO_TSF
6614 #undef TSF_TO_TU
6615 }
6616 
6617 /*
6618  * Transmit a beacon frame at SWBA.  Dynamic updates
6619  * to the frame contents are done as needed.
6620  */
6621 static void
6622 ath_tdma_beacon_send(struct ath_softc *sc, struct ieee80211vap *vap)
6623 {
6624 	struct ath_hal *ah = sc->sc_ah;
6625 	struct ath_buf *bf;
6626 	int otherant;
6627 
6628 	/*
6629 	 * Check if the previous beacon has gone out.  If
6630 	 * not don't try to post another, skip this period
6631 	 * and wait for the next.  Missed beacons indicate
6632 	 * a problem and should not occur.  If we miss too
6633 	 * many consecutive beacons reset the device.
6634 	 */
6635 	if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) {
6636 		sc->sc_bmisscount++;
6637 		DPRINTF(sc, ATH_DEBUG_BEACON,
6638 			"%s: missed %u consecutive beacons\n",
6639 			__func__, sc->sc_bmisscount);
6640 		if (sc->sc_bmisscount >= ath_bstuck_threshold)
6641 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask);
6642 		return;
6643 	}
6644 	if (sc->sc_bmisscount != 0) {
6645 		DPRINTF(sc, ATH_DEBUG_BEACON,
6646 			"%s: resume beacon xmit after %u misses\n",
6647 			__func__, sc->sc_bmisscount);
6648 		sc->sc_bmisscount = 0;
6649 	}
6650 
6651 	/*
6652 	 * Check recent per-antenna transmit statistics and flip
6653 	 * the default antenna if noticeably more frames went out
6654 	 * on the non-default antenna.
6655 	 * XXX assumes 2 anntenae
6656 	 */
6657 	if (!sc->sc_diversity) {
6658 		otherant = sc->sc_defant & 1 ? 2 : 1;
6659 		if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2)
6660 			ath_setdefantenna(sc, otherant);
6661 		sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0;
6662 	}
6663 
6664 	bf = ath_beacon_generate(sc, vap);
6665 	if (bf != NULL) {
6666 		/*
6667 		 * Stop any current dma and put the new frame on the queue.
6668 		 * This should never fail since we check above that no frames
6669 		 * are still pending on the queue.
6670 		 */
6671 		if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
6672 			DPRINTF(sc, ATH_DEBUG_ANY,
6673 				"%s: beacon queue %u did not stop?\n",
6674 				__func__, sc->sc_bhalq);
6675 			/* NB: the HAL still stops DMA, so proceed */
6676 		}
6677 		ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
6678 		ath_hal_txstart(ah, sc->sc_bhalq);
6679 
6680 		sc->sc_stats.ast_be_xmit++;		/* XXX per-vap? */
6681 
6682 		/*
6683 		 * Record local TSF for our last send for use
6684 		 * in arbitrating slot collisions.
6685 		 */
6686 		vap->iv_bss->ni_tstamp.tsf = ath_hal_gettsf64(ah);
6687 	}
6688 }
6689 #endif /* IEEE80211_SUPPORT_TDMA */
6690 
6691 static void
6692 ath_dfs_tasklet(void *p, int npending)
6693 {
6694 	struct ath_softc *sc = (struct ath_softc *) p;
6695 	struct ifnet *ifp = sc->sc_ifp;
6696 	struct ieee80211com *ic = ifp->if_l2com;
6697 
6698 	/*
6699 	 * If previous processing has found a radar event,
6700 	 * signal this to the net80211 layer to begin DFS
6701 	 * processing.
6702 	 */
6703 	if (ath_dfs_process_radar_event(sc, sc->sc_curchan)) {
6704 		/* DFS event found, initiate channel change */
6705 		ieee80211_dfs_notify_radar(ic, sc->sc_curchan);
6706 	}
6707 }
6708 
6709 MODULE_VERSION(if_ath, 1);
6710 MODULE_DEPEND(if_ath, wlan, 1, 1, 1);          /* 802.11 media layer */
6711