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