xref: /freebsd/sys/dev/ath/if_ath.c (revision 3e0efd2ec4fcb4cd68fb8ccf8aea6fc6151c454b)
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  * It's also required for any AH_DEBUG checks in here, eg the
47  * module dependencies.
48  */
49 #include "opt_ah.h"
50 #include "opt_wlan.h"
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/sysctl.h>
55 #include <sys/mbuf.h>
56 #include <sys/malloc.h>
57 #include <sys/lock.h>
58 #include <sys/mutex.h>
59 #include <sys/kernel.h>
60 #include <sys/socket.h>
61 #include <sys/sockio.h>
62 #include <sys/errno.h>
63 #include <sys/callout.h>
64 #include <sys/bus.h>
65 #include <sys/endian.h>
66 #include <sys/kthread.h>
67 #include <sys/taskqueue.h>
68 #include <sys/priv.h>
69 #include <sys/module.h>
70 #include <sys/ktr.h>
71 #include <sys/smp.h>	/* for mp_ncpus */
72 
73 #include <machine/bus.h>
74 
75 #include <net/if.h>
76 #include <net/if_dl.h>
77 #include <net/if_media.h>
78 #include <net/if_types.h>
79 #include <net/if_arp.h>
80 #include <net/ethernet.h>
81 #include <net/if_llc.h>
82 
83 #include <net80211/ieee80211_var.h>
84 #include <net80211/ieee80211_regdomain.h>
85 #ifdef IEEE80211_SUPPORT_SUPERG
86 #include <net80211/ieee80211_superg.h>
87 #endif
88 #ifdef IEEE80211_SUPPORT_TDMA
89 #include <net80211/ieee80211_tdma.h>
90 #endif
91 
92 #include <net/bpf.h>
93 
94 #ifdef INET
95 #include <netinet/in.h>
96 #include <netinet/if_ether.h>
97 #endif
98 
99 #include <dev/ath/if_athvar.h>
100 #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
101 #include <dev/ath/ath_hal/ah_diagcodes.h>
102 
103 #include <dev/ath/if_ath_debug.h>
104 #include <dev/ath/if_ath_misc.h>
105 #include <dev/ath/if_ath_tsf.h>
106 #include <dev/ath/if_ath_tx.h>
107 #include <dev/ath/if_ath_sysctl.h>
108 #include <dev/ath/if_ath_led.h>
109 #include <dev/ath/if_ath_keycache.h>
110 #include <dev/ath/if_ath_rx.h>
111 #include <dev/ath/if_ath_beacon.h>
112 #include <dev/ath/if_athdfs.h>
113 
114 #ifdef ATH_TX99_DIAG
115 #include <dev/ath/ath_tx99/ath_tx99.h>
116 #endif
117 
118 #define	ATH_KTR_INTR	KTR_SPARE4
119 #define	ATH_KTR_ERR	KTR_SPARE3
120 
121 /*
122  * ATH_BCBUF determines the number of vap's that can transmit
123  * beacons and also (currently) the number of vap's that can
124  * have unique mac addresses/bssid.  When staggering beacons
125  * 4 is probably a good max as otherwise the beacons become
126  * very closely spaced and there is limited time for cab q traffic
127  * to go out.  You can burst beacons instead but that is not good
128  * for stations in power save and at some point you really want
129  * another radio (and channel).
130  *
131  * The limit on the number of mac addresses is tied to our use of
132  * the U/L bit and tracking addresses in a byte; it would be
133  * worthwhile to allow more for applications like proxy sta.
134  */
135 CTASSERT(ATH_BCBUF <= 8);
136 
137 static struct ieee80211vap *ath_vap_create(struct ieee80211com *,
138 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
139 		    const uint8_t [IEEE80211_ADDR_LEN],
140 		    const uint8_t [IEEE80211_ADDR_LEN]);
141 static void	ath_vap_delete(struct ieee80211vap *);
142 static void	ath_init(void *);
143 static void	ath_stop_locked(struct ifnet *);
144 static void	ath_stop(struct ifnet *);
145 static int	ath_reset_vap(struct ieee80211vap *, u_long);
146 static int	ath_media_change(struct ifnet *);
147 static void	ath_watchdog(void *);
148 static int	ath_ioctl(struct ifnet *, u_long, caddr_t);
149 static void	ath_fatal_proc(void *, int);
150 static void	ath_bmiss_vap(struct ieee80211vap *);
151 static void	ath_bmiss_proc(void *, int);
152 static void	ath_key_update_begin(struct ieee80211vap *);
153 static void	ath_key_update_end(struct ieee80211vap *);
154 static void	ath_update_mcast(struct ifnet *);
155 static void	ath_update_promisc(struct ifnet *);
156 static void	ath_updateslot(struct ifnet *);
157 static void	ath_bstuck_proc(void *, int);
158 static void	ath_reset_proc(void *, int);
159 static void	ath_descdma_cleanup(struct ath_softc *sc,
160 			struct ath_descdma *, ath_bufhead *);
161 static int	ath_desc_alloc(struct ath_softc *);
162 static void	ath_desc_free(struct ath_softc *);
163 static struct ieee80211_node *ath_node_alloc(struct ieee80211vap *,
164 			const uint8_t [IEEE80211_ADDR_LEN]);
165 static void	ath_node_cleanup(struct ieee80211_node *);
166 static void	ath_node_free(struct ieee80211_node *);
167 static void	ath_node_getsignal(const struct ieee80211_node *,
168 			int8_t *, int8_t *);
169 static void	ath_txq_init(struct ath_softc *sc, struct ath_txq *, int);
170 static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype);
171 static int	ath_tx_setup(struct ath_softc *, int, int);
172 static void	ath_tx_cleanupq(struct ath_softc *, struct ath_txq *);
173 static void	ath_tx_cleanup(struct ath_softc *);
174 static void	ath_tx_proc_q0(void *, int);
175 static void	ath_tx_proc_q0123(void *, int);
176 static void	ath_tx_proc(void *, int);
177 static void	ath_txq_sched_tasklet(void *, int);
178 static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
179 static void	ath_draintxq(struct ath_softc *, ATH_RESET_TYPE reset_type);
180 static void	ath_chan_change(struct ath_softc *, struct ieee80211_channel *);
181 static void	ath_scan_start(struct ieee80211com *);
182 static void	ath_scan_end(struct ieee80211com *);
183 static void	ath_set_channel(struct ieee80211com *);
184 #ifdef	ATH_ENABLE_11N
185 static void	ath_update_chw(struct ieee80211com *);
186 #endif	/* ATH_ENABLE_11N */
187 static void	ath_calibrate(void *);
188 static int	ath_newstate(struct ieee80211vap *, enum ieee80211_state, int);
189 static void	ath_setup_stationkey(struct ieee80211_node *);
190 static void	ath_newassoc(struct ieee80211_node *, int);
191 static int	ath_setregdomain(struct ieee80211com *,
192 		    struct ieee80211_regdomain *, int,
193 		    struct ieee80211_channel []);
194 static void	ath_getradiocaps(struct ieee80211com *, int, int *,
195 		    struct ieee80211_channel []);
196 static int	ath_getchannels(struct ath_softc *);
197 
198 static int	ath_rate_setup(struct ath_softc *, u_int mode);
199 static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
200 
201 static void	ath_announce(struct ath_softc *);
202 
203 static void	ath_dfs_tasklet(void *, int);
204 
205 #ifdef IEEE80211_SUPPORT_TDMA
206 #include <dev/ath/if_ath_tdma.h>
207 #endif
208 
209 #if 0
210 #define	TDMA_EP_MULTIPLIER	(1<<10) /* pow2 to optimize out * and / */
211 #define	TDMA_LPF_LEN		6
212 #define	TDMA_DUMMY_MARKER	0x127
213 #define	TDMA_EP_MUL(x, mul)	((x) * (mul))
214 #define	TDMA_IN(x)		(TDMA_EP_MUL((x), TDMA_EP_MULTIPLIER))
215 #define	TDMA_LPF(x, y, len) \
216     ((x != TDMA_DUMMY_MARKER) ? (((x) * ((len)-1) + (y)) / (len)) : (y))
217 #define	TDMA_SAMPLE(x, y) do {					\
218 	x = TDMA_LPF((x), TDMA_IN(y), TDMA_LPF_LEN);		\
219 } while (0)
220 #define	TDMA_EP_RND(x,mul) \
221 	((((x)%(mul)) >= ((mul)/2)) ? ((x) + ((mul) - 1)) / (mul) : (x)/(mul))
222 #define	TDMA_AVG(x)		TDMA_EP_RND(x, TDMA_EP_MULTIPLIER)
223 #endif /* IEEE80211_SUPPORT_TDMA */
224 
225 SYSCTL_DECL(_hw_ath);
226 
227 /* XXX validate sysctl values */
228 static	int ath_longcalinterval = 30;		/* long cals every 30 secs */
229 SYSCTL_INT(_hw_ath, OID_AUTO, longcal, CTLFLAG_RW, &ath_longcalinterval,
230 	    0, "long chip calibration interval (secs)");
231 static	int ath_shortcalinterval = 100;		/* short cals every 100 ms */
232 SYSCTL_INT(_hw_ath, OID_AUTO, shortcal, CTLFLAG_RW, &ath_shortcalinterval,
233 	    0, "short chip calibration interval (msecs)");
234 static	int ath_resetcalinterval = 20*60;	/* reset cal state 20 mins */
235 SYSCTL_INT(_hw_ath, OID_AUTO, resetcal, CTLFLAG_RW, &ath_resetcalinterval,
236 	    0, "reset chip calibration results (secs)");
237 static	int ath_anicalinterval = 100;		/* ANI calibration - 100 msec */
238 SYSCTL_INT(_hw_ath, OID_AUTO, anical, CTLFLAG_RW, &ath_anicalinterval,
239 	    0, "ANI calibration (msecs)");
240 
241 static	int ath_rxbuf = ATH_RXBUF;		/* # rx buffers to allocate */
242 SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RW, &ath_rxbuf,
243 	    0, "rx buffers allocated");
244 TUNABLE_INT("hw.ath.rxbuf", &ath_rxbuf);
245 static	int ath_txbuf = ATH_TXBUF;		/* # tx buffers to allocate */
246 SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RW, &ath_txbuf,
247 	    0, "tx buffers allocated");
248 TUNABLE_INT("hw.ath.txbuf", &ath_txbuf);
249 
250 int ath_bstuck_threshold = 4;		/* max missed beacons */
251 SYSCTL_INT(_hw_ath, OID_AUTO, bstuck, CTLFLAG_RW, &ath_bstuck_threshold,
252 	    0, "max missed beacon xmits before chip reset");
253 
254 MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
255 
256 #define	HAL_MODE_HT20 (HAL_MODE_11NG_HT20 | HAL_MODE_11NA_HT20)
257 #define	HAL_MODE_HT40 \
258 	(HAL_MODE_11NG_HT40PLUS | HAL_MODE_11NG_HT40MINUS | \
259 	HAL_MODE_11NA_HT40PLUS | HAL_MODE_11NA_HT40MINUS)
260 int
261 ath_attach(u_int16_t devid, struct ath_softc *sc)
262 {
263 	struct ifnet *ifp;
264 	struct ieee80211com *ic;
265 	struct ath_hal *ah = NULL;
266 	HAL_STATUS status;
267 	int error = 0, i;
268 	u_int wmodes;
269 	uint8_t macaddr[IEEE80211_ADDR_LEN];
270 	int rx_chainmask, tx_chainmask;
271 
272 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
273 
274 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
275 	if (ifp == NULL) {
276 		device_printf(sc->sc_dev, "can not if_alloc()\n");
277 		error = ENOSPC;
278 		goto bad;
279 	}
280 	ic = ifp->if_l2com;
281 
282 	/* set these up early for if_printf use */
283 	if_initname(ifp, device_get_name(sc->sc_dev),
284 		device_get_unit(sc->sc_dev));
285 
286 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh,
287 	    sc->sc_eepromdata, &status);
288 	if (ah == NULL) {
289 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
290 			status);
291 		error = ENXIO;
292 		goto bad;
293 	}
294 	sc->sc_ah = ah;
295 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
296 #ifdef	ATH_DEBUG
297 	sc->sc_debug = ath_debug;
298 #endif
299 
300 	/*
301 	 * Check if the MAC has multi-rate retry support.
302 	 * We do this by trying to setup a fake extended
303 	 * descriptor.  MAC's that don't have support will
304 	 * return false w/o doing anything.  MAC's that do
305 	 * support it will return true w/o doing anything.
306 	 */
307 	sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
308 
309 	/*
310 	 * Check if the device has hardware counters for PHY
311 	 * errors.  If so we need to enable the MIB interrupt
312 	 * so we can act on stat triggers.
313 	 */
314 	if (ath_hal_hwphycounters(ah))
315 		sc->sc_needmib = 1;
316 
317 	/*
318 	 * Get the hardware key cache size.
319 	 */
320 	sc->sc_keymax = ath_hal_keycachesize(ah);
321 	if (sc->sc_keymax > ATH_KEYMAX) {
322 		if_printf(ifp, "Warning, using only %u of %u key cache slots\n",
323 			ATH_KEYMAX, sc->sc_keymax);
324 		sc->sc_keymax = ATH_KEYMAX;
325 	}
326 	/*
327 	 * Reset the key cache since some parts do not
328 	 * reset the contents on initial power up.
329 	 */
330 	for (i = 0; i < sc->sc_keymax; i++)
331 		ath_hal_keyreset(ah, i);
332 
333 	/*
334 	 * Collect the default channel list.
335 	 */
336 	error = ath_getchannels(sc);
337 	if (error != 0)
338 		goto bad;
339 
340 	/*
341 	 * Setup rate tables for all potential media types.
342 	 */
343 	ath_rate_setup(sc, IEEE80211_MODE_11A);
344 	ath_rate_setup(sc, IEEE80211_MODE_11B);
345 	ath_rate_setup(sc, IEEE80211_MODE_11G);
346 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
347 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
348 	ath_rate_setup(sc, IEEE80211_MODE_STURBO_A);
349 	ath_rate_setup(sc, IEEE80211_MODE_11NA);
350 	ath_rate_setup(sc, IEEE80211_MODE_11NG);
351 	ath_rate_setup(sc, IEEE80211_MODE_HALF);
352 	ath_rate_setup(sc, IEEE80211_MODE_QUARTER);
353 
354 	/* NB: setup here so ath_rate_update is happy */
355 	ath_setcurmode(sc, IEEE80211_MODE_11A);
356 
357 	/*
358 	 * Allocate tx+rx descriptors and populate the lists.
359 	 */
360 	error = ath_desc_alloc(sc);
361 	if (error != 0) {
362 		if_printf(ifp, "failed to allocate descriptors: %d\n", error);
363 		goto bad;
364 	}
365 	callout_init_mtx(&sc->sc_cal_ch, &sc->sc_mtx, 0);
366 	callout_init_mtx(&sc->sc_wd_ch, &sc->sc_mtx, 0);
367 
368 	ATH_TXBUF_LOCK_INIT(sc);
369 
370 	sc->sc_tq = taskqueue_create("ath_taskq", M_NOWAIT,
371 		taskqueue_thread_enqueue, &sc->sc_tq);
372 	taskqueue_start_threads(&sc->sc_tq, 1, PI_NET,
373 		"%s taskq", ifp->if_xname);
374 
375 	TASK_INIT(&sc->sc_rxtask, 0, ath_rx_tasklet, sc);
376 	TASK_INIT(&sc->sc_txstarttask, 0, ath_tx_tasklet, sc);
377 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
378 	TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc);
379 	TASK_INIT(&sc->sc_resettask,0, ath_reset_proc, sc);
380 	TASK_INIT(&sc->sc_txqtask,0, ath_txq_sched_tasklet, sc);
381 	TASK_INIT(&sc->sc_fataltask,0, ath_fatal_proc, sc);
382 
383 	/*
384 	 * Allocate hardware transmit queues: one queue for
385 	 * beacon frames and one data queue for each QoS
386 	 * priority.  Note that the hal handles resetting
387 	 * these queues at the needed time.
388 	 *
389 	 * XXX PS-Poll
390 	 */
391 	sc->sc_bhalq = ath_beaconq_setup(ah);
392 	if (sc->sc_bhalq == (u_int) -1) {
393 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
394 		error = EIO;
395 		goto bad2;
396 	}
397 	sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
398 	if (sc->sc_cabq == NULL) {
399 		if_printf(ifp, "unable to setup CAB xmit queue!\n");
400 		error = EIO;
401 		goto bad2;
402 	}
403 	/* NB: insure BK queue is the lowest priority h/w queue */
404 	if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
405 		if_printf(ifp, "unable to setup xmit queue for %s traffic!\n",
406 			ieee80211_wme_acnames[WME_AC_BK]);
407 		error = EIO;
408 		goto bad2;
409 	}
410 	if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
411 	    !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
412 	    !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
413 		/*
414 		 * Not enough hardware tx queues to properly do WME;
415 		 * just punt and assign them all to the same h/w queue.
416 		 * We could do a better job of this if, for example,
417 		 * we allocate queues when we switch from station to
418 		 * AP mode.
419 		 */
420 		if (sc->sc_ac2q[WME_AC_VI] != NULL)
421 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
422 		if (sc->sc_ac2q[WME_AC_BE] != NULL)
423 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
424 		sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
425 		sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
426 		sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
427 	}
428 
429 	/*
430 	 * Special case certain configurations.  Note the
431 	 * CAB queue is handled by these specially so don't
432 	 * include them when checking the txq setup mask.
433 	 */
434 	switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
435 	case 0x01:
436 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
437 		break;
438 	case 0x0f:
439 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
440 		break;
441 	default:
442 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
443 		break;
444 	}
445 
446 	/*
447 	 * Setup rate control.  Some rate control modules
448 	 * call back to change the anntena state so expose
449 	 * the necessary entry points.
450 	 * XXX maybe belongs in struct ath_ratectrl?
451 	 */
452 	sc->sc_setdefantenna = ath_setdefantenna;
453 	sc->sc_rc = ath_rate_attach(sc);
454 	if (sc->sc_rc == NULL) {
455 		error = EIO;
456 		goto bad2;
457 	}
458 
459 	/* Attach DFS module */
460 	if (! ath_dfs_attach(sc)) {
461 		device_printf(sc->sc_dev,
462 		    "%s: unable to attach DFS\n", __func__);
463 		error = EIO;
464 		goto bad2;
465 	}
466 
467 	/* Start DFS processing tasklet */
468 	TASK_INIT(&sc->sc_dfstask, 0, ath_dfs_tasklet, sc);
469 
470 	/* Configure LED state */
471 	sc->sc_blinking = 0;
472 	sc->sc_ledstate = 1;
473 	sc->sc_ledon = 0;			/* low true */
474 	sc->sc_ledidle = (2700*hz)/1000;	/* 2.7sec */
475 	callout_init(&sc->sc_ledtimer, CALLOUT_MPSAFE);
476 
477 	/*
478 	 * Don't setup hardware-based blinking.
479 	 *
480 	 * Although some NICs may have this configured in the
481 	 * default reset register values, the user may wish
482 	 * to alter which pins have which function.
483 	 *
484 	 * The reference driver attaches the MAC network LED to GPIO1 and
485 	 * the MAC power LED to GPIO2.  However, the DWA-552 cardbus
486 	 * NIC has these reversed.
487 	 */
488 	sc->sc_hardled = (1 == 0);
489 	sc->sc_led_net_pin = -1;
490 	sc->sc_led_pwr_pin = -1;
491 	/*
492 	 * Auto-enable soft led processing for IBM cards and for
493 	 * 5211 minipci cards.  Users can also manually enable/disable
494 	 * support with a sysctl.
495 	 */
496 	sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
497 	ath_led_config(sc);
498 	ath_hal_setledstate(ah, HAL_LED_INIT);
499 
500 	ifp->if_softc = sc;
501 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
502 	ifp->if_start = ath_start;
503 	ifp->if_ioctl = ath_ioctl;
504 	ifp->if_init = ath_init;
505 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
506 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
507 	IFQ_SET_READY(&ifp->if_snd);
508 
509 	ic->ic_ifp = ifp;
510 	/* XXX not right but it's not used anywhere important */
511 	ic->ic_phytype = IEEE80211_T_OFDM;
512 	ic->ic_opmode = IEEE80211_M_STA;
513 	ic->ic_caps =
514 		  IEEE80211_C_STA		/* station mode */
515 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
516 		| IEEE80211_C_HOSTAP		/* hostap mode */
517 		| IEEE80211_C_MONITOR		/* monitor mode */
518 		| IEEE80211_C_AHDEMO		/* adhoc demo mode */
519 		| IEEE80211_C_WDS		/* 4-address traffic works */
520 		| IEEE80211_C_MBSS		/* mesh point link mode */
521 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
522 		| IEEE80211_C_SHSLOT		/* short slot time supported */
523 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
524 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
525 		| IEEE80211_C_TXFRAG		/* handle tx frags */
526 #ifdef	ATH_ENABLE_DFS
527 		| IEEE80211_C_DFS		/* Enable radar detection */
528 #endif
529 		;
530 	/*
531 	 * Query the hal to figure out h/w crypto support.
532 	 */
533 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
534 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
535 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
536 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_OCB;
537 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
538 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_CCM;
539 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
540 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_CKIP;
541 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
542 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIP;
543 		/*
544 		 * Check if h/w does the MIC and/or whether the
545 		 * separate key cache entries are required to
546 		 * handle both tx+rx MIC keys.
547 		 */
548 		if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
549 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
550 		/*
551 		 * If the h/w supports storing tx+rx MIC keys
552 		 * in one cache slot automatically enable use.
553 		 */
554 		if (ath_hal_hastkipsplit(ah) ||
555 		    !ath_hal_settkipsplit(ah, AH_FALSE))
556 			sc->sc_splitmic = 1;
557 		/*
558 		 * If the h/w can do TKIP MIC together with WME then
559 		 * we use it; otherwise we force the MIC to be done
560 		 * in software by the net80211 layer.
561 		 */
562 		if (ath_hal_haswmetkipmic(ah))
563 			sc->sc_wmetkipmic = 1;
564 	}
565 	sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR);
566 	/*
567 	 * Check for multicast key search support.
568 	 */
569 	if (ath_hal_hasmcastkeysearch(sc->sc_ah) &&
570 	    !ath_hal_getmcastkeysearch(sc->sc_ah)) {
571 		ath_hal_setmcastkeysearch(sc->sc_ah, 1);
572 	}
573 	sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah);
574 	/*
575 	 * Mark key cache slots associated with global keys
576 	 * as in use.  If we knew TKIP was not to be used we
577 	 * could leave the +32, +64, and +32+64 slots free.
578 	 */
579 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
580 		setbit(sc->sc_keymap, i);
581 		setbit(sc->sc_keymap, i+64);
582 		if (sc->sc_splitmic) {
583 			setbit(sc->sc_keymap, i+32);
584 			setbit(sc->sc_keymap, i+32+64);
585 		}
586 	}
587 	/*
588 	 * TPC support can be done either with a global cap or
589 	 * per-packet support.  The latter is not available on
590 	 * all parts.  We're a bit pedantic here as all parts
591 	 * support a global cap.
592 	 */
593 	if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah))
594 		ic->ic_caps |= IEEE80211_C_TXPMGT;
595 
596 	/*
597 	 * Mark WME capability only if we have sufficient
598 	 * hardware queues to do proper priority scheduling.
599 	 */
600 	if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
601 		ic->ic_caps |= IEEE80211_C_WME;
602 	/*
603 	 * Check for misc other capabilities.
604 	 */
605 	if (ath_hal_hasbursting(ah))
606 		ic->ic_caps |= IEEE80211_C_BURST;
607 	sc->sc_hasbmask = ath_hal_hasbssidmask(ah);
608 	sc->sc_hasbmatch = ath_hal_hasbssidmatch(ah);
609 	sc->sc_hastsfadd = ath_hal_hastsfadjust(ah);
610 	sc->sc_rxslink = ath_hal_self_linked_final_rxdesc(ah);
611 	sc->sc_rxtsf32 = ath_hal_has_long_rxdesc_tsf(ah);
612 	if (ath_hal_hasfastframes(ah))
613 		ic->ic_caps |= IEEE80211_C_FF;
614 	wmodes = ath_hal_getwirelessmodes(ah);
615 	if (wmodes & (HAL_MODE_108G|HAL_MODE_TURBO))
616 		ic->ic_caps |= IEEE80211_C_TURBOP;
617 #ifdef IEEE80211_SUPPORT_TDMA
618 	if (ath_hal_macversion(ah) > 0x78) {
619 		ic->ic_caps |= IEEE80211_C_TDMA; /* capable of TDMA */
620 		ic->ic_tdma_update = ath_tdma_update;
621 	}
622 #endif
623 
624 	/*
625 	 * TODO: enforce that at least this many frames are available
626 	 * in the txbuf list before allowing data frames (raw or
627 	 * otherwise) to be transmitted.
628 	 */
629 	sc->sc_txq_data_minfree = 10;
630 	/*
631 	 * Leave this as default to maintain legacy behaviour.
632 	 * Shortening the cabq/mcastq may end up causing some
633 	 * undesirable behaviour.
634 	 */
635 	sc->sc_txq_mcastq_maxdepth = ath_txbuf;
636 
637 	/*
638 	 * Allow the TX and RX chainmasks to be overridden by
639 	 * environment variables and/or device.hints.
640 	 *
641 	 * This must be done early - before the hardware is
642 	 * calibrated or before the 802.11n stream calculation
643 	 * is done.
644 	 */
645 	if (resource_int_value(device_get_name(sc->sc_dev),
646 	    device_get_unit(sc->sc_dev), "rx_chainmask",
647 	    &rx_chainmask) == 0) {
648 		device_printf(sc->sc_dev, "Setting RX chainmask to 0x%x\n",
649 		    rx_chainmask);
650 		(void) ath_hal_setrxchainmask(sc->sc_ah, rx_chainmask);
651 	}
652 	if (resource_int_value(device_get_name(sc->sc_dev),
653 	    device_get_unit(sc->sc_dev), "tx_chainmask",
654 	    &tx_chainmask) == 0) {
655 		device_printf(sc->sc_dev, "Setting TX chainmask to 0x%x\n",
656 		    tx_chainmask);
657 		(void) ath_hal_settxchainmask(sc->sc_ah, tx_chainmask);
658 	}
659 
660 	/*
661 	 * The if_ath 11n support is completely not ready for normal use.
662 	 * Enabling this option will likely break everything and everything.
663 	 * Don't think of doing that unless you know what you're doing.
664 	 */
665 
666 #ifdef	ATH_ENABLE_11N
667 	/*
668 	 * Query HT capabilities
669 	 */
670 	if (ath_hal_getcapability(ah, HAL_CAP_HT, 0, NULL) == HAL_OK &&
671 	    (wmodes & (HAL_MODE_HT20 | HAL_MODE_HT40))) {
672 		int rxs, txs;
673 
674 		device_printf(sc->sc_dev, "[HT] enabling HT modes\n");
675 		ic->ic_htcaps = IEEE80211_HTC_HT	/* HT operation */
676 			    | IEEE80211_HTC_AMPDU	/* A-MPDU tx/rx */
677 			    | IEEE80211_HTC_AMSDU	/* A-MSDU tx/rx */
678 			    | IEEE80211_HTCAP_MAXAMSDU_3839
679 			    				/* max A-MSDU length */
680 			    | IEEE80211_HTCAP_SMPS_OFF;	/* SM power save off */
681 			;
682 
683 		/*
684 		 * Enable short-GI for HT20 only if the hardware
685 		 * advertises support.
686 		 * Notably, anything earlier than the AR9287 doesn't.
687 		 */
688 		if ((ath_hal_getcapability(ah,
689 		    HAL_CAP_HT20_SGI, 0, NULL) == HAL_OK) &&
690 		    (wmodes & HAL_MODE_HT20)) {
691 			device_printf(sc->sc_dev,
692 			    "[HT] enabling short-GI in 20MHz mode\n");
693 			ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
694 		}
695 
696 		if (wmodes & HAL_MODE_HT40)
697 			ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40
698 			    |  IEEE80211_HTCAP_SHORTGI40;
699 
700 		/*
701 		 * TX/RX streams need to be taken into account when
702 		 * negotiating which MCS rates it'll receive and
703 		 * what MCS rates are available for TX.
704 		 */
705 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 0, &txs);
706 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 1, &rxs);
707 
708 		ath_hal_getrxchainmask(ah, &sc->sc_rxchainmask);
709 		ath_hal_gettxchainmask(ah, &sc->sc_txchainmask);
710 
711 		ic->ic_txstream = txs;
712 		ic->ic_rxstream = rxs;
713 
714 		(void) ath_hal_getcapability(ah, HAL_CAP_RTS_AGGR_LIMIT, 1,
715 		    &sc->sc_rts_aggr_limit);
716 		if (sc->sc_rts_aggr_limit != (64 * 1024))
717 			device_printf(sc->sc_dev,
718 			    "[HT] RTS aggregates limited to %d KiB\n",
719 			    sc->sc_rts_aggr_limit / 1024);
720 
721 		device_printf(sc->sc_dev,
722 		    "[HT] %d RX streams; %d TX streams\n", rxs, txs);
723 	}
724 #endif
725 
726 	/*
727 	 * Check if the hardware requires PCI register serialisation.
728 	 * Some of the Owl based MACs require this.
729 	 */
730 	if (mp_ncpus > 1 &&
731 	    ath_hal_getcapability(ah, HAL_CAP_SERIALISE_WAR,
732 	     0, NULL) == HAL_OK) {
733 		sc->sc_ah->ah_config.ah_serialise_reg_war = 1;
734 		device_printf(sc->sc_dev,
735 		    "Enabling register serialisation\n");
736 	}
737 
738 	/*
739 	 * Indicate we need the 802.11 header padded to a
740 	 * 32-bit boundary for 4-address and QoS frames.
741 	 */
742 	ic->ic_flags |= IEEE80211_F_DATAPAD;
743 
744 	/*
745 	 * Query the hal about antenna support.
746 	 */
747 	sc->sc_defant = ath_hal_getdefantenna(ah);
748 
749 	/*
750 	 * Not all chips have the VEOL support we want to
751 	 * use with IBSS beacons; check here for it.
752 	 */
753 	sc->sc_hasveol = ath_hal_hasveol(ah);
754 
755 	/* get mac address from hardware */
756 	ath_hal_getmac(ah, macaddr);
757 	if (sc->sc_hasbmask)
758 		ath_hal_getbssidmask(ah, sc->sc_hwbssidmask);
759 
760 	/* NB: used to size node table key mapping array */
761 	ic->ic_max_keyix = sc->sc_keymax;
762 	/* call MI attach routine. */
763 	ieee80211_ifattach(ic, macaddr);
764 	ic->ic_setregdomain = ath_setregdomain;
765 	ic->ic_getradiocaps = ath_getradiocaps;
766 	sc->sc_opmode = HAL_M_STA;
767 
768 	/* override default methods */
769 	ic->ic_newassoc = ath_newassoc;
770 	ic->ic_updateslot = ath_updateslot;
771 	ic->ic_wme.wme_update = ath_wme_update;
772 	ic->ic_vap_create = ath_vap_create;
773 	ic->ic_vap_delete = ath_vap_delete;
774 	ic->ic_raw_xmit = ath_raw_xmit;
775 	ic->ic_update_mcast = ath_update_mcast;
776 	ic->ic_update_promisc = ath_update_promisc;
777 	ic->ic_node_alloc = ath_node_alloc;
778 	sc->sc_node_free = ic->ic_node_free;
779 	ic->ic_node_free = ath_node_free;
780 	sc->sc_node_cleanup = ic->ic_node_cleanup;
781 	ic->ic_node_cleanup = ath_node_cleanup;
782 	ic->ic_node_getsignal = ath_node_getsignal;
783 	ic->ic_scan_start = ath_scan_start;
784 	ic->ic_scan_end = ath_scan_end;
785 	ic->ic_set_channel = ath_set_channel;
786 #ifdef	ATH_ENABLE_11N
787 	/* 802.11n specific - but just override anyway */
788 	sc->sc_addba_request = ic->ic_addba_request;
789 	sc->sc_addba_response = ic->ic_addba_response;
790 	sc->sc_addba_stop = ic->ic_addba_stop;
791 	sc->sc_bar_response = ic->ic_bar_response;
792 	sc->sc_addba_response_timeout = ic->ic_addba_response_timeout;
793 
794 	ic->ic_addba_request = ath_addba_request;
795 	ic->ic_addba_response = ath_addba_response;
796 	ic->ic_addba_response_timeout = ath_addba_response_timeout;
797 	ic->ic_addba_stop = ath_addba_stop;
798 	ic->ic_bar_response = ath_bar_response;
799 
800 	ic->ic_update_chw = ath_update_chw;
801 #endif	/* ATH_ENABLE_11N */
802 
803 	ieee80211_radiotap_attach(ic,
804 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
805 		ATH_TX_RADIOTAP_PRESENT,
806 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
807 		ATH_RX_RADIOTAP_PRESENT);
808 
809 	/*
810 	 * Setup dynamic sysctl's now that country code and
811 	 * regdomain are available from the hal.
812 	 */
813 	ath_sysctlattach(sc);
814 	ath_sysctl_stats_attach(sc);
815 	ath_sysctl_hal_attach(sc);
816 
817 	if (bootverbose)
818 		ieee80211_announce(ic);
819 	ath_announce(sc);
820 	return 0;
821 bad2:
822 	ath_tx_cleanup(sc);
823 	ath_desc_free(sc);
824 bad:
825 	if (ah)
826 		ath_hal_detach(ah);
827 	if (ifp != NULL)
828 		if_free(ifp);
829 	sc->sc_invalid = 1;
830 	return error;
831 }
832 
833 int
834 ath_detach(struct ath_softc *sc)
835 {
836 	struct ifnet *ifp = sc->sc_ifp;
837 
838 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
839 		__func__, ifp->if_flags);
840 
841 	/*
842 	 * NB: the order of these is important:
843 	 * o stop the chip so no more interrupts will fire
844 	 * o call the 802.11 layer before detaching the hal to
845 	 *   insure callbacks into the driver to delete global
846 	 *   key cache entries can be handled
847 	 * o free the taskqueue which drains any pending tasks
848 	 * o reclaim the tx queue data structures after calling
849 	 *   the 802.11 layer as we'll get called back to reclaim
850 	 *   node state and potentially want to use them
851 	 * o to cleanup the tx queues the hal is called, so detach
852 	 *   it last
853 	 * Other than that, it's straightforward...
854 	 */
855 	ath_stop(ifp);
856 	ieee80211_ifdetach(ifp->if_l2com);
857 	taskqueue_free(sc->sc_tq);
858 #ifdef ATH_TX99_DIAG
859 	if (sc->sc_tx99 != NULL)
860 		sc->sc_tx99->detach(sc->sc_tx99);
861 #endif
862 	ath_rate_detach(sc->sc_rc);
863 
864 	ath_dfs_detach(sc);
865 	ath_desc_free(sc);
866 	ath_tx_cleanup(sc);
867 	ath_hal_detach(sc->sc_ah);	/* NB: sets chip in full sleep */
868 	if_free(ifp);
869 
870 	return 0;
871 }
872 
873 /*
874  * MAC address handling for multiple BSS on the same radio.
875  * The first vap uses the MAC address from the EEPROM.  For
876  * subsequent vap's we set the U/L bit (bit 1) in the MAC
877  * address and use the next six bits as an index.
878  */
879 static void
880 assign_address(struct ath_softc *sc, uint8_t mac[IEEE80211_ADDR_LEN], int clone)
881 {
882 	int i;
883 
884 	if (clone && sc->sc_hasbmask) {
885 		/* NB: we only do this if h/w supports multiple bssid */
886 		for (i = 0; i < 8; i++)
887 			if ((sc->sc_bssidmask & (1<<i)) == 0)
888 				break;
889 		if (i != 0)
890 			mac[0] |= (i << 2)|0x2;
891 	} else
892 		i = 0;
893 	sc->sc_bssidmask |= 1<<i;
894 	sc->sc_hwbssidmask[0] &= ~mac[0];
895 	if (i == 0)
896 		sc->sc_nbssid0++;
897 }
898 
899 static void
900 reclaim_address(struct ath_softc *sc, const uint8_t mac[IEEE80211_ADDR_LEN])
901 {
902 	int i = mac[0] >> 2;
903 	uint8_t mask;
904 
905 	if (i != 0 || --sc->sc_nbssid0 == 0) {
906 		sc->sc_bssidmask &= ~(1<<i);
907 		/* recalculate bssid mask from remaining addresses */
908 		mask = 0xff;
909 		for (i = 1; i < 8; i++)
910 			if (sc->sc_bssidmask & (1<<i))
911 				mask &= ~((i<<2)|0x2);
912 		sc->sc_hwbssidmask[0] |= mask;
913 	}
914 }
915 
916 /*
917  * Assign a beacon xmit slot.  We try to space out
918  * assignments so when beacons are staggered the
919  * traffic coming out of the cab q has maximal time
920  * to go out before the next beacon is scheduled.
921  */
922 static int
923 assign_bslot(struct ath_softc *sc)
924 {
925 	u_int slot, free;
926 
927 	free = 0;
928 	for (slot = 0; slot < ATH_BCBUF; slot++)
929 		if (sc->sc_bslot[slot] == NULL) {
930 			if (sc->sc_bslot[(slot+1)%ATH_BCBUF] == NULL &&
931 			    sc->sc_bslot[(slot-1)%ATH_BCBUF] == NULL)
932 				return slot;
933 			free = slot;
934 			/* NB: keep looking for a double slot */
935 		}
936 	return free;
937 }
938 
939 static struct ieee80211vap *
940 ath_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
941     enum ieee80211_opmode opmode, int flags,
942     const uint8_t bssid[IEEE80211_ADDR_LEN],
943     const uint8_t mac0[IEEE80211_ADDR_LEN])
944 {
945 	struct ath_softc *sc = ic->ic_ifp->if_softc;
946 	struct ath_vap *avp;
947 	struct ieee80211vap *vap;
948 	uint8_t mac[IEEE80211_ADDR_LEN];
949 	int needbeacon, error;
950 	enum ieee80211_opmode ic_opmode;
951 
952 	avp = (struct ath_vap *) malloc(sizeof(struct ath_vap),
953 	    M_80211_VAP, M_WAITOK | M_ZERO);
954 	needbeacon = 0;
955 	IEEE80211_ADDR_COPY(mac, mac0);
956 
957 	ATH_LOCK(sc);
958 	ic_opmode = opmode;		/* default to opmode of new vap */
959 	switch (opmode) {
960 	case IEEE80211_M_STA:
961 		if (sc->sc_nstavaps != 0) {	/* XXX only 1 for now */
962 			device_printf(sc->sc_dev, "only 1 sta vap supported\n");
963 			goto bad;
964 		}
965 		if (sc->sc_nvaps) {
966 			/*
967 			 * With multiple vaps we must fall back
968 			 * to s/w beacon miss handling.
969 			 */
970 			flags |= IEEE80211_CLONE_NOBEACONS;
971 		}
972 		if (flags & IEEE80211_CLONE_NOBEACONS) {
973 			/*
974 			 * Station mode w/o beacons are implemented w/ AP mode.
975 			 */
976 			ic_opmode = IEEE80211_M_HOSTAP;
977 		}
978 		break;
979 	case IEEE80211_M_IBSS:
980 		if (sc->sc_nvaps != 0) {	/* XXX only 1 for now */
981 			device_printf(sc->sc_dev,
982 			    "only 1 ibss vap supported\n");
983 			goto bad;
984 		}
985 		needbeacon = 1;
986 		break;
987 	case IEEE80211_M_AHDEMO:
988 #ifdef IEEE80211_SUPPORT_TDMA
989 		if (flags & IEEE80211_CLONE_TDMA) {
990 			if (sc->sc_nvaps != 0) {
991 				device_printf(sc->sc_dev,
992 				    "only 1 tdma vap supported\n");
993 				goto bad;
994 			}
995 			needbeacon = 1;
996 			flags |= IEEE80211_CLONE_NOBEACONS;
997 		}
998 		/* fall thru... */
999 #endif
1000 	case IEEE80211_M_MONITOR:
1001 		if (sc->sc_nvaps != 0 && ic->ic_opmode != opmode) {
1002 			/*
1003 			 * Adopt existing mode.  Adding a monitor or ahdemo
1004 			 * vap to an existing configuration is of dubious
1005 			 * value but should be ok.
1006 			 */
1007 			/* XXX not right for monitor mode */
1008 			ic_opmode = ic->ic_opmode;
1009 		}
1010 		break;
1011 	case IEEE80211_M_HOSTAP:
1012 	case IEEE80211_M_MBSS:
1013 		needbeacon = 1;
1014 		break;
1015 	case IEEE80211_M_WDS:
1016 		if (sc->sc_nvaps != 0 && ic->ic_opmode == IEEE80211_M_STA) {
1017 			device_printf(sc->sc_dev,
1018 			    "wds not supported in sta mode\n");
1019 			goto bad;
1020 		}
1021 		/*
1022 		 * Silently remove any request for a unique
1023 		 * bssid; WDS vap's always share the local
1024 		 * mac address.
1025 		 */
1026 		flags &= ~IEEE80211_CLONE_BSSID;
1027 		if (sc->sc_nvaps == 0)
1028 			ic_opmode = IEEE80211_M_HOSTAP;
1029 		else
1030 			ic_opmode = ic->ic_opmode;
1031 		break;
1032 	default:
1033 		device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
1034 		goto bad;
1035 	}
1036 	/*
1037 	 * Check that a beacon buffer is available; the code below assumes it.
1038 	 */
1039 	if (needbeacon & TAILQ_EMPTY(&sc->sc_bbuf)) {
1040 		device_printf(sc->sc_dev, "no beacon buffer available\n");
1041 		goto bad;
1042 	}
1043 
1044 	/* STA, AHDEMO? */
1045 	if (opmode == IEEE80211_M_HOSTAP || opmode == IEEE80211_M_MBSS) {
1046 		assign_address(sc, mac, flags & IEEE80211_CLONE_BSSID);
1047 		ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1048 	}
1049 
1050 	vap = &avp->av_vap;
1051 	/* XXX can't hold mutex across if_alloc */
1052 	ATH_UNLOCK(sc);
1053 	error = ieee80211_vap_setup(ic, vap, name, unit, opmode, flags,
1054 	    bssid, mac);
1055 	ATH_LOCK(sc);
1056 	if (error != 0) {
1057 		device_printf(sc->sc_dev, "%s: error %d creating vap\n",
1058 		    __func__, error);
1059 		goto bad2;
1060 	}
1061 
1062 	/* h/w crypto support */
1063 	vap->iv_key_alloc = ath_key_alloc;
1064 	vap->iv_key_delete = ath_key_delete;
1065 	vap->iv_key_set = ath_key_set;
1066 	vap->iv_key_update_begin = ath_key_update_begin;
1067 	vap->iv_key_update_end = ath_key_update_end;
1068 
1069 	/* override various methods */
1070 	avp->av_recv_mgmt = vap->iv_recv_mgmt;
1071 	vap->iv_recv_mgmt = ath_recv_mgmt;
1072 	vap->iv_reset = ath_reset_vap;
1073 	vap->iv_update_beacon = ath_beacon_update;
1074 	avp->av_newstate = vap->iv_newstate;
1075 	vap->iv_newstate = ath_newstate;
1076 	avp->av_bmiss = vap->iv_bmiss;
1077 	vap->iv_bmiss = ath_bmiss_vap;
1078 
1079 	/* Set default parameters */
1080 
1081 	/*
1082 	 * Anything earlier than some AR9300 series MACs don't
1083 	 * support a smaller MPDU density.
1084 	 */
1085 	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_8;
1086 	/*
1087 	 * All NICs can handle the maximum size, however
1088 	 * AR5416 based MACs can only TX aggregates w/ RTS
1089 	 * protection when the total aggregate size is <= 8k.
1090 	 * However, for now that's enforced by the TX path.
1091 	 */
1092 	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K;
1093 
1094 	avp->av_bslot = -1;
1095 	if (needbeacon) {
1096 		/*
1097 		 * Allocate beacon state and setup the q for buffered
1098 		 * multicast frames.  We know a beacon buffer is
1099 		 * available because we checked above.
1100 		 */
1101 		avp->av_bcbuf = TAILQ_FIRST(&sc->sc_bbuf);
1102 		TAILQ_REMOVE(&sc->sc_bbuf, avp->av_bcbuf, bf_list);
1103 		if (opmode != IEEE80211_M_IBSS || !sc->sc_hasveol) {
1104 			/*
1105 			 * Assign the vap to a beacon xmit slot.  As above
1106 			 * this cannot fail to find a free one.
1107 			 */
1108 			avp->av_bslot = assign_bslot(sc);
1109 			KASSERT(sc->sc_bslot[avp->av_bslot] == NULL,
1110 			    ("beacon slot %u not empty", avp->av_bslot));
1111 			sc->sc_bslot[avp->av_bslot] = vap;
1112 			sc->sc_nbcnvaps++;
1113 		}
1114 		if (sc->sc_hastsfadd && sc->sc_nbcnvaps > 0) {
1115 			/*
1116 			 * Multple vaps are to transmit beacons and we
1117 			 * have h/w support for TSF adjusting; enable
1118 			 * use of staggered beacons.
1119 			 */
1120 			sc->sc_stagbeacons = 1;
1121 		}
1122 		ath_txq_init(sc, &avp->av_mcastq, ATH_TXQ_SWQ);
1123 	}
1124 
1125 	ic->ic_opmode = ic_opmode;
1126 	if (opmode != IEEE80211_M_WDS) {
1127 		sc->sc_nvaps++;
1128 		if (opmode == IEEE80211_M_STA)
1129 			sc->sc_nstavaps++;
1130 		if (opmode == IEEE80211_M_MBSS)
1131 			sc->sc_nmeshvaps++;
1132 	}
1133 	switch (ic_opmode) {
1134 	case IEEE80211_M_IBSS:
1135 		sc->sc_opmode = HAL_M_IBSS;
1136 		break;
1137 	case IEEE80211_M_STA:
1138 		sc->sc_opmode = HAL_M_STA;
1139 		break;
1140 	case IEEE80211_M_AHDEMO:
1141 #ifdef IEEE80211_SUPPORT_TDMA
1142 		if (vap->iv_caps & IEEE80211_C_TDMA) {
1143 			sc->sc_tdma = 1;
1144 			/* NB: disable tsf adjust */
1145 			sc->sc_stagbeacons = 0;
1146 		}
1147 		/*
1148 		 * NB: adhoc demo mode is a pseudo mode; to the hal it's
1149 		 * just ap mode.
1150 		 */
1151 		/* fall thru... */
1152 #endif
1153 	case IEEE80211_M_HOSTAP:
1154 	case IEEE80211_M_MBSS:
1155 		sc->sc_opmode = HAL_M_HOSTAP;
1156 		break;
1157 	case IEEE80211_M_MONITOR:
1158 		sc->sc_opmode = HAL_M_MONITOR;
1159 		break;
1160 	default:
1161 		/* XXX should not happen */
1162 		break;
1163 	}
1164 	if (sc->sc_hastsfadd) {
1165 		/*
1166 		 * Configure whether or not TSF adjust should be done.
1167 		 */
1168 		ath_hal_settsfadjust(sc->sc_ah, sc->sc_stagbeacons);
1169 	}
1170 	if (flags & IEEE80211_CLONE_NOBEACONS) {
1171 		/*
1172 		 * Enable s/w beacon miss handling.
1173 		 */
1174 		sc->sc_swbmiss = 1;
1175 	}
1176 	ATH_UNLOCK(sc);
1177 
1178 	/* complete setup */
1179 	ieee80211_vap_attach(vap, ath_media_change, ieee80211_media_status);
1180 	return vap;
1181 bad2:
1182 	reclaim_address(sc, mac);
1183 	ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1184 bad:
1185 	free(avp, M_80211_VAP);
1186 	ATH_UNLOCK(sc);
1187 	return NULL;
1188 }
1189 
1190 static void
1191 ath_vap_delete(struct ieee80211vap *vap)
1192 {
1193 	struct ieee80211com *ic = vap->iv_ic;
1194 	struct ifnet *ifp = ic->ic_ifp;
1195 	struct ath_softc *sc = ifp->if_softc;
1196 	struct ath_hal *ah = sc->sc_ah;
1197 	struct ath_vap *avp = ATH_VAP(vap);
1198 
1199 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
1200 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1201 		/*
1202 		 * Quiesce the hardware while we remove the vap.  In
1203 		 * particular we need to reclaim all references to
1204 		 * the vap state by any frames pending on the tx queues.
1205 		 */
1206 		ath_hal_intrset(ah, 0);		/* disable interrupts */
1207 		ath_draintxq(sc, ATH_RESET_DEFAULT);		/* stop hw xmit side */
1208 		/* XXX Do all frames from all vaps/nodes need draining here? */
1209 		ath_stoprecv(sc, 1);		/* stop recv side */
1210 	}
1211 
1212 	ieee80211_vap_detach(vap);
1213 
1214 	/*
1215 	 * XXX Danger Will Robinson! Danger!
1216 	 *
1217 	 * Because ieee80211_vap_detach() can queue a frame (the station
1218 	 * diassociate message?) after we've drained the TXQ and
1219 	 * flushed the software TXQ, we will end up with a frame queued
1220 	 * to a node whose vap is about to be freed.
1221 	 *
1222 	 * To work around this, flush the hardware/software again.
1223 	 * This may be racy - the ath task may be running and the packet
1224 	 * may be being scheduled between sw->hw txq. Tsk.
1225 	 *
1226 	 * TODO: figure out why a new node gets allocated somewhere around
1227 	 * here (after the ath_tx_swq() call; and after an ath_stop_locked()
1228 	 * call!)
1229 	 */
1230 
1231 	ath_draintxq(sc, ATH_RESET_DEFAULT);
1232 
1233 	ATH_LOCK(sc);
1234 	/*
1235 	 * Reclaim beacon state.  Note this must be done before
1236 	 * the vap instance is reclaimed as we may have a reference
1237 	 * to it in the buffer for the beacon frame.
1238 	 */
1239 	if (avp->av_bcbuf != NULL) {
1240 		if (avp->av_bslot != -1) {
1241 			sc->sc_bslot[avp->av_bslot] = NULL;
1242 			sc->sc_nbcnvaps--;
1243 		}
1244 		ath_beacon_return(sc, avp->av_bcbuf);
1245 		avp->av_bcbuf = NULL;
1246 		if (sc->sc_nbcnvaps == 0) {
1247 			sc->sc_stagbeacons = 0;
1248 			if (sc->sc_hastsfadd)
1249 				ath_hal_settsfadjust(sc->sc_ah, 0);
1250 		}
1251 		/*
1252 		 * Reclaim any pending mcast frames for the vap.
1253 		 */
1254 		ath_tx_draintxq(sc, &avp->av_mcastq);
1255 		ATH_TXQ_LOCK_DESTROY(&avp->av_mcastq);
1256 	}
1257 	/*
1258 	 * Update bookkeeping.
1259 	 */
1260 	if (vap->iv_opmode == IEEE80211_M_STA) {
1261 		sc->sc_nstavaps--;
1262 		if (sc->sc_nstavaps == 0 && sc->sc_swbmiss)
1263 			sc->sc_swbmiss = 0;
1264 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1265 	    vap->iv_opmode == IEEE80211_M_MBSS) {
1266 		reclaim_address(sc, vap->iv_myaddr);
1267 		ath_hal_setbssidmask(ah, sc->sc_hwbssidmask);
1268 		if (vap->iv_opmode == IEEE80211_M_MBSS)
1269 			sc->sc_nmeshvaps--;
1270 	}
1271 	if (vap->iv_opmode != IEEE80211_M_WDS)
1272 		sc->sc_nvaps--;
1273 #ifdef IEEE80211_SUPPORT_TDMA
1274 	/* TDMA operation ceases when the last vap is destroyed */
1275 	if (sc->sc_tdma && sc->sc_nvaps == 0) {
1276 		sc->sc_tdma = 0;
1277 		sc->sc_swbmiss = 0;
1278 	}
1279 #endif
1280 	free(avp, M_80211_VAP);
1281 
1282 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1283 		/*
1284 		 * Restart rx+tx machines if still running (RUNNING will
1285 		 * be reset if we just destroyed the last vap).
1286 		 */
1287 		if (ath_startrecv(sc) != 0)
1288 			if_printf(ifp, "%s: unable to restart recv logic\n",
1289 			    __func__);
1290 		if (sc->sc_beacons) {		/* restart beacons */
1291 #ifdef IEEE80211_SUPPORT_TDMA
1292 			if (sc->sc_tdma)
1293 				ath_tdma_config(sc, NULL);
1294 			else
1295 #endif
1296 				ath_beacon_config(sc, NULL);
1297 		}
1298 		ath_hal_intrset(ah, sc->sc_imask);
1299 	}
1300 	ATH_UNLOCK(sc);
1301 }
1302 
1303 void
1304 ath_suspend(struct ath_softc *sc)
1305 {
1306 	struct ifnet *ifp = sc->sc_ifp;
1307 	struct ieee80211com *ic = ifp->if_l2com;
1308 
1309 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1310 		__func__, ifp->if_flags);
1311 
1312 	sc->sc_resume_up = (ifp->if_flags & IFF_UP) != 0;
1313 	if (ic->ic_opmode == IEEE80211_M_STA)
1314 		ath_stop(ifp);
1315 	else
1316 		ieee80211_suspend_all(ic);
1317 	/*
1318 	 * NB: don't worry about putting the chip in low power
1319 	 * mode; pci will power off our socket on suspend and
1320 	 * CardBus detaches the device.
1321 	 */
1322 
1323 	/*
1324 	 * XXX ensure none of the taskqueues are running
1325 	 * XXX ensure sc_invalid is 1
1326 	 * XXX ensure the calibration callout is disabled
1327 	 */
1328 
1329 	/* Disable the PCIe PHY, complete with workarounds */
1330 	ath_hal_enablepcie(sc->sc_ah, 1, 1);
1331 }
1332 
1333 /*
1334  * Reset the key cache since some parts do not reset the
1335  * contents on resume.  First we clear all entries, then
1336  * re-load keys that the 802.11 layer assumes are setup
1337  * in h/w.
1338  */
1339 static void
1340 ath_reset_keycache(struct ath_softc *sc)
1341 {
1342 	struct ifnet *ifp = sc->sc_ifp;
1343 	struct ieee80211com *ic = ifp->if_l2com;
1344 	struct ath_hal *ah = sc->sc_ah;
1345 	int i;
1346 
1347 	for (i = 0; i < sc->sc_keymax; i++)
1348 		ath_hal_keyreset(ah, i);
1349 	ieee80211_crypto_reload_keys(ic);
1350 }
1351 
1352 void
1353 ath_resume(struct ath_softc *sc)
1354 {
1355 	struct ifnet *ifp = sc->sc_ifp;
1356 	struct ieee80211com *ic = ifp->if_l2com;
1357 	struct ath_hal *ah = sc->sc_ah;
1358 	HAL_STATUS status;
1359 
1360 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1361 		__func__, ifp->if_flags);
1362 
1363 	/* Re-enable PCIe, re-enable the PCIe bus */
1364 	ath_hal_enablepcie(ah, 0, 0);
1365 
1366 	/*
1367 	 * Must reset the chip before we reload the
1368 	 * keycache as we were powered down on suspend.
1369 	 */
1370 	ath_hal_reset(ah, sc->sc_opmode,
1371 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan,
1372 	    AH_FALSE, &status);
1373 	ath_reset_keycache(sc);
1374 
1375 	/* Let DFS at it in case it's a DFS channel */
1376 	ath_dfs_radar_enable(sc, ic->ic_curchan);
1377 
1378 	/* Restore the LED configuration */
1379 	ath_led_config(sc);
1380 	ath_hal_setledstate(ah, HAL_LED_INIT);
1381 
1382 	if (sc->sc_resume_up) {
1383 		if (ic->ic_opmode == IEEE80211_M_STA) {
1384 			ath_init(sc);
1385 			ath_hal_setledstate(ah, HAL_LED_RUN);
1386 			/*
1387 			 * Program the beacon registers using the last rx'd
1388 			 * beacon frame and enable sync on the next beacon
1389 			 * we see.  This should handle the case where we
1390 			 * wakeup and find the same AP and also the case where
1391 			 * we wakeup and need to roam.  For the latter we
1392 			 * should get bmiss events that trigger a roam.
1393 			 */
1394 			ath_beacon_config(sc, NULL);
1395 			sc->sc_syncbeacon = 1;
1396 		} else
1397 			ieee80211_resume_all(ic);
1398 	}
1399 
1400 	/* XXX beacons ? */
1401 }
1402 
1403 void
1404 ath_shutdown(struct ath_softc *sc)
1405 {
1406 	struct ifnet *ifp = sc->sc_ifp;
1407 
1408 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1409 		__func__, ifp->if_flags);
1410 
1411 	ath_stop(ifp);
1412 	/* NB: no point powering down chip as we're about to reboot */
1413 }
1414 
1415 /*
1416  * Interrupt handler.  Most of the actual processing is deferred.
1417  */
1418 void
1419 ath_intr(void *arg)
1420 {
1421 	struct ath_softc *sc = arg;
1422 	struct ifnet *ifp = sc->sc_ifp;
1423 	struct ath_hal *ah = sc->sc_ah;
1424 	HAL_INT status = 0;
1425 	uint32_t txqs;
1426 
1427 	/*
1428 	 * If we're inside a reset path, just print a warning and
1429 	 * clear the ISR. The reset routine will finish it for us.
1430 	 */
1431 	ATH_PCU_LOCK(sc);
1432 	if (sc->sc_inreset_cnt) {
1433 		HAL_INT status;
1434 		ath_hal_getisr(ah, &status);	/* clear ISR */
1435 		ath_hal_intrset(ah, 0);		/* disable further intr's */
1436 		DPRINTF(sc, ATH_DEBUG_ANY,
1437 		    "%s: in reset, ignoring: status=0x%x\n",
1438 		    __func__, status);
1439 		ATH_PCU_UNLOCK(sc);
1440 		return;
1441 	}
1442 
1443 	if (sc->sc_invalid) {
1444 		/*
1445 		 * The hardware is not ready/present, don't touch anything.
1446 		 * Note this can happen early on if the IRQ is shared.
1447 		 */
1448 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
1449 		ATH_PCU_UNLOCK(sc);
1450 		return;
1451 	}
1452 	if (!ath_hal_intrpend(ah)) {		/* shared irq, not for us */
1453 		ATH_PCU_UNLOCK(sc);
1454 		return;
1455 	}
1456 
1457 	if ((ifp->if_flags & IFF_UP) == 0 ||
1458 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1459 		HAL_INT status;
1460 
1461 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1462 			__func__, ifp->if_flags);
1463 		ath_hal_getisr(ah, &status);	/* clear ISR */
1464 		ath_hal_intrset(ah, 0);		/* disable further intr's */
1465 		ATH_PCU_UNLOCK(sc);
1466 		return;
1467 	}
1468 
1469 	/*
1470 	 * Figure out the reason(s) for the interrupt.  Note
1471 	 * that the hal returns a pseudo-ISR that may include
1472 	 * bits we haven't explicitly enabled so we mask the
1473 	 * value to insure we only process bits we requested.
1474 	 */
1475 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
1476 	DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
1477 	CTR1(ATH_KTR_INTR, "ath_intr: mask=0x%.8x", status);
1478 #ifdef	ATH_KTR_INTR_DEBUG
1479 	CTR5(ATH_KTR_INTR,
1480 	    "ath_intr: ISR=0x%.8x, ISR_S0=0x%.8x, ISR_S1=0x%.8x, ISR_S2=0x%.8x, ISR_S5=0x%.8x",
1481 	    ah->ah_intrstate[0],
1482 	    ah->ah_intrstate[1],
1483 	    ah->ah_intrstate[2],
1484 	    ah->ah_intrstate[3],
1485 	    ah->ah_intrstate[6]);
1486 #endif
1487 
1488 	/* Squirrel away SYNC interrupt debugging */
1489 	if (ah->ah_syncstate != 0) {
1490 		int i;
1491 		for (i = 0; i < 32; i++)
1492 			if (ah->ah_syncstate & (i << i))
1493 				sc->sc_intr_stats.sync_intr[i]++;
1494 	}
1495 
1496 	status &= sc->sc_imask;			/* discard unasked for bits */
1497 
1498 	/* Short-circuit un-handled interrupts */
1499 	if (status == 0x0) {
1500 		ATH_PCU_UNLOCK(sc);
1501 		return;
1502 	}
1503 
1504 	/*
1505 	 * Take a note that we're inside the interrupt handler, so
1506 	 * the reset routines know to wait.
1507 	 */
1508 	sc->sc_intr_cnt++;
1509 	ATH_PCU_UNLOCK(sc);
1510 
1511 	/*
1512 	 * Handle the interrupt. We won't run concurrent with the reset
1513 	 * or channel change routines as they'll wait for sc_intr_cnt
1514 	 * to be 0 before continuing.
1515 	 */
1516 	if (status & HAL_INT_FATAL) {
1517 		sc->sc_stats.ast_hardware++;
1518 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
1519 		taskqueue_enqueue(sc->sc_tq, &sc->sc_fataltask);
1520 	} else {
1521 		if (status & HAL_INT_SWBA) {
1522 			/*
1523 			 * Software beacon alert--time to send a beacon.
1524 			 * Handle beacon transmission directly; deferring
1525 			 * this is too slow to meet timing constraints
1526 			 * under load.
1527 			 */
1528 #ifdef IEEE80211_SUPPORT_TDMA
1529 			if (sc->sc_tdma) {
1530 				if (sc->sc_tdmaswba == 0) {
1531 					struct ieee80211com *ic = ifp->if_l2com;
1532 					struct ieee80211vap *vap =
1533 					    TAILQ_FIRST(&ic->ic_vaps);
1534 					ath_tdma_beacon_send(sc, vap);
1535 					sc->sc_tdmaswba =
1536 					    vap->iv_tdma->tdma_bintval;
1537 				} else
1538 					sc->sc_tdmaswba--;
1539 			} else
1540 #endif
1541 			{
1542 				ath_beacon_proc(sc, 0);
1543 #ifdef IEEE80211_SUPPORT_SUPERG
1544 				/*
1545 				 * Schedule the rx taskq in case there's no
1546 				 * traffic so any frames held on the staging
1547 				 * queue are aged and potentially flushed.
1548 				 */
1549 				taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1550 #endif
1551 			}
1552 		}
1553 		if (status & HAL_INT_RXEOL) {
1554 			int imask;
1555 			CTR0(ATH_KTR_ERR, "ath_intr: RXEOL");
1556 			ATH_PCU_LOCK(sc);
1557 			/*
1558 			 * NB: the hardware should re-read the link when
1559 			 *     RXE bit is written, but it doesn't work at
1560 			 *     least on older hardware revs.
1561 			 */
1562 			sc->sc_stats.ast_rxeol++;
1563 			/*
1564 			 * Disable RXEOL/RXORN - prevent an interrupt
1565 			 * storm until the PCU logic can be reset.
1566 			 * In case the interface is reset some other
1567 			 * way before "sc_kickpcu" is called, don't
1568 			 * modify sc_imask - that way if it is reset
1569 			 * by a call to ath_reset() somehow, the
1570 			 * interrupt mask will be correctly reprogrammed.
1571 			 */
1572 			imask = sc->sc_imask;
1573 			imask &= ~(HAL_INT_RXEOL | HAL_INT_RXORN);
1574 			ath_hal_intrset(ah, imask);
1575 			/*
1576 			 * Only blank sc_rxlink if we've not yet kicked
1577 			 * the PCU.
1578 			 *
1579 			 * This isn't entirely correct - the correct solution
1580 			 * would be to have a PCU lock and engage that for
1581 			 * the duration of the PCU fiddling; which would include
1582 			 * running the RX process. Otherwise we could end up
1583 			 * messing up the RX descriptor chain and making the
1584 			 * RX desc list much shorter.
1585 			 */
1586 			if (! sc->sc_kickpcu)
1587 				sc->sc_rxlink = NULL;
1588 			sc->sc_kickpcu = 1;
1589 			/*
1590 			 * Enqueue an RX proc, to handled whatever
1591 			 * is in the RX queue.
1592 			 * This will then kick the PCU.
1593 			 */
1594 			taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1595 			ATH_PCU_UNLOCK(sc);
1596 		}
1597 		if (status & HAL_INT_TXURN) {
1598 			sc->sc_stats.ast_txurn++;
1599 			/* bump tx trigger level */
1600 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
1601 		}
1602 		if (status & HAL_INT_RX) {
1603 			sc->sc_stats.ast_rx_intr++;
1604 			taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1605 		}
1606 		if (status & HAL_INT_TX) {
1607 			sc->sc_stats.ast_tx_intr++;
1608 			/*
1609 			 * Grab all the currently set bits in the HAL txq bitmap
1610 			 * and blank them. This is the only place we should be
1611 			 * doing this.
1612 			 */
1613 			ATH_PCU_LOCK(sc);
1614 			txqs = 0xffffffff;
1615 			ath_hal_gettxintrtxqs(sc->sc_ah, &txqs);
1616 			sc->sc_txq_active |= txqs;
1617 			taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask);
1618 			ATH_PCU_UNLOCK(sc);
1619 		}
1620 		if (status & HAL_INT_BMISS) {
1621 			sc->sc_stats.ast_bmiss++;
1622 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask);
1623 		}
1624 		if (status & HAL_INT_GTT)
1625 			sc->sc_stats.ast_tx_timeout++;
1626 		if (status & HAL_INT_CST)
1627 			sc->sc_stats.ast_tx_cst++;
1628 		if (status & HAL_INT_MIB) {
1629 			sc->sc_stats.ast_mib++;
1630 			ATH_PCU_LOCK(sc);
1631 			/*
1632 			 * Disable interrupts until we service the MIB
1633 			 * interrupt; otherwise it will continue to fire.
1634 			 */
1635 			ath_hal_intrset(ah, 0);
1636 			/*
1637 			 * Let the hal handle the event.  We assume it will
1638 			 * clear whatever condition caused the interrupt.
1639 			 */
1640 			ath_hal_mibevent(ah, &sc->sc_halstats);
1641 			/*
1642 			 * Don't reset the interrupt if we've just
1643 			 * kicked the PCU, or we may get a nested
1644 			 * RXEOL before the rxproc has had a chance
1645 			 * to run.
1646 			 */
1647 			if (sc->sc_kickpcu == 0)
1648 				ath_hal_intrset(ah, sc->sc_imask);
1649 			ATH_PCU_UNLOCK(sc);
1650 		}
1651 		if (status & HAL_INT_RXORN) {
1652 			/* NB: hal marks HAL_INT_FATAL when RXORN is fatal */
1653 			CTR0(ATH_KTR_ERR, "ath_intr: RXORN");
1654 			sc->sc_stats.ast_rxorn++;
1655 		}
1656 	}
1657 	ATH_PCU_LOCK(sc);
1658 	sc->sc_intr_cnt--;
1659 	ATH_PCU_UNLOCK(sc);
1660 }
1661 
1662 static void
1663 ath_fatal_proc(void *arg, int pending)
1664 {
1665 	struct ath_softc *sc = arg;
1666 	struct ifnet *ifp = sc->sc_ifp;
1667 	u_int32_t *state;
1668 	u_int32_t len;
1669 	void *sp;
1670 
1671 	if_printf(ifp, "hardware error; resetting\n");
1672 	/*
1673 	 * Fatal errors are unrecoverable.  Typically these
1674 	 * are caused by DMA errors.  Collect h/w state from
1675 	 * the hal so we can diagnose what's going on.
1676 	 */
1677 	if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) {
1678 		KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len));
1679 		state = sp;
1680 		if_printf(ifp, "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n",
1681 		    state[0], state[1] , state[2], state[3],
1682 		    state[4], state[5]);
1683 	}
1684 	ath_reset(ifp, ATH_RESET_NOLOSS);
1685 }
1686 
1687 static void
1688 ath_bmiss_vap(struct ieee80211vap *vap)
1689 {
1690 	/*
1691 	 * Workaround phantom bmiss interrupts by sanity-checking
1692 	 * the time of our last rx'd frame.  If it is within the
1693 	 * beacon miss interval then ignore the interrupt.  If it's
1694 	 * truly a bmiss we'll get another interrupt soon and that'll
1695 	 * be dispatched up for processing.  Note this applies only
1696 	 * for h/w beacon miss events.
1697 	 */
1698 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
1699 		struct ifnet *ifp = vap->iv_ic->ic_ifp;
1700 		struct ath_softc *sc = ifp->if_softc;
1701 		u_int64_t lastrx = sc->sc_lastrx;
1702 		u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah);
1703 		/* XXX should take a locked ref to iv_bss */
1704 		u_int bmisstimeout =
1705 			vap->iv_bmissthreshold * vap->iv_bss->ni_intval * 1024;
1706 
1707 		DPRINTF(sc, ATH_DEBUG_BEACON,
1708 		    "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n",
1709 		    __func__, (unsigned long long) tsf,
1710 		    (unsigned long long)(tsf - lastrx),
1711 		    (unsigned long long) lastrx, bmisstimeout);
1712 
1713 		if (tsf - lastrx <= bmisstimeout) {
1714 			sc->sc_stats.ast_bmiss_phantom++;
1715 			return;
1716 		}
1717 	}
1718 	ATH_VAP(vap)->av_bmiss(vap);
1719 }
1720 
1721 static int
1722 ath_hal_gethangstate(struct ath_hal *ah, uint32_t mask, uint32_t *hangs)
1723 {
1724 	uint32_t rsize;
1725 	void *sp;
1726 
1727 	if (!ath_hal_getdiagstate(ah, HAL_DIAG_CHECK_HANGS, &mask, sizeof(mask), &sp, &rsize))
1728 		return 0;
1729 	KASSERT(rsize == sizeof(uint32_t), ("resultsize %u", rsize));
1730 	*hangs = *(uint32_t *)sp;
1731 	return 1;
1732 }
1733 
1734 static void
1735 ath_bmiss_proc(void *arg, int pending)
1736 {
1737 	struct ath_softc *sc = arg;
1738 	struct ifnet *ifp = sc->sc_ifp;
1739 	uint32_t hangs;
1740 
1741 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
1742 
1743 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) {
1744 		if_printf(ifp, "bb hang detected (0x%x), resetting\n", hangs);
1745 		ath_reset(ifp, ATH_RESET_NOLOSS);
1746 	} else
1747 		ieee80211_beacon_miss(ifp->if_l2com);
1748 }
1749 
1750 /*
1751  * Handle TKIP MIC setup to deal hardware that doesn't do MIC
1752  * calcs together with WME.  If necessary disable the crypto
1753  * hardware and mark the 802.11 state so keys will be setup
1754  * with the MIC work done in software.
1755  */
1756 static void
1757 ath_settkipmic(struct ath_softc *sc)
1758 {
1759 	struct ifnet *ifp = sc->sc_ifp;
1760 	struct ieee80211com *ic = ifp->if_l2com;
1761 
1762 	if ((ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIP) && !sc->sc_wmetkipmic) {
1763 		if (ic->ic_flags & IEEE80211_F_WME) {
1764 			ath_hal_settkipmic(sc->sc_ah, AH_FALSE);
1765 			ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC;
1766 		} else {
1767 			ath_hal_settkipmic(sc->sc_ah, AH_TRUE);
1768 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
1769 		}
1770 	}
1771 }
1772 
1773 static void
1774 ath_init(void *arg)
1775 {
1776 	struct ath_softc *sc = (struct ath_softc *) arg;
1777 	struct ifnet *ifp = sc->sc_ifp;
1778 	struct ieee80211com *ic = ifp->if_l2com;
1779 	struct ath_hal *ah = sc->sc_ah;
1780 	HAL_STATUS status;
1781 
1782 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1783 		__func__, ifp->if_flags);
1784 
1785 	ATH_LOCK(sc);
1786 	/*
1787 	 * Stop anything previously setup.  This is safe
1788 	 * whether this is the first time through or not.
1789 	 */
1790 	ath_stop_locked(ifp);
1791 
1792 	/*
1793 	 * The basic interface to setting the hardware in a good
1794 	 * state is ``reset''.  On return the hardware is known to
1795 	 * be powered up and with interrupts disabled.  This must
1796 	 * be followed by initialization of the appropriate bits
1797 	 * and then setup of the interrupt mask.
1798 	 */
1799 	ath_settkipmic(sc);
1800 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_FALSE, &status)) {
1801 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
1802 			status);
1803 		ATH_UNLOCK(sc);
1804 		return;
1805 	}
1806 	ath_chan_change(sc, ic->ic_curchan);
1807 
1808 	/* Let DFS at it in case it's a DFS channel */
1809 	ath_dfs_radar_enable(sc, ic->ic_curchan);
1810 
1811 	/*
1812 	 * Likewise this is set during reset so update
1813 	 * state cached in the driver.
1814 	 */
1815 	sc->sc_diversity = ath_hal_getdiversity(ah);
1816 	sc->sc_lastlongcal = 0;
1817 	sc->sc_resetcal = 1;
1818 	sc->sc_lastcalreset = 0;
1819 	sc->sc_lastani = 0;
1820 	sc->sc_lastshortcal = 0;
1821 	sc->sc_doresetcal = AH_FALSE;
1822 	/*
1823 	 * Beacon timers were cleared here; give ath_newstate()
1824 	 * a hint that the beacon timers should be poked when
1825 	 * things transition to the RUN state.
1826 	 */
1827 	sc->sc_beacons = 0;
1828 
1829 	/*
1830 	 * Initial aggregation settings.
1831 	 */
1832 	sc->sc_hwq_limit = ATH_AGGR_MIN_QDEPTH;
1833 	sc->sc_tid_hwq_lo = ATH_AGGR_SCHED_LOW;
1834 	sc->sc_tid_hwq_hi = ATH_AGGR_SCHED_HIGH;
1835 
1836 	/*
1837 	 * Setup the hardware after reset: the key cache
1838 	 * is filled as needed and the receive engine is
1839 	 * set going.  Frame transmit is handled entirely
1840 	 * in the frame output path; there's nothing to do
1841 	 * here except setup the interrupt mask.
1842 	 */
1843 	if (ath_startrecv(sc) != 0) {
1844 		if_printf(ifp, "unable to start recv logic\n");
1845 		ATH_UNLOCK(sc);
1846 		return;
1847 	}
1848 
1849 	/*
1850 	 * Enable interrupts.
1851 	 */
1852 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
1853 		  | HAL_INT_RXEOL | HAL_INT_RXORN
1854 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
1855 	/*
1856 	 * Enable MIB interrupts when there are hardware phy counters.
1857 	 * Note we only do this (at the moment) for station mode.
1858 	 */
1859 	if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
1860 		sc->sc_imask |= HAL_INT_MIB;
1861 
1862 	/* Enable global TX timeout and carrier sense timeout if available */
1863 	if (ath_hal_gtxto_supported(ah))
1864 		sc->sc_imask |= HAL_INT_GTT;
1865 
1866 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: imask=0x%x\n",
1867 		__func__, sc->sc_imask);
1868 
1869 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1870 	callout_reset(&sc->sc_wd_ch, hz, ath_watchdog, sc);
1871 	ath_hal_intrset(ah, sc->sc_imask);
1872 
1873 	ATH_UNLOCK(sc);
1874 
1875 #ifdef ATH_TX99_DIAG
1876 	if (sc->sc_tx99 != NULL)
1877 		sc->sc_tx99->start(sc->sc_tx99);
1878 	else
1879 #endif
1880 	ieee80211_start_all(ic);		/* start all vap's */
1881 }
1882 
1883 static void
1884 ath_stop_locked(struct ifnet *ifp)
1885 {
1886 	struct ath_softc *sc = ifp->if_softc;
1887 	struct ath_hal *ah = sc->sc_ah;
1888 
1889 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
1890 		__func__, sc->sc_invalid, ifp->if_flags);
1891 
1892 	ATH_LOCK_ASSERT(sc);
1893 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1894 		/*
1895 		 * Shutdown the hardware and driver:
1896 		 *    reset 802.11 state machine
1897 		 *    turn off timers
1898 		 *    disable interrupts
1899 		 *    turn off the radio
1900 		 *    clear transmit machinery
1901 		 *    clear receive machinery
1902 		 *    drain and release tx queues
1903 		 *    reclaim beacon resources
1904 		 *    power down hardware
1905 		 *
1906 		 * Note that some of this work is not possible if the
1907 		 * hardware is gone (invalid).
1908 		 */
1909 #ifdef ATH_TX99_DIAG
1910 		if (sc->sc_tx99 != NULL)
1911 			sc->sc_tx99->stop(sc->sc_tx99);
1912 #endif
1913 		callout_stop(&sc->sc_wd_ch);
1914 		sc->sc_wd_timer = 0;
1915 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1916 		if (!sc->sc_invalid) {
1917 			if (sc->sc_softled) {
1918 				callout_stop(&sc->sc_ledtimer);
1919 				ath_hal_gpioset(ah, sc->sc_ledpin,
1920 					!sc->sc_ledon);
1921 				sc->sc_blinking = 0;
1922 			}
1923 			ath_hal_intrset(ah, 0);
1924 		}
1925 		ath_draintxq(sc, ATH_RESET_DEFAULT);
1926 		if (!sc->sc_invalid) {
1927 			ath_stoprecv(sc, 1);
1928 			ath_hal_phydisable(ah);
1929 		} else
1930 			sc->sc_rxlink = NULL;
1931 		ath_beacon_free(sc);	/* XXX not needed */
1932 	}
1933 }
1934 
1935 #define	MAX_TXRX_ITERATIONS	1000
1936 static void
1937 ath_txrx_stop_locked(struct ath_softc *sc)
1938 {
1939 	int i = MAX_TXRX_ITERATIONS;
1940 
1941 	ATH_UNLOCK_ASSERT(sc);
1942 	ATH_PCU_LOCK_ASSERT(sc);
1943 
1944 	/*
1945 	 * Sleep until all the pending operations have completed.
1946 	 *
1947 	 * The caller must ensure that reset has been incremented
1948 	 * or the pending operations may continue being queued.
1949 	 */
1950 	while (sc->sc_rxproc_cnt || sc->sc_txproc_cnt ||
1951 	    sc->sc_txstart_cnt || sc->sc_intr_cnt) {
1952 		if (i <= 0)
1953 			break;
1954 		msleep(sc, &sc->sc_pcu_mtx, 0, "ath_txrx_stop", 1);
1955 		i--;
1956 	}
1957 
1958 	if (i <= 0)
1959 		device_printf(sc->sc_dev,
1960 		    "%s: didn't finish after %d iterations\n",
1961 		    __func__, MAX_TXRX_ITERATIONS);
1962 }
1963 #undef	MAX_TXRX_ITERATIONS
1964 
1965 #if 0
1966 static void
1967 ath_txrx_stop(struct ath_softc *sc)
1968 {
1969 	ATH_UNLOCK_ASSERT(sc);
1970 	ATH_PCU_UNLOCK_ASSERT(sc);
1971 
1972 	ATH_PCU_LOCK(sc);
1973 	ath_txrx_stop_locked(sc);
1974 	ATH_PCU_UNLOCK(sc);
1975 }
1976 #endif
1977 
1978 static void
1979 ath_txrx_start(struct ath_softc *sc)
1980 {
1981 
1982 	taskqueue_unblock(sc->sc_tq);
1983 }
1984 
1985 /*
1986  * Grab the reset lock, and wait around until noone else
1987  * is trying to do anything with it.
1988  *
1989  * This is totally horrible but we can't hold this lock for
1990  * long enough to do TX/RX or we end up with net80211/ip stack
1991  * LORs and eventual deadlock.
1992  *
1993  * "dowait" signals whether to spin, waiting for the reset
1994  * lock count to reach 0. This should (for now) only be used
1995  * during the reset path, as the rest of the code may not
1996  * be locking-reentrant enough to behave correctly.
1997  *
1998  * Another, cleaner way should be found to serialise all of
1999  * these operations.
2000  */
2001 #define	MAX_RESET_ITERATIONS	10
2002 static int
2003 ath_reset_grablock(struct ath_softc *sc, int dowait)
2004 {
2005 	int w = 0;
2006 	int i = MAX_RESET_ITERATIONS;
2007 
2008 	ATH_PCU_LOCK_ASSERT(sc);
2009 	do {
2010 		if (sc->sc_inreset_cnt == 0) {
2011 			w = 1;
2012 			break;
2013 		}
2014 		if (dowait == 0) {
2015 			w = 0;
2016 			break;
2017 		}
2018 		ATH_PCU_UNLOCK(sc);
2019 		pause("ath_reset_grablock", 1);
2020 		i--;
2021 		ATH_PCU_LOCK(sc);
2022 	} while (i > 0);
2023 
2024 	/*
2025 	 * We always increment the refcounter, regardless
2026 	 * of whether we succeeded to get it in an exclusive
2027 	 * way.
2028 	 */
2029 	sc->sc_inreset_cnt++;
2030 
2031 	if (i <= 0)
2032 		device_printf(sc->sc_dev,
2033 		    "%s: didn't finish after %d iterations\n",
2034 		    __func__, MAX_RESET_ITERATIONS);
2035 
2036 	if (w == 0)
2037 		device_printf(sc->sc_dev,
2038 		    "%s: warning, recursive reset path!\n",
2039 		    __func__);
2040 
2041 	return w;
2042 }
2043 #undef MAX_RESET_ITERATIONS
2044 
2045 /*
2046  * XXX TODO: write ath_reset_releaselock
2047  */
2048 
2049 static void
2050 ath_stop(struct ifnet *ifp)
2051 {
2052 	struct ath_softc *sc = ifp->if_softc;
2053 
2054 	ATH_LOCK(sc);
2055 	ath_stop_locked(ifp);
2056 	ATH_UNLOCK(sc);
2057 }
2058 
2059 /*
2060  * Reset the hardware w/o losing operational state.  This is
2061  * basically a more efficient way of doing ath_stop, ath_init,
2062  * followed by state transitions to the current 802.11
2063  * operational state.  Used to recover from various errors and
2064  * to reset or reload hardware state.
2065  */
2066 int
2067 ath_reset(struct ifnet *ifp, ATH_RESET_TYPE reset_type)
2068 {
2069 	struct ath_softc *sc = ifp->if_softc;
2070 	struct ieee80211com *ic = ifp->if_l2com;
2071 	struct ath_hal *ah = sc->sc_ah;
2072 	HAL_STATUS status;
2073 	int i;
2074 
2075 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
2076 
2077 	/* Ensure ATH_LOCK isn't held; ath_rx_proc can't be locked */
2078 	ATH_PCU_UNLOCK_ASSERT(sc);
2079 	ATH_UNLOCK_ASSERT(sc);
2080 
2081 	/* Try to (stop any further TX/RX from occuring */
2082 	taskqueue_block(sc->sc_tq);
2083 
2084 	ATH_PCU_LOCK(sc);
2085 	ath_hal_intrset(ah, 0);		/* disable interrupts */
2086 	ath_txrx_stop_locked(sc);	/* Ensure TX/RX is stopped */
2087 	if (ath_reset_grablock(sc, 1) == 0) {
2088 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
2089 		    __func__);
2090 	}
2091 	ATH_PCU_UNLOCK(sc);
2092 
2093 	/*
2094 	 * Should now wait for pending TX/RX to complete
2095 	 * and block future ones from occuring. This needs to be
2096 	 * done before the TX queue is drained.
2097 	 */
2098 	ath_draintxq(sc, reset_type);	/* stop xmit side */
2099 
2100 	/*
2101 	 * Regardless of whether we're doing a no-loss flush or
2102 	 * not, stop the PCU and handle what's in the RX queue.
2103 	 * That way frames aren't dropped which shouldn't be.
2104 	 */
2105 	ath_stoprecv(sc, (reset_type != ATH_RESET_NOLOSS));
2106 	ath_rx_proc(sc, 0);
2107 
2108 	ath_settkipmic(sc);		/* configure TKIP MIC handling */
2109 	/* NB: indicate channel change so we do a full reset */
2110 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_TRUE, &status))
2111 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
2112 			__func__, status);
2113 	sc->sc_diversity = ath_hal_getdiversity(ah);
2114 
2115 	/* Let DFS at it in case it's a DFS channel */
2116 	ath_dfs_radar_enable(sc, ic->ic_curchan);
2117 
2118 	if (ath_startrecv(sc) != 0)	/* restart recv */
2119 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
2120 	/*
2121 	 * We may be doing a reset in response to an ioctl
2122 	 * that changes the channel so update any state that
2123 	 * might change as a result.
2124 	 */
2125 	ath_chan_change(sc, ic->ic_curchan);
2126 	if (sc->sc_beacons) {		/* restart beacons */
2127 #ifdef IEEE80211_SUPPORT_TDMA
2128 		if (sc->sc_tdma)
2129 			ath_tdma_config(sc, NULL);
2130 		else
2131 #endif
2132 			ath_beacon_config(sc, NULL);
2133 	}
2134 
2135 	/*
2136 	 * Release the reset lock and re-enable interrupts here.
2137 	 * If an interrupt was being processed in ath_intr(),
2138 	 * it would disable interrupts at this point. So we have
2139 	 * to atomically enable interrupts and decrement the
2140 	 * reset counter - this way ath_intr() doesn't end up
2141 	 * disabling interrupts without a corresponding enable
2142 	 * in the rest or channel change path.
2143 	 */
2144 	ATH_PCU_LOCK(sc);
2145 	sc->sc_inreset_cnt--;
2146 	/* XXX only do this if sc_inreset_cnt == 0? */
2147 	ath_hal_intrset(ah, sc->sc_imask);
2148 	ATH_PCU_UNLOCK(sc);
2149 
2150 	/*
2151 	 * TX and RX can be started here. If it were started with
2152 	 * sc_inreset_cnt > 0, the TX and RX path would abort.
2153 	 * Thus if this is a nested call through the reset or
2154 	 * channel change code, TX completion will occur but
2155 	 * RX completion and ath_start / ath_tx_start will not
2156 	 * run.
2157 	 */
2158 
2159 	/* Restart TX/RX as needed */
2160 	ath_txrx_start(sc);
2161 
2162 	/* XXX Restart TX completion and pending TX */
2163 	if (reset_type == ATH_RESET_NOLOSS) {
2164 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
2165 			if (ATH_TXQ_SETUP(sc, i)) {
2166 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
2167 				ath_txq_restart_dma(sc, &sc->sc_txq[i]);
2168 				ath_txq_sched(sc, &sc->sc_txq[i]);
2169 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
2170 			}
2171 		}
2172 	}
2173 
2174 	/*
2175 	 * This may have been set during an ath_start() call which
2176 	 * set this once it detected a concurrent TX was going on.
2177 	 * So, clear it.
2178 	 */
2179 	IF_LOCK(&ifp->if_snd);
2180 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2181 	IF_UNLOCK(&ifp->if_snd);
2182 
2183 	/* Handle any frames in the TX queue */
2184 	/*
2185 	 * XXX should this be done by the caller, rather than
2186 	 * ath_reset() ?
2187 	 */
2188 	ath_start(ifp);			/* restart xmit */
2189 	return 0;
2190 }
2191 
2192 static int
2193 ath_reset_vap(struct ieee80211vap *vap, u_long cmd)
2194 {
2195 	struct ieee80211com *ic = vap->iv_ic;
2196 	struct ifnet *ifp = ic->ic_ifp;
2197 	struct ath_softc *sc = ifp->if_softc;
2198 	struct ath_hal *ah = sc->sc_ah;
2199 
2200 	switch (cmd) {
2201 	case IEEE80211_IOC_TXPOWER:
2202 		/*
2203 		 * If per-packet TPC is enabled, then we have nothing
2204 		 * to do; otherwise we need to force the global limit.
2205 		 * All this can happen directly; no need to reset.
2206 		 */
2207 		if (!ath_hal_gettpc(ah))
2208 			ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
2209 		return 0;
2210 	}
2211 	/* XXX? Full or NOLOSS? */
2212 	return ath_reset(ifp, ATH_RESET_FULL);
2213 }
2214 
2215 struct ath_buf *
2216 _ath_getbuf_locked(struct ath_softc *sc)
2217 {
2218 	struct ath_buf *bf;
2219 
2220 	ATH_TXBUF_LOCK_ASSERT(sc);
2221 
2222 	bf = TAILQ_FIRST(&sc->sc_txbuf);
2223 	if (bf == NULL) {
2224 		sc->sc_stats.ast_tx_getnobuf++;
2225 	} else {
2226 		if (bf->bf_flags & ATH_BUF_BUSY) {
2227 			sc->sc_stats.ast_tx_getbusybuf++;
2228 			bf = NULL;
2229 		}
2230 	}
2231 
2232 	if (bf != NULL && (bf->bf_flags & ATH_BUF_BUSY) == 0)
2233 		TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
2234 	else
2235 		bf = NULL;
2236 
2237 	if (bf == NULL) {
2238 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: %s\n", __func__,
2239 		    TAILQ_FIRST(&sc->sc_txbuf) == NULL ?
2240 			"out of xmit buffers" : "xmit buffer busy");
2241 		return NULL;
2242 	}
2243 
2244 	/* Valid bf here; clear some basic fields */
2245 	bf->bf_next = NULL;	/* XXX just to be sure */
2246 	bf->bf_last = NULL;	/* XXX again, just to be sure */
2247 	bf->bf_comp = NULL;	/* XXX again, just to be sure */
2248 	bzero(&bf->bf_state, sizeof(bf->bf_state));
2249 
2250 	return bf;
2251 }
2252 
2253 /*
2254  * When retrying a software frame, buffers marked ATH_BUF_BUSY
2255  * can't be thrown back on the queue as they could still be
2256  * in use by the hardware.
2257  *
2258  * This duplicates the buffer, or returns NULL.
2259  *
2260  * The descriptor is also copied but the link pointers and
2261  * the DMA segments aren't copied; this frame should thus
2262  * be again passed through the descriptor setup/chain routines
2263  * so the link is correct.
2264  *
2265  * The caller must free the buffer using ath_freebuf().
2266  *
2267  * XXX TODO: this call shouldn't fail as it'll cause packet loss
2268  * XXX in the TX pathway when retries are needed.
2269  * XXX Figure out how to keep some buffers free, or factor the
2270  * XXX number of busy buffers into the xmit path (ath_start())
2271  * XXX so we don't over-commit.
2272  */
2273 struct ath_buf *
2274 ath_buf_clone(struct ath_softc *sc, const struct ath_buf *bf)
2275 {
2276 	struct ath_buf *tbf;
2277 
2278 	tbf = ath_getbuf(sc);
2279 	if (tbf == NULL)
2280 		return NULL;	/* XXX failure? Why? */
2281 
2282 	/* Copy basics */
2283 	tbf->bf_next = NULL;
2284 	tbf->bf_nseg = bf->bf_nseg;
2285 	tbf->bf_flags = bf->bf_flags & ~ATH_BUF_BUSY;
2286 	tbf->bf_status = bf->bf_status;
2287 	tbf->bf_m = bf->bf_m;
2288 	tbf->bf_node = bf->bf_node;
2289 	/* will be setup by the chain/setup function */
2290 	tbf->bf_lastds = NULL;
2291 	/* for now, last == self */
2292 	tbf->bf_last = tbf;
2293 	tbf->bf_comp = bf->bf_comp;
2294 
2295 	/* NOTE: DMA segments will be setup by the setup/chain functions */
2296 
2297 	/* The caller has to re-init the descriptor + links */
2298 
2299 	/* Copy state */
2300 	memcpy(&tbf->bf_state, &bf->bf_state, sizeof(bf->bf_state));
2301 
2302 	return tbf;
2303 }
2304 
2305 struct ath_buf *
2306 ath_getbuf(struct ath_softc *sc)
2307 {
2308 	struct ath_buf *bf;
2309 
2310 	ATH_TXBUF_LOCK(sc);
2311 	bf = _ath_getbuf_locked(sc);
2312 	ATH_TXBUF_UNLOCK(sc);
2313 	if (bf == NULL) {
2314 		struct ifnet *ifp = sc->sc_ifp;
2315 
2316 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: stop queue\n", __func__);
2317 		sc->sc_stats.ast_tx_qstop++;
2318 		IF_LOCK(&ifp->if_snd);
2319 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2320 		IF_UNLOCK(&ifp->if_snd);
2321 	}
2322 	return bf;
2323 }
2324 
2325 void
2326 ath_start(struct ifnet *ifp)
2327 {
2328 	struct ath_softc *sc = ifp->if_softc;
2329 
2330 	taskqueue_enqueue(sc->sc_tq, &sc->sc_txstarttask);
2331 }
2332 
2333 void
2334 ath_tx_tasklet(void *arg, int npending)
2335 {
2336 	struct ath_softc *sc = (struct ath_softc *) arg;
2337 	struct ifnet *ifp = sc->sc_ifp;
2338 	struct ieee80211_node *ni;
2339 	struct ath_buf *bf;
2340 	struct mbuf *m, *next;
2341 	ath_bufhead frags;
2342 
2343 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid)
2344 		return;
2345 
2346 	/* XXX is it ok to hold the ATH_LOCK here? */
2347 	ATH_PCU_LOCK(sc);
2348 	if (sc->sc_inreset_cnt > 0) {
2349 		device_printf(sc->sc_dev,
2350 		    "%s: sc_inreset_cnt > 0; bailing\n", __func__);
2351 		ATH_PCU_UNLOCK(sc);
2352 		IF_LOCK(&ifp->if_snd);
2353 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2354 		IF_UNLOCK(&ifp->if_snd);
2355 		return;
2356 	}
2357 	sc->sc_txstart_cnt++;
2358 	ATH_PCU_UNLOCK(sc);
2359 
2360 	for (;;) {
2361 		/*
2362 		 * Grab a TX buffer and associated resources.
2363 		 */
2364 		bf = ath_getbuf(sc);
2365 		if (bf == NULL)
2366 			break;
2367 
2368 		IFQ_DEQUEUE(&ifp->if_snd, m);
2369 		if (m == NULL) {
2370 			ATH_TXBUF_LOCK(sc);
2371 			TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
2372 			ATH_TXBUF_UNLOCK(sc);
2373 			break;
2374 		}
2375 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
2376 		/*
2377 		 * Check for fragmentation.  If this frame
2378 		 * has been broken up verify we have enough
2379 		 * buffers to send all the fragments so all
2380 		 * go out or none...
2381 		 */
2382 		TAILQ_INIT(&frags);
2383 		if ((m->m_flags & M_FRAG) &&
2384 		    !ath_txfrag_setup(sc, &frags, m, ni)) {
2385 			DPRINTF(sc, ATH_DEBUG_XMIT,
2386 			    "%s: out of txfrag buffers\n", __func__);
2387 			sc->sc_stats.ast_tx_nofrag++;
2388 			ifp->if_oerrors++;
2389 			ath_freetx(m);
2390 			goto bad;
2391 		}
2392 		ifp->if_opackets++;
2393 	nextfrag:
2394 		/*
2395 		 * Pass the frame to the h/w for transmission.
2396 		 * Fragmented frames have each frag chained together
2397 		 * with m_nextpkt.  We know there are sufficient ath_buf's
2398 		 * to send all the frags because of work done by
2399 		 * ath_txfrag_setup.  We leave m_nextpkt set while
2400 		 * calling ath_tx_start so it can use it to extend the
2401 		 * the tx duration to cover the subsequent frag and
2402 		 * so it can reclaim all the mbufs in case of an error;
2403 		 * ath_tx_start clears m_nextpkt once it commits to
2404 		 * handing the frame to the hardware.
2405 		 */
2406 		next = m->m_nextpkt;
2407 		if (ath_tx_start(sc, ni, bf, m)) {
2408 	bad:
2409 			ifp->if_oerrors++;
2410 	reclaim:
2411 			bf->bf_m = NULL;
2412 			bf->bf_node = NULL;
2413 			ATH_TXBUF_LOCK(sc);
2414 			TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
2415 			ath_txfrag_cleanup(sc, &frags, ni);
2416 			ATH_TXBUF_UNLOCK(sc);
2417 			if (ni != NULL)
2418 				ieee80211_free_node(ni);
2419 			continue;
2420 		}
2421 		if (next != NULL) {
2422 			/*
2423 			 * Beware of state changing between frags.
2424 			 * XXX check sta power-save state?
2425 			 */
2426 			if (ni->ni_vap->iv_state != IEEE80211_S_RUN) {
2427 				DPRINTF(sc, ATH_DEBUG_XMIT,
2428 				    "%s: flush fragmented packet, state %s\n",
2429 				    __func__,
2430 				    ieee80211_state_name[ni->ni_vap->iv_state]);
2431 				ath_freetx(next);
2432 				goto reclaim;
2433 			}
2434 			m = next;
2435 			bf = TAILQ_FIRST(&frags);
2436 			KASSERT(bf != NULL, ("no buf for txfrag"));
2437 			TAILQ_REMOVE(&frags, bf, bf_list);
2438 			goto nextfrag;
2439 		}
2440 
2441 		sc->sc_wd_timer = 5;
2442 	}
2443 
2444 	ATH_PCU_LOCK(sc);
2445 	sc->sc_txstart_cnt--;
2446 	ATH_PCU_UNLOCK(sc);
2447 }
2448 
2449 static int
2450 ath_media_change(struct ifnet *ifp)
2451 {
2452 	int error = ieee80211_media_change(ifp);
2453 	/* NB: only the fixed rate can change and that doesn't need a reset */
2454 	return (error == ENETRESET ? 0 : error);
2455 }
2456 
2457 /*
2458  * Block/unblock tx+rx processing while a key change is done.
2459  * We assume the caller serializes key management operations
2460  * so we only need to worry about synchronization with other
2461  * uses that originate in the driver.
2462  */
2463 static void
2464 ath_key_update_begin(struct ieee80211vap *vap)
2465 {
2466 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2467 	struct ath_softc *sc = ifp->if_softc;
2468 
2469 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
2470 	taskqueue_block(sc->sc_tq);
2471 	IF_LOCK(&ifp->if_snd);		/* NB: doesn't block mgmt frames */
2472 }
2473 
2474 static void
2475 ath_key_update_end(struct ieee80211vap *vap)
2476 {
2477 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2478 	struct ath_softc *sc = ifp->if_softc;
2479 
2480 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
2481 	IF_UNLOCK(&ifp->if_snd);
2482 	taskqueue_unblock(sc->sc_tq);
2483 }
2484 
2485 static void
2486 ath_update_promisc(struct ifnet *ifp)
2487 {
2488 	struct ath_softc *sc = ifp->if_softc;
2489 	u_int32_t rfilt;
2490 
2491 	/* configure rx filter */
2492 	rfilt = ath_calcrxfilter(sc);
2493 	ath_hal_setrxfilter(sc->sc_ah, rfilt);
2494 
2495 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x\n", __func__, rfilt);
2496 }
2497 
2498 static void
2499 ath_update_mcast(struct ifnet *ifp)
2500 {
2501 	struct ath_softc *sc = ifp->if_softc;
2502 	u_int32_t mfilt[2];
2503 
2504 	/* calculate and install multicast filter */
2505 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
2506 		struct ifmultiaddr *ifma;
2507 		/*
2508 		 * Merge multicast addresses to form the hardware filter.
2509 		 */
2510 		mfilt[0] = mfilt[1] = 0;
2511 		if_maddr_rlock(ifp);	/* XXX need some fiddling to remove? */
2512 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2513 			caddr_t dl;
2514 			u_int32_t val;
2515 			u_int8_t pos;
2516 
2517 			/* calculate XOR of eight 6bit values */
2518 			dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
2519 			val = LE_READ_4(dl + 0);
2520 			pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2521 			val = LE_READ_4(dl + 3);
2522 			pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2523 			pos &= 0x3f;
2524 			mfilt[pos / 32] |= (1 << (pos % 32));
2525 		}
2526 		if_maddr_runlock(ifp);
2527 	} else
2528 		mfilt[0] = mfilt[1] = ~0;
2529 	ath_hal_setmcastfilter(sc->sc_ah, mfilt[0], mfilt[1]);
2530 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: MC filter %08x:%08x\n",
2531 		__func__, mfilt[0], mfilt[1]);
2532 }
2533 
2534 void
2535 ath_mode_init(struct ath_softc *sc)
2536 {
2537 	struct ifnet *ifp = sc->sc_ifp;
2538 	struct ath_hal *ah = sc->sc_ah;
2539 	u_int32_t rfilt;
2540 
2541 	/* configure rx filter */
2542 	rfilt = ath_calcrxfilter(sc);
2543 	ath_hal_setrxfilter(ah, rfilt);
2544 
2545 	/* configure operational mode */
2546 	ath_hal_setopmode(ah);
2547 
2548 	/* handle any link-level address change */
2549 	ath_hal_setmac(ah, IF_LLADDR(ifp));
2550 
2551 	/* calculate and install multicast filter */
2552 	ath_update_mcast(ifp);
2553 }
2554 
2555 /*
2556  * Set the slot time based on the current setting.
2557  */
2558 void
2559 ath_setslottime(struct ath_softc *sc)
2560 {
2561 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2562 	struct ath_hal *ah = sc->sc_ah;
2563 	u_int usec;
2564 
2565 	if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan))
2566 		usec = 13;
2567 	else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan))
2568 		usec = 21;
2569 	else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) {
2570 		/* honor short/long slot time only in 11g */
2571 		/* XXX shouldn't honor on pure g or turbo g channel */
2572 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
2573 			usec = HAL_SLOT_TIME_9;
2574 		else
2575 			usec = HAL_SLOT_TIME_20;
2576 	} else
2577 		usec = HAL_SLOT_TIME_9;
2578 
2579 	DPRINTF(sc, ATH_DEBUG_RESET,
2580 	    "%s: chan %u MHz flags 0x%x %s slot, %u usec\n",
2581 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
2582 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec);
2583 
2584 	ath_hal_setslottime(ah, usec);
2585 	sc->sc_updateslot = OK;
2586 }
2587 
2588 /*
2589  * Callback from the 802.11 layer to update the
2590  * slot time based on the current setting.
2591  */
2592 static void
2593 ath_updateslot(struct ifnet *ifp)
2594 {
2595 	struct ath_softc *sc = ifp->if_softc;
2596 	struct ieee80211com *ic = ifp->if_l2com;
2597 
2598 	/*
2599 	 * When not coordinating the BSS, change the hardware
2600 	 * immediately.  For other operation we defer the change
2601 	 * until beacon updates have propagated to the stations.
2602 	 */
2603 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
2604 	    ic->ic_opmode == IEEE80211_M_MBSS)
2605 		sc->sc_updateslot = UPDATE;
2606 	else
2607 		ath_setslottime(sc);
2608 }
2609 
2610 /*
2611  * Append the contents of src to dst; both queues
2612  * are assumed to be locked.
2613  */
2614 void
2615 ath_txqmove(struct ath_txq *dst, struct ath_txq *src)
2616 {
2617 
2618 	ATH_TXQ_LOCK_ASSERT(dst);
2619 	ATH_TXQ_LOCK_ASSERT(src);
2620 
2621 	TAILQ_CONCAT(&dst->axq_q, &src->axq_q, bf_list);
2622 	dst->axq_link = src->axq_link;
2623 	src->axq_link = NULL;
2624 	dst->axq_depth += src->axq_depth;
2625 	dst->axq_aggr_depth += src->axq_aggr_depth;
2626 	src->axq_depth = 0;
2627 	src->axq_aggr_depth = 0;
2628 }
2629 
2630 /*
2631  * Reset the hardware, with no loss.
2632  *
2633  * This can't be used for a general case reset.
2634  */
2635 static void
2636 ath_reset_proc(void *arg, int pending)
2637 {
2638 	struct ath_softc *sc = arg;
2639 	struct ifnet *ifp = sc->sc_ifp;
2640 
2641 #if 0
2642 	if_printf(ifp, "%s: resetting\n", __func__);
2643 #endif
2644 	ath_reset(ifp, ATH_RESET_NOLOSS);
2645 }
2646 
2647 /*
2648  * Reset the hardware after detecting beacons have stopped.
2649  */
2650 static void
2651 ath_bstuck_proc(void *arg, int pending)
2652 {
2653 	struct ath_softc *sc = arg;
2654 	struct ifnet *ifp = sc->sc_ifp;
2655 	uint32_t hangs = 0;
2656 
2657 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0)
2658 		if_printf(ifp, "bb hang detected (0x%x)\n", hangs);
2659 
2660 	if_printf(ifp, "stuck beacon; resetting (bmiss count %u)\n",
2661 		sc->sc_bmisscount);
2662 	sc->sc_stats.ast_bstuck++;
2663 	/*
2664 	 * This assumes that there's no simultaneous channel mode change
2665 	 * occuring.
2666 	 */
2667 	ath_reset(ifp, ATH_RESET_NOLOSS);
2668 }
2669 
2670 static void
2671 ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
2672 {
2673 	bus_addr_t *paddr = (bus_addr_t*) arg;
2674 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
2675 	*paddr = segs->ds_addr;
2676 }
2677 
2678 static int
2679 ath_descdma_setup(struct ath_softc *sc,
2680 	struct ath_descdma *dd, ath_bufhead *head,
2681 	const char *name, int nbuf, int ndesc)
2682 {
2683 #define	DS2PHYS(_dd, _ds) \
2684 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
2685 #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
2686 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
2687 	struct ifnet *ifp = sc->sc_ifp;
2688 	uint8_t *ds;
2689 	struct ath_buf *bf;
2690 	int i, bsize, error;
2691 	int desc_len;
2692 
2693 	desc_len = sizeof(struct ath_desc);
2694 
2695 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers %u desc/buf\n",
2696 	    __func__, name, nbuf, ndesc);
2697 
2698 	dd->dd_name = name;
2699 	dd->dd_desc_len = desc_len * nbuf * ndesc;
2700 
2701 	/*
2702 	 * Merlin work-around:
2703 	 * Descriptors that cross the 4KB boundary can't be used.
2704 	 * Assume one skipped descriptor per 4KB page.
2705 	 */
2706 	if (! ath_hal_split4ktrans(sc->sc_ah)) {
2707 		int numdescpage = 4096 / (desc_len * ndesc);
2708 		dd->dd_desc_len = (nbuf / numdescpage + 1) * 4096;
2709 	}
2710 
2711 	/*
2712 	 * Setup DMA descriptor area.
2713 	 */
2714 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev),	/* parent */
2715 		       PAGE_SIZE, 0,		/* alignment, bounds */
2716 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
2717 		       BUS_SPACE_MAXADDR,	/* highaddr */
2718 		       NULL, NULL,		/* filter, filterarg */
2719 		       dd->dd_desc_len,		/* maxsize */
2720 		       1,			/* nsegments */
2721 		       dd->dd_desc_len,		/* maxsegsize */
2722 		       BUS_DMA_ALLOCNOW,	/* flags */
2723 		       NULL,			/* lockfunc */
2724 		       NULL,			/* lockarg */
2725 		       &dd->dd_dmat);
2726 	if (error != 0) {
2727 		if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name);
2728 		return error;
2729 	}
2730 
2731 	/* allocate descriptors */
2732 	error = bus_dmamap_create(dd->dd_dmat, BUS_DMA_NOWAIT, &dd->dd_dmamap);
2733 	if (error != 0) {
2734 		if_printf(ifp, "unable to create dmamap for %s descriptors, "
2735 			"error %u\n", dd->dd_name, error);
2736 		goto fail0;
2737 	}
2738 
2739 	error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
2740 				 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
2741 				 &dd->dd_dmamap);
2742 	if (error != 0) {
2743 		if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
2744 			"error %u\n", nbuf * ndesc, dd->dd_name, error);
2745 		goto fail1;
2746 	}
2747 
2748 	error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
2749 				dd->dd_desc, dd->dd_desc_len,
2750 				ath_load_cb, &dd->dd_desc_paddr,
2751 				BUS_DMA_NOWAIT);
2752 	if (error != 0) {
2753 		if_printf(ifp, "unable to map %s descriptors, error %u\n",
2754 			dd->dd_name, error);
2755 		goto fail2;
2756 	}
2757 
2758 	ds = (uint8_t *) dd->dd_desc;
2759 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
2760 	    __func__, dd->dd_name, ds, (u_long) dd->dd_desc_len,
2761 	    (caddr_t) dd->dd_desc_paddr, /*XXX*/ (u_long) dd->dd_desc_len);
2762 
2763 	/* allocate rx buffers */
2764 	bsize = sizeof(struct ath_buf) * nbuf;
2765 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
2766 	if (bf == NULL) {
2767 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
2768 			dd->dd_name, bsize);
2769 		goto fail3;
2770 	}
2771 	dd->dd_bufptr = bf;
2772 
2773 	TAILQ_INIT(head);
2774 	for (i = 0; i < nbuf; i++, bf++, ds += (ndesc * desc_len)) {
2775 		bf->bf_desc = (struct ath_desc *) ds;
2776 		bf->bf_daddr = DS2PHYS(dd, ds);
2777 		if (! ath_hal_split4ktrans(sc->sc_ah)) {
2778 			/*
2779 			 * Merlin WAR: Skip descriptor addresses which
2780 			 * cause 4KB boundary crossing along any point
2781 			 * in the descriptor.
2782 			 */
2783 			 if (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr,
2784 			     desc_len * ndesc)) {
2785 				/* Start at the next page */
2786 				ds += 0x1000 - (bf->bf_daddr & 0xFFF);
2787 				bf->bf_desc = (struct ath_desc *) ds;
2788 				bf->bf_daddr = DS2PHYS(dd, ds);
2789 			}
2790 		}
2791 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
2792 				&bf->bf_dmamap);
2793 		if (error != 0) {
2794 			if_printf(ifp, "unable to create dmamap for %s "
2795 				"buffer %u, error %u\n", dd->dd_name, i, error);
2796 			ath_descdma_cleanup(sc, dd, head);
2797 			return error;
2798 		}
2799 		bf->bf_lastds = bf->bf_desc;	/* Just an initial value */
2800 		TAILQ_INSERT_TAIL(head, bf, bf_list);
2801 	}
2802 	return 0;
2803 fail3:
2804 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
2805 fail2:
2806 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
2807 fail1:
2808 	bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
2809 fail0:
2810 	bus_dma_tag_destroy(dd->dd_dmat);
2811 	memset(dd, 0, sizeof(*dd));
2812 	return error;
2813 #undef DS2PHYS
2814 #undef ATH_DESC_4KB_BOUND_CHECK
2815 }
2816 
2817 static void
2818 ath_descdma_cleanup(struct ath_softc *sc,
2819 	struct ath_descdma *dd, ath_bufhead *head)
2820 {
2821 	struct ath_buf *bf;
2822 	struct ieee80211_node *ni;
2823 
2824 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
2825 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
2826 	bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
2827 	bus_dma_tag_destroy(dd->dd_dmat);
2828 
2829 	TAILQ_FOREACH(bf, head, bf_list) {
2830 		if (bf->bf_m) {
2831 			m_freem(bf->bf_m);
2832 			bf->bf_m = NULL;
2833 		}
2834 		if (bf->bf_dmamap != NULL) {
2835 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
2836 			bf->bf_dmamap = NULL;
2837 		}
2838 		ni = bf->bf_node;
2839 		bf->bf_node = NULL;
2840 		if (ni != NULL) {
2841 			/*
2842 			 * Reclaim node reference.
2843 			 */
2844 			ieee80211_free_node(ni);
2845 		}
2846 	}
2847 
2848 	TAILQ_INIT(head);
2849 	free(dd->dd_bufptr, M_ATHDEV);
2850 	memset(dd, 0, sizeof(*dd));
2851 }
2852 
2853 static int
2854 ath_desc_alloc(struct ath_softc *sc)
2855 {
2856 	int error;
2857 
2858 	error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
2859 			"rx", ath_rxbuf, 1);
2860 	if (error != 0)
2861 		return error;
2862 
2863 	error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
2864 			"tx", ath_txbuf, ATH_TXDESC);
2865 	if (error != 0) {
2866 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
2867 		return error;
2868 	}
2869 
2870 	error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
2871 			"beacon", ATH_BCBUF, 1);
2872 	if (error != 0) {
2873 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
2874 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
2875 		return error;
2876 	}
2877 	return 0;
2878 }
2879 
2880 static void
2881 ath_desc_free(struct ath_softc *sc)
2882 {
2883 
2884 	if (sc->sc_bdma.dd_desc_len != 0)
2885 		ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
2886 	if (sc->sc_txdma.dd_desc_len != 0)
2887 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
2888 	if (sc->sc_rxdma.dd_desc_len != 0)
2889 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
2890 }
2891 
2892 static struct ieee80211_node *
2893 ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
2894 {
2895 	struct ieee80211com *ic = vap->iv_ic;
2896 	struct ath_softc *sc = ic->ic_ifp->if_softc;
2897 	const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
2898 	struct ath_node *an;
2899 
2900 	an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
2901 	if (an == NULL) {
2902 		/* XXX stat+msg */
2903 		return NULL;
2904 	}
2905 	ath_rate_node_init(sc, an);
2906 
2907 	/* Setup the mutex - there's no associd yet so set the name to NULL */
2908 	snprintf(an->an_name, sizeof(an->an_name), "%s: node %p",
2909 	    device_get_nameunit(sc->sc_dev), an);
2910 	mtx_init(&an->an_mtx, an->an_name, NULL, MTX_DEF);
2911 
2912 	/* XXX setup ath_tid */
2913 	ath_tx_tid_init(sc, an);
2914 
2915 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: an %p\n", __func__, an);
2916 	return &an->an_node;
2917 }
2918 
2919 static void
2920 ath_node_cleanup(struct ieee80211_node *ni)
2921 {
2922 	struct ieee80211com *ic = ni->ni_ic;
2923 	struct ath_softc *sc = ic->ic_ifp->if_softc;
2924 
2925 	/* Cleanup ath_tid, free unused bufs, unlink bufs in TXQ */
2926 	ath_tx_node_flush(sc, ATH_NODE(ni));
2927 	ath_rate_node_cleanup(sc, ATH_NODE(ni));
2928 	sc->sc_node_cleanup(ni);
2929 }
2930 
2931 static void
2932 ath_node_free(struct ieee80211_node *ni)
2933 {
2934 	struct ieee80211com *ic = ni->ni_ic;
2935 	struct ath_softc *sc = ic->ic_ifp->if_softc;
2936 
2937 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: ni %p\n", __func__, ni);
2938 	mtx_destroy(&ATH_NODE(ni)->an_mtx);
2939 	sc->sc_node_free(ni);
2940 }
2941 
2942 static void
2943 ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
2944 {
2945 	struct ieee80211com *ic = ni->ni_ic;
2946 	struct ath_softc *sc = ic->ic_ifp->if_softc;
2947 	struct ath_hal *ah = sc->sc_ah;
2948 
2949 	*rssi = ic->ic_node_getrssi(ni);
2950 	if (ni->ni_chan != IEEE80211_CHAN_ANYC)
2951 		*noise = ath_hal_getchannoise(ah, ni->ni_chan);
2952 	else
2953 		*noise = -95;		/* nominally correct */
2954 }
2955 
2956 /*
2957  * Set the default antenna.
2958  */
2959 void
2960 ath_setdefantenna(struct ath_softc *sc, u_int antenna)
2961 {
2962 	struct ath_hal *ah = sc->sc_ah;
2963 
2964 	/* XXX block beacon interrupts */
2965 	ath_hal_setdefantenna(ah, antenna);
2966 	if (sc->sc_defant != antenna)
2967 		sc->sc_stats.ast_ant_defswitch++;
2968 	sc->sc_defant = antenna;
2969 	sc->sc_rxotherant = 0;
2970 }
2971 
2972 static void
2973 ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum)
2974 {
2975 	txq->axq_qnum = qnum;
2976 	txq->axq_ac = 0;
2977 	txq->axq_depth = 0;
2978 	txq->axq_aggr_depth = 0;
2979 	txq->axq_intrcnt = 0;
2980 	txq->axq_link = NULL;
2981 	txq->axq_softc = sc;
2982 	TAILQ_INIT(&txq->axq_q);
2983 	TAILQ_INIT(&txq->axq_tidq);
2984 	ATH_TXQ_LOCK_INIT(sc, txq);
2985 }
2986 
2987 /*
2988  * Setup a h/w transmit queue.
2989  */
2990 static struct ath_txq *
2991 ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
2992 {
2993 #define	N(a)	(sizeof(a)/sizeof(a[0]))
2994 	struct ath_hal *ah = sc->sc_ah;
2995 	HAL_TXQ_INFO qi;
2996 	int qnum;
2997 
2998 	memset(&qi, 0, sizeof(qi));
2999 	qi.tqi_subtype = subtype;
3000 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
3001 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
3002 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
3003 	/*
3004 	 * Enable interrupts only for EOL and DESC conditions.
3005 	 * We mark tx descriptors to receive a DESC interrupt
3006 	 * when a tx queue gets deep; otherwise waiting for the
3007 	 * EOL to reap descriptors.  Note that this is done to
3008 	 * reduce interrupt load and this only defers reaping
3009 	 * descriptors, never transmitting frames.  Aside from
3010 	 * reducing interrupts this also permits more concurrency.
3011 	 * The only potential downside is if the tx queue backs
3012 	 * up in which case the top half of the kernel may backup
3013 	 * due to a lack of tx descriptors.
3014 	 */
3015 	qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE | HAL_TXQ_TXDESCINT_ENABLE;
3016 	qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
3017 	if (qnum == -1) {
3018 		/*
3019 		 * NB: don't print a message, this happens
3020 		 * normally on parts with too few tx queues
3021 		 */
3022 		return NULL;
3023 	}
3024 	if (qnum >= N(sc->sc_txq)) {
3025 		device_printf(sc->sc_dev,
3026 			"hal qnum %u out of range, max %zu!\n",
3027 			qnum, N(sc->sc_txq));
3028 		ath_hal_releasetxqueue(ah, qnum);
3029 		return NULL;
3030 	}
3031 	if (!ATH_TXQ_SETUP(sc, qnum)) {
3032 		ath_txq_init(sc, &sc->sc_txq[qnum], qnum);
3033 		sc->sc_txqsetup |= 1<<qnum;
3034 	}
3035 	return &sc->sc_txq[qnum];
3036 #undef N
3037 }
3038 
3039 /*
3040  * Setup a hardware data transmit queue for the specified
3041  * access control.  The hal may not support all requested
3042  * queues in which case it will return a reference to a
3043  * previously setup queue.  We record the mapping from ac's
3044  * to h/w queues for use by ath_tx_start and also track
3045  * the set of h/w queues being used to optimize work in the
3046  * transmit interrupt handler and related routines.
3047  */
3048 static int
3049 ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
3050 {
3051 #define	N(a)	(sizeof(a)/sizeof(a[0]))
3052 	struct ath_txq *txq;
3053 
3054 	if (ac >= N(sc->sc_ac2q)) {
3055 		device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
3056 			ac, N(sc->sc_ac2q));
3057 		return 0;
3058 	}
3059 	txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
3060 	if (txq != NULL) {
3061 		txq->axq_ac = ac;
3062 		sc->sc_ac2q[ac] = txq;
3063 		return 1;
3064 	} else
3065 		return 0;
3066 #undef N
3067 }
3068 
3069 /*
3070  * Update WME parameters for a transmit queue.
3071  */
3072 static int
3073 ath_txq_update(struct ath_softc *sc, int ac)
3074 {
3075 #define	ATH_EXPONENT_TO_VALUE(v)	((1<<v)-1)
3076 #define	ATH_TXOP_TO_US(v)		(v<<5)
3077 	struct ifnet *ifp = sc->sc_ifp;
3078 	struct ieee80211com *ic = ifp->if_l2com;
3079 	struct ath_txq *txq = sc->sc_ac2q[ac];
3080 	struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
3081 	struct ath_hal *ah = sc->sc_ah;
3082 	HAL_TXQ_INFO qi;
3083 
3084 	ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
3085 #ifdef IEEE80211_SUPPORT_TDMA
3086 	if (sc->sc_tdma) {
3087 		/*
3088 		 * AIFS is zero so there's no pre-transmit wait.  The
3089 		 * burst time defines the slot duration and is configured
3090 		 * through net80211.  The QCU is setup to not do post-xmit
3091 		 * back off, lockout all lower-priority QCU's, and fire
3092 		 * off the DMA beacon alert timer which is setup based
3093 		 * on the slot configuration.
3094 		 */
3095 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
3096 			      | HAL_TXQ_TXERRINT_ENABLE
3097 			      | HAL_TXQ_TXURNINT_ENABLE
3098 			      | HAL_TXQ_TXEOLINT_ENABLE
3099 			      | HAL_TXQ_DBA_GATED
3100 			      | HAL_TXQ_BACKOFF_DISABLE
3101 			      | HAL_TXQ_ARB_LOCKOUT_GLOBAL
3102 			      ;
3103 		qi.tqi_aifs = 0;
3104 		/* XXX +dbaprep? */
3105 		qi.tqi_readyTime = sc->sc_tdmaslotlen;
3106 		qi.tqi_burstTime = qi.tqi_readyTime;
3107 	} else {
3108 #endif
3109 		/*
3110 		 * XXX shouldn't this just use the default flags
3111 		 * used in the previous queue setup?
3112 		 */
3113 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
3114 			      | HAL_TXQ_TXERRINT_ENABLE
3115 			      | HAL_TXQ_TXDESCINT_ENABLE
3116 			      | HAL_TXQ_TXURNINT_ENABLE
3117 			      | HAL_TXQ_TXEOLINT_ENABLE
3118 			      ;
3119 		qi.tqi_aifs = wmep->wmep_aifsn;
3120 		qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
3121 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
3122 		qi.tqi_readyTime = 0;
3123 		qi.tqi_burstTime = ATH_TXOP_TO_US(wmep->wmep_txopLimit);
3124 #ifdef IEEE80211_SUPPORT_TDMA
3125 	}
3126 #endif
3127 
3128 	DPRINTF(sc, ATH_DEBUG_RESET,
3129 	    "%s: Q%u qflags 0x%x aifs %u cwmin %u cwmax %u burstTime %u\n",
3130 	    __func__, txq->axq_qnum, qi.tqi_qflags,
3131 	    qi.tqi_aifs, qi.tqi_cwmin, qi.tqi_cwmax, qi.tqi_burstTime);
3132 
3133 	if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
3134 		if_printf(ifp, "unable to update hardware queue "
3135 			"parameters for %s traffic!\n",
3136 			ieee80211_wme_acnames[ac]);
3137 		return 0;
3138 	} else {
3139 		ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
3140 		return 1;
3141 	}
3142 #undef ATH_TXOP_TO_US
3143 #undef ATH_EXPONENT_TO_VALUE
3144 }
3145 
3146 /*
3147  * Callback from the 802.11 layer to update WME parameters.
3148  */
3149 int
3150 ath_wme_update(struct ieee80211com *ic)
3151 {
3152 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3153 
3154 	return !ath_txq_update(sc, WME_AC_BE) ||
3155 	    !ath_txq_update(sc, WME_AC_BK) ||
3156 	    !ath_txq_update(sc, WME_AC_VI) ||
3157 	    !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
3158 }
3159 
3160 /*
3161  * Reclaim resources for a setup queue.
3162  */
3163 static void
3164 ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
3165 {
3166 
3167 	ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
3168 	ATH_TXQ_LOCK_DESTROY(txq);
3169 	sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
3170 }
3171 
3172 /*
3173  * Reclaim all tx queue resources.
3174  */
3175 static void
3176 ath_tx_cleanup(struct ath_softc *sc)
3177 {
3178 	int i;
3179 
3180 	ATH_TXBUF_LOCK_DESTROY(sc);
3181 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
3182 		if (ATH_TXQ_SETUP(sc, i))
3183 			ath_tx_cleanupq(sc, &sc->sc_txq[i]);
3184 }
3185 
3186 /*
3187  * Return h/w rate index for an IEEE rate (w/o basic rate bit)
3188  * using the current rates in sc_rixmap.
3189  */
3190 int
3191 ath_tx_findrix(const struct ath_softc *sc, uint8_t rate)
3192 {
3193 	int rix = sc->sc_rixmap[rate];
3194 	/* NB: return lowest rix for invalid rate */
3195 	return (rix == 0xff ? 0 : rix);
3196 }
3197 
3198 static void
3199 ath_tx_update_stats(struct ath_softc *sc, struct ath_tx_status *ts,
3200     struct ath_buf *bf)
3201 {
3202 	struct ieee80211_node *ni = bf->bf_node;
3203 	struct ifnet *ifp = sc->sc_ifp;
3204 	struct ieee80211com *ic = ifp->if_l2com;
3205 	int sr, lr, pri;
3206 
3207 	if (ts->ts_status == 0) {
3208 		u_int8_t txant = ts->ts_antenna;
3209 		sc->sc_stats.ast_ant_tx[txant]++;
3210 		sc->sc_ant_tx[txant]++;
3211 		if (ts->ts_finaltsi != 0)
3212 			sc->sc_stats.ast_tx_altrate++;
3213 		pri = M_WME_GETAC(bf->bf_m);
3214 		if (pri >= WME_AC_VO)
3215 			ic->ic_wme.wme_hipri_traffic++;
3216 		if ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)
3217 			ni->ni_inact = ni->ni_inact_reload;
3218 	} else {
3219 		if (ts->ts_status & HAL_TXERR_XRETRY)
3220 			sc->sc_stats.ast_tx_xretries++;
3221 		if (ts->ts_status & HAL_TXERR_FIFO)
3222 			sc->sc_stats.ast_tx_fifoerr++;
3223 		if (ts->ts_status & HAL_TXERR_FILT)
3224 			sc->sc_stats.ast_tx_filtered++;
3225 		if (ts->ts_status & HAL_TXERR_XTXOP)
3226 			sc->sc_stats.ast_tx_xtxop++;
3227 		if (ts->ts_status & HAL_TXERR_TIMER_EXPIRED)
3228 			sc->sc_stats.ast_tx_timerexpired++;
3229 
3230 		if (ts->ts_status & HAL_TX_DATA_UNDERRUN)
3231 			sc->sc_stats.ast_tx_data_underrun++;
3232 		if (ts->ts_status & HAL_TX_DELIM_UNDERRUN)
3233 			sc->sc_stats.ast_tx_delim_underrun++;
3234 
3235 		if (bf->bf_m->m_flags & M_FF)
3236 			sc->sc_stats.ast_ff_txerr++;
3237 	}
3238 	/* XXX when is this valid? */
3239 	if (ts->ts_status & HAL_TX_DESC_CFG_ERR)
3240 		sc->sc_stats.ast_tx_desccfgerr++;
3241 
3242 	sr = ts->ts_shortretry;
3243 	lr = ts->ts_longretry;
3244 	sc->sc_stats.ast_tx_shortretry += sr;
3245 	sc->sc_stats.ast_tx_longretry += lr;
3246 
3247 }
3248 
3249 /*
3250  * The default completion. If fail is 1, this means
3251  * "please don't retry the frame, and just return -1 status
3252  * to the net80211 stack.
3253  */
3254 void
3255 ath_tx_default_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
3256 {
3257 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
3258 	int st;
3259 
3260 	if (fail == 1)
3261 		st = -1;
3262 	else
3263 		st = ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) ?
3264 		    ts->ts_status : HAL_TXERR_XRETRY;
3265 
3266 	if (bf->bf_state.bfs_dobaw)
3267 		device_printf(sc->sc_dev,
3268 		    "%s: bf %p: seqno %d: dobaw should've been cleared!\n",
3269 		    __func__,
3270 		    bf,
3271 		    SEQNO(bf->bf_state.bfs_seqno));
3272 	if (bf->bf_next != NULL)
3273 		device_printf(sc->sc_dev,
3274 		    "%s: bf %p: seqno %d: bf_next not NULL!\n",
3275 		    __func__,
3276 		    bf,
3277 		    SEQNO(bf->bf_state.bfs_seqno));
3278 
3279 	/*
3280 	 * Do any tx complete callback.  Note this must
3281 	 * be done before releasing the node reference.
3282 	 * This will free the mbuf, release the net80211
3283 	 * node and recycle the ath_buf.
3284 	 */
3285 	ath_tx_freebuf(sc, bf, st);
3286 }
3287 
3288 /*
3289  * Update rate control with the given completion status.
3290  */
3291 void
3292 ath_tx_update_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
3293     struct ath_rc_series *rc, struct ath_tx_status *ts, int frmlen,
3294     int nframes, int nbad)
3295 {
3296 	struct ath_node *an;
3297 
3298 	/* Only for unicast frames */
3299 	if (ni == NULL)
3300 		return;
3301 
3302 	an = ATH_NODE(ni);
3303 
3304 	if ((ts->ts_status & HAL_TXERR_FILT) == 0) {
3305 		ATH_NODE_LOCK(an);
3306 		ath_rate_tx_complete(sc, an, rc, ts, frmlen, nframes, nbad);
3307 		ATH_NODE_UNLOCK(an);
3308 	}
3309 }
3310 
3311 /*
3312  * Update the busy status of the last frame on the free list.
3313  * When doing TDMA, the busy flag tracks whether the hardware
3314  * currently points to this buffer or not, and thus gated DMA
3315  * may restart by re-reading the last descriptor in this
3316  * buffer.
3317  *
3318  * This should be called in the completion function once one
3319  * of the buffers has been used.
3320  */
3321 static void
3322 ath_tx_update_busy(struct ath_softc *sc)
3323 {
3324 	struct ath_buf *last;
3325 
3326 	/*
3327 	 * Since the last frame may still be marked
3328 	 * as ATH_BUF_BUSY, unmark it here before
3329 	 * finishing the frame processing.
3330 	 * Since we've completed a frame (aggregate
3331 	 * or otherwise), the hardware has moved on
3332 	 * and is no longer referencing the previous
3333 	 * descriptor.
3334 	 */
3335 	ATH_TXBUF_LOCK_ASSERT(sc);
3336 	last = TAILQ_LAST(&sc->sc_txbuf, ath_bufhead_s);
3337 	if (last != NULL)
3338 		last->bf_flags &= ~ATH_BUF_BUSY;
3339 }
3340 
3341 
3342 /*
3343  * Process completed xmit descriptors from the specified queue.
3344  * Kick the packet scheduler if needed. This can occur from this
3345  * particular task.
3346  */
3347 static int
3348 ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq, int dosched)
3349 {
3350 	struct ath_hal *ah = sc->sc_ah;
3351 	struct ath_buf *bf;
3352 	struct ath_desc *ds;
3353 	struct ath_tx_status *ts;
3354 	struct ieee80211_node *ni;
3355 	struct ath_node *an;
3356 #ifdef	IEEE80211_SUPPORT_SUPERG
3357 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3358 #endif	/* IEEE80211_SUPPORT_SUPERG */
3359 	int nacked;
3360 	HAL_STATUS status;
3361 
3362 	DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
3363 		__func__, txq->axq_qnum,
3364 		(caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
3365 		txq->axq_link);
3366 	nacked = 0;
3367 	for (;;) {
3368 		ATH_TXQ_LOCK(txq);
3369 		txq->axq_intrcnt = 0;	/* reset periodic desc intr count */
3370 		bf = TAILQ_FIRST(&txq->axq_q);
3371 		if (bf == NULL) {
3372 			ATH_TXQ_UNLOCK(txq);
3373 			break;
3374 		}
3375 		ds = bf->bf_lastds;	/* XXX must be setup correctly! */
3376 		ts = &bf->bf_status.ds_txstat;
3377 		status = ath_hal_txprocdesc(ah, ds, ts);
3378 #ifdef ATH_DEBUG
3379 		if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
3380 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
3381 			    status == HAL_OK);
3382 		else if ((sc->sc_debug & ATH_DEBUG_RESET) && (dosched == 0)) {
3383 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
3384 			    status == HAL_OK);
3385 		}
3386 #endif
3387 		if (status == HAL_EINPROGRESS) {
3388 			ATH_TXQ_UNLOCK(txq);
3389 			break;
3390 		}
3391 		ATH_TXQ_REMOVE(txq, bf, bf_list);
3392 #ifdef IEEE80211_SUPPORT_TDMA
3393 		if (txq->axq_depth > 0) {
3394 			/*
3395 			 * More frames follow.  Mark the buffer busy
3396 			 * so it's not re-used while the hardware may
3397 			 * still re-read the link field in the descriptor.
3398 			 *
3399 			 * Use the last buffer in an aggregate as that
3400 			 * is where the hardware may be - intermediate
3401 			 * descriptors won't be "busy".
3402 			 */
3403 			bf->bf_last->bf_flags |= ATH_BUF_BUSY;
3404 		} else
3405 #else
3406 		if (txq->axq_depth == 0)
3407 #endif
3408 			txq->axq_link = NULL;
3409 		if (bf->bf_state.bfs_aggr)
3410 			txq->axq_aggr_depth--;
3411 
3412 		ni = bf->bf_node;
3413 		/*
3414 		 * If unicast frame was ack'd update RSSI,
3415 		 * including the last rx time used to
3416 		 * workaround phantom bmiss interrupts.
3417 		 */
3418 		if (ni != NULL && ts->ts_status == 0 &&
3419 		    ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)) {
3420 			nacked++;
3421 			sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
3422 			ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi,
3423 				ts->ts_rssi);
3424 		}
3425 		ATH_TXQ_UNLOCK(txq);
3426 
3427 		/* If unicast frame, update general statistics */
3428 		if (ni != NULL) {
3429 			an = ATH_NODE(ni);
3430 			/* update statistics */
3431 			ath_tx_update_stats(sc, ts, bf);
3432 		}
3433 
3434 		/*
3435 		 * Call the completion handler.
3436 		 * The completion handler is responsible for
3437 		 * calling the rate control code.
3438 		 *
3439 		 * Frames with no completion handler get the
3440 		 * rate control code called here.
3441 		 */
3442 		if (bf->bf_comp == NULL) {
3443 			if ((ts->ts_status & HAL_TXERR_FILT) == 0 &&
3444 			    (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) {
3445 				/*
3446 				 * XXX assume this isn't an aggregate
3447 				 * frame.
3448 				 */
3449 				ath_tx_update_ratectrl(sc, ni,
3450 				     bf->bf_state.bfs_rc, ts,
3451 				    bf->bf_state.bfs_pktlen, 1,
3452 				    (ts->ts_status == 0 ? 0 : 1));
3453 			}
3454 			ath_tx_default_comp(sc, bf, 0);
3455 		} else
3456 			bf->bf_comp(sc, bf, 0);
3457 	}
3458 #ifdef IEEE80211_SUPPORT_SUPERG
3459 	/*
3460 	 * Flush fast-frame staging queue when traffic slows.
3461 	 */
3462 	if (txq->axq_depth <= 1)
3463 		ieee80211_ff_flush(ic, txq->axq_ac);
3464 #endif
3465 
3466 	/* Kick the TXQ scheduler */
3467 	if (dosched) {
3468 		ATH_TXQ_LOCK(txq);
3469 		ath_txq_sched(sc, txq);
3470 		ATH_TXQ_UNLOCK(txq);
3471 	}
3472 
3473 	return nacked;
3474 }
3475 
3476 #define	TXQACTIVE(t, q)		( (t) & (1 << (q)))
3477 
3478 /*
3479  * Deferred processing of transmit interrupt; special-cased
3480  * for a single hardware transmit queue (e.g. 5210 and 5211).
3481  */
3482 static void
3483 ath_tx_proc_q0(void *arg, int npending)
3484 {
3485 	struct ath_softc *sc = arg;
3486 	struct ifnet *ifp = sc->sc_ifp;
3487 	uint32_t txqs;
3488 
3489 	ATH_PCU_LOCK(sc);
3490 	sc->sc_txproc_cnt++;
3491 	txqs = sc->sc_txq_active;
3492 	sc->sc_txq_active &= ~txqs;
3493 	ATH_PCU_UNLOCK(sc);
3494 
3495 	if (TXQACTIVE(txqs, 0) && ath_tx_processq(sc, &sc->sc_txq[0], 1))
3496 		/* XXX why is lastrx updated in tx code? */
3497 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
3498 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
3499 		ath_tx_processq(sc, sc->sc_cabq, 1);
3500 	IF_LOCK(&ifp->if_snd);
3501 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3502 	IF_UNLOCK(&ifp->if_snd);
3503 	sc->sc_wd_timer = 0;
3504 
3505 	if (sc->sc_softled)
3506 		ath_led_event(sc, sc->sc_txrix);
3507 
3508 	ATH_PCU_LOCK(sc);
3509 	sc->sc_txproc_cnt--;
3510 	ATH_PCU_UNLOCK(sc);
3511 
3512 	// ath_start(ifp);
3513 	ath_tx_tasklet(sc, 1);
3514 }
3515 
3516 /*
3517  * Deferred processing of transmit interrupt; special-cased
3518  * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
3519  */
3520 static void
3521 ath_tx_proc_q0123(void *arg, int npending)
3522 {
3523 	struct ath_softc *sc = arg;
3524 	struct ifnet *ifp = sc->sc_ifp;
3525 	int nacked;
3526 	uint32_t txqs;
3527 
3528 	ATH_PCU_LOCK(sc);
3529 	sc->sc_txproc_cnt++;
3530 	txqs = sc->sc_txq_active;
3531 	sc->sc_txq_active &= ~txqs;
3532 	ATH_PCU_UNLOCK(sc);
3533 
3534 	/*
3535 	 * Process each active queue.
3536 	 */
3537 	nacked = 0;
3538 	if (TXQACTIVE(txqs, 0))
3539 		nacked += ath_tx_processq(sc, &sc->sc_txq[0], 1);
3540 	if (TXQACTIVE(txqs, 1))
3541 		nacked += ath_tx_processq(sc, &sc->sc_txq[1], 1);
3542 	if (TXQACTIVE(txqs, 2))
3543 		nacked += ath_tx_processq(sc, &sc->sc_txq[2], 1);
3544 	if (TXQACTIVE(txqs, 3))
3545 		nacked += ath_tx_processq(sc, &sc->sc_txq[3], 1);
3546 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
3547 		ath_tx_processq(sc, sc->sc_cabq, 1);
3548 	if (nacked)
3549 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
3550 
3551 	IF_LOCK(&ifp->if_snd);
3552 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3553 	IF_UNLOCK(&ifp->if_snd);
3554 	sc->sc_wd_timer = 0;
3555 
3556 	if (sc->sc_softled)
3557 		ath_led_event(sc, sc->sc_txrix);
3558 
3559 	ATH_PCU_LOCK(sc);
3560 	sc->sc_txproc_cnt--;
3561 	ATH_PCU_UNLOCK(sc);
3562 
3563 	//ath_start(ifp);
3564 	ath_tx_tasklet(sc, 1);
3565 }
3566 
3567 /*
3568  * Deferred processing of transmit interrupt.
3569  */
3570 static void
3571 ath_tx_proc(void *arg, int npending)
3572 {
3573 	struct ath_softc *sc = arg;
3574 	struct ifnet *ifp = sc->sc_ifp;
3575 	int i, nacked;
3576 	uint32_t txqs;
3577 
3578 	ATH_PCU_LOCK(sc);
3579 	sc->sc_txproc_cnt++;
3580 	txqs = sc->sc_txq_active;
3581 	sc->sc_txq_active &= ~txqs;
3582 	ATH_PCU_UNLOCK(sc);
3583 
3584 	/*
3585 	 * Process each active queue.
3586 	 */
3587 	nacked = 0;
3588 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
3589 		if (ATH_TXQ_SETUP(sc, i) && TXQACTIVE(txqs, i))
3590 			nacked += ath_tx_processq(sc, &sc->sc_txq[i], 1);
3591 	if (nacked)
3592 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
3593 
3594 	/* XXX check this inside of IF_LOCK? */
3595 	IF_LOCK(&ifp->if_snd);
3596 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3597 	IF_UNLOCK(&ifp->if_snd);
3598 	sc->sc_wd_timer = 0;
3599 
3600 	if (sc->sc_softled)
3601 		ath_led_event(sc, sc->sc_txrix);
3602 
3603 	ATH_PCU_LOCK(sc);
3604 	sc->sc_txproc_cnt--;
3605 	ATH_PCU_UNLOCK(sc);
3606 
3607 	//ath_start(ifp);
3608 	ath_tx_tasklet(sc, 1);
3609 }
3610 #undef	TXQACTIVE
3611 
3612 /*
3613  * Deferred processing of TXQ rescheduling.
3614  */
3615 static void
3616 ath_txq_sched_tasklet(void *arg, int npending)
3617 {
3618 	struct ath_softc *sc = arg;
3619 	int i;
3620 
3621 	/* XXX is skipping ok? */
3622 	ATH_PCU_LOCK(sc);
3623 #if 0
3624 	if (sc->sc_inreset_cnt > 0) {
3625 		device_printf(sc->sc_dev,
3626 		    "%s: sc_inreset_cnt > 0; skipping\n", __func__);
3627 		ATH_PCU_UNLOCK(sc);
3628 		return;
3629 	}
3630 #endif
3631 	sc->sc_txproc_cnt++;
3632 	ATH_PCU_UNLOCK(sc);
3633 
3634 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
3635 		if (ATH_TXQ_SETUP(sc, i)) {
3636 			ATH_TXQ_LOCK(&sc->sc_txq[i]);
3637 			ath_txq_sched(sc, &sc->sc_txq[i]);
3638 			ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
3639 		}
3640 	}
3641 
3642 	ATH_PCU_LOCK(sc);
3643 	sc->sc_txproc_cnt--;
3644 	ATH_PCU_UNLOCK(sc);
3645 }
3646 
3647 /*
3648  * Return a buffer to the pool and update the 'busy' flag on the
3649  * previous 'tail' entry.
3650  *
3651  * This _must_ only be called when the buffer is involved in a completed
3652  * TX. The logic is that if it was part of an active TX, the previous
3653  * buffer on the list is now not involved in a halted TX DMA queue, waiting
3654  * for restart (eg for TDMA.)
3655  *
3656  * The caller must free the mbuf and recycle the node reference.
3657  */
3658 void
3659 ath_freebuf(struct ath_softc *sc, struct ath_buf *bf)
3660 {
3661 	bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3662 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_POSTWRITE);
3663 
3664 	KASSERT((bf->bf_node == NULL), ("%s: bf->bf_node != NULL\n", __func__));
3665 	KASSERT((bf->bf_m == NULL), ("%s: bf->bf_m != NULL\n", __func__));
3666 
3667 	ATH_TXBUF_LOCK(sc);
3668 	ath_tx_update_busy(sc);
3669 	TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
3670 	ATH_TXBUF_UNLOCK(sc);
3671 }
3672 
3673 /*
3674  * This is currently used by ath_tx_draintxq() and
3675  * ath_tx_tid_free_pkts().
3676  *
3677  * It recycles a single ath_buf.
3678  */
3679 void
3680 ath_tx_freebuf(struct ath_softc *sc, struct ath_buf *bf, int status)
3681 {
3682 	struct ieee80211_node *ni = bf->bf_node;
3683 	struct mbuf *m0 = bf->bf_m;
3684 
3685 	bf->bf_node = NULL;
3686 	bf->bf_m = NULL;
3687 
3688 	/* Free the buffer, it's not needed any longer */
3689 	ath_freebuf(sc, bf);
3690 
3691 	if (ni != NULL) {
3692 		/*
3693 		 * Do any callback and reclaim the node reference.
3694 		 */
3695 		if (m0->m_flags & M_TXCB)
3696 			ieee80211_process_callback(ni, m0, status);
3697 		ieee80211_free_node(ni);
3698 	}
3699 	m_freem(m0);
3700 
3701 	/*
3702 	 * XXX the buffer used to be freed -after-, but the DMA map was
3703 	 * freed where ath_freebuf() now is. I've no idea what this
3704 	 * will do.
3705 	 */
3706 }
3707 
3708 void
3709 ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
3710 {
3711 #ifdef ATH_DEBUG
3712 	struct ath_hal *ah = sc->sc_ah;
3713 #endif
3714 	struct ath_buf *bf;
3715 	u_int ix;
3716 
3717 	/*
3718 	 * NB: this assumes output has been stopped and
3719 	 *     we do not need to block ath_tx_proc
3720 	 */
3721 	ATH_TXBUF_LOCK(sc);
3722 	bf = TAILQ_LAST(&sc->sc_txbuf, ath_bufhead_s);
3723 	if (bf != NULL)
3724 		bf->bf_flags &= ~ATH_BUF_BUSY;
3725 	ATH_TXBUF_UNLOCK(sc);
3726 
3727 	for (ix = 0;; ix++) {
3728 		ATH_TXQ_LOCK(txq);
3729 		bf = TAILQ_FIRST(&txq->axq_q);
3730 		if (bf == NULL) {
3731 			txq->axq_link = NULL;
3732 			ATH_TXQ_UNLOCK(txq);
3733 			break;
3734 		}
3735 		ATH_TXQ_REMOVE(txq, bf, bf_list);
3736 		if (bf->bf_state.bfs_aggr)
3737 			txq->axq_aggr_depth--;
3738 #ifdef ATH_DEBUG
3739 		if (sc->sc_debug & ATH_DEBUG_RESET) {
3740 			struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3741 
3742 			ath_printtxbuf(sc, bf, txq->axq_qnum, ix,
3743 				ath_hal_txprocdesc(ah, bf->bf_lastds,
3744 				    &bf->bf_status.ds_txstat) == HAL_OK);
3745 			ieee80211_dump_pkt(ic, mtod(bf->bf_m, const uint8_t *),
3746 			    bf->bf_m->m_len, 0, -1);
3747 		}
3748 #endif /* ATH_DEBUG */
3749 		/*
3750 		 * Since we're now doing magic in the completion
3751 		 * functions, we -must- call it for aggregation
3752 		 * destinations or BAW tracking will get upset.
3753 		 */
3754 		/*
3755 		 * Clear ATH_BUF_BUSY; the completion handler
3756 		 * will free the buffer.
3757 		 */
3758 		ATH_TXQ_UNLOCK(txq);
3759 		bf->bf_flags &= ~ATH_BUF_BUSY;
3760 		if (bf->bf_comp)
3761 			bf->bf_comp(sc, bf, 1);
3762 		else
3763 			ath_tx_default_comp(sc, bf, 1);
3764 	}
3765 
3766 	/*
3767 	 * Drain software queued frames which are on
3768 	 * active TIDs.
3769 	 */
3770 	ath_tx_txq_drain(sc, txq);
3771 }
3772 
3773 static void
3774 ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
3775 {
3776 	struct ath_hal *ah = sc->sc_ah;
3777 
3778 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
3779 	    __func__, txq->axq_qnum,
3780 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
3781 	    txq->axq_link);
3782 	(void) ath_hal_stoptxdma(ah, txq->axq_qnum);
3783 }
3784 
3785 static int
3786 ath_stoptxdma(struct ath_softc *sc)
3787 {
3788 	struct ath_hal *ah = sc->sc_ah;
3789 	int i;
3790 
3791 	/* XXX return value */
3792 	if (sc->sc_invalid)
3793 		return 0;
3794 
3795 	if (!sc->sc_invalid) {
3796 		/* don't touch the hardware if marked invalid */
3797 		DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
3798 		    __func__, sc->sc_bhalq,
3799 		    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq),
3800 		    NULL);
3801 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
3802 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
3803 			if (ATH_TXQ_SETUP(sc, i))
3804 				ath_tx_stopdma(sc, &sc->sc_txq[i]);
3805 	}
3806 
3807 	return 1;
3808 }
3809 
3810 /*
3811  * Drain the transmit queues and reclaim resources.
3812  */
3813 static void
3814 ath_draintxq(struct ath_softc *sc, ATH_RESET_TYPE reset_type)
3815 {
3816 #ifdef	ATH_DEBUG
3817 	struct ath_hal *ah = sc->sc_ah;
3818 #endif
3819 	struct ifnet *ifp = sc->sc_ifp;
3820 	int i;
3821 
3822 	(void) ath_stoptxdma(sc);
3823 
3824 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
3825 		/*
3826 		 * XXX TODO: should we just handle the completed TX frames
3827 		 * here, whether or not the reset is a full one or not?
3828 		 */
3829 		if (ATH_TXQ_SETUP(sc, i)) {
3830 			if (reset_type == ATH_RESET_NOLOSS)
3831 				ath_tx_processq(sc, &sc->sc_txq[i], 0);
3832 			else
3833 				ath_tx_draintxq(sc, &sc->sc_txq[i]);
3834 		}
3835 	}
3836 #ifdef ATH_DEBUG
3837 	if (sc->sc_debug & ATH_DEBUG_RESET) {
3838 		struct ath_buf *bf = TAILQ_FIRST(&sc->sc_bbuf);
3839 		if (bf != NULL && bf->bf_m != NULL) {
3840 			ath_printtxbuf(sc, bf, sc->sc_bhalq, 0,
3841 				ath_hal_txprocdesc(ah, bf->bf_lastds,
3842 				    &bf->bf_status.ds_txstat) == HAL_OK);
3843 			ieee80211_dump_pkt(ifp->if_l2com,
3844 			    mtod(bf->bf_m, const uint8_t *), bf->bf_m->m_len,
3845 			    0, -1);
3846 		}
3847 	}
3848 #endif /* ATH_DEBUG */
3849 	IF_LOCK(&ifp->if_snd);
3850 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3851 	IF_UNLOCK(&ifp->if_snd);
3852 	sc->sc_wd_timer = 0;
3853 }
3854 
3855 /*
3856  * Update internal state after a channel change.
3857  */
3858 static void
3859 ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
3860 {
3861 	enum ieee80211_phymode mode;
3862 
3863 	/*
3864 	 * Change channels and update the h/w rate map
3865 	 * if we're switching; e.g. 11a to 11b/g.
3866 	 */
3867 	mode = ieee80211_chan2mode(chan);
3868 	if (mode != sc->sc_curmode)
3869 		ath_setcurmode(sc, mode);
3870 	sc->sc_curchan = chan;
3871 }
3872 
3873 /*
3874  * Set/change channels.  If the channel is really being changed,
3875  * it's done by resetting the chip.  To accomplish this we must
3876  * first cleanup any pending DMA, then restart stuff after a la
3877  * ath_init.
3878  */
3879 static int
3880 ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
3881 {
3882 	struct ifnet *ifp = sc->sc_ifp;
3883 	struct ieee80211com *ic = ifp->if_l2com;
3884 	struct ath_hal *ah = sc->sc_ah;
3885 	int ret = 0;
3886 
3887 	/* Treat this as an interface reset */
3888 	ATH_PCU_UNLOCK_ASSERT(sc);
3889 	ATH_UNLOCK_ASSERT(sc);
3890 
3891 	/* (Try to) stop TX/RX from occuring */
3892 	taskqueue_block(sc->sc_tq);
3893 
3894 	ATH_PCU_LOCK(sc);
3895 	ath_hal_intrset(ah, 0);		/* Stop new RX/TX completion */
3896 	ath_txrx_stop_locked(sc);	/* Stop pending RX/TX completion */
3897 	if (ath_reset_grablock(sc, 1) == 0) {
3898 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
3899 		    __func__);
3900 	}
3901 	ATH_PCU_UNLOCK(sc);
3902 
3903 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz, flags 0x%x)\n",
3904 	    __func__, ieee80211_chan2ieee(ic, chan),
3905 	    chan->ic_freq, chan->ic_flags);
3906 	if (chan != sc->sc_curchan) {
3907 		HAL_STATUS status;
3908 		/*
3909 		 * To switch channels clear any pending DMA operations;
3910 		 * wait long enough for the RX fifo to drain, reset the
3911 		 * hardware at the new frequency, and then re-enable
3912 		 * the relevant bits of the h/w.
3913 		 */
3914 #if 0
3915 		ath_hal_intrset(ah, 0);		/* disable interrupts */
3916 #endif
3917 		ath_stoprecv(sc, 1);		/* turn off frame recv */
3918 		/*
3919 		 * First, handle completed TX/RX frames.
3920 		 */
3921 		ath_rx_proc(sc, 0);
3922 		ath_draintxq(sc, ATH_RESET_NOLOSS);
3923 		/*
3924 		 * Next, flush the non-scheduled frames.
3925 		 */
3926 		ath_draintxq(sc, ATH_RESET_FULL);	/* clear pending tx frames */
3927 
3928 		if (!ath_hal_reset(ah, sc->sc_opmode, chan, AH_TRUE, &status)) {
3929 			if_printf(ifp, "%s: unable to reset "
3930 			    "channel %u (%u MHz, flags 0x%x), hal status %u\n",
3931 			    __func__, ieee80211_chan2ieee(ic, chan),
3932 			    chan->ic_freq, chan->ic_flags, status);
3933 			ret = EIO;
3934 			goto finish;
3935 		}
3936 		sc->sc_diversity = ath_hal_getdiversity(ah);
3937 
3938 		/* Let DFS at it in case it's a DFS channel */
3939 		ath_dfs_radar_enable(sc, chan);
3940 
3941 		/*
3942 		 * Re-enable rx framework.
3943 		 */
3944 		if (ath_startrecv(sc) != 0) {
3945 			if_printf(ifp, "%s: unable to restart recv logic\n",
3946 			    __func__);
3947 			ret = EIO;
3948 			goto finish;
3949 		}
3950 
3951 		/*
3952 		 * Change channels and update the h/w rate map
3953 		 * if we're switching; e.g. 11a to 11b/g.
3954 		 */
3955 		ath_chan_change(sc, chan);
3956 
3957 		/*
3958 		 * Reset clears the beacon timers; reset them
3959 		 * here if needed.
3960 		 */
3961 		if (sc->sc_beacons) {		/* restart beacons */
3962 #ifdef IEEE80211_SUPPORT_TDMA
3963 			if (sc->sc_tdma)
3964 				ath_tdma_config(sc, NULL);
3965 			else
3966 #endif
3967 			ath_beacon_config(sc, NULL);
3968 		}
3969 
3970 		/*
3971 		 * Re-enable interrupts.
3972 		 */
3973 #if 0
3974 		ath_hal_intrset(ah, sc->sc_imask);
3975 #endif
3976 	}
3977 
3978 finish:
3979 	ATH_PCU_LOCK(sc);
3980 	sc->sc_inreset_cnt--;
3981 	/* XXX only do this if sc_inreset_cnt == 0? */
3982 	ath_hal_intrset(ah, sc->sc_imask);
3983 	ATH_PCU_UNLOCK(sc);
3984 
3985 	IF_LOCK(&ifp->if_snd);
3986 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3987 	IF_UNLOCK(&ifp->if_snd);
3988 	ath_txrx_start(sc);
3989 	/* XXX ath_start? */
3990 
3991 	return ret;
3992 }
3993 
3994 /*
3995  * Periodically recalibrate the PHY to account
3996  * for temperature/environment changes.
3997  */
3998 static void
3999 ath_calibrate(void *arg)
4000 {
4001 	struct ath_softc *sc = arg;
4002 	struct ath_hal *ah = sc->sc_ah;
4003 	struct ifnet *ifp = sc->sc_ifp;
4004 	struct ieee80211com *ic = ifp->if_l2com;
4005 	HAL_BOOL longCal, isCalDone;
4006 	HAL_BOOL aniCal, shortCal = AH_FALSE;
4007 	int nextcal;
4008 
4009 	if (ic->ic_flags & IEEE80211_F_SCAN)	/* defer, off channel */
4010 		goto restart;
4011 	longCal = (ticks - sc->sc_lastlongcal >= ath_longcalinterval*hz);
4012 	aniCal = (ticks - sc->sc_lastani >= ath_anicalinterval*hz/1000);
4013 	if (sc->sc_doresetcal)
4014 		shortCal = (ticks - sc->sc_lastshortcal >= ath_shortcalinterval*hz/1000);
4015 
4016 	DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: shortCal=%d; longCal=%d; aniCal=%d\n", __func__, shortCal, longCal, aniCal);
4017 	if (aniCal) {
4018 		sc->sc_stats.ast_ani_cal++;
4019 		sc->sc_lastani = ticks;
4020 		ath_hal_ani_poll(ah, sc->sc_curchan);
4021 	}
4022 
4023 	if (longCal) {
4024 		sc->sc_stats.ast_per_cal++;
4025 		sc->sc_lastlongcal = ticks;
4026 		if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
4027 			/*
4028 			 * Rfgain is out of bounds, reset the chip
4029 			 * to load new gain values.
4030 			 */
4031 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
4032 				"%s: rfgain change\n", __func__);
4033 			sc->sc_stats.ast_per_rfgain++;
4034 			sc->sc_resetcal = 0;
4035 			sc->sc_doresetcal = AH_TRUE;
4036 			taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
4037 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
4038 			return;
4039 		}
4040 		/*
4041 		 * If this long cal is after an idle period, then
4042 		 * reset the data collection state so we start fresh.
4043 		 */
4044 		if (sc->sc_resetcal) {
4045 			(void) ath_hal_calreset(ah, sc->sc_curchan);
4046 			sc->sc_lastcalreset = ticks;
4047 			sc->sc_lastshortcal = ticks;
4048 			sc->sc_resetcal = 0;
4049 			sc->sc_doresetcal = AH_TRUE;
4050 		}
4051 	}
4052 
4053 	/* Only call if we're doing a short/long cal, not for ANI calibration */
4054 	if (shortCal || longCal) {
4055 		if (ath_hal_calibrateN(ah, sc->sc_curchan, longCal, &isCalDone)) {
4056 			if (longCal) {
4057 				/*
4058 				 * Calibrate noise floor data again in case of change.
4059 				 */
4060 				ath_hal_process_noisefloor(ah);
4061 			}
4062 		} else {
4063 			DPRINTF(sc, ATH_DEBUG_ANY,
4064 				"%s: calibration of channel %u failed\n",
4065 				__func__, sc->sc_curchan->ic_freq);
4066 			sc->sc_stats.ast_per_calfail++;
4067 		}
4068 		if (shortCal)
4069 			sc->sc_lastshortcal = ticks;
4070 	}
4071 	if (!isCalDone) {
4072 restart:
4073 		/*
4074 		 * Use a shorter interval to potentially collect multiple
4075 		 * data samples required to complete calibration.  Once
4076 		 * we're told the work is done we drop back to a longer
4077 		 * interval between requests.  We're more aggressive doing
4078 		 * work when operating as an AP to improve operation right
4079 		 * after startup.
4080 		 */
4081 		sc->sc_lastshortcal = ticks;
4082 		nextcal = ath_shortcalinterval*hz/1000;
4083 		if (sc->sc_opmode != HAL_M_HOSTAP)
4084 			nextcal *= 10;
4085 		sc->sc_doresetcal = AH_TRUE;
4086 	} else {
4087 		/* nextcal should be the shortest time for next event */
4088 		nextcal = ath_longcalinterval*hz;
4089 		if (sc->sc_lastcalreset == 0)
4090 			sc->sc_lastcalreset = sc->sc_lastlongcal;
4091 		else if (ticks - sc->sc_lastcalreset >= ath_resetcalinterval*hz)
4092 			sc->sc_resetcal = 1;	/* setup reset next trip */
4093 		sc->sc_doresetcal = AH_FALSE;
4094 	}
4095 	/* ANI calibration may occur more often than short/long/resetcal */
4096 	if (ath_anicalinterval > 0)
4097 		nextcal = MIN(nextcal, ath_anicalinterval*hz/1000);
4098 
4099 	if (nextcal != 0) {
4100 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: next +%u (%sisCalDone)\n",
4101 		    __func__, nextcal, isCalDone ? "" : "!");
4102 		callout_reset(&sc->sc_cal_ch, nextcal, ath_calibrate, sc);
4103 	} else {
4104 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: calibration disabled\n",
4105 		    __func__);
4106 		/* NB: don't rearm timer */
4107 	}
4108 }
4109 
4110 static void
4111 ath_scan_start(struct ieee80211com *ic)
4112 {
4113 	struct ifnet *ifp = ic->ic_ifp;
4114 	struct ath_softc *sc = ifp->if_softc;
4115 	struct ath_hal *ah = sc->sc_ah;
4116 	u_int32_t rfilt;
4117 
4118 	/* XXX calibration timer? */
4119 
4120 	ATH_LOCK(sc);
4121 	sc->sc_scanning = 1;
4122 	sc->sc_syncbeacon = 0;
4123 	rfilt = ath_calcrxfilter(sc);
4124 	ATH_UNLOCK(sc);
4125 
4126 	ATH_PCU_LOCK(sc);
4127 	ath_hal_setrxfilter(ah, rfilt);
4128 	ath_hal_setassocid(ah, ifp->if_broadcastaddr, 0);
4129 	ATH_PCU_UNLOCK(sc);
4130 
4131 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0\n",
4132 		 __func__, rfilt, ether_sprintf(ifp->if_broadcastaddr));
4133 }
4134 
4135 static void
4136 ath_scan_end(struct ieee80211com *ic)
4137 {
4138 	struct ifnet *ifp = ic->ic_ifp;
4139 	struct ath_softc *sc = ifp->if_softc;
4140 	struct ath_hal *ah = sc->sc_ah;
4141 	u_int32_t rfilt;
4142 
4143 	ATH_LOCK(sc);
4144 	sc->sc_scanning = 0;
4145 	rfilt = ath_calcrxfilter(sc);
4146 	ATH_UNLOCK(sc);
4147 
4148 	ATH_PCU_LOCK(sc);
4149 	ath_hal_setrxfilter(ah, rfilt);
4150 	ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
4151 
4152 	ath_hal_process_noisefloor(ah);
4153 	ATH_PCU_UNLOCK(sc);
4154 
4155 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
4156 		 __func__, rfilt, ether_sprintf(sc->sc_curbssid),
4157 		 sc->sc_curaid);
4158 }
4159 
4160 #ifdef	ATH_ENABLE_11N
4161 /*
4162  * For now, just do a channel change.
4163  *
4164  * Later, we'll go through the hard slog of suspending tx/rx, changing rate
4165  * control state and resetting the hardware without dropping frames out
4166  * of the queue.
4167  *
4168  * The unfortunate trouble here is making absolutely sure that the
4169  * channel width change has propagated enough so the hardware
4170  * absolutely isn't handed bogus frames for it's current operating
4171  * mode. (Eg, 40MHz frames in 20MHz mode.) Since TX and RX can and
4172  * does occur in parallel, we need to make certain we've blocked
4173  * any further ongoing TX (and RX, that can cause raw TX)
4174  * before we do this.
4175  */
4176 static void
4177 ath_update_chw(struct ieee80211com *ic)
4178 {
4179 	struct ifnet *ifp = ic->ic_ifp;
4180 	struct ath_softc *sc = ifp->if_softc;
4181 
4182 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: called\n", __func__);
4183 	ath_set_channel(ic);
4184 }
4185 #endif	/* ATH_ENABLE_11N */
4186 
4187 static void
4188 ath_set_channel(struct ieee80211com *ic)
4189 {
4190 	struct ifnet *ifp = ic->ic_ifp;
4191 	struct ath_softc *sc = ifp->if_softc;
4192 
4193 	(void) ath_chan_set(sc, ic->ic_curchan);
4194 	/*
4195 	 * If we are returning to our bss channel then mark state
4196 	 * so the next recv'd beacon's tsf will be used to sync the
4197 	 * beacon timers.  Note that since we only hear beacons in
4198 	 * sta/ibss mode this has no effect in other operating modes.
4199 	 */
4200 	ATH_LOCK(sc);
4201 	if (!sc->sc_scanning && ic->ic_curchan == ic->ic_bsschan)
4202 		sc->sc_syncbeacon = 1;
4203 	ATH_UNLOCK(sc);
4204 }
4205 
4206 /*
4207  * Walk the vap list and check if there any vap's in RUN state.
4208  */
4209 static int
4210 ath_isanyrunningvaps(struct ieee80211vap *this)
4211 {
4212 	struct ieee80211com *ic = this->iv_ic;
4213 	struct ieee80211vap *vap;
4214 
4215 	IEEE80211_LOCK_ASSERT(ic);
4216 
4217 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
4218 		if (vap != this && vap->iv_state >= IEEE80211_S_RUN)
4219 			return 1;
4220 	}
4221 	return 0;
4222 }
4223 
4224 static int
4225 ath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
4226 {
4227 	struct ieee80211com *ic = vap->iv_ic;
4228 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4229 	struct ath_vap *avp = ATH_VAP(vap);
4230 	struct ath_hal *ah = sc->sc_ah;
4231 	struct ieee80211_node *ni = NULL;
4232 	int i, error, stamode;
4233 	u_int32_t rfilt;
4234 	int csa_run_transition = 0;
4235 	static const HAL_LED_STATE leds[] = {
4236 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
4237 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
4238 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
4239 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
4240 	    HAL_LED_RUN, 	/* IEEE80211_S_CAC */
4241 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
4242 	    HAL_LED_RUN, 	/* IEEE80211_S_CSA */
4243 	    HAL_LED_RUN, 	/* IEEE80211_S_SLEEP */
4244 	};
4245 
4246 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
4247 		ieee80211_state_name[vap->iv_state],
4248 		ieee80211_state_name[nstate]);
4249 
4250 	/*
4251 	 * net80211 _should_ have the comlock asserted at this point.
4252 	 * There are some comments around the calls to vap->iv_newstate
4253 	 * which indicate that it (newstate) may end up dropping the
4254 	 * lock.  This and the subsequent lock assert check after newstate
4255 	 * are an attempt to catch these and figure out how/why.
4256 	 */
4257 	IEEE80211_LOCK_ASSERT(ic);
4258 
4259 	if (vap->iv_state == IEEE80211_S_CSA && nstate == IEEE80211_S_RUN)
4260 		csa_run_transition = 1;
4261 
4262 	callout_drain(&sc->sc_cal_ch);
4263 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
4264 
4265 	if (nstate == IEEE80211_S_SCAN) {
4266 		/*
4267 		 * Scanning: turn off beacon miss and don't beacon.
4268 		 * Mark beacon state so when we reach RUN state we'll
4269 		 * [re]setup beacons.  Unblock the task q thread so
4270 		 * deferred interrupt processing is done.
4271 		 */
4272 		ath_hal_intrset(ah,
4273 		    sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
4274 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
4275 		sc->sc_beacons = 0;
4276 		taskqueue_unblock(sc->sc_tq);
4277 	}
4278 
4279 	ni = ieee80211_ref_node(vap->iv_bss);
4280 	rfilt = ath_calcrxfilter(sc);
4281 	stamode = (vap->iv_opmode == IEEE80211_M_STA ||
4282 		   vap->iv_opmode == IEEE80211_M_AHDEMO ||
4283 		   vap->iv_opmode == IEEE80211_M_IBSS);
4284 	if (stamode && nstate == IEEE80211_S_RUN) {
4285 		sc->sc_curaid = ni->ni_associd;
4286 		IEEE80211_ADDR_COPY(sc->sc_curbssid, ni->ni_bssid);
4287 		ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
4288 	}
4289 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
4290 	   __func__, rfilt, ether_sprintf(sc->sc_curbssid), sc->sc_curaid);
4291 	ath_hal_setrxfilter(ah, rfilt);
4292 
4293 	/* XXX is this to restore keycache on resume? */
4294 	if (vap->iv_opmode != IEEE80211_M_STA &&
4295 	    (vap->iv_flags & IEEE80211_F_PRIVACY)) {
4296 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
4297 			if (ath_hal_keyisvalid(ah, i))
4298 				ath_hal_keysetmac(ah, i, ni->ni_bssid);
4299 	}
4300 
4301 	/*
4302 	 * Invoke the parent method to do net80211 work.
4303 	 */
4304 	error = avp->av_newstate(vap, nstate, arg);
4305 	if (error != 0)
4306 		goto bad;
4307 
4308 	/*
4309 	 * See above: ensure av_newstate() doesn't drop the lock
4310 	 * on us.
4311 	 */
4312 	IEEE80211_LOCK_ASSERT(ic);
4313 
4314 	if (nstate == IEEE80211_S_RUN) {
4315 		/* NB: collect bss node again, it may have changed */
4316 		ieee80211_free_node(ni);
4317 		ni = ieee80211_ref_node(vap->iv_bss);
4318 
4319 		DPRINTF(sc, ATH_DEBUG_STATE,
4320 		    "%s(RUN): iv_flags 0x%08x bintvl %d bssid %s "
4321 		    "capinfo 0x%04x chan %d\n", __func__,
4322 		    vap->iv_flags, ni->ni_intval, ether_sprintf(ni->ni_bssid),
4323 		    ni->ni_capinfo, ieee80211_chan2ieee(ic, ic->ic_curchan));
4324 
4325 		switch (vap->iv_opmode) {
4326 #ifdef IEEE80211_SUPPORT_TDMA
4327 		case IEEE80211_M_AHDEMO:
4328 			if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
4329 				break;
4330 			/* fall thru... */
4331 #endif
4332 		case IEEE80211_M_HOSTAP:
4333 		case IEEE80211_M_IBSS:
4334 		case IEEE80211_M_MBSS:
4335 			/*
4336 			 * Allocate and setup the beacon frame.
4337 			 *
4338 			 * Stop any previous beacon DMA.  This may be
4339 			 * necessary, for example, when an ibss merge
4340 			 * causes reconfiguration; there will be a state
4341 			 * transition from RUN->RUN that means we may
4342 			 * be called with beacon transmission active.
4343 			 */
4344 			ath_hal_stoptxdma(ah, sc->sc_bhalq);
4345 
4346 			error = ath_beacon_alloc(sc, ni);
4347 			if (error != 0)
4348 				goto bad;
4349 			/*
4350 			 * If joining an adhoc network defer beacon timer
4351 			 * configuration to the next beacon frame so we
4352 			 * have a current TSF to use.  Otherwise we're
4353 			 * starting an ibss/bss so there's no need to delay;
4354 			 * if this is the first vap moving to RUN state, then
4355 			 * beacon state needs to be [re]configured.
4356 			 */
4357 			if (vap->iv_opmode == IEEE80211_M_IBSS &&
4358 			    ni->ni_tstamp.tsf != 0) {
4359 				sc->sc_syncbeacon = 1;
4360 			} else if (!sc->sc_beacons) {
4361 #ifdef IEEE80211_SUPPORT_TDMA
4362 				if (vap->iv_caps & IEEE80211_C_TDMA)
4363 					ath_tdma_config(sc, vap);
4364 				else
4365 #endif
4366 					ath_beacon_config(sc, vap);
4367 				sc->sc_beacons = 1;
4368 			}
4369 			break;
4370 		case IEEE80211_M_STA:
4371 			/*
4372 			 * Defer beacon timer configuration to the next
4373 			 * beacon frame so we have a current TSF to use
4374 			 * (any TSF collected when scanning is likely old).
4375 			 * However if it's due to a CSA -> RUN transition,
4376 			 * force a beacon update so we pick up a lack of
4377 			 * beacons from an AP in CAC and thus force a
4378 			 * scan.
4379 			 */
4380 			sc->sc_syncbeacon = 1;
4381 			if (csa_run_transition)
4382 				ath_beacon_config(sc, vap);
4383 			break;
4384 		case IEEE80211_M_MONITOR:
4385 			/*
4386 			 * Monitor mode vaps have only INIT->RUN and RUN->RUN
4387 			 * transitions so we must re-enable interrupts here to
4388 			 * handle the case of a single monitor mode vap.
4389 			 */
4390 			ath_hal_intrset(ah, sc->sc_imask);
4391 			break;
4392 		case IEEE80211_M_WDS:
4393 			break;
4394 		default:
4395 			break;
4396 		}
4397 		/*
4398 		 * Let the hal process statistics collected during a
4399 		 * scan so it can provide calibrated noise floor data.
4400 		 */
4401 		ath_hal_process_noisefloor(ah);
4402 		/*
4403 		 * Reset rssi stats; maybe not the best place...
4404 		 */
4405 		sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
4406 		sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
4407 		sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
4408 		/*
4409 		 * Finally, start any timers and the task q thread
4410 		 * (in case we didn't go through SCAN state).
4411 		 */
4412 		if (ath_longcalinterval != 0) {
4413 			/* start periodic recalibration timer */
4414 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
4415 		} else {
4416 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
4417 			    "%s: calibration disabled\n", __func__);
4418 		}
4419 		taskqueue_unblock(sc->sc_tq);
4420 	} else if (nstate == IEEE80211_S_INIT) {
4421 		/*
4422 		 * If there are no vaps left in RUN state then
4423 		 * shutdown host/driver operation:
4424 		 * o disable interrupts
4425 		 * o disable the task queue thread
4426 		 * o mark beacon processing as stopped
4427 		 */
4428 		if (!ath_isanyrunningvaps(vap)) {
4429 			sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
4430 			/* disable interrupts  */
4431 			ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL);
4432 			taskqueue_block(sc->sc_tq);
4433 			sc->sc_beacons = 0;
4434 		}
4435 #ifdef IEEE80211_SUPPORT_TDMA
4436 		ath_hal_setcca(ah, AH_TRUE);
4437 #endif
4438 	}
4439 bad:
4440 	ieee80211_free_node(ni);
4441 	return error;
4442 }
4443 
4444 /*
4445  * Allocate a key cache slot to the station so we can
4446  * setup a mapping from key index to node. The key cache
4447  * slot is needed for managing antenna state and for
4448  * compression when stations do not use crypto.  We do
4449  * it uniliaterally here; if crypto is employed this slot
4450  * will be reassigned.
4451  */
4452 static void
4453 ath_setup_stationkey(struct ieee80211_node *ni)
4454 {
4455 	struct ieee80211vap *vap = ni->ni_vap;
4456 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
4457 	ieee80211_keyix keyix, rxkeyix;
4458 
4459 	/* XXX should take a locked ref to vap->iv_bss */
4460 	if (!ath_key_alloc(vap, &ni->ni_ucastkey, &keyix, &rxkeyix)) {
4461 		/*
4462 		 * Key cache is full; we'll fall back to doing
4463 		 * the more expensive lookup in software.  Note
4464 		 * this also means no h/w compression.
4465 		 */
4466 		/* XXX msg+statistic */
4467 	} else {
4468 		/* XXX locking? */
4469 		ni->ni_ucastkey.wk_keyix = keyix;
4470 		ni->ni_ucastkey.wk_rxkeyix = rxkeyix;
4471 		/* NB: must mark device key to get called back on delete */
4472 		ni->ni_ucastkey.wk_flags |= IEEE80211_KEY_DEVKEY;
4473 		IEEE80211_ADDR_COPY(ni->ni_ucastkey.wk_macaddr, ni->ni_macaddr);
4474 		/* NB: this will create a pass-thru key entry */
4475 		ath_keyset(sc, vap, &ni->ni_ucastkey, vap->iv_bss);
4476 	}
4477 }
4478 
4479 /*
4480  * Setup driver-specific state for a newly associated node.
4481  * Note that we're called also on a re-associate, the isnew
4482  * param tells us if this is the first time or not.
4483  */
4484 static void
4485 ath_newassoc(struct ieee80211_node *ni, int isnew)
4486 {
4487 	struct ath_node *an = ATH_NODE(ni);
4488 	struct ieee80211vap *vap = ni->ni_vap;
4489 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
4490 	const struct ieee80211_txparam *tp = ni->ni_txparms;
4491 
4492 	an->an_mcastrix = ath_tx_findrix(sc, tp->mcastrate);
4493 	an->an_mgmtrix = ath_tx_findrix(sc, tp->mgmtrate);
4494 
4495 	ath_rate_newassoc(sc, an, isnew);
4496 	if (isnew &&
4497 	    (vap->iv_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey &&
4498 	    ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE)
4499 		ath_setup_stationkey(ni);
4500 }
4501 
4502 static int
4503 ath_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *reg,
4504 	int nchans, struct ieee80211_channel chans[])
4505 {
4506 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4507 	struct ath_hal *ah = sc->sc_ah;
4508 	HAL_STATUS status;
4509 
4510 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
4511 	    "%s: rd %u cc %u location %c%s\n",
4512 	    __func__, reg->regdomain, reg->country, reg->location,
4513 	    reg->ecm ? " ecm" : "");
4514 
4515 	status = ath_hal_set_channels(ah, chans, nchans,
4516 	    reg->country, reg->regdomain);
4517 	if (status != HAL_OK) {
4518 		DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: failed, status %u\n",
4519 		    __func__, status);
4520 		return EINVAL;		/* XXX */
4521 	}
4522 
4523 	return 0;
4524 }
4525 
4526 static void
4527 ath_getradiocaps(struct ieee80211com *ic,
4528 	int maxchans, int *nchans, struct ieee80211_channel chans[])
4529 {
4530 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4531 	struct ath_hal *ah = sc->sc_ah;
4532 
4533 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: use rd %u cc %d\n",
4534 	    __func__, SKU_DEBUG, CTRY_DEFAULT);
4535 
4536 	/* XXX check return */
4537 	(void) ath_hal_getchannels(ah, chans, maxchans, nchans,
4538 	    HAL_MODE_ALL, CTRY_DEFAULT, SKU_DEBUG, AH_TRUE);
4539 
4540 }
4541 
4542 static int
4543 ath_getchannels(struct ath_softc *sc)
4544 {
4545 	struct ifnet *ifp = sc->sc_ifp;
4546 	struct ieee80211com *ic = ifp->if_l2com;
4547 	struct ath_hal *ah = sc->sc_ah;
4548 	HAL_STATUS status;
4549 
4550 	/*
4551 	 * Collect channel set based on EEPROM contents.
4552 	 */
4553 	status = ath_hal_init_channels(ah, ic->ic_channels, IEEE80211_CHAN_MAX,
4554 	    &ic->ic_nchans, HAL_MODE_ALL, CTRY_DEFAULT, SKU_NONE, AH_TRUE);
4555 	if (status != HAL_OK) {
4556 		if_printf(ifp, "%s: unable to collect channel list from hal, "
4557 		    "status %d\n", __func__, status);
4558 		return EINVAL;
4559 	}
4560 	(void) ath_hal_getregdomain(ah, &sc->sc_eerd);
4561 	ath_hal_getcountrycode(ah, &sc->sc_eecc);	/* NB: cannot fail */
4562 	/* XXX map Atheros sku's to net80211 SKU's */
4563 	/* XXX net80211 types too small */
4564 	ic->ic_regdomain.regdomain = (uint16_t) sc->sc_eerd;
4565 	ic->ic_regdomain.country = (uint16_t) sc->sc_eecc;
4566 	ic->ic_regdomain.isocc[0] = ' ';	/* XXX don't know */
4567 	ic->ic_regdomain.isocc[1] = ' ';
4568 
4569 	ic->ic_regdomain.ecm = 1;
4570 	ic->ic_regdomain.location = 'I';
4571 
4572 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
4573 	    "%s: eeprom rd %u cc %u (mapped rd %u cc %u) location %c%s\n",
4574 	    __func__, sc->sc_eerd, sc->sc_eecc,
4575 	    ic->ic_regdomain.regdomain, ic->ic_regdomain.country,
4576 	    ic->ic_regdomain.location, ic->ic_regdomain.ecm ? " ecm" : "");
4577 	return 0;
4578 }
4579 
4580 static int
4581 ath_rate_setup(struct ath_softc *sc, u_int mode)
4582 {
4583 	struct ath_hal *ah = sc->sc_ah;
4584 	const HAL_RATE_TABLE *rt;
4585 
4586 	switch (mode) {
4587 	case IEEE80211_MODE_11A:
4588 		rt = ath_hal_getratetable(ah, HAL_MODE_11A);
4589 		break;
4590 	case IEEE80211_MODE_HALF:
4591 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_HALF_RATE);
4592 		break;
4593 	case IEEE80211_MODE_QUARTER:
4594 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_QUARTER_RATE);
4595 		break;
4596 	case IEEE80211_MODE_11B:
4597 		rt = ath_hal_getratetable(ah, HAL_MODE_11B);
4598 		break;
4599 	case IEEE80211_MODE_11G:
4600 		rt = ath_hal_getratetable(ah, HAL_MODE_11G);
4601 		break;
4602 	case IEEE80211_MODE_TURBO_A:
4603 		rt = ath_hal_getratetable(ah, HAL_MODE_108A);
4604 		break;
4605 	case IEEE80211_MODE_TURBO_G:
4606 		rt = ath_hal_getratetable(ah, HAL_MODE_108G);
4607 		break;
4608 	case IEEE80211_MODE_STURBO_A:
4609 		rt = ath_hal_getratetable(ah, HAL_MODE_TURBO);
4610 		break;
4611 	case IEEE80211_MODE_11NA:
4612 		rt = ath_hal_getratetable(ah, HAL_MODE_11NA_HT20);
4613 		break;
4614 	case IEEE80211_MODE_11NG:
4615 		rt = ath_hal_getratetable(ah, HAL_MODE_11NG_HT20);
4616 		break;
4617 	default:
4618 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
4619 			__func__, mode);
4620 		return 0;
4621 	}
4622 	sc->sc_rates[mode] = rt;
4623 	return (rt != NULL);
4624 }
4625 
4626 static void
4627 ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
4628 {
4629 #define	N(a)	(sizeof(a)/sizeof(a[0]))
4630 	/* NB: on/off times from the Atheros NDIS driver, w/ permission */
4631 	static const struct {
4632 		u_int		rate;		/* tx/rx 802.11 rate */
4633 		u_int16_t	timeOn;		/* LED on time (ms) */
4634 		u_int16_t	timeOff;	/* LED off time (ms) */
4635 	} blinkrates[] = {
4636 		{ 108,  40,  10 },
4637 		{  96,  44,  11 },
4638 		{  72,  50,  13 },
4639 		{  48,  57,  14 },
4640 		{  36,  67,  16 },
4641 		{  24,  80,  20 },
4642 		{  22, 100,  25 },
4643 		{  18, 133,  34 },
4644 		{  12, 160,  40 },
4645 		{  10, 200,  50 },
4646 		{   6, 240,  58 },
4647 		{   4, 267,  66 },
4648 		{   2, 400, 100 },
4649 		{   0, 500, 130 },
4650 		/* XXX half/quarter rates */
4651 	};
4652 	const HAL_RATE_TABLE *rt;
4653 	int i, j;
4654 
4655 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
4656 	rt = sc->sc_rates[mode];
4657 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
4658 	for (i = 0; i < rt->rateCount; i++) {
4659 		uint8_t ieeerate = rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
4660 		if (rt->info[i].phy != IEEE80211_T_HT)
4661 			sc->sc_rixmap[ieeerate] = i;
4662 		else
4663 			sc->sc_rixmap[ieeerate | IEEE80211_RATE_MCS] = i;
4664 	}
4665 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
4666 	for (i = 0; i < N(sc->sc_hwmap); i++) {
4667 		if (i >= rt->rateCount) {
4668 			sc->sc_hwmap[i].ledon = (500 * hz) / 1000;
4669 			sc->sc_hwmap[i].ledoff = (130 * hz) / 1000;
4670 			continue;
4671 		}
4672 		sc->sc_hwmap[i].ieeerate =
4673 			rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
4674 		if (rt->info[i].phy == IEEE80211_T_HT)
4675 			sc->sc_hwmap[i].ieeerate |= IEEE80211_RATE_MCS;
4676 		sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
4677 		if (rt->info[i].shortPreamble ||
4678 		    rt->info[i].phy == IEEE80211_T_OFDM)
4679 			sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4680 		sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags;
4681 		for (j = 0; j < N(blinkrates)-1; j++)
4682 			if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate)
4683 				break;
4684 		/* NB: this uses the last entry if the rate isn't found */
4685 		/* XXX beware of overlow */
4686 		sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000;
4687 		sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000;
4688 	}
4689 	sc->sc_currates = rt;
4690 	sc->sc_curmode = mode;
4691 	/*
4692 	 * All protection frames are transmited at 2Mb/s for
4693 	 * 11g, otherwise at 1Mb/s.
4694 	 */
4695 	if (mode == IEEE80211_MODE_11G)
4696 		sc->sc_protrix = ath_tx_findrix(sc, 2*2);
4697 	else
4698 		sc->sc_protrix = ath_tx_findrix(sc, 2*1);
4699 	/* NB: caller is responsible for resetting rate control state */
4700 #undef N
4701 }
4702 
4703 static void
4704 ath_watchdog(void *arg)
4705 {
4706 	struct ath_softc *sc = arg;
4707 	int do_reset = 0;
4708 
4709 	if (sc->sc_wd_timer != 0 && --sc->sc_wd_timer == 0) {
4710 		struct ifnet *ifp = sc->sc_ifp;
4711 		uint32_t hangs;
4712 
4713 		if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) &&
4714 		    hangs != 0) {
4715 			if_printf(ifp, "%s hang detected (0x%x)\n",
4716 			    hangs & 0xff ? "bb" : "mac", hangs);
4717 		} else
4718 			if_printf(ifp, "device timeout\n");
4719 		do_reset = 1;
4720 		ifp->if_oerrors++;
4721 		sc->sc_stats.ast_watchdog++;
4722 	}
4723 
4724 	/*
4725 	 * We can't hold the lock across the ath_reset() call.
4726 	 *
4727 	 * And since this routine can't hold a lock and sleep,
4728 	 * do the reset deferred.
4729 	 */
4730 	if (do_reset) {
4731 		taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
4732 	}
4733 
4734 	callout_schedule(&sc->sc_wd_ch, hz);
4735 }
4736 
4737 #ifdef ATH_DIAGAPI
4738 /*
4739  * Diagnostic interface to the HAL.  This is used by various
4740  * tools to do things like retrieve register contents for
4741  * debugging.  The mechanism is intentionally opaque so that
4742  * it can change frequently w/o concern for compatiblity.
4743  */
4744 static int
4745 ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
4746 {
4747 	struct ath_hal *ah = sc->sc_ah;
4748 	u_int id = ad->ad_id & ATH_DIAG_ID;
4749 	void *indata = NULL;
4750 	void *outdata = NULL;
4751 	u_int32_t insize = ad->ad_in_size;
4752 	u_int32_t outsize = ad->ad_out_size;
4753 	int error = 0;
4754 
4755 	if (ad->ad_id & ATH_DIAG_IN) {
4756 		/*
4757 		 * Copy in data.
4758 		 */
4759 		indata = malloc(insize, M_TEMP, M_NOWAIT);
4760 		if (indata == NULL) {
4761 			error = ENOMEM;
4762 			goto bad;
4763 		}
4764 		error = copyin(ad->ad_in_data, indata, insize);
4765 		if (error)
4766 			goto bad;
4767 	}
4768 	if (ad->ad_id & ATH_DIAG_DYN) {
4769 		/*
4770 		 * Allocate a buffer for the results (otherwise the HAL
4771 		 * returns a pointer to a buffer where we can read the
4772 		 * results).  Note that we depend on the HAL leaving this
4773 		 * pointer for us to use below in reclaiming the buffer;
4774 		 * may want to be more defensive.
4775 		 */
4776 		outdata = malloc(outsize, M_TEMP, M_NOWAIT);
4777 		if (outdata == NULL) {
4778 			error = ENOMEM;
4779 			goto bad;
4780 		}
4781 	}
4782 	if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) {
4783 		if (outsize < ad->ad_out_size)
4784 			ad->ad_out_size = outsize;
4785 		if (outdata != NULL)
4786 			error = copyout(outdata, ad->ad_out_data,
4787 					ad->ad_out_size);
4788 	} else {
4789 		error = EINVAL;
4790 	}
4791 bad:
4792 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
4793 		free(indata, M_TEMP);
4794 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
4795 		free(outdata, M_TEMP);
4796 	return error;
4797 }
4798 #endif /* ATH_DIAGAPI */
4799 
4800 static int
4801 ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
4802 {
4803 #define	IS_RUNNING(ifp) \
4804 	((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
4805 	struct ath_softc *sc = ifp->if_softc;
4806 	struct ieee80211com *ic = ifp->if_l2com;
4807 	struct ifreq *ifr = (struct ifreq *)data;
4808 	const HAL_RATE_TABLE *rt;
4809 	int error = 0;
4810 
4811 	switch (cmd) {
4812 	case SIOCSIFFLAGS:
4813 		ATH_LOCK(sc);
4814 		if (IS_RUNNING(ifp)) {
4815 			/*
4816 			 * To avoid rescanning another access point,
4817 			 * do not call ath_init() here.  Instead,
4818 			 * only reflect promisc mode settings.
4819 			 */
4820 			ath_mode_init(sc);
4821 		} else if (ifp->if_flags & IFF_UP) {
4822 			/*
4823 			 * Beware of being called during attach/detach
4824 			 * to reset promiscuous mode.  In that case we
4825 			 * will still be marked UP but not RUNNING.
4826 			 * However trying to re-init the interface
4827 			 * is the wrong thing to do as we've already
4828 			 * torn down much of our state.  There's
4829 			 * probably a better way to deal with this.
4830 			 */
4831 			if (!sc->sc_invalid)
4832 				ath_init(sc);	/* XXX lose error */
4833 		} else {
4834 			ath_stop_locked(ifp);
4835 #ifdef notyet
4836 			/* XXX must wakeup in places like ath_vap_delete */
4837 			if (!sc->sc_invalid)
4838 				ath_hal_setpower(sc->sc_ah, HAL_PM_FULL_SLEEP);
4839 #endif
4840 		}
4841 		ATH_UNLOCK(sc);
4842 		break;
4843 	case SIOCGIFMEDIA:
4844 	case SIOCSIFMEDIA:
4845 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
4846 		break;
4847 	case SIOCGATHSTATS:
4848 		/* NB: embed these numbers to get a consistent view */
4849 		sc->sc_stats.ast_tx_packets = ifp->if_opackets;
4850 		sc->sc_stats.ast_rx_packets = ifp->if_ipackets;
4851 		sc->sc_stats.ast_tx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgtxrssi);
4852 		sc->sc_stats.ast_rx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgrssi);
4853 #ifdef IEEE80211_SUPPORT_TDMA
4854 		sc->sc_stats.ast_tdma_tsfadjp = TDMA_AVG(sc->sc_avgtsfdeltap);
4855 		sc->sc_stats.ast_tdma_tsfadjm = TDMA_AVG(sc->sc_avgtsfdeltam);
4856 #endif
4857 		rt = sc->sc_currates;
4858 		sc->sc_stats.ast_tx_rate =
4859 		    rt->info[sc->sc_txrix].dot11Rate &~ IEEE80211_RATE_BASIC;
4860 		if (rt->info[sc->sc_txrix].phy & IEEE80211_T_HT)
4861 			sc->sc_stats.ast_tx_rate |= IEEE80211_RATE_MCS;
4862 		return copyout(&sc->sc_stats,
4863 		    ifr->ifr_data, sizeof (sc->sc_stats));
4864 	case SIOCZATHSTATS:
4865 		error = priv_check(curthread, PRIV_DRIVER);
4866 		if (error == 0) {
4867 			memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
4868 			memset(&sc->sc_aggr_stats, 0,
4869 			    sizeof(sc->sc_aggr_stats));
4870 			memset(&sc->sc_intr_stats, 0,
4871 			    sizeof(sc->sc_intr_stats));
4872 		}
4873 		break;
4874 #ifdef ATH_DIAGAPI
4875 	case SIOCGATHDIAG:
4876 		error = ath_ioctl_diag(sc, (struct ath_diag *) ifr);
4877 		break;
4878 	case SIOCGATHPHYERR:
4879 		error = ath_ioctl_phyerr(sc,(struct ath_diag*) ifr);
4880 		break;
4881 #endif
4882 	case SIOCGIFADDR:
4883 		error = ether_ioctl(ifp, cmd, data);
4884 		break;
4885 	default:
4886 		error = EINVAL;
4887 		break;
4888 	}
4889 	return error;
4890 #undef IS_RUNNING
4891 }
4892 
4893 /*
4894  * Announce various information on device/driver attach.
4895  */
4896 static void
4897 ath_announce(struct ath_softc *sc)
4898 {
4899 	struct ifnet *ifp = sc->sc_ifp;
4900 	struct ath_hal *ah = sc->sc_ah;
4901 
4902 	if_printf(ifp, "AR%s mac %d.%d RF%s phy %d.%d\n",
4903 		ath_hal_mac_name(ah), ah->ah_macVersion, ah->ah_macRev,
4904 		ath_hal_rf_name(ah), ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
4905 	if_printf(ifp, "2GHz radio: 0x%.4x; 5GHz radio: 0x%.4x\n",
4906 		ah->ah_analog2GhzRev, ah->ah_analog5GhzRev);
4907 	if (bootverbose) {
4908 		int i;
4909 		for (i = 0; i <= WME_AC_VO; i++) {
4910 			struct ath_txq *txq = sc->sc_ac2q[i];
4911 			if_printf(ifp, "Use hw queue %u for %s traffic\n",
4912 				txq->axq_qnum, ieee80211_wme_acnames[i]);
4913 		}
4914 		if_printf(ifp, "Use hw queue %u for CAB traffic\n",
4915 			sc->sc_cabq->axq_qnum);
4916 		if_printf(ifp, "Use hw queue %u for beacons\n", sc->sc_bhalq);
4917 	}
4918 	if (ath_rxbuf != ATH_RXBUF)
4919 		if_printf(ifp, "using %u rx buffers\n", ath_rxbuf);
4920 	if (ath_txbuf != ATH_TXBUF)
4921 		if_printf(ifp, "using %u tx buffers\n", ath_txbuf);
4922 	if (sc->sc_mcastkey && bootverbose)
4923 		if_printf(ifp, "using multicast key search\n");
4924 }
4925 
4926 static void
4927 ath_dfs_tasklet(void *p, int npending)
4928 {
4929 	struct ath_softc *sc = (struct ath_softc *) p;
4930 	struct ifnet *ifp = sc->sc_ifp;
4931 	struct ieee80211com *ic = ifp->if_l2com;
4932 
4933 	/*
4934 	 * If previous processing has found a radar event,
4935 	 * signal this to the net80211 layer to begin DFS
4936 	 * processing.
4937 	 */
4938 	if (ath_dfs_process_radar_event(sc, sc->sc_curchan)) {
4939 		/* DFS event found, initiate channel change */
4940 		/*
4941 		 * XXX doesn't currently tell us whether the event
4942 		 * XXX was found in the primary or extension
4943 		 * XXX channel!
4944 		 */
4945 		IEEE80211_LOCK(ic);
4946 		ieee80211_dfs_notify_radar(ic, sc->sc_curchan);
4947 		IEEE80211_UNLOCK(ic);
4948 	}
4949 }
4950 
4951 MODULE_VERSION(if_ath, 1);
4952 MODULE_DEPEND(if_ath, wlan, 1, 1, 1);          /* 802.11 media layer */
4953 #if	defined(IEEE80211_ALQ) || defined(AH_DEBUG_ALQ)
4954 MODULE_DEPEND(if_ath, alq, 1, 1, 1);
4955 #endif
4956